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