]> Pileus Git - grits/blob - src/plugins/sat.c
Better error checking for elev and sat plugins
[grits] / src / plugins / sat.c
1 /*
2  * Copyright (C) 2009-2010 Andy Spencer <andy753421@gmail.com>
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 "sat.h"
25
26 #define MAX_RESOLUTION 500
27 #define TILE_WIDTH     1024
28 #define TILE_HEIGHT    512
29
30 struct _LoadTileData {
31         GisPluginSat *self;
32         GisTile      *tile;
33         GdkPixbuf    *pixbuf;
34 };
35 static gboolean _load_tile_cb(gpointer _data)
36 {
37         struct _LoadTileData *data = _data;
38         GisPluginSat *self   = data->self;
39         GisTile      *tile   = data->tile;
40         GdkPixbuf    *pixbuf = data->pixbuf;
41         g_free(data);
42
43         /* Create Texture */
44         g_debug("GisPluginSat: _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         GisPluginSat *self = _self;
75         g_debug("GisPluginSat: _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("GisPluginSat: _load_tile - Error loading pixbuf %s", path);
85                 g_free(data);
86                 g_remove(path);
87         }
88         g_free(path);
89         g_debug("GisPluginSat: _load_tile end %p", g_thread_self());
90 }
91
92 static gboolean _free_tile_cb(gpointer data)
93 {
94         glDeleteTextures(1, data);
95         g_free(data);
96         return FALSE;
97 }
98 static void _free_tile(GisTile *tile, gpointer _self)
99 {
100         GisPluginSat *self = _self;
101         g_debug("GisPluginSat: _free_tile: %p=%d", tile->data, *(guint*)tile->data);
102         g_idle_add_full(G_PRIORITY_LOW, _free_tile_cb, tile->data, NULL);
103 }
104
105 static gpointer _update_tiles(gpointer _self)
106 {
107         g_debug("GisPluginSat: _update_tiles");
108         GisPluginSat *self = _self;
109         g_mutex_lock(self->mutex);
110         gdouble lat, lon, elev;
111         gis_viewer_get_location(self->viewer, &lat, &lon, &elev);
112         gis_tile_update(self->tiles,
113                         MAX_RESOLUTION, TILE_WIDTH, TILE_WIDTH,
114                         lat, lon, elev,
115                         _load_tile, self);
116         gis_tile_gc(self->tiles, time(NULL)-10,
117                         _free_tile, self);
118         g_mutex_unlock(self->mutex);
119         return NULL;
120 }
121
122 /*************
123  * Callbacks *
124  *************/
125 static void _on_location_changed(GisViewer *viewer,
126                 gdouble lat, gdouble lon, gdouble elev, GisPluginSat *self)
127 {
128         g_thread_create(_update_tiles, self, FALSE, NULL);
129 }
130
131 static gpointer _expose(GisCallback *callback, gpointer _self)
132 {
133         GisPluginSat *self = GIS_PLUGIN_SAT(_self);
134         g_debug("GisPluginSat: expose viewer=%p tiles=%p,%p",
135                         self->viewer, self->tiles, self->tiles->data);
136         gis_viewer_render_tiles(self->viewer, self->tiles);
137         return NULL;
138 }
139
140 /***********
141  * Methods *
142  ***********/
143 GisPluginSat *gis_plugin_sat_new(GisViewer *viewer)
144 {
145         g_debug("GisPluginSat: new");
146         GisPluginSat *self = g_object_new(GIS_TYPE_PLUGIN_SAT, NULL);
147         self->viewer = g_object_ref(viewer);
148
149         /* Load initial tiles */
150         _load_tile(self->tiles, self);
151         g_thread_create(_update_tiles, self, FALSE, NULL);
152
153         /* Connect signals */
154         self->sigid = g_signal_connect(self->viewer, "location-changed",
155                         G_CALLBACK(_on_location_changed), self);
156
157         /* Add renderers */
158         GisCallback *callback = gis_callback_new(_expose, self);
159         gis_viewer_add(viewer, GIS_OBJECT(callback), GIS_LEVEL_WORLD, 0);
160
161         return self;
162 }
163
164
165 /****************
166  * GObject code *
167  ****************/
168 /* Plugin init */
169 static void gis_plugin_sat_plugin_init(GisPluginInterface *iface);
170 G_DEFINE_TYPE_WITH_CODE(GisPluginSat, gis_plugin_sat, G_TYPE_OBJECT,
171                 G_IMPLEMENT_INTERFACE(GIS_TYPE_PLUGIN,
172                         gis_plugin_sat_plugin_init));
173 static void gis_plugin_sat_plugin_init(GisPluginInterface *iface)
174 {
175         g_debug("GisPluginSat: plugin_init");
176         /* Add methods to the interface */
177 }
178 /* Class/Object init */
179 static void gis_plugin_sat_init(GisPluginSat *self)
180 {
181         g_debug("GisPluginSat: init");
182         /* Set defaults */
183         self->mutex = g_mutex_new();
184         self->tiles = gis_tile_new(NULL, NORTH, SOUTH, EAST, WEST);
185         self->wms   = gis_wms_new(
186                 "http://www.nasa.network.com/wms", "bmng200406", "image/jpeg",
187                 "bmng/", "jpg", TILE_WIDTH, TILE_HEIGHT);
188 }
189 static void gis_plugin_sat_dispose(GObject *gobject)
190 {
191         g_debug("GisPluginSat: dispose");
192         GisPluginSat *self = GIS_PLUGIN_SAT(gobject);
193         /* Drop references */
194         if (self->viewer) {
195                 g_signal_handler_disconnect(self->viewer, self->sigid);
196                 g_object_unref(self->viewer);
197                 self->viewer = NULL;
198         }
199         G_OBJECT_CLASS(gis_plugin_sat_parent_class)->dispose(gobject);
200 }
201 static void gis_plugin_sat_finalize(GObject *gobject)
202 {
203         g_debug("GisPluginSat: finalize");
204         GisPluginSat *self = GIS_PLUGIN_SAT(gobject);
205         /* Free data */
206         gis_tile_free(self->tiles, _free_tile, self);
207         gis_wms_free(self->wms);
208         g_mutex_free(self->mutex);
209         G_OBJECT_CLASS(gis_plugin_sat_parent_class)->finalize(gobject);
210
211 }
212 static void gis_plugin_sat_class_init(GisPluginSatClass *klass)
213 {
214         g_debug("GisPluginSat: class_init");
215         GObjectClass *gobject_class = (GObjectClass*)klass;
216         gobject_class->dispose  = gis_plugin_sat_dispose;
217         gobject_class->finalize = gis_plugin_sat_finalize;
218 }