]> Pileus Git - grits/blob - src/plugins/map.c
d8ff6c2458ee9d0defa3793ded3d6aae744d669b
[grits] / src / plugins / map.c
1 /*
2  * Copyright (C) 2009 Andy Spencer <spenceal@rose-hulman.edu>
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 #include <time.h>
19 #include <glib/gstdio.h>
20 #include <GL/gl.h>
21
22 #include <gis.h>
23
24 #include "map.h"
25
26 #define MAX_RESOLUTION 500
27 #define TILE_WIDTH     1024
28 #define TILE_HEIGHT    512
29
30 struct _LoadTileData {
31         GisPluginMap *self;
32         GisTile       *tile;
33         GdkPixbuf     *pixbuf;
34 };
35 static gboolean _load_tile_cb(gpointer _data)
36 {
37         struct _LoadTileData *data = _data;
38         GisPluginMap *self   = data->self;
39         GisTile       *tile   = data->tile;
40         GdkPixbuf     *pixbuf = data->pixbuf;
41         g_free(data);
42
43         /* Create Texture */
44         g_debug("GisPluginMap: _load_tile_cb start");
45         guchar   *pixels = gdk_pixbuf_get_pixels(pixbuf);
46         gboolean  alpha  = gdk_pixbuf_get_has_alpha(pixbuf);
47         gint      width  = gdk_pixbuf_get_width(pixbuf);
48         gint      height = gdk_pixbuf_get_height(pixbuf);
49
50         guint *tex = g_new0(guint, 1);
51         gis_viewer_begin(self->viewer);
52         glGenTextures(1, tex);
53         glBindTexture(GL_TEXTURE_2D, *tex);
54
55         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
56         glPixelStorei(GL_PACK_ALIGNMENT, 1);
57         glTexImage2D(GL_TEXTURE_2D, 0, 4, width, height, 0,
58                         (alpha ? GL_RGBA : GL_RGB), GL_UNSIGNED_BYTE, pixels);
59         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
60         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
61         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
62         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
63         glFlush();
64         gis_viewer_end(self->viewer);
65
66         tile->data = tex;
67         gtk_widget_queue_draw(GTK_WIDGET(self->viewer));
68         g_object_unref(pixbuf);
69         return FALSE;
70 }
71
72 static void _load_tile(GisTile *tile, gpointer _self)
73 {
74         GisPluginMap *self = _self;
75         g_debug("GisPluginMap: _load_tile start %p", g_thread_self());
76         char *path = gis_wms_make_local(self->wms, tile);
77         struct _LoadTileData *data = g_new0(struct _LoadTileData, 1);
78         data->self   = self;
79         data->tile   = tile;
80         data->pixbuf = gdk_pixbuf_new_from_file(path, NULL);
81         if (data->pixbuf) {
82                 g_idle_add_full(G_PRIORITY_LOW, _load_tile_cb, data, NULL);
83         } else {
84                 g_warning("GisPluginMap: _load_tile - Error loading pixbuf %s", path);
85                 g_remove(path);
86         }
87         g_free(path);
88         g_debug("GisPluginMap: _load_tile end %p", g_thread_self());
89 }
90
91 static gboolean _free_tile_cb(gpointer data)
92 {
93         glDeleteTextures(1, data);
94         g_free(data);
95         return FALSE;
96 }
97 static void _free_tile(GisTile *tile, gpointer _self)
98 {
99         GisPluginMap *self = _self;
100         g_debug("GisPluginMap: _free_tile: %p=%d", tile->data, *(guint*)tile->data);
101         g_idle_add_full(G_PRIORITY_LOW, _free_tile_cb, tile->data, NULL);
102 }
103
104 static gpointer _update_tiles(gpointer _self)
105 {
106         g_debug("GisPluginMap: _update_tiles");
107         GisPluginMap *self = _self;
108         g_mutex_lock(self->mutex);
109         gdouble lat, lon, elev;
110         gis_viewer_get_location(self->viewer, &lat, &lon, &elev);
111         gis_tile_update(self->tiles,
112                         MAX_RESOLUTION, TILE_WIDTH, TILE_WIDTH,
113                         lat, lon, elev,
114                         _load_tile, self);
115         gis_tile_gc(self->tiles, time(NULL)-10,
116                         _free_tile, self);
117         g_mutex_unlock(self->mutex);
118         return NULL;
119 }
120
121 /*************
122  * Callbacks *
123  *************/
124 static void _on_location_changed(GisViewer *viewer,
125                 gdouble lat, gdouble lon, gdouble elev, GisPluginMap *self)
126 {
127         g_thread_create(_update_tiles, self, FALSE, NULL);
128 }
129
130 static gpointer _expose(GisCallback *callback, gpointer _self)
131 {
132         GisPluginMap *self = GIS_PLUGIN_MAP(_self);
133         g_debug("GisPluginMap: expose viewer=%p tiles=%p,%p",
134                         self->viewer, self->tiles, self->tiles->data);
135         gis_viewer_render_tiles(self->viewer, self->tiles);
136         return NULL;
137 }
138
139 /***********
140  * Methods *
141  ***********/
142 GisPluginMap *gis_plugin_map_new(GisViewer *viewer)
143 {
144         g_debug("GisPluginMap: new");
145         GisPluginMap *self = g_object_new(GIS_TYPE_PLUGIN_MAP, NULL);
146         self->viewer = g_object_ref(viewer);
147
148         /* Load initial tiles */
149         _load_tile(self->tiles, self);
150         g_thread_create(_update_tiles, self, FALSE, NULL);
151
152         /* Connect signals */
153         self->sigid = g_signal_connect(self->viewer, "location-changed",
154                         G_CALLBACK(_on_location_changed), self);
155
156         /* Add renderers */
157         GisCallback *callback = gis_callback_new(_expose, self);
158         gis_viewer_add(viewer, GIS_OBJECT(callback), GIS_LEVEL_WORLD, 0);
159
160         return self;
161 }
162
163
164 /****************
165  * GObject code *
166  ****************/
167 /* Plugin init */
168 static void gis_plugin_map_plugin_init(GisPluginInterface *iface);
169 G_DEFINE_TYPE_WITH_CODE(GisPluginMap, gis_plugin_map, G_TYPE_OBJECT,
170                 G_IMPLEMENT_INTERFACE(GIS_TYPE_PLUGIN,
171                         gis_plugin_map_plugin_init));
172 static void gis_plugin_map_plugin_init(GisPluginInterface *iface)
173 {
174         g_debug("GisPluginMap: plugin_init");
175         /* Add methods to the interface */
176 }
177 /* Class/Object init */
178 static void gis_plugin_map_init(GisPluginMap *self)
179 {
180         g_debug("GisPluginMap: init");
181         /* Set defaults */
182         self->mutex  = g_mutex_new();
183         self->tiles  = gis_tile_new(NULL, NORTH, SOUTH, EAST, WEST);
184         self->wms    = gis_wms_new(
185                 "http://labs.metacarta.com/wms/vmap0", "basic", "image/png",
186                 "osm/", "png", TILE_WIDTH, TILE_HEIGHT);
187 }
188 static void gis_plugin_map_dispose(GObject *gobject)
189 {
190         g_debug("GisPluginMap: dispose");
191         GisPluginMap *self = GIS_PLUGIN_MAP(gobject);
192         /* Drop references */
193         if (self->viewer) {
194                 g_signal_handler_disconnect(self->viewer, self->sigid);
195                 g_object_unref(self->viewer);
196                 self->viewer = NULL;
197         }
198         G_OBJECT_CLASS(gis_plugin_map_parent_class)->dispose(gobject);
199 }
200 static void gis_plugin_map_finalize(GObject *gobject)
201 {
202         g_debug("GisPluginMap: finalize");
203         GisPluginMap *self = GIS_PLUGIN_MAP(gobject);
204         /* Free data */
205         gis_tile_free(self->tiles, _free_tile, self);
206         gis_wms_free(self->wms);
207         g_mutex_free(self->mutex);
208         G_OBJECT_CLASS(gis_plugin_map_parent_class)->finalize(gobject);
209
210 }
211 static void gis_plugin_map_class_init(GisPluginMapClass *klass)
212 {
213         g_debug("GisPluginMap: class_init");
214         GObjectClass *gobject_class = (GObjectClass*)klass;
215         gobject_class->dispose  = gis_plugin_map_dispose;
216         gobject_class->finalize = gis_plugin_map_finalize;
217 }