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/>.
20 * @short_description: Elevation plugin
22 * #GisPluginElev provides access to ground elevation. It does this in two ways:
23 * First, it provides a height function used by the viewer when drawing the
24 * world. Second, it can load the elevation data into an image and draw a
25 * greyscale elevation overlay on the planets surface.
28 #include <gtk/gtkgl.h>
29 #include <glib/gstdio.h>
36 #define MAX_RESOLUTION 500
37 #define TILE_WIDTH 1024
38 #define TILE_HEIGHT 512
39 #define TILE_SIZE (TILE_WIDTH*TILE_HEIGHT*sizeof(guint16))
42 /* OpenGL has to be first to make gis_opengl_render_tiles happy */
47 static gdouble _height_func(gdouble lat, gdouble lon, gpointer _elev)
49 GisPluginElev *elev = _elev;
52 GisTile *tile = gis_tile_find(elev->tiles, lat, lon);
55 struct _TileData *data = tile->data;
58 guint16 *bil = data->bil;
64 gdouble ymin = tile->edge.s;
65 gdouble ymax = tile->edge.n;
66 gdouble xmin = tile->edge.w;
67 gdouble xmax = tile->edge.e;
69 gdouble xdist = xmax - xmin;
70 gdouble ydist = ymax - ymin;
72 gdouble x = (lon-xmin)/xdist * w;
73 gdouble y = (1-(lat-ymin)/ydist) * h;
75 gdouble x_rem = x - (int)x;
76 gdouble y_rem = y - (int)y;
80 //if (lon == 180 || lon == -180)
81 // g_message("lon=%f w=%d min=%f max=%f dist=%f x=%f rem=%f flr=%d",
82 // lon, w, xmin, xmax, xdist, x, x_rem, x_flr);
84 /* TODO: Fix interpolation at edges:
85 * - Pad these at the edges instead of wrapping/truncating
86 * - Figure out which pixels to index (is 0,0 edge, center, etc) */
87 gint16 px00 = bil[MIN((y_flr ),h-1)*w + MIN((x_flr ),w-1)];
88 gint16 px10 = bil[MIN((y_flr ),h-1)*w + MIN((x_flr+1),w-1)];
89 gint16 px01 = bil[MIN((y_flr+1),h-1)*w + MIN((x_flr ),w-1)];
90 gint16 px11 = bil[MIN((y_flr+1),h-1)*w + MIN((x_flr+1),w-1)];
92 return px00 * (1-x_rem) * (1-y_rem) +
93 px10 * ( x_rem) * (1-y_rem) +
94 px01 * (1-x_rem) * ( y_rem) +
95 px11 * ( x_rem) * ( y_rem);
98 /**********************
99 * Loader and Freeers *
100 **********************/
101 #define LOAD_BIL TRUE
102 #define LOAD_OPENGL FALSE
103 struct _LoadTileData {
108 struct _TileData *data;
110 static guint16 *_load_bil(gchar *path)
114 g_file_get_contents(path, &data, &len, NULL);
115 g_debug("GisPluginElev: load_bil %p", data);
116 if (len != TILE_SIZE) {
117 g_warning("GisPluginElev: _load_bil - unexpected tile size %d, != %d",
122 return (guint16*)data;
124 static GdkPixbuf *_load_pixbuf(guint16 *bil)
126 GdkPixbuf *pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, TILE_WIDTH, TILE_HEIGHT);
127 guchar *pixels = gdk_pixbuf_get_pixels(pixbuf);
128 gint stride = gdk_pixbuf_get_rowstride(pixbuf);
129 gint nchan = gdk_pixbuf_get_n_channels(pixbuf);
131 for (int r = 0; r < TILE_HEIGHT; r++) {
132 for (int c = 0; c < TILE_WIDTH; c++) {
133 gint16 value = bil[r*TILE_WIDTH + c];
134 //guchar color = (float)(MAX(value,0))/8848 * 255;
135 guchar color = (float)value/8848 * 255;
136 pixels[r*stride + c*nchan + 0] = color;
137 pixels[r*stride + c*nchan + 1] = color;
138 pixels[r*stride + c*nchan + 2] = color;
140 pixels[r*stride + c*nchan + 3] = 128;
143 g_debug("GisPluginElev: load_pixbuf %p", pixbuf);
146 static guint _load_opengl(GdkPixbuf *pixbuf)
149 guchar *pixels = gdk_pixbuf_get_pixels(pixbuf);
150 gint alpha = gdk_pixbuf_get_has_alpha(pixbuf);
151 gint nchan = 4; // gdk_pixbuf_get_n_channels(pixbuf);
152 gint width = gdk_pixbuf_get_width(pixbuf);
153 gint height = gdk_pixbuf_get_height(pixbuf);
157 glGenTextures(1, &opengl);
158 glBindTexture(GL_TEXTURE_2D, opengl);
160 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
161 glPixelStorei(GL_PACK_ALIGNMENT, 1);
162 glTexImage2D(GL_TEXTURE_2D, 0, nchan, width, height, 0,
163 (alpha ? GL_RGBA : GL_RGB), GL_UNSIGNED_BYTE, pixels);
164 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
165 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
166 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
167 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
169 g_debug("GisPluginElev: load_opengl %d", opengl);
172 static gboolean _load_tile_cb(gpointer _load)
174 struct _LoadTileData *load = _load;
175 g_debug("GisPluginElev: _load_tile_cb: %s", load->path);
176 GisPluginElev *elev = load->elev;
177 GisTile *tile = load->tile;
178 GdkPixbuf *pixbuf = load->pixbuf;
179 struct _TileData *data = load->data;
184 data->opengl = _load_opengl(pixbuf);
188 /* Do necessasairy processing */
189 /* TODO: Lock this and move to thread, can remove elev from _load then */
191 gis_viewer_set_height_func(elev->viewer, tile, _height_func, elev, TRUE);
193 /* Cleanup unneeded things */
197 g_object_unref(pixbuf);
201 static void _load_tile(GisTile *tile, gpointer _elev)
203 GisPluginElev *elev = _elev;
205 struct _LoadTileData *load = g_new0(struct _LoadTileData, 1);
206 load->path = gis_wms_fetch(elev->wms, tile, GIS_ONCE, NULL, NULL);
207 g_debug("GisPluginElev: _load_tile: %s", load->path);
210 load->data = g_new0(struct _TileData, 1);
211 if (LOAD_BIL || LOAD_OPENGL) {
212 load->data->bil = _load_bil(load->path);
213 if (!load->data->bil) {
214 g_remove(load->path);
222 load->pixbuf = _load_pixbuf(load->data->bil);
225 g_idle_add_full(G_PRIORITY_LOW, _load_tile_cb, load, NULL);
228 static gboolean _free_tile_cb(gpointer _data)
230 struct _TileData *data = _data;
234 glDeleteTextures(1, &data->opengl);
238 static void _free_tile(GisTile *tile, gpointer _elev)
240 g_debug("GisPluginElev: _free_tile: %p", tile->data);
242 g_idle_add_full(G_PRIORITY_LOW, _free_tile_cb, tile->data, NULL);
245 static gpointer _update_tiles(gpointer _elev)
247 GisPluginElev *elev = _elev;
248 if (!g_mutex_trylock(elev->mutex))
251 gis_viewer_get_location(elev->viewer, &eye.lat, &eye.lon, &eye.elev);
252 gis_tile_update(elev->tiles, &eye,
253 MAX_RESOLUTION, TILE_WIDTH, TILE_WIDTH,
255 gis_tile_gc(elev->tiles, time(NULL)-10,
257 g_mutex_unlock(elev->mutex);
264 static void _on_location_changed(GisViewer *viewer,
265 gdouble lat, gdouble lon, gdouble elevation, GisPluginElev *elev)
267 g_thread_create(_update_tiles, elev, FALSE, NULL);
270 static gpointer _threaded_init(GisPluginElev *elev)
272 _load_tile(elev->tiles, elev);
281 * gis_plugin_elev_new:
282 * @viewer: the #GisViewer to use for drawing
284 * Create a new instance of the elevation plugin.
286 * Returns: the new #GisPluginElev
288 GisPluginElev *gis_plugin_elev_new(GisViewer *viewer)
290 g_debug("GisPluginElev: new");
291 GisPluginElev *elev = g_object_new(GIS_TYPE_PLUGIN_ELEV, NULL);
292 elev->viewer = g_object_ref(viewer);
294 /* Load initial tiles */
295 g_thread_create((GThreadFunc)_threaded_init, elev, FALSE, NULL);
297 /* Connect signals */
298 elev->sigid = g_signal_connect(elev->viewer, "location-changed",
299 G_CALLBACK(_on_location_changed), elev);
303 gis_viewer_add(viewer, GIS_OBJECT(elev->tiles), GIS_LEVEL_WORLD, 0);
313 static void gis_plugin_elev_plugin_init(GisPluginInterface *iface);
314 G_DEFINE_TYPE_WITH_CODE(GisPluginElev, gis_plugin_elev, G_TYPE_OBJECT,
315 G_IMPLEMENT_INTERFACE(GIS_TYPE_PLUGIN,
316 gis_plugin_elev_plugin_init));
317 static void gis_plugin_elev_plugin_init(GisPluginInterface *iface)
319 g_debug("GisPluginElev: plugin_init");
320 /* Add methods to the interface */
322 /* Class/Object init */
323 static void gis_plugin_elev_init(GisPluginElev *elev)
325 g_debug("GisPluginElev: init");
327 elev->mutex = g_mutex_new();
328 elev->tiles = gis_tile_new(NULL, NORTH, SOUTH, EAST, WEST);
329 elev->wms = gis_wms_new(
330 "http://www.nasa.network.com/elev", "srtm30", "application/bil",
331 "srtm/", "bil", TILE_WIDTH, TILE_HEIGHT);
333 static void gis_plugin_elev_dispose(GObject *gobject)
335 g_debug("GisPluginElev: dispose");
336 GisPluginElev *elev = GIS_PLUGIN_ELEV(gobject);
337 /* Drop references */
339 gis_viewer_clear_height_func(elev->viewer);
341 g_signal_handler_disconnect(elev->viewer, elev->sigid);
342 g_object_unref(elev->viewer);
345 G_OBJECT_CLASS(gis_plugin_elev_parent_class)->dispose(gobject);
347 static void gis_plugin_elev_finalize(GObject *gobject)
349 g_debug("GisPluginElev: finalize");
350 GisPluginElev *elev = GIS_PLUGIN_ELEV(gobject);
352 gis_tile_free(elev->tiles, _free_tile, elev);
353 gis_wms_free(elev->wms);
354 g_mutex_free(elev->mutex);
355 G_OBJECT_CLASS(gis_plugin_elev_parent_class)->finalize(gobject);
358 static void gis_plugin_elev_class_init(GisPluginElevClass *klass)
360 g_debug("GisPluginElev: class_init");
361 GObjectClass *gobject_class = (GObjectClass*)klass;
362 gobject_class->dispose = gis_plugin_elev_dispose;
363 gobject_class->finalize = gis_plugin_elev_finalize;