]> Pileus Git - grits/blob - src/plugins/bmng.c
Add Blue Marble Next Gen plugin and tile rendering code
[grits] / src / plugins / bmng.c
1 /*
2  * Copyright (C) 2009 Andy Spencer <spenceal@rose-hulman.edu>
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 #include <time.h>
19 #include <GL/gl.h>
20
21 #include <gis.h>
22
23 #include "bmng.h"
24
25 #define MAX_RESOLUTION 500
26 #define TILE_WIDTH     1024
27 #define TILE_HEIGHT    512
28
29 static void _load_tile(GisTile *tile, gpointer _self)
30 {
31         GisPluginBmng *self = _self;
32         g_debug("GisPluginBmng: _load_tile start");
33
34         char *path = gis_wms_make_local(self->wms, tile);
35         GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(path, NULL);
36         g_free(path);
37
38         guchar   *pixels = gdk_pixbuf_get_pixels(pixbuf);
39         gboolean  alpha  = gdk_pixbuf_get_has_alpha(pixbuf);
40         gint      width  = gdk_pixbuf_get_width(pixbuf);
41         gint      height = gdk_pixbuf_get_height(pixbuf);
42
43         guint *tex = g_new0(guint, 1);
44         gis_opengl_begin(self->opengl);
45         glGenTextures(1, tex);
46         glBindTexture(GL_TEXTURE_2D, *tex);
47
48         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
49         glPixelStorei(GL_PACK_ALIGNMENT, 1);
50         glTexImage2D(GL_TEXTURE_2D, 0, 4, width, height, 0,
51                         (alpha ? GL_RGBA : GL_RGB), GL_UNSIGNED_BYTE, pixels);
52         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
53         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
54         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
55         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
56         glFlush();
57         gis_opengl_end(self->opengl);
58
59         tile->data = tex;
60         gis_opengl_redraw(self->opengl);
61         g_object_unref(pixbuf);
62 }
63
64 static void _free_tile(GisTile *tile, gpointer _self)
65 {
66         GisPluginBmng *self = _self;
67         g_debug("GisPluginBmng: _free_tile: %p=%d", tile->data, *(guint*)tile->data);
68         guint *data = tile->data;
69         glDeleteTextures(1, data);
70         g_free(data);
71 }
72
73 static gpointer _update_tiles(gpointer _self)
74 {
75         g_debug("GisPluginBmng: _update_tiles");
76         GisPluginBmng *self = _self;
77         gdouble lat, lon, elev;
78         gis_view_get_location(self->view, &lat, &lon, &elev);
79         gis_tile_update(self->tiles,
80                         MAX_RESOLUTION, TILE_WIDTH, TILE_WIDTH,
81                         lat, lon, elev,
82                         _load_tile, self);
83         gis_tile_gc(self->tiles, time(NULL)-10,
84                         _free_tile, self);
85         return NULL;
86 }
87
88 /*************
89  * Callbacks *
90  *************/
91 static void _on_location_changed(GisView *view, gdouble lat, gdouble lon, gdouble elev,
92                 GisPluginBmng *self)
93 {
94         _update_tiles(self);
95 }
96
97 /***********
98  * Methods *
99  ***********/
100 GisPluginBmng *gis_plugin_bmng_new(GisWorld *world, GisView *view, GisOpenGL *opengl)
101 {
102         g_debug("GisPluginBmng: new");
103         GisPluginBmng *self = g_object_new(GIS_TYPE_PLUGIN_BMNG, NULL);
104         self->view   = view;
105         self->opengl = opengl;
106
107         /* Load initial tiles */
108         _load_tile(self->tiles, self);
109         _update_tiles(self);
110
111         /* Connect signals */
112         g_signal_connect(self->view, "location-changed", G_CALLBACK(_on_location_changed), self);
113
114         return self;
115 }
116
117 static void gis_plugin_bmng_expose(GisPlugin *_self)
118 {
119         GisPluginBmng *self = GIS_PLUGIN_BMNG(_self);
120         g_debug("GisPluginBmng: expose");
121         gis_opengl_render_tiles(self->opengl, self->tiles);
122 }
123
124
125 /****************
126  * GObject code *
127  ****************/
128 /* Plugin init */
129 static void gis_plugin_bmng_plugin_init(GisPluginInterface *iface);
130 G_DEFINE_TYPE_WITH_CODE(GisPluginBmng, gis_plugin_bmng, G_TYPE_OBJECT,
131                 G_IMPLEMENT_INTERFACE(GIS_TYPE_PLUGIN,
132                         gis_plugin_bmng_plugin_init));
133 static void gis_plugin_bmng_plugin_init(GisPluginInterface *iface)
134 {
135         g_debug("GisPluginBmng: plugin_init");
136         /* Add methods to the interface */
137         iface->expose = gis_plugin_bmng_expose;
138 }
139 /* Class/Object init */
140 static void gis_plugin_bmng_init(GisPluginBmng *self)
141 {
142         g_debug("GisPluginBmng: init");
143         /* Set defaults */
144         self->tiles = gis_tile_new(NULL, NORTH, SOUTH, EAST, WEST);
145         self->wms   = gis_wms_new(
146                 "http://www.nasa.network.com/wms", "bmng200406", "image/jpeg",
147                 "bmng", ".jpg", TILE_WIDTH, TILE_HEIGHT);
148 }
149 static void gis_plugin_bmng_dispose(GObject *gobject)
150 {
151         g_debug("GisPluginBmng: dispose");
152         GisPluginBmng *self = GIS_PLUGIN_BMNG(gobject);
153         /* Drop references */
154         G_OBJECT_CLASS(gis_plugin_bmng_parent_class)->dispose(gobject);
155 }
156 static void gis_plugin_bmng_finalize(GObject *gobject)
157 {
158         g_debug("GisPluginBmng: finalize");
159         GisPluginBmng *self = GIS_PLUGIN_BMNG(gobject);
160         /* Free data */
161         gis_tile_free(self->tiles, _free_tile, self);
162         gis_wms_free(self->wms);
163         G_OBJECT_CLASS(gis_plugin_bmng_parent_class)->finalize(gobject);
164
165 }
166 static void gis_plugin_bmng_class_init(GisPluginBmngClass *klass)
167 {
168         g_debug("GisPluginBmng: class_init");
169         GObjectClass *gobject_class = (GObjectClass*)klass;
170         gobject_class->dispose  = gis_plugin_bmng_dispose;
171         gobject_class->finalize = gis_plugin_bmng_finalize;
172 }