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