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>
33 #define MAX_RESOLUTION 500
34 #define TILE_WIDTH 1024
35 #define TILE_HEIGHT 512
37 struct _LoadTileData {
45 static gboolean _load_tile_cb(gpointer _data)
47 struct _LoadTileData *data = _data;
48 g_debug("GritsPluginSat: _load_tile_cb start");
50 guint *tex = g_new0(guint, 1);
51 glGenTextures(1, tex);
52 glBindTexture(GL_TEXTURE_2D, *tex);
54 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
55 glPixelStorei(GL_PACK_ALIGNMENT, 1);
56 glTexImage2D(GL_TEXTURE_2D, 0, 4, data->width, data->height, 0,
57 (data->alpha ? GL_RGBA : GL_RGB), GL_UNSIGNED_BYTE, data->pixels);
58 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
59 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
60 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
61 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
64 data->tile->data = tex;
65 gtk_widget_queue_draw(GTK_WIDGET(data->sat->viewer));
71 static void _load_tile(GritsTile *tile, gpointer _sat)
73 GritsPluginSat *sat = _sat;
74 g_debug("GritsPluginSat: _load_tile start %p", g_thread_self());
76 g_debug("GritsPluginSat: _load_tile - aborted");
81 gchar *path = grits_wms_fetch(sat->wms, tile, GRITS_ONCE, NULL, NULL);
82 if (!path) return; // Canceled/error
85 GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(path, NULL);
87 g_warning("GritsPluginSat: _load_tile - Error loading pixbuf %s", path);
94 /* Copy pixbuf data for callback */
95 struct _LoadTileData *data = g_new0(struct _LoadTileData, 1);
98 data->pixels = gdk_pixbuf_get_pixels(pixbuf);
99 data->alpha = gdk_pixbuf_get_has_alpha(pixbuf);
100 data->width = gdk_pixbuf_get_width(pixbuf);
101 data->height = gdk_pixbuf_get_height(pixbuf);
102 data->pixels = g_memdup(data->pixels,
103 data->width * data->height * (data->alpha ? 4 : 3));
104 g_object_unref(pixbuf);
108 //gint stride = gdk_pixbuf_get_rowstride(pixbuf);
109 //for (int i = 0; i < border; i++) {
110 // memset(&pixels[( i)*stride], 0xff, stride);
111 // memset(&pixels[(height-i)*stride], 0xff, stride);
113 //for (int i = 0; i < height; i++) {
114 // memset(&pixels[(i*stride)], 0xff, border*4);
115 // memset(&pixels[(i*stride)+((width-border)*4)], 0xff, border*4);
118 /* Load the GL texture from the main thread */
119 g_idle_add_full(G_PRIORITY_LOW, _load_tile_cb, data, NULL);
120 g_debug("GritsPluginSat: _load_tile end %p", g_thread_self());
123 static gboolean _free_tile_cb(gpointer data)
125 glDeleteTextures(1, data);
129 static void _free_tile(GritsTile *tile, gpointer _sat)
131 g_debug("GritsPluginSat: _free_tile: %p", tile->data);
133 g_idle_add_full(G_PRIORITY_LOW, _free_tile_cb, tile->data, NULL);
136 static void _update_tiles(gpointer _, gpointer _sat)
138 g_debug("GritsPluginSat: _update_tiles");
139 GritsPluginSat *sat = _sat;
141 grits_viewer_get_location(sat->viewer, &eye.lat, &eye.lon, &eye.elev);
142 grits_tile_update(sat->tiles, &eye,
143 MAX_RESOLUTION, TILE_WIDTH, TILE_WIDTH,
145 grits_tile_gc(sat->tiles, time(NULL)-10,
152 static void _on_location_changed(GritsViewer *viewer,
153 gdouble lat, gdouble lon, gdouble elev, GritsPluginSat *sat)
155 g_thread_pool_push(sat->threads, NULL+1, NULL);
162 * grits_plugin_sat_new:
163 * @viewer: the #GritsViewer to use for drawing
165 * Create a new instance of the satellite plugin.
167 * Returns: the new #GritsPluginSat
169 GritsPluginSat *grits_plugin_sat_new(GritsViewer *viewer)
171 g_debug("GritsPluginSat: new");
172 GritsPluginSat *sat = g_object_new(GRITS_TYPE_PLUGIN_SAT, NULL);
173 sat->viewer = g_object_ref(viewer);
175 /* Load initial tiles */
176 _load_tile(sat->tiles, sat);
177 _update_tiles(NULL, sat);
179 /* Connect signals */
180 sat->sigid = g_signal_connect(sat->viewer, "location-changed",
181 G_CALLBACK(_on_location_changed), sat);
184 grits_viewer_add(viewer, GRITS_OBJECT(sat->tiles), GRITS_LEVEL_WORLD, FALSE);
194 static void grits_plugin_sat_plugin_init(GritsPluginInterface *iface);
195 G_DEFINE_TYPE_WITH_CODE(GritsPluginSat, grits_plugin_sat, G_TYPE_OBJECT,
196 G_IMPLEMENT_INTERFACE(GRITS_TYPE_PLUGIN,
197 grits_plugin_sat_plugin_init));
198 static void grits_plugin_sat_plugin_init(GritsPluginInterface *iface)
200 g_debug("GritsPluginSat: plugin_init");
201 /* Add methods to the interface */
203 /* Class/Object init */
204 static void grits_plugin_sat_init(GritsPluginSat *sat)
206 g_debug("GritsPluginSat: init");
208 sat->threads = g_thread_pool_new(_update_tiles, sat, 1, FALSE, NULL);
209 sat->tiles = grits_tile_new(NULL, NORTH, SOUTH, EAST, WEST);
210 sat->wms = grits_wms_new(
211 "http://www.nasa.network.com/wms", "bmng200406", "image/jpeg",
212 "bmng/", "jpg", TILE_WIDTH, TILE_HEIGHT);
213 g_object_ref(sat->tiles);
215 static void grits_plugin_sat_dispose(GObject *gobject)
217 g_debug("GritsPluginSat: dispose");
218 GritsPluginSat *sat = GRITS_PLUGIN_SAT(gobject);
220 /* Drop references */
222 GritsViewer *viewer = sat->viewer;
224 g_signal_handler_disconnect(viewer, sat->sigid);
225 grits_viewer_remove(viewer, GRITS_OBJECT(sat->tiles));
226 soup_session_abort(sat->wms->http->soup);
227 g_thread_pool_free(sat->threads, TRUE, TRUE);
228 while (gtk_events_pending())
229 gtk_main_iteration();
230 g_object_unref(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;