2 * Copyright (C) 2009 Andy Spencer <spenceal@rose-hulman.edu>
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.
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.
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/>.
25 #define MAX_RESOLUTION 500
26 #define TILE_WIDTH 1024
27 #define TILE_HEIGHT 512
29 struct _LoadTileData {
34 static gboolean _load_tile_cb(gpointer _data)
36 struct _LoadTileData *data = _data;
37 GisPluginBmng *self = data->self;
38 GisTile *tile = data->tile;
39 GdkPixbuf *pixbuf = data->pixbuf;
43 g_debug("GisPluginBmng: _load_tile_cb start");
44 guchar *pixels = gdk_pixbuf_get_pixels(pixbuf);
45 gboolean alpha = gdk_pixbuf_get_has_alpha(pixbuf);
46 gint width = gdk_pixbuf_get_width(pixbuf);
47 gint height = gdk_pixbuf_get_height(pixbuf);
49 guint *tex = g_new0(guint, 1);
50 gis_opengl_begin(self->opengl);
51 glGenTextures(1, tex);
52 glBindTexture(GL_TEXTURE_2D, *tex);
54 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
55 glPixelStorei(GL_PACK_ALIGNMENT, 1);
56 glTexImage2D(GL_TEXTURE_2D, 0, 4, width, height, 0,
57 (alpha ? GL_RGBA : GL_RGB), GL_UNSIGNED_BYTE, pixels);
58 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
59 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
60 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
61 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
63 gis_opengl_end(self->opengl);
66 gis_opengl_redraw(self->opengl);
67 g_object_unref(pixbuf);
71 static void _load_tile(GisTile *tile, gpointer _self)
73 GisPluginBmng *self = _self;
74 g_debug("GisPluginBmng: _load_tile start %p", g_thread_self());
75 char *path = gis_wms_make_local(self->wms, tile);
76 struct _LoadTileData *data = g_new0(struct _LoadTileData, 1);
79 data->pixbuf = gdk_pixbuf_new_from_file(path, NULL);
81 g_idle_add_full(G_PRIORITY_LOW, _load_tile_cb, data, NULL);
82 g_debug("GisPluginBmng: _load_tile end %p", g_thread_self());
85 static gboolean _free_tile_cb(gpointer data)
87 glDeleteTextures(1, data);
91 static void _free_tile(GisTile *tile, gpointer _self)
93 GisPluginBmng *self = _self;
94 g_debug("GisPluginBmng: _free_tile: %p=%d", tile->data, *(guint*)tile->data);
95 g_idle_add_full(G_PRIORITY_LOW, _free_tile_cb, tile->data, NULL);
98 static gpointer _update_tiles(gpointer _self)
100 g_debug("GisPluginBmng: _update_tiles");
101 GisPluginBmng *self = _self;
102 g_mutex_lock(self->mutex);
103 gdouble lat, lon, elev;
104 gis_view_get_location(self->view, &lat, &lon, &elev);
105 gis_tile_update(self->tiles,
106 MAX_RESOLUTION, TILE_WIDTH, TILE_WIDTH,
109 gis_tile_gc(self->tiles, time(NULL)-10,
111 g_mutex_unlock(self->mutex);
118 static void _on_location_changed(GisView *view, gdouble lat, gdouble lon, gdouble elev,
121 g_thread_create(_update_tiles, self, FALSE, NULL);
127 GisPluginBmng *gis_plugin_bmng_new(GisWorld *world, GisView *view, GisOpenGL *opengl)
129 g_debug("GisPluginBmng: new");
130 GisPluginBmng *self = g_object_new(GIS_TYPE_PLUGIN_BMNG, NULL);
132 self->opengl = opengl;
134 /* Load initial tiles */
135 _load_tile(self->tiles, self);
136 g_thread_create(_update_tiles, self, FALSE, NULL);
138 /* Connect signals */
139 self->sigid = g_signal_connect(self->view, "location-changed",
140 G_CALLBACK(_on_location_changed), self);
145 static void gis_plugin_bmng_expose(GisPlugin *_self)
147 GisPluginBmng *self = GIS_PLUGIN_BMNG(_self);
148 g_debug("GisPluginBmng: expose opengl=%p tiles=%p,%p",
149 self->opengl, self->tiles, self->tiles->data);
150 gis_opengl_render_tiles(self->opengl, self->tiles);
158 static void gis_plugin_bmng_plugin_init(GisPluginInterface *iface);
159 G_DEFINE_TYPE_WITH_CODE(GisPluginBmng, gis_plugin_bmng, G_TYPE_OBJECT,
160 G_IMPLEMENT_INTERFACE(GIS_TYPE_PLUGIN,
161 gis_plugin_bmng_plugin_init));
162 static void gis_plugin_bmng_plugin_init(GisPluginInterface *iface)
164 g_debug("GisPluginBmng: plugin_init");
165 /* Add methods to the interface */
166 iface->expose = gis_plugin_bmng_expose;
168 /* Class/Object init */
169 static void gis_plugin_bmng_init(GisPluginBmng *self)
171 g_debug("GisPluginBmng: init");
173 self->mutex = g_mutex_new();
174 self->tiles = gis_tile_new(NULL, NORTH, SOUTH, EAST, WEST);
175 self->wms = gis_wms_new(
176 "http://www.nasa.network.com/wms", "bmng200406", "image/jpeg",
177 "bmng/", "jpg", TILE_WIDTH, TILE_HEIGHT);
179 static void gis_plugin_bmng_dispose(GObject *gobject)
181 g_debug("GisPluginBmng: dispose");
182 GisPluginBmng *self = GIS_PLUGIN_BMNG(gobject);
183 /* Drop references */
184 g_signal_handler_disconnect(self->view, self->sigid);
185 G_OBJECT_CLASS(gis_plugin_bmng_parent_class)->dispose(gobject);
187 static void gis_plugin_bmng_finalize(GObject *gobject)
189 g_debug("GisPluginBmng: finalize");
190 GisPluginBmng *self = GIS_PLUGIN_BMNG(gobject);
192 gis_tile_free(self->tiles, _free_tile, self);
193 gis_wms_free(self->wms);
194 g_mutex_free(self->mutex);
195 G_OBJECT_CLASS(gis_plugin_bmng_parent_class)->finalize(gobject);
198 static void gis_plugin_bmng_class_init(GisPluginBmngClass *klass)
200 g_debug("GisPluginBmng: class_init");
201 GObjectClass *gobject_class = (GObjectClass*)klass;
202 gobject_class->dispose = gis_plugin_bmng_dispose;
203 gobject_class->finalize = gis_plugin_bmng_finalize;