X-Git-Url: http://pileus.org/git/?a=blobdiff_plain;f=src%2Fplugins%2Felev.c;h=7540a0816bc1d3bb7569fe93ac8d7a2528a1cd41;hb=fb8e4db7bd22e6723a6e22b8f5de52d5c8bcc334;hp=4c28847a40b2972098013e0b2018c6477df36679;hpb=ccfdb537f75386aa38db18660f14f0dc41b54ed8;p=grits diff --git a/src/plugins/elev.c b/src/plugins/elev.c index 4c28847..7540a08 100644 --- a/src/plugins/elev.c +++ b/src/plugins/elev.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009-2010 Andy Spencer + * Copyright (C) 2009-2011 Andy Spencer * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -15,31 +15,46 @@ * along with this program. If not, see . */ -#include +/** + * SECTION:elev + * @short_description: Elevation plugin + * + * #GritsPluginElev provides access to ground elevation. It does this in two ways: + * First, it provides a height function used by the viewer when drawing the + * world. Second, it can load the elevation data into an image and draw a + * greyscale elevation overlay on the planets surface. + */ + +#include #include -#include -#include +#include #include "elev.h" -#define MAX_RESOLUTION 500 +/* Configuration */ +#define LOAD_BIL TRUE +#define LOAD_TEX FALSE + +/* Tile size constnats */ +#define MAX_RESOLUTION 50 #define TILE_WIDTH 1024 #define TILE_HEIGHT 512 +#define TILE_CHANNELS 4 #define TILE_SIZE (TILE_WIDTH*TILE_HEIGHT*sizeof(guint16)) struct _TileData { - /* OpenGL has to be first to make gis_opengl_render_tiles happy */ - guint opengl; - guint16 *bil; + /* OpenGL has to be first to make grits_tile_draw happy */ + guint tex; + guint16 *bil; }; -static gdouble _height_func(gdouble lat, gdouble lon, gpointer _self) +static gdouble _height_func(gdouble lat, gdouble lon, gpointer _elev) { - GisPluginElev *self = _self; - if (!self) return 0; + GritsPluginElev *elev = _elev; + if (!elev) return 0; - GisTile *tile = gis_tile_find(self->tiles, lat, lon); + GritsTile *tile = grits_tile_find(elev->tiles, lat, lon); if (!tile) return 0; struct _TileData *data = tile->data; @@ -79,141 +94,143 @@ static gdouble _height_func(gdouble lat, gdouble lon, gpointer _self) gint16 px01 = bil[MIN((y_flr+1),h-1)*w + MIN((x_flr ),w-1)]; gint16 px11 = bil[MIN((y_flr+1),h-1)*w + MIN((x_flr+1),w-1)]; - gdouble elev = - px00 * (1-x_rem) * (1-y_rem) + - px10 * ( x_rem) * (1-y_rem) + - px01 * (1-x_rem) * ( y_rem) + - px11 * ( x_rem) * ( y_rem); - return elev; + return px00 * (1-x_rem) * (1-y_rem) + + px10 * ( x_rem) * (1-y_rem) + + px01 * (1-x_rem) * ( y_rem) + + px11 * ( x_rem) * ( y_rem); } /********************** * Loader and Freeers * **********************/ -#define LOAD_BIL TRUE -#define LOAD_OPENGL FALSE + struct _LoadTileData { - GisPluginElev *self; - gchar *path; - GisTile *tile; - GdkPixbuf *pixbuf; - struct _TileData *data; + GritsPluginElev *elev; + GritsTile *tile; + guint8 *pixels; + struct _TileData *tdata; }; + static guint16 *_load_bil(gchar *path) { gsize len; gchar *data = NULL; g_file_get_contents(path, &data, &len, NULL); - g_debug("GisPluginElev: load_bil %p", data); + g_debug("GritsPluginElev: load_bil %p", data); if (len != TILE_SIZE) { - g_warning("GisPluginElev: _load_bil - unexpected tile size %d, != %d", - len, TILE_SIZE); + g_warning("GritsPluginElev: _load_bil - unexpected tile size %ld, != %ld", + (glong)len, (glong)TILE_SIZE); g_free(data); return NULL; } return (guint16*)data; } -static GdkPixbuf *_load_pixbuf(guint16 *bil) + +static guchar *_load_pixels(guint16 *bil) { - GdkPixbuf *pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, TILE_WIDTH, TILE_HEIGHT); - guchar *pixels = gdk_pixbuf_get_pixels(pixbuf); - gint stride = gdk_pixbuf_get_rowstride(pixbuf); - gint nchan = gdk_pixbuf_get_n_channels(pixbuf); + g_assert(TILE_CHANNELS == 4); + + guchar (*pixels)[TILE_WIDTH][TILE_CHANNELS] + = g_malloc0(TILE_HEIGHT * TILE_WIDTH * TILE_CHANNELS); for (int r = 0; r < TILE_HEIGHT; r++) { for (int c = 0; c < TILE_WIDTH; c++) { gint16 value = bil[r*TILE_WIDTH + c]; - //guchar color = (float)(MAX(value,0))/8848 * 255; guchar color = (float)value/8848 * 255; - pixels[r*stride + c*nchan + 0] = color; - pixels[r*stride + c*nchan + 1] = color; - pixels[r*stride + c*nchan + 2] = color; - if (nchan == 4) - pixels[r*stride + c*nchan + 3] = 128; + //guchar color = (float)(MAX(value,0))/8848 * 255; + pixels[r][c][0] = color; + pixels[r][c][1] = color; + pixels[r][c][2] = color; + pixels[r][c][3] = 0xff; } } - g_debug("GisPluginElev: load_pixbuf %p", pixbuf); - return pixbuf; -} -static guint _load_opengl(GdkPixbuf *pixbuf) -{ - /* Load image */ - guchar *pixels = gdk_pixbuf_get_pixels(pixbuf); - gint alpha = gdk_pixbuf_get_has_alpha(pixbuf); - gint nchan = 4; // gdk_pixbuf_get_n_channels(pixbuf); - gint width = gdk_pixbuf_get_width(pixbuf); - gint height = gdk_pixbuf_get_height(pixbuf); - - /* Create Texture */ - guint opengl; - glGenTextures(1, &opengl); - glBindTexture(GL_TEXTURE_2D, opengl); - - glPixelStorei(GL_UNPACK_ALIGNMENT, 1); - glPixelStorei(GL_PACK_ALIGNMENT, 1); - glTexImage2D(GL_TEXTURE_2D, 0, nchan, width, height, 0, - (alpha ? GL_RGBA : GL_RGB), GL_UNSIGNED_BYTE, pixels); - glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER); - - g_debug("GisPluginElev: load_opengl %d", opengl); - return opengl; + + g_debug("GritsPluginElev: load_pixels %p", pixels); + return (guchar*)pixels; } -static gboolean _load_tile_cb(gpointer _load) + +static gboolean _load_tile_cb(gpointer _data) { - struct _LoadTileData *load = _load; - g_debug("GisPluginElev: _load_tile_cb: %s", load->path); - GisPluginElev *self = load->self; - GisTile *tile = load->tile; - GdkPixbuf *pixbuf = load->pixbuf; - struct _TileData *data = load->data; - g_free(load->path); - g_free(load); - - if (LOAD_OPENGL) - data->opengl = _load_opengl(pixbuf); - - tile->data = data; - - /* Do necessasairy processing */ - /* TODO: Lock this and move to thread, can remove self from _load then */ - if (LOAD_BIL) - gis_viewer_set_height_func(self->viewer, tile, _height_func, self, TRUE); + struct _LoadTileData *data = _data; + struct _TileData *tdata = data->tdata; + g_debug("GritsPluginElev: _load_tile_cb start"); + if (data->elev->aborted) { + g_debug("GritsPluginElev: _load_tile - aborted"); + return FALSE; + } - /* Cleanup unneeded things */ - if (!LOAD_BIL) - g_free(data->bil); - if (LOAD_OPENGL) - g_object_unref(pixbuf); + /* Load OpenGL texture (from main thread) */ + if (data->pixels) { + glGenTextures(1, &tdata->tex); + glBindTexture(GL_TEXTURE_2D, tdata->tex); + + glPixelStorei(GL_UNPACK_ALIGNMENT, 1); + glPixelStorei(GL_PACK_ALIGNMENT, 1); + glTexImage2D(GL_TEXTURE_2D, 0, TILE_CHANNELS, TILE_WIDTH, TILE_HEIGHT, 0, + GL_RGBA, GL_UNSIGNED_BYTE, data->pixels); + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + glFlush(); + } + + /* Set hight function (from main thread) */ + if (tdata->bil) { + grits_viewer_set_height_func(data->elev->viewer, &data->tile->edge, + _height_func, data->elev, TRUE); + } + /* Queue tiles for drawing */ + data->tile->data = tdata; + gtk_widget_queue_draw(GTK_WIDGET(data->elev->viewer)); + + /* Cleanup */ + g_free(data->pixels); + g_free(data); return FALSE; } -static void _load_tile(GisTile *tile, gpointer _self) + +static void _load_tile(GritsTile *tile, gpointer _elev) { - GisPluginElev *self = _self; - - struct _LoadTileData *load = g_new0(struct _LoadTileData, 1); - load->path = gis_wms_make_local(self->wms, tile); - g_debug("GisPluginElev: _load_tile: %s", load->path); - load->self = self; - load->tile = tile; - load->data = g_new0(struct _TileData, 1); - if (LOAD_BIL || LOAD_OPENGL) { - load->data->bil = _load_bil(load->path); - if (!load->data->bil) { - g_remove(load->path); - g_free(load->path); - g_free(load); - return; - } - } - if (LOAD_OPENGL) { - load->pixbuf = _load_pixbuf(load->data->bil); + GritsPluginElev *elev = _elev; + guint16 *bil = NULL; + guchar *pixels = NULL; + + g_debug("GritsPluginElev: _load_tile start %p", g_thread_self()); + if (elev->aborted) { + g_debug("GritsPluginElev: _load_tile - aborted"); + return; } - g_idle_add_full(G_PRIORITY_LOW, _load_tile_cb, load, NULL); + /* Download tile */ + gchar *path = grits_wms_fetch(elev->wms, tile, GRITS_ONCE, NULL, NULL); + if (!path) return; + + /* Load bil */ + bil = _load_bil(path); + g_free(path); + if (!bil) return; + + /* Load pixels */ + if (LOAD_TEX) + pixels = _load_pixels(bil); + if (!LOAD_BIL) + g_free(bil); + + /* Copy pixbuf data for callback */ + struct _LoadTileData *data = g_new0(struct _LoadTileData, 1); + struct _TileData *tdata = g_new0(struct _TileData, 1); + data->elev = elev; + data->tile = tile; + data->pixels = pixels; + data->tdata = tdata; + tdata->tex = 0; + tdata->bil = bil; + + /* Load the GL texture from the main thread */ + g_idle_add_full(G_PRIORITY_LOW, _load_tile_cb, data, NULL); + g_debug("GritsPluginElev: _load_tile end %p", g_thread_self()); } static gboolean _free_tile_cb(gpointer _data) @@ -221,75 +238,69 @@ static gboolean _free_tile_cb(gpointer _data) struct _TileData *data = _data; if (LOAD_BIL) g_free(data->bil); - if (LOAD_OPENGL) - glDeleteTextures(1, &data->opengl); + if (LOAD_TEX) + glDeleteTextures(1, &data->tex); g_free(data); return FALSE; } -static void _free_tile(GisTile *tile, gpointer _self) +static void _free_tile(GritsTile *tile, gpointer _elev) { - GisPluginElev *self = _self; - g_debug("GisPluginElev: _free_tile: %p", tile->data); - g_idle_add_full(G_PRIORITY_LOW, _free_tile_cb, tile->data, NULL); + g_debug("GritsPluginElev: _free_tile: %p", tile->data); + if (tile->data) + g_idle_add_full(G_PRIORITY_LOW, _free_tile_cb, tile->data, NULL); } -static gpointer _update_tiles(gpointer _self) +static void _update_tiles(gpointer _, gpointer _elev) { - GisPluginElev *self = _self; - g_mutex_lock(self->mutex); - gdouble lat, lon, elev; - gis_viewer_get_location(self->viewer, &lat, &lon, &elev); - gis_tile_update(self->tiles, + g_debug("GritsPluginElev: _update_tiles"); + GritsPluginElev *elev = _elev; + GritsPoint eye; + grits_viewer_get_location(elev->viewer, &eye.lat, &eye.lon, &eye.elev); + grits_tile_update(elev->tiles, &eye, MAX_RESOLUTION, TILE_WIDTH, TILE_WIDTH, - lat, lon, elev, - _load_tile, self); - gis_tile_gc(self->tiles, time(NULL)-10, - _free_tile, self); - g_mutex_unlock(self->mutex); - return NULL; + _load_tile, elev); + grits_tile_gc(elev->tiles, time(NULL)-10, + _free_tile, elev); } /************* * Callbacks * *************/ -static void _on_location_changed(GisViewer *viewer, - gdouble lat, gdouble lon, gdouble elev, GisPluginElev *self) +static void _on_location_changed(GritsViewer *viewer, + gdouble lat, gdouble lon, gdouble elevation, GritsPluginElev *elev) { - g_thread_create(_update_tiles, self, FALSE, NULL); -} - -static gpointer _expose(GisCallback *callback, gpointer _self) -{ - GisPluginElev *self = GIS_PLUGIN_ELEV(_self); - g_debug("GisPluginElev: expose tiles=%p data=%p", - self->tiles, self->tiles->data); - if (LOAD_OPENGL) - gis_viewer_render_tiles(self->viewer, self->tiles); - return NULL; + g_thread_pool_push(elev->threads, NULL+1, NULL); } /*********** * Methods * ***********/ -GisPluginElev *gis_plugin_elev_new(GisViewer *viewer) +/** + * grits_plugin_elev_new: + * @viewer: the #GritsViewer to use for drawing + * + * Create a new instance of the elevation plugin. + * + * Returns: the new #GritsPluginElev + */ +GritsPluginElev *grits_plugin_elev_new(GritsViewer *viewer) { - g_debug("GisPluginElev: new"); - GisPluginElev *self = g_object_new(GIS_TYPE_PLUGIN_ELEV, NULL); - self->viewer = g_object_ref(viewer); + g_debug("GritsPluginElev: new"); + GritsPluginElev *elev = g_object_new(GRITS_TYPE_PLUGIN_ELEV, NULL); + elev->viewer = g_object_ref(viewer); /* Load initial tiles */ - _load_tile(self->tiles, self); - g_thread_create(_update_tiles, self, FALSE, NULL); + _update_tiles(NULL, elev); /* Connect signals */ - self->sigid = g_signal_connect(self->viewer, "location-changed", - G_CALLBACK(_on_location_changed), self); + elev->sigid = g_signal_connect(elev->viewer, "location-changed", + G_CALLBACK(_on_location_changed), elev); /* Add renderers */ - GisCallback *callback = gis_callback_new(_expose, self); - gis_viewer_add(viewer, GIS_OBJECT(callback), GIS_LEVEL_WORLD, 0); + if (LOAD_TEX) + grits_viewer_add(viewer, GRITS_OBJECT(elev->tiles), GRITS_LEVEL_WORLD, FALSE); - return self; + return elev; } @@ -297,55 +308,65 @@ GisPluginElev *gis_plugin_elev_new(GisViewer *viewer) * GObject code * ****************/ /* Plugin init */ -static void gis_plugin_elev_plugin_init(GisPluginInterface *iface); -G_DEFINE_TYPE_WITH_CODE(GisPluginElev, gis_plugin_elev, G_TYPE_OBJECT, - G_IMPLEMENT_INTERFACE(GIS_TYPE_PLUGIN, - gis_plugin_elev_plugin_init)); -static void gis_plugin_elev_plugin_init(GisPluginInterface *iface) +static void grits_plugin_elev_plugin_init(GritsPluginInterface *iface); +G_DEFINE_TYPE_WITH_CODE(GritsPluginElev, grits_plugin_elev, G_TYPE_OBJECT, + G_IMPLEMENT_INTERFACE(GRITS_TYPE_PLUGIN, + grits_plugin_elev_plugin_init)); +static void grits_plugin_elev_plugin_init(GritsPluginInterface *iface) { - g_debug("GisPluginElev: plugin_init"); + g_debug("GritsPluginElev: plugin_init"); /* Add methods to the interface */ } /* Class/Object init */ -static void gis_plugin_elev_init(GisPluginElev *self) +static void grits_plugin_elev_init(GritsPluginElev *elev) { - g_debug("GisPluginElev: init"); + g_debug("GritsPluginElev: init"); /* Set defaults */ - self->mutex = g_mutex_new(); - self->tiles = gis_tile_new(NULL, NORTH, SOUTH, EAST, WEST); - self->wms = gis_wms_new( - "http://www.nasa.network.com/srtm", "srtm30", "application/bil", + elev->threads = g_thread_pool_new(_update_tiles, elev, 1, FALSE, NULL); + elev->tiles = grits_tile_new(NULL, NORTH, SOUTH, EAST, WEST); + elev->wms = grits_wms_new( + "http://www.nasa.network.com/elev", "mergedSrtm", "application/bil", "srtm/", "bil", TILE_WIDTH, TILE_HEIGHT); + g_object_ref(elev->tiles); } -static void gis_plugin_elev_dispose(GObject *gobject) +static void grits_plugin_elev_dispose(GObject *gobject) { - g_debug("GisPluginElev: dispose"); - GisPluginElev *self = GIS_PLUGIN_ELEV(gobject); + g_debug("GritsPluginElev: dispose"); + GritsPluginElev *elev = GRITS_PLUGIN_ELEV(gobject); + elev->aborted = TRUE; /* Drop references */ - if (LOAD_BIL) - gis_viewer_clear_height_func(self->viewer); - if (self->viewer) { - g_signal_handler_disconnect(self->viewer, self->sigid); - g_object_unref(self->viewer); - self->viewer = NULL; + if (elev->viewer) { + GritsViewer *viewer = elev->viewer; + elev->viewer = NULL; + g_signal_handler_disconnect(viewer, elev->sigid); + if (LOAD_BIL) + grits_viewer_clear_height_func(viewer); + if (LOAD_TEX) + grits_viewer_remove(viewer, GRITS_OBJECT(elev->tiles)); + else + g_object_unref(elev->tiles); + soup_session_abort(elev->wms->http->soup); + g_thread_pool_free(elev->threads, TRUE, TRUE); + while (gtk_events_pending()) + gtk_main_iteration(); + g_object_unref(viewer); } - G_OBJECT_CLASS(gis_plugin_elev_parent_class)->dispose(gobject); + G_OBJECT_CLASS(grits_plugin_elev_parent_class)->dispose(gobject); } -static void gis_plugin_elev_finalize(GObject *gobject) +static void grits_plugin_elev_finalize(GObject *gobject) { - g_debug("GisPluginElev: finalize"); - GisPluginElev *self = GIS_PLUGIN_ELEV(gobject); + g_debug("GritsPluginElev: finalize"); + GritsPluginElev *elev = GRITS_PLUGIN_ELEV(gobject); /* Free data */ - gis_tile_free(self->tiles, _free_tile, self); - gis_wms_free(self->wms); - g_mutex_free(self->mutex); - G_OBJECT_CLASS(gis_plugin_elev_parent_class)->finalize(gobject); + grits_wms_free(elev->wms); + grits_tile_free(elev->tiles, _free_tile, elev); + G_OBJECT_CLASS(grits_plugin_elev_parent_class)->finalize(gobject); } -static void gis_plugin_elev_class_init(GisPluginElevClass *klass) +static void grits_plugin_elev_class_init(GritsPluginElevClass *klass) { - g_debug("GisPluginElev: class_init"); + g_debug("GritsPluginElev: class_init"); GObjectClass *gobject_class = (GObjectClass*)klass; - gobject_class->dispose = gis_plugin_elev_dispose; - gobject_class->finalize = gis_plugin_elev_finalize; + gobject_class->dispose = grits_plugin_elev_dispose; + gobject_class->finalize = grits_plugin_elev_finalize; }