2 * Copyright (C) 2009-2010 Andy Spencer <andy753421@gmail.com>
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/>.
18 #include <gtk/gtkgl.h>
25 #define MAX_RESOLUTION 500
26 #define TILE_WIDTH 1024
27 #define TILE_HEIGHT 512
30 /* OpenGL has to be first to make gis_opengl_render_tiles happy */
35 static gdouble _height_func(gdouble lat, gdouble lon, gpointer _self)
37 GisPluginElev *self = _self;
40 GisTile *tile = gis_tile_find(self->tiles, lat, lon);
43 struct _TileData *data = tile->data;
46 guint16 *bil = data->bil;
52 gdouble ymin = tile->edge.s;
53 gdouble ymax = tile->edge.n;
54 gdouble xmin = tile->edge.w;
55 gdouble xmax = tile->edge.e;
57 gdouble xdist = xmax - xmin;
58 gdouble ydist = ymax - ymin;
60 gdouble x = (lon-xmin)/xdist * w;
61 gdouble y = (1-(lat-ymin)/ydist) * h;
63 gdouble x_rem = x - (int)x;
64 gdouble y_rem = y - (int)y;
68 //if (lon == 180 || lon == -180)
69 // g_message("lon=%f w=%d min=%f max=%f dist=%f x=%f rem=%f flr=%d",
70 // lon, w, xmin, xmax, xdist, x, x_rem, x_flr);
72 /* TODO: Fix interpolation at edges:
73 * - Pad these at the edges instead of wrapping/truncating
74 * - Figure out which pixels to index (is 0,0 edge, center, etc) */
75 gint16 px00 = bil[MIN((y_flr ),h-1)*w + MIN((x_flr ),w-1)];
76 gint16 px10 = bil[MIN((y_flr ),h-1)*w + MIN((x_flr+1),w-1)];
77 gint16 px01 = bil[MIN((y_flr+1),h-1)*w + MIN((x_flr ),w-1)];
78 gint16 px11 = bil[MIN((y_flr+1),h-1)*w + MIN((x_flr+1),w-1)];
81 px00 * (1-x_rem) * (1-y_rem) +
82 px10 * ( x_rem) * (1-y_rem) +
83 px01 * (1-x_rem) * ( y_rem) +
84 px11 * ( x_rem) * ( y_rem);
88 /**********************
89 * Loader and Freeers *
90 **********************/
92 #define LOAD_OPENGL FALSE
93 struct _LoadTileData {
98 struct _TileData *data;
100 static guint16 *_load_bil(gchar *path)
103 g_file_get_contents(path, &data, NULL, NULL);
104 g_debug("GisPluginElev: load_bil %p", data);
105 return (guint16*)data;
107 static GdkPixbuf *_load_pixbuf(guint16 *bil)
109 GdkPixbuf *pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, TILE_WIDTH, TILE_HEIGHT);
110 guchar *pixels = gdk_pixbuf_get_pixels(pixbuf);
111 gint stride = gdk_pixbuf_get_rowstride(pixbuf);
112 gint nchan = gdk_pixbuf_get_n_channels(pixbuf);
114 for (int r = 0; r < TILE_HEIGHT; r++) {
115 for (int c = 0; c < TILE_WIDTH; c++) {
116 gint16 value = bil[r*TILE_WIDTH + c];
117 //guchar color = (float)(MAX(value,0))/8848 * 255;
118 guchar color = (float)value/8848 * 255;
119 pixels[r*stride + c*nchan + 0] = color;
120 pixels[r*stride + c*nchan + 1] = color;
121 pixels[r*stride + c*nchan + 2] = color;
123 pixels[r*stride + c*nchan + 3] = 128;
126 g_debug("GisPluginElev: load_pixbuf %p", pixbuf);
129 static guint _load_opengl(GdkPixbuf *pixbuf)
132 guchar *pixels = gdk_pixbuf_get_pixels(pixbuf);
133 gint alpha = gdk_pixbuf_get_has_alpha(pixbuf);
134 gint nchan = 4; // gdk_pixbuf_get_n_channels(pixbuf);
135 gint width = gdk_pixbuf_get_width(pixbuf);
136 gint height = gdk_pixbuf_get_height(pixbuf);
140 glGenTextures(1, &opengl);
141 glBindTexture(GL_TEXTURE_2D, opengl);
143 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
144 glPixelStorei(GL_PACK_ALIGNMENT, 1);
145 glTexImage2D(GL_TEXTURE_2D, 0, nchan, width, height, 0,
146 (alpha ? GL_RGBA : GL_RGB), GL_UNSIGNED_BYTE, pixels);
147 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
148 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
149 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
150 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
152 g_debug("GisPluginElev: load_opengl %d", opengl);
155 static gboolean _load_tile_cb(gpointer _load)
157 struct _LoadTileData *load = _load;
158 g_debug("GisPluginElev: _load_tile_cb: %s", load->path);
159 GisPluginElev *self = load->self;
160 GisTile *tile = load->tile;
161 GdkPixbuf *pixbuf = load->pixbuf;
162 struct _TileData *data = load->data;
167 data->opengl = _load_opengl(pixbuf);
171 /* Do necessasairy processing */
172 /* TODO: Lock this and move to thread, can remove self from _load then */
174 gis_viewer_set_height_func(self->viewer, tile, _height_func, self, TRUE);
176 /* Cleanup unneeded things */
180 g_object_unref(pixbuf);
184 static void _load_tile(GisTile *tile, gpointer _self)
186 GisPluginElev *self = _self;
188 struct _LoadTileData *load = g_new0(struct _LoadTileData, 1);
189 load->path = gis_wms_make_local(self->wms, tile);
190 g_debug("GisPluginElev: _load_tile: %s", load->path);
193 load->data = g_new0(struct _TileData, 1);
194 if (LOAD_BIL || LOAD_OPENGL)
195 load->data->bil = _load_bil(load->path);
197 load->pixbuf = _load_pixbuf(load->data->bil);
199 g_idle_add_full(G_PRIORITY_LOW, _load_tile_cb, load, NULL);
202 static gboolean _free_tile_cb(gpointer _data)
204 struct _TileData *data = _data;
208 glDeleteTextures(1, &data->opengl);
212 static void _free_tile(GisTile *tile, gpointer _self)
214 GisPluginElev *self = _self;
215 g_debug("GisPluginElev: _free_tile: %p", tile->data);
216 g_idle_add_full(G_PRIORITY_LOW, _free_tile_cb, tile->data, NULL);
219 static gpointer _update_tiles(gpointer _self)
221 GisPluginElev *self = _self;
222 g_mutex_lock(self->mutex);
223 gdouble lat, lon, elev;
224 gis_viewer_get_location(self->viewer, &lat, &lon, &elev);
225 gis_tile_update(self->tiles,
226 MAX_RESOLUTION, TILE_WIDTH, TILE_WIDTH,
229 gis_tile_gc(self->tiles, time(NULL)-10,
231 g_mutex_unlock(self->mutex);
238 static void _on_location_changed(GisViewer *viewer,
239 gdouble lat, gdouble lon, gdouble elev, GisPluginElev *self)
241 g_thread_create(_update_tiles, self, FALSE, NULL);
244 static gpointer _expose(GisCallback *callback, gpointer _self)
246 GisPluginElev *self = GIS_PLUGIN_ELEV(_self);
247 g_debug("GisPluginElev: expose tiles=%p data=%p",
248 self->tiles, self->tiles->data);
250 gis_viewer_render_tiles(self->viewer, self->tiles);
257 GisPluginElev *gis_plugin_elev_new(GisViewer *viewer)
259 g_debug("GisPluginElev: new");
260 GisPluginElev *self = g_object_new(GIS_TYPE_PLUGIN_ELEV, NULL);
261 self->viewer = g_object_ref(viewer);
263 /* Load initial tiles */
264 _load_tile(self->tiles, self);
265 g_thread_create(_update_tiles, self, FALSE, NULL);
267 /* Connect signals */
268 self->sigid = g_signal_connect(self->viewer, "location-changed",
269 G_CALLBACK(_on_location_changed), self);
272 GisCallback *callback = gis_callback_new(_expose, self);
273 gis_viewer_add(viewer, GIS_OBJECT(callback), GIS_LEVEL_WORLD, 0);
283 static void gis_plugin_elev_plugin_init(GisPluginInterface *iface);
284 G_DEFINE_TYPE_WITH_CODE(GisPluginElev, gis_plugin_elev, G_TYPE_OBJECT,
285 G_IMPLEMENT_INTERFACE(GIS_TYPE_PLUGIN,
286 gis_plugin_elev_plugin_init));
287 static void gis_plugin_elev_plugin_init(GisPluginInterface *iface)
289 g_debug("GisPluginElev: plugin_init");
290 /* Add methods to the interface */
292 /* Class/Object init */
293 static void gis_plugin_elev_init(GisPluginElev *self)
295 g_debug("GisPluginElev: init");
297 self->mutex = g_mutex_new();
298 self->tiles = gis_tile_new(NULL, NORTH, SOUTH, EAST, WEST);
299 self->wms = gis_wms_new(
300 "http://www.nasa.network.com/srtm", "srtm30", "application/bil",
301 "srtm/", "bil", TILE_WIDTH, TILE_HEIGHT);
303 static void gis_plugin_elev_dispose(GObject *gobject)
305 g_debug("GisPluginElev: dispose");
306 GisPluginElev *self = GIS_PLUGIN_ELEV(gobject);
307 /* Drop references */
309 gis_viewer_clear_height_func(self->viewer);
311 g_signal_handler_disconnect(self->viewer, self->sigid);
312 g_object_unref(self->viewer);
315 G_OBJECT_CLASS(gis_plugin_elev_parent_class)->dispose(gobject);
317 static void gis_plugin_elev_finalize(GObject *gobject)
319 g_debug("GisPluginElev: finalize");
320 GisPluginElev *self = GIS_PLUGIN_ELEV(gobject);
322 gis_tile_free(self->tiles, _free_tile, self);
323 gis_wms_free(self->wms);
324 g_mutex_free(self->mutex);
325 G_OBJECT_CLASS(gis_plugin_elev_parent_class)->finalize(gobject);
328 static void gis_plugin_elev_class_init(GisPluginElevClass *klass)
330 g_debug("GisPluginElev: class_init");
331 GObjectClass *gobject_class = (GObjectClass*)klass;
332 gobject_class->dispose = gis_plugin_elev_dispose;
333 gobject_class->finalize = gis_plugin_elev_finalize;