]> Pileus Git - grits/blob - src/plugins/sat.c
9e1af30e6a5eca5a561c19aac13be5814b4426a9
[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=%d", tile->data, *(guint*)tile->data);
121         g_idle_add_full(G_PRIORITY_LOW, _free_tile_cb, tile->data, NULL);
122 }
123
124 static gpointer _update_tiles(gpointer _sat)
125 {
126         g_debug("GisPluginSat: _update_tiles");
127         GisPluginSat *sat = _sat;
128         if (!g_mutex_trylock(sat->mutex))
129                 return NULL;
130         GisPoint eye;
131         gis_viewer_get_location(sat->viewer, &eye.lat, &eye.lon, &eye.elev);
132         gis_tile_update(sat->tiles, &eye,
133                         MAX_RESOLUTION, TILE_WIDTH, TILE_WIDTH,
134                         _load_tile, sat);
135         gis_tile_gc(sat->tiles, time(NULL)-10,
136                         _free_tile, sat);
137         g_mutex_unlock(sat->mutex);
138         return NULL;
139 }
140
141 /*************
142  * Callbacks *
143  *************/
144 static void _on_location_changed(GisViewer *viewer,
145                 gdouble lat, gdouble lon, gdouble elev, GisPluginSat *sat)
146 {
147         g_thread_create(_update_tiles, sat, FALSE, NULL);
148 }
149
150 static gpointer _threaded_init(GisPluginSat *sat)
151 {
152         _load_tile(sat->tiles, sat);
153         _update_tiles(sat);
154         return NULL;
155 }
156
157 /***********
158  * Methods *
159  ***********/
160 /**
161  * gis_plugin_sat_new:
162  * @viewer: the #GisViewer to use for drawing
163  *
164  * Create a new instance of the satellite plugin.
165  *
166  * Returns: the new #GisPluginSat
167  */
168 GisPluginSat *gis_plugin_sat_new(GisViewer *viewer)
169 {
170         g_debug("GisPluginSat: new");
171         GisPluginSat *sat = g_object_new(GIS_TYPE_PLUGIN_SAT, NULL);
172         sat->viewer = g_object_ref(viewer);
173
174         /* Load initial tiles */
175         g_thread_create((GThreadFunc)_threaded_init, sat, FALSE, NULL);
176
177         /* Connect signals */
178         sat->sigid = g_signal_connect(sat->viewer, "location-changed",
179                         G_CALLBACK(_on_location_changed), sat);
180
181         /* Add renderers */
182         gis_viewer_add(viewer, GIS_OBJECT(sat->tiles), GIS_LEVEL_WORLD, FALSE);
183
184         return sat;
185 }
186
187
188 /****************
189  * GObject code *
190  ****************/
191 /* Plugin init */
192 static void gis_plugin_sat_plugin_init(GisPluginInterface *iface);
193 G_DEFINE_TYPE_WITH_CODE(GisPluginSat, gis_plugin_sat, G_TYPE_OBJECT,
194                 G_IMPLEMENT_INTERFACE(GIS_TYPE_PLUGIN,
195                         gis_plugin_sat_plugin_init));
196 static void gis_plugin_sat_plugin_init(GisPluginInterface *iface)
197 {
198         g_debug("GisPluginSat: plugin_init");
199         /* Add methods to the interface */
200 }
201 /* Class/Object init */
202 static void gis_plugin_sat_init(GisPluginSat *sat)
203 {
204         g_debug("GisPluginSat: init");
205         /* Set defaults */
206         sat->mutex = g_mutex_new();
207         sat->tiles = gis_tile_new(NULL, NORTH, SOUTH, EAST, WEST);
208         sat->wms   = gis_wms_new(
209                 "http://www.nasa.network.com/wms", "bmng200406", "image/jpeg",
210                 "bmng/", "jpg", TILE_WIDTH, TILE_HEIGHT);
211 }
212 static void gis_plugin_sat_dispose(GObject *gobject)
213 {
214         g_debug("GisPluginSat: dispose");
215         GisPluginSat *sat = GIS_PLUGIN_SAT(gobject);
216         /* Drop references */
217         if (sat->viewer) {
218                 g_signal_handler_disconnect(sat->viewer, sat->sigid);
219                 g_object_unref(sat->viewer);
220                 sat->viewer = NULL;
221         }
222         G_OBJECT_CLASS(gis_plugin_sat_parent_class)->dispose(gobject);
223 }
224 static void gis_plugin_sat_finalize(GObject *gobject)
225 {
226         g_debug("GisPluginSat: finalize");
227         GisPluginSat *sat = GIS_PLUGIN_SAT(gobject);
228         /* Free data */
229         gis_tile_free(sat->tiles, _free_tile, sat);
230         gis_wms_free(sat->wms);
231         g_mutex_free(sat->mutex);
232         G_OBJECT_CLASS(gis_plugin_sat_parent_class)->finalize(gobject);
233
234 }
235 static void gis_plugin_sat_class_init(GisPluginSatClass *klass)
236 {
237         g_debug("GisPluginSat: class_init");
238         GObjectClass *gobject_class = (GObjectClass*)klass;
239         gobject_class->dispose  = gis_plugin_sat_dispose;
240         gobject_class->finalize = gis_plugin_sat_finalize;
241 }