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 static void _load_tile(GisTile *tile, gpointer _self)
31 GisPluginBmng *self = _self;
32 g_debug("GisPluginBmng: _load_tile start");
34 char *path = gis_wms_make_local(self->wms, tile);
35 GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(path, NULL);
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);
43 guint *tex = g_new0(guint, 1);
44 gis_opengl_begin(self->opengl);
45 glGenTextures(1, tex);
46 glBindTexture(GL_TEXTURE_2D, *tex);
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);
57 gis_opengl_end(self->opengl);
60 gis_opengl_redraw(self->opengl);
61 g_object_unref(pixbuf);
64 static void _free_tile(GisTile *tile, gpointer _self)
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);
73 static gpointer _update_tiles(gpointer _self)
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,
83 gis_tile_gc(self->tiles, time(NULL)-10,
91 static void _on_location_changed(GisView *view, gdouble lat, gdouble lon, gdouble elev,
100 GisPluginBmng *gis_plugin_bmng_new(GisWorld *world, GisView *view, GisOpenGL *opengl)
102 g_debug("GisPluginBmng: new");
103 GisPluginBmng *self = g_object_new(GIS_TYPE_PLUGIN_BMNG, NULL);
105 self->opengl = opengl;
107 /* Load initial tiles */
108 _load_tile(self->tiles, self);
111 /* Connect signals */
112 g_signal_connect(self->view, "location-changed", G_CALLBACK(_on_location_changed), self);
117 static void gis_plugin_bmng_expose(GisPlugin *_self)
119 GisPluginBmng *self = GIS_PLUGIN_BMNG(_self);
120 g_debug("GisPluginBmng: expose");
121 gis_opengl_render_tiles(self->opengl, self->tiles);
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)
135 g_debug("GisPluginBmng: plugin_init");
136 /* Add methods to the interface */
137 iface->expose = gis_plugin_bmng_expose;
139 /* Class/Object init */
140 static void gis_plugin_bmng_init(GisPluginBmng *self)
142 g_debug("GisPluginBmng: init");
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);
149 static void gis_plugin_bmng_dispose(GObject *gobject)
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);
156 static void gis_plugin_bmng_finalize(GObject *gobject)
158 g_debug("GisPluginBmng: finalize");
159 GisPluginBmng *self = GIS_PLUGIN_BMNG(gobject);
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);
166 static void gis_plugin_bmng_class_init(GisPluginBmngClass *klass)
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;