]> Pileus Git - grits/blob - src/plugins/sat.c
Miscellaneous bug fixes
[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 /**
19  * SECTION:sat
20  * @short_description: Satellite plugin
21  *
22  * #GisPluginSat provides overlays using satellite imagery. This is mostly
23  * provided by NASA's Blue Marble Next Generation.
24  */
25
26 #include <time.h>
27 #include <string.h>
28 #include <glib/gstdio.h>
29 #include <GL/gl.h>
30
31 #include <gis.h>
32
33 #include "sat.h"
34
35 #define MAX_RESOLUTION 500
36 #define TILE_WIDTH     1024
37 #define TILE_HEIGHT    512
38
39 struct _LoadTileData {
40         GisPluginSat *sat;
41         GisTile      *tile;
42         GdkPixbuf    *pixbuf;
43 };
44 static gboolean _load_tile_cb(gpointer _data)
45 {
46         struct _LoadTileData *data = _data;
47         GisPluginSat *sat    = data->sat;
48         GisTile      *tile   = data->tile;
49         GdkPixbuf    *pixbuf = data->pixbuf;
50         g_free(data);
51
52         /* Create Texture */
53         g_debug("GisPluginSat: _load_tile_cb start");
54         guchar   *pixels = gdk_pixbuf_get_pixels(pixbuf);
55         gboolean  alpha  = gdk_pixbuf_get_has_alpha(pixbuf);
56         gint      width  = gdk_pixbuf_get_width(pixbuf);
57         gint      height = gdk_pixbuf_get_height(pixbuf);
58
59         /* Draw a border */
60         //gint border = 10;
61         //gint stride = gdk_pixbuf_get_rowstride(pixbuf);
62         //for (int i = 0; i < border; i++) {
63         //      memset(&pixels[(       i)*stride], 0xff, stride);
64         //      memset(&pixels[(height-i)*stride], 0xff, stride);
65         //}
66         //for (int i = 0; i < height; i++) {
67         //      memset(&pixels[(i*stride)], 0xff, border*4);
68         //      memset(&pixels[(i*stride)+((width-border)*4)], 0xff, border*4);
69         //}
70
71         guint *tex = g_new0(guint, 1);
72         glGenTextures(1, tex);
73         glBindTexture(GL_TEXTURE_2D, *tex);
74
75         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
76         glPixelStorei(GL_PACK_ALIGNMENT, 1);
77         glTexImage2D(GL_TEXTURE_2D, 0, 4, width, height, 0,
78                         (alpha ? GL_RGBA : GL_RGB), GL_UNSIGNED_BYTE, pixels);
79         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
80         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
81         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
82         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
83         glFlush();
84
85         tile->data = tex;
86         gtk_widget_queue_draw(GTK_WIDGET(sat->viewer));
87         g_object_unref(pixbuf);
88         return FALSE;
89 }
90
91 static void _load_tile(GisTile *tile, gpointer _sat)
92 {
93         GisPluginSat *sat = _sat;
94         g_debug("GisPluginSat: _load_tile start %p", g_thread_self());
95         char *path = gis_wms_fetch(sat->wms, tile, GIS_ONCE, NULL, NULL);
96         struct _LoadTileData *data = g_new0(struct _LoadTileData, 1);
97         data->sat   = sat;
98         data->tile   = tile;
99         data->pixbuf = gdk_pixbuf_new_from_file(path, NULL);
100         if (data->pixbuf) {
101                 g_idle_add_full(G_PRIORITY_LOW, _load_tile_cb, data, NULL);
102         } else {
103                 g_warning("GisPluginSat: _load_tile - Error loading pixbuf %s", path);
104                 g_free(data);
105                 g_remove(path);
106         }
107         g_free(path);
108         g_debug("GisPluginSat: _load_tile end %p", g_thread_self());
109 }
110
111 static gboolean _free_tile_cb(gpointer data)
112 {
113         glDeleteTextures(1, data);
114         g_free(data);
115         return FALSE;
116 }
117 static void _free_tile(GisTile *tile, gpointer _sat)
118 {
119         GisPluginSat *sat = _sat;
120         g_debug("GisPluginSat: _free_tile: %p", tile->data);
121         if (tile->data)
122                 g_idle_add_full(G_PRIORITY_LOW, _free_tile_cb, tile->data, NULL);
123 }
124
125 static gpointer _update_tiles(gpointer _sat)
126 {
127         g_debug("GisPluginSat: _update_tiles");
128         GisPluginSat *sat = _sat;
129         if (!g_mutex_trylock(sat->mutex))
130                 return NULL;
131         GisPoint eye;
132         gis_viewer_get_location(sat->viewer, &eye.lat, &eye.lon, &eye.elev);
133         gis_tile_update(sat->tiles, &eye,
134                         MAX_RESOLUTION, TILE_WIDTH, TILE_WIDTH,
135                         _load_tile, sat);
136         gis_tile_gc(sat->tiles, time(NULL)-10,
137                         _free_tile, sat);
138         g_mutex_unlock(sat->mutex);
139         return NULL;
140 }
141
142 /*************
143  * Callbacks *
144  *************/
145 static void _on_location_changed(GisViewer *viewer,
146                 gdouble lat, gdouble lon, gdouble elev, GisPluginSat *sat)
147 {
148         g_thread_create(_update_tiles, sat, FALSE, NULL);
149 }
150
151 static gpointer _threaded_init(GisPluginSat *sat)
152 {
153         _load_tile(sat->tiles, sat);
154         _update_tiles(sat);
155         return NULL;
156 }
157
158 /***********
159  * Methods *
160  ***********/
161 /**
162  * gis_plugin_sat_new:
163  * @viewer: the #GisViewer to use for drawing
164  *
165  * Create a new instance of the satellite plugin.
166  *
167  * Returns: the new #GisPluginSat
168  */
169 GisPluginSat *gis_plugin_sat_new(GisViewer *viewer)
170 {
171         g_debug("GisPluginSat: new");
172         GisPluginSat *sat = g_object_new(GIS_TYPE_PLUGIN_SAT, NULL);
173         sat->viewer = g_object_ref(viewer);
174
175         /* Load initial tiles */
176         g_thread_create((GThreadFunc)_threaded_init, sat, FALSE, NULL);
177
178         /* Connect signals */
179         sat->sigid = g_signal_connect(sat->viewer, "location-changed",
180                         G_CALLBACK(_on_location_changed), sat);
181
182         /* Add renderers */
183         gis_viewer_add(viewer, GIS_OBJECT(sat->tiles), GIS_LEVEL_WORLD, FALSE);
184
185         return sat;
186 }
187
188
189 /****************
190  * GObject code *
191  ****************/
192 /* Plugin init */
193 static void gis_plugin_sat_plugin_init(GisPluginInterface *iface);
194 G_DEFINE_TYPE_WITH_CODE(GisPluginSat, gis_plugin_sat, G_TYPE_OBJECT,
195                 G_IMPLEMENT_INTERFACE(GIS_TYPE_PLUGIN,
196                         gis_plugin_sat_plugin_init));
197 static void gis_plugin_sat_plugin_init(GisPluginInterface *iface)
198 {
199         g_debug("GisPluginSat: plugin_init");
200         /* Add methods to the interface */
201 }
202 /* Class/Object init */
203 static void gis_plugin_sat_init(GisPluginSat *sat)
204 {
205         g_debug("GisPluginSat: init");
206         /* Set defaults */
207         sat->mutex = g_mutex_new();
208         sat->tiles = gis_tile_new(NULL, NORTH, SOUTH, EAST, WEST);
209         sat->wms   = gis_wms_new(
210                 "http://www.nasa.network.com/wms", "bmng200406", "image/jpeg",
211                 "bmng/", "jpg", TILE_WIDTH, TILE_HEIGHT);
212 }
213 static void gis_plugin_sat_dispose(GObject *gobject)
214 {
215         g_debug("GisPluginSat: dispose");
216         GisPluginSat *sat = GIS_PLUGIN_SAT(gobject);
217         /* Drop references */
218         if (sat->viewer) {
219                 g_signal_handler_disconnect(sat->viewer, sat->sigid);
220                 g_object_unref(sat->viewer);
221                 sat->viewer = NULL;
222         }
223         G_OBJECT_CLASS(gis_plugin_sat_parent_class)->dispose(gobject);
224 }
225 static void gis_plugin_sat_finalize(GObject *gobject)
226 {
227         g_debug("GisPluginSat: finalize");
228         GisPluginSat *sat = GIS_PLUGIN_SAT(gobject);
229         /* Free data */
230         gis_tile_free(sat->tiles, _free_tile, sat);
231         gis_wms_free(sat->wms);
232         g_mutex_free(sat->mutex);
233         G_OBJECT_CLASS(gis_plugin_sat_parent_class)->finalize(gobject);
234
235 }
236 static void gis_plugin_sat_class_init(GisPluginSatClass *klass)
237 {
238         g_debug("GisPluginSat: class_init");
239         GObjectClass *gobject_class = (GObjectClass*)klass;
240         gobject_class->dispose  = gis_plugin_sat_dispose;
241         gobject_class->finalize = gis_plugin_sat_finalize;
242 }