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