2 * Copyright (C) 2009-2011 Andy Spencer <andy753421@gmail.com>
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.
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.
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/>.
20 * @short_description: Satellite plugin
22 * #GritsPluginSat provides overlays using satellite imagery. This is mostly
23 * provided by NASA's Blue Marble Next Generation.
27 #include <glib/gstdio.h>
34 #define MAX_RESOLUTION 500
35 #define TILE_WIDTH 1024
36 #define TILE_HEIGHT 512
38 struct _LoadTileData {
46 static gboolean _load_tile_cb(gpointer _data)
48 struct _LoadTileData *data = _data;
49 g_debug("GritsPluginSat: _load_tile_cb start");
51 guint *tex = g_new0(guint, 1);
52 glGenTextures(1, tex);
53 glBindTexture(GL_TEXTURE_2D, *tex);
55 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
56 glPixelStorei(GL_PACK_ALIGNMENT, 1);
57 glTexImage2D(GL_TEXTURE_2D, 0, 4, data->width, data->height, 0,
58 (data->alpha ? GL_RGBA : GL_RGB), GL_UNSIGNED_BYTE, data->pixels);
59 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
60 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
61 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
62 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
65 data->tile->data = tex;
66 gtk_widget_queue_draw(GTK_WIDGET(data->sat->viewer));
72 static void _load_tile(GritsTile *tile, gpointer _sat)
74 GritsPluginSat *sat = _sat;
75 g_debug("GritsPluginSat: _load_tile start %p", g_thread_self());
77 g_debug("GritsPluginSat: _load_tile - aborted");
82 gchar *path = grits_wms_fetch(sat->wms, tile, GRITS_ONCE, NULL, NULL);
83 if (!path) return; // Canceled/error
86 GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(path, NULL);
88 g_warning("GritsPluginSat: _load_tile - Error loading pixbuf %s", path);
95 /* Copy pixbuf data for callback */
96 struct _LoadTileData *data = g_new0(struct _LoadTileData, 1);
99 data->pixels = gdk_pixbuf_get_pixels(pixbuf);
100 data->alpha = gdk_pixbuf_get_has_alpha(pixbuf);
101 data->width = gdk_pixbuf_get_width(pixbuf);
102 data->height = gdk_pixbuf_get_height(pixbuf);
103 data->pixels = g_memdup(data->pixels,
104 data->width * data->height * (data->alpha ? 4 : 3));
105 g_object_unref(pixbuf);
109 //gint stride = gdk_pixbuf_get_rowstride(pixbuf);
110 //for (int i = 0; i < border; i++) {
111 // memset(&pixels[( i)*stride], 0xff, stride);
112 // memset(&pixels[(height-i)*stride], 0xff, stride);
114 //for (int i = 0; i < height; i++) {
115 // memset(&pixels[(i*stride)], 0xff, border*4);
116 // memset(&pixels[(i*stride)+((width-border)*4)], 0xff, border*4);
119 /* Load the GL texture from the main thread */
120 g_idle_add_full(G_PRIORITY_LOW, _load_tile_cb, data, NULL);
121 g_debug("GritsPluginSat: _load_tile end %p", g_thread_self());
124 static gboolean _free_tile_cb(gpointer data)
126 glDeleteTextures(1, data);
130 static void _free_tile(GritsTile *tile, gpointer _sat)
132 g_debug("GritsPluginSat: _free_tile: %p", tile->data);
134 g_idle_add_full(G_PRIORITY_LOW, _free_tile_cb, tile->data, NULL);
137 static void _update_tiles(gpointer _, gpointer _sat)
139 g_debug("GritsPluginSat: _update_tiles");
140 GritsPluginSat *sat = _sat;
142 grits_viewer_get_location(sat->viewer, &eye.lat, &eye.lon, &eye.elev);
143 grits_tile_update(sat->tiles, &eye,
144 MAX_RESOLUTION, TILE_WIDTH, TILE_WIDTH,
146 grits_tile_gc(sat->tiles, time(NULL)-10,
153 static void _on_location_changed(GritsViewer *viewer,
154 gdouble lat, gdouble lon, gdouble elev, GritsPluginSat *sat)
156 g_thread_pool_push(sat->threads, NULL+1, NULL);
163 * grits_plugin_sat_new:
164 * @viewer: the #GritsViewer to use for drawing
166 * Create a new instance of the satellite plugin.
168 * Returns: the new #GritsPluginSat
170 GritsPluginSat *grits_plugin_sat_new(GritsViewer *viewer)
172 g_debug("GritsPluginSat: new");
173 GritsPluginSat *sat = g_object_new(GRITS_TYPE_PLUGIN_SAT, NULL);
174 sat->viewer = g_object_ref(viewer);
176 /* Load initial tiles */
177 _load_tile(sat->tiles, sat);
178 _update_tiles(NULL, sat);
180 /* Connect signals */
181 sat->sigid = g_signal_connect(sat->viewer, "location-changed",
182 G_CALLBACK(_on_location_changed), sat);
185 grits_viewer_add(viewer, GRITS_OBJECT(sat->tiles), GRITS_LEVEL_WORLD, FALSE);
195 static void grits_plugin_sat_plugin_init(GritsPluginInterface *iface);
196 G_DEFINE_TYPE_WITH_CODE(GritsPluginSat, grits_plugin_sat, G_TYPE_OBJECT,
197 G_IMPLEMENT_INTERFACE(GRITS_TYPE_PLUGIN,
198 grits_plugin_sat_plugin_init));
199 static void grits_plugin_sat_plugin_init(GritsPluginInterface *iface)
201 g_debug("GritsPluginSat: plugin_init");
202 /* Add methods to the interface */
204 /* Class/Object init */
205 static void grits_plugin_sat_init(GritsPluginSat *sat)
207 g_debug("GritsPluginSat: init");
209 sat->threads = g_thread_pool_new(_update_tiles, sat, 1, FALSE, NULL);
210 sat->tiles = grits_tile_new(NULL, NORTH, SOUTH, EAST, WEST);
211 sat->wms = grits_wms_new(
212 "http://www.nasa.network.com/wms", "bmng200406", "image/jpeg",
213 "bmng/", "jpg", TILE_WIDTH, TILE_HEIGHT);
214 g_object_ref(sat->tiles);
216 static void grits_plugin_sat_dispose(GObject *gobject)
218 g_debug("GritsPluginSat: dispose");
219 GritsPluginSat *sat = GRITS_PLUGIN_SAT(gobject);
221 /* Drop references */
223 g_signal_handler_disconnect(sat->viewer, sat->sigid);
224 grits_viewer_remove(sat->viewer, GRITS_OBJECT(sat->tiles));
225 soup_session_abort(sat->wms->http->soup);
226 g_thread_pool_free(sat->threads, TRUE, TRUE);
227 while (gtk_events_pending())
228 gtk_main_iteration();
229 g_object_unref(sat->viewer);
232 G_OBJECT_CLASS(grits_plugin_sat_parent_class)->dispose(gobject);
234 static void grits_plugin_sat_finalize(GObject *gobject)
236 g_debug("GritsPluginSat: finalize");
237 GritsPluginSat *sat = GRITS_PLUGIN_SAT(gobject);
239 grits_wms_free(sat->wms);
240 grits_tile_free(sat->tiles, _free_tile, sat);
241 G_OBJECT_CLASS(grits_plugin_sat_parent_class)->finalize(gobject);
244 static void grits_plugin_sat_class_init(GritsPluginSatClass *klass)
246 g_debug("GritsPluginSat: class_init");
247 GObjectClass *gobject_class = (GObjectClass*)klass;
248 gobject_class->dispose = grits_plugin_sat_dispose;
249 gobject_class->finalize = grits_plugin_sat_finalize;