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>
19 #include <glib/gstdio.h>
26 #define MAX_RESOLUTION 500
27 #define TILE_WIDTH 1024
28 #define TILE_HEIGHT 512
29 #define TILE_SIZE (TILE_WIDTH*TILE_HEIGHT*sizeof(guint16))
32 /* OpenGL has to be first to make gis_opengl_render_tiles happy */
37 static gdouble _height_func(gdouble lat, gdouble lon, gpointer _elev)
39 GisPluginElev *elev = _elev;
42 GisTile *tile = gis_tile_find(elev->tiles, lat, lon);
45 struct _TileData *data = tile->data;
48 guint16 *bil = data->bil;
54 gdouble ymin = tile->edge.s;
55 gdouble ymax = tile->edge.n;
56 gdouble xmin = tile->edge.w;
57 gdouble xmax = tile->edge.e;
59 gdouble xdist = xmax - xmin;
60 gdouble ydist = ymax - ymin;
62 gdouble x = (lon-xmin)/xdist * w;
63 gdouble y = (1-(lat-ymin)/ydist) * h;
65 gdouble x_rem = x - (int)x;
66 gdouble y_rem = y - (int)y;
70 //if (lon == 180 || lon == -180)
71 // g_message("lon=%f w=%d min=%f max=%f dist=%f x=%f rem=%f flr=%d",
72 // lon, w, xmin, xmax, xdist, x, x_rem, x_flr);
74 /* TODO: Fix interpolation at edges:
75 * - Pad these at the edges instead of wrapping/truncating
76 * - Figure out which pixels to index (is 0,0 edge, center, etc) */
77 gint16 px00 = bil[MIN((y_flr ),h-1)*w + MIN((x_flr ),w-1)];
78 gint16 px10 = bil[MIN((y_flr ),h-1)*w + MIN((x_flr+1),w-1)];
79 gint16 px01 = bil[MIN((y_flr+1),h-1)*w + MIN((x_flr ),w-1)];
80 gint16 px11 = bil[MIN((y_flr+1),h-1)*w + MIN((x_flr+1),w-1)];
82 return px00 * (1-x_rem) * (1-y_rem) +
83 px10 * ( x_rem) * (1-y_rem) +
84 px01 * (1-x_rem) * ( y_rem) +
85 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)
104 g_file_get_contents(path, &data, &len, NULL);
105 g_debug("GisPluginElev: load_bil %p", data);
106 if (len != TILE_SIZE) {
107 g_warning("GisPluginElev: _load_bil - unexpected tile size %d, != %d",
112 return (guint16*)data;
114 static GdkPixbuf *_load_pixbuf(guint16 *bil)
116 GdkPixbuf *pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, TILE_WIDTH, TILE_HEIGHT);
117 guchar *pixels = gdk_pixbuf_get_pixels(pixbuf);
118 gint stride = gdk_pixbuf_get_rowstride(pixbuf);
119 gint nchan = gdk_pixbuf_get_n_channels(pixbuf);
121 for (int r = 0; r < TILE_HEIGHT; r++) {
122 for (int c = 0; c < TILE_WIDTH; c++) {
123 gint16 value = bil[r*TILE_WIDTH + c];
124 //guchar color = (float)(MAX(value,0))/8848 * 255;
125 guchar color = (float)value/8848 * 255;
126 pixels[r*stride + c*nchan + 0] = color;
127 pixels[r*stride + c*nchan + 1] = color;
128 pixels[r*stride + c*nchan + 2] = color;
130 pixels[r*stride + c*nchan + 3] = 128;
133 g_debug("GisPluginElev: load_pixbuf %p", pixbuf);
136 static guint _load_opengl(GdkPixbuf *pixbuf)
139 guchar *pixels = gdk_pixbuf_get_pixels(pixbuf);
140 gint alpha = gdk_pixbuf_get_has_alpha(pixbuf);
141 gint nchan = 4; // gdk_pixbuf_get_n_channels(pixbuf);
142 gint width = gdk_pixbuf_get_width(pixbuf);
143 gint height = gdk_pixbuf_get_height(pixbuf);
147 glGenTextures(1, &opengl);
148 glBindTexture(GL_TEXTURE_2D, opengl);
150 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
151 glPixelStorei(GL_PACK_ALIGNMENT, 1);
152 glTexImage2D(GL_TEXTURE_2D, 0, nchan, width, height, 0,
153 (alpha ? GL_RGBA : GL_RGB), GL_UNSIGNED_BYTE, pixels);
154 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
155 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
156 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
157 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
159 g_debug("GisPluginElev: load_opengl %d", opengl);
162 static gboolean _load_tile_cb(gpointer _load)
164 struct _LoadTileData *load = _load;
165 g_debug("GisPluginElev: _load_tile_cb: %s", load->path);
166 GisPluginElev *elev = load->elev;
167 GisTile *tile = load->tile;
168 GdkPixbuf *pixbuf = load->pixbuf;
169 struct _TileData *data = load->data;
174 data->opengl = _load_opengl(pixbuf);
178 /* Do necessasairy processing */
179 /* TODO: Lock this and move to thread, can remove elev from _load then */
181 gis_viewer_set_height_func(elev->viewer, tile, _height_func, elev, TRUE);
183 /* Cleanup unneeded things */
187 g_object_unref(pixbuf);
191 static void _load_tile(GisTile *tile, gpointer _elev)
193 GisPluginElev *elev = _elev;
195 struct _LoadTileData *load = g_new0(struct _LoadTileData, 1);
196 load->path = gis_wms_fetch(elev->wms, tile, GIS_ONCE, NULL, NULL);
197 g_debug("GisPluginElev: _load_tile: %s", load->path);
200 load->data = g_new0(struct _TileData, 1);
201 if (LOAD_BIL || LOAD_OPENGL) {
202 load->data->bil = _load_bil(load->path);
203 if (!load->data->bil) {
204 g_remove(load->path);
212 load->pixbuf = _load_pixbuf(load->data->bil);
215 g_idle_add_full(G_PRIORITY_LOW, _load_tile_cb, load, NULL);
218 static gboolean _free_tile_cb(gpointer _data)
220 struct _TileData *data = _data;
224 glDeleteTextures(1, &data->opengl);
228 static void _free_tile(GisTile *tile, gpointer _elev)
230 GisPluginElev *elev = _elev;
231 g_debug("GisPluginElev: _free_tile: %p", tile->data);
233 g_idle_add_full(G_PRIORITY_LOW, _free_tile_cb, tile->data, NULL);
236 static gpointer _update_tiles(gpointer _elev)
238 GisPluginElev *elev = _elev;
239 g_mutex_lock(elev->mutex);
240 gdouble lat, lon, elevation;
241 gis_viewer_get_location(elev->viewer, &lat, &lon, &elevation);
242 gis_tile_update(elev->tiles,
243 MAX_RESOLUTION, TILE_WIDTH, TILE_WIDTH,
246 gis_tile_gc(elev->tiles, time(NULL)-10,
248 g_mutex_unlock(elev->mutex);
255 static void _on_location_changed(GisViewer *viewer,
256 gdouble lat, gdouble lon, gdouble elevation, GisPluginElev *elev)
258 g_thread_create(_update_tiles, elev, FALSE, NULL);
264 GisPluginElev *gis_plugin_elev_new(GisViewer *viewer)
266 g_debug("GisPluginElev: new");
267 GisPluginElev *elev = g_object_new(GIS_TYPE_PLUGIN_ELEV, NULL);
268 elev->viewer = g_object_ref(viewer);
270 /* Load initial tiles */
271 _load_tile(elev->tiles, elev);
272 g_thread_create(_update_tiles, elev, FALSE, NULL);
274 /* Connect signals */
275 elev->sigid = g_signal_connect(elev->viewer, "location-changed",
276 G_CALLBACK(_on_location_changed), elev);
280 gis_viewer_add(viewer, GIS_OBJECT(elev->tiles), GIS_LEVEL_WORLD, 0);
290 static void gis_plugin_elev_plugin_init(GisPluginInterface *iface);
291 G_DEFINE_TYPE_WITH_CODE(GisPluginElev, gis_plugin_elev, G_TYPE_OBJECT,
292 G_IMPLEMENT_INTERFACE(GIS_TYPE_PLUGIN,
293 gis_plugin_elev_plugin_init));
294 static void gis_plugin_elev_plugin_init(GisPluginInterface *iface)
296 g_debug("GisPluginElev: plugin_init");
297 /* Add methods to the interface */
299 /* Class/Object init */
300 static void gis_plugin_elev_init(GisPluginElev *elev)
302 g_debug("GisPluginElev: init");
304 elev->mutex = g_mutex_new();
305 elev->tiles = gis_tile_new(NULL, NORTH, SOUTH, EAST, WEST);
306 elev->wms = gis_wms_new(
307 "http://www.nasa.network.com/elev", "srtm30", "application/bil",
308 "srtm/", "bil", TILE_WIDTH, TILE_HEIGHT);
310 static void gis_plugin_elev_dispose(GObject *gobject)
312 g_debug("GisPluginElev: dispose");
313 GisPluginElev *elev = GIS_PLUGIN_ELEV(gobject);
314 /* Drop references */
316 gis_viewer_clear_height_func(elev->viewer);
318 g_signal_handler_disconnect(elev->viewer, elev->sigid);
319 g_object_unref(elev->viewer);
322 G_OBJECT_CLASS(gis_plugin_elev_parent_class)->dispose(gobject);
324 static void gis_plugin_elev_finalize(GObject *gobject)
326 g_debug("GisPluginElev: finalize");
327 GisPluginElev *elev = GIS_PLUGIN_ELEV(gobject);
329 gis_tile_free(elev->tiles, _free_tile, elev);
330 gis_wms_free(elev->wms);
331 g_mutex_free(elev->mutex);
332 G_OBJECT_CLASS(gis_plugin_elev_parent_class)->finalize(gobject);
335 static void gis_plugin_elev_class_init(GisPluginElevClass *klass)
337 g_debug("GisPluginElev: class_init");
338 GObjectClass *gobject_class = (GObjectClass*)klass;
339 gobject_class->dispose = gis_plugin_elev_dispose;
340 gobject_class->finalize = gis_plugin_elev_finalize;