]> Pileus Git - grits/blob - src/plugins/ridge.c
Splitting GIS into a shared library, and a lot more
[grits] / src / plugins / ridge.c
1 /*
2  * Copyright (C) 2009 Andy Spencer <spenceal@rose-hulman.edu>
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 <config.h>
19 #include <stdio.h>
20 #include <gtk/gtk.h>
21 #include <curl/curl.h>
22 #include <GL/gl.h>
23
24 #include <gis/gis.h>
25
26 #include "ridge.h"
27
28 /****************
29  * GObject code *
30  ****************/
31 /* Plugin init */
32 static void gis_plugin_ridge_plugin_init(GisPluginInterface *iface);
33 static void gis_plugin_ridge_expose(GisPlugin *_self);
34 static GtkWidget *gis_plugin_ridge_get_config(GisPlugin *_self);
35 G_DEFINE_TYPE_WITH_CODE(GisPluginRidge, gis_plugin_ridge, G_TYPE_OBJECT,
36                 G_IMPLEMENT_INTERFACE(GIS_TYPE_PLUGIN,
37                         gis_plugin_ridge_plugin_init));
38 static void gis_plugin_ridge_plugin_init(GisPluginInterface *iface)
39 {
40         g_debug("GisPluginRidge: plugin_init");
41         /* Add methods to the interface */
42         iface->expose     = gis_plugin_ridge_expose;
43         iface->get_config = gis_plugin_ridge_get_config;
44 }
45 /* Class/Object init */
46 static void gis_plugin_ridge_init(GisPluginRidge *self)
47 {
48         g_debug("GisPluginRidge: init");
49         /* Set defaults */
50 }
51 static void gis_plugin_ridge_dispose(GObject *gobject)
52 {
53         g_debug("GisPluginRidge: dispose");
54         GisPluginRidge *self = GIS_PLUGIN_RIDGE(gobject);
55         /* Drop references */
56         G_OBJECT_CLASS(gis_plugin_ridge_parent_class)->dispose(gobject);
57 }
58 static void gis_plugin_ridge_finalize(GObject *gobject)
59 {
60         g_debug("GisPluginRidge: finalize");
61         GisPluginRidge *self = GIS_PLUGIN_RIDGE(gobject);
62         /* Free data */
63         G_OBJECT_CLASS(gis_plugin_ridge_parent_class)->finalize(gobject);
64
65 }
66 static void gis_plugin_ridge_class_init(GisPluginRidgeClass *klass)
67 {
68         g_debug("GisPluginRidge: class_init");
69         GObjectClass *gobject_class = (GObjectClass*)klass;
70         gobject_class->dispose  = gis_plugin_ridge_dispose;
71         gobject_class->finalize = gis_plugin_ridge_finalize;
72 }
73
74 /*********************
75  * Overlay constants *
76  *********************/
77 enum {
78         LAYER_TOPO,
79         LAYER_COUNTY,
80         LAYER_RIVERS,
81         LAYER_HIGHWAYS,
82         LAYER_CITY,
83         LAYER_COUNT
84 };
85
86 typedef struct {
87         gchar *name;
88         gchar *fmt;
89         gboolean enabled;
90         float z;
91         guint tex;
92 } layer_t;
93
94 static layer_t layers[] = {
95         [LAYER_TOPO]     = {"Topo",     "Overlays/Topo/Short/%s_Topo_Short.jpg",         TRUE,  1, 0},
96         [LAYER_COUNTY]   = {"Counties", "Overlays/County/Short/%s_County_Short.gif",     TRUE,  3, 0},
97         [LAYER_RIVERS]   = {"Rivers",   "Overlays/Rivers/Short/%s_Rivers_Short.gif",     FALSE, 4, 0},
98         [LAYER_HIGHWAYS] = {"Highways", "Overlays/Highways/Short/%s_Highways_Short.gif", FALSE, 5, 0},
99         [LAYER_CITY]     = {"Cities",   "Overlays/Cities/Short/%s_City_Short.gif",       TRUE,  6, 0},
100 };
101
102
103 /***********
104  * Helpers *
105  ***********/
106 /*
107  * Load an image into an OpenGL texture
108  * \param  filename  Path to the image file
109  * \return The OpenGL identifier for the texture
110  */
111 void load_texture(GisPluginRidge *self, layer_t *layer, gchar *filename)
112 {
113         gis_opengl_begin(self->opengl);
114
115         /* Load image */
116         GError *error = NULL;
117         GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(filename, &error);
118         if (!pixbuf || error) {
119                 g_warning("Failed to load texture: %s", filename);
120                 return;
121         }
122         guchar    *pixels = gdk_pixbuf_get_pixels(pixbuf);
123         int        width  = gdk_pixbuf_get_width(pixbuf);
124         int        height = gdk_pixbuf_get_height(pixbuf);
125         int        format = gdk_pixbuf_get_has_alpha(pixbuf) ? GL_RGBA : GL_RGB;
126
127         /* Create Texture */
128         glDeleteTextures(1, &layer->tex);
129         glGenTextures(1, &layer->tex);
130         glBindTexture(GL_TEXTURE_2D, layer->tex); // 2d texture (x and y size)
131
132         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
133         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
134                         format, GL_UNSIGNED_BYTE, pixels);
135         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
136         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
137
138         char *base = g_path_get_basename(filename);
139         g_debug("GisPluginRidge: load_texture - "
140                 "w=%-3d  h=%-3d  fmt=%x  px=(%02x,%02x,%02x,%02x)  img=%s",
141                 width, height, format, pixels[0], pixels[1], pixels[2], pixels[3],
142                 base);
143         g_free(base);
144
145         gis_opengl_end(self->opengl);
146
147         g_object_unref(pixbuf);
148
149         /* Redraw */
150         gis_opengl_redraw(self->opengl);
151 }
152
153
154 /*****************
155  * ASync helpers *
156  *****************/
157 typedef struct {
158         GisPluginRidge *self;
159         layer_t *layer;
160 } cached_t;
161 void cached_cb(gchar *filename, gboolean updated, gpointer _udata)
162 {
163         cached_t *udata = _udata;
164         load_texture(udata->self, udata->layer, filename);
165         g_free(udata);
166 }
167
168 /*************
169  * callbacks *
170  *************/
171 static void on_site_changed(GisView *view, gchar *site, GisPluginRidge *self)
172 {
173         g_debug("GisPluginRidge: on_site_changed - site=%s", site);
174         if (site == NULL || site[0] == '\0')
175                 return;
176         for (int i = 0; i < LAYER_COUNT; i++) {
177                 gchar *base = "http://radar.weather.gov/ridge/";
178                 gchar *path  = g_strdup_printf(layers[i].fmt, site+1);
179                 cached_t *udata = g_malloc(sizeof(cached_t));
180                 udata->self  = self;
181                 udata->layer = &layers[i];
182                 cache_file(base, path, AWEATHER_ONCE, NULL, cached_cb, udata);
183                 //cache_file(base, path, AWEATHER_UPDATE, cached_cb, udata);
184                 g_free(path);
185         }
186 }
187
188 void toggle_layer(GtkToggleButton *check, GisPluginRidge *self)
189 {
190         layer_t *layer = g_object_get_data(G_OBJECT(check), "layer");
191         layer->enabled = gtk_toggle_button_get_active(check);
192         gis_opengl_redraw(self->opengl);
193 }
194
195 /***********
196  * Methods *
197  ***********/
198 GisPluginRidge *gis_plugin_ridge_new(GisWorld *world, GisView *view, GisOpenGL *opengl)
199 {
200         GisPluginRidge *self = g_object_new(GIS_TYPE_PLUGIN_RIDGE, NULL);
201         self->world  = world;
202         self->view   = view;
203         self->opengl = opengl;
204
205         g_signal_connect(view, "site-changed", G_CALLBACK(on_site_changed), self);
206         on_site_changed(view, gis_view_get_site(view), self);
207
208         return self;
209 }
210
211 static GtkWidget *gis_plugin_ridge_get_config(GisPlugin *_self)
212 {
213         GisPluginRidge *self = GIS_PLUGIN_RIDGE(_self);
214         GtkWidget *body = gtk_alignment_new(0.5, 0, 0, 0);
215         GtkWidget *hbox = gtk_hbox_new(FALSE, 10);
216         for (int i = 0; i < LAYER_COUNT; i++) {
217                 GtkWidget *check = gtk_check_button_new_with_label(layers[i].name);
218                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check), layers[i].enabled);
219                 gtk_box_pack_start(GTK_BOX(hbox), check, FALSE, TRUE, 0);
220                 g_object_set_data(G_OBJECT(check), "layer", &layers[i]);
221                 g_signal_connect(check, "toggled", G_CALLBACK(toggle_layer), self);
222         }
223         gtk_container_add(GTK_CONTAINER(body), hbox);
224         return body;
225 }
226
227 static void gis_plugin_ridge_expose(GisPlugin *_self)
228 {
229         GisPluginRidge *self = GIS_PLUGIN_RIDGE(_self);
230
231         g_debug("GisPluginRidge: expose");
232         glPushMatrix();
233         glEnable(GL_TEXTURE_2D);
234         glColor4f(1,1,1,1);
235
236         for (int i = 0; i < LAYER_COUNT; i++) {
237                 if (!layers[i].enabled)
238                         continue;
239                 glBindTexture(GL_TEXTURE_2D, layers[i].tex);
240                 glBegin(GL_POLYGON);
241                 glTexCoord2f(0.0, 0.0); glVertex3f(240*1000*-1.0, 282*1000* 1.0, layers[i].z);
242                 glTexCoord2f(0.0, 1.0); glVertex3f(240*1000*-1.0, 282*1000*-1.0, layers[i].z);
243                 glTexCoord2f(1.0, 1.0); glVertex3f(240*1000* 1.0, 282*1000*-1.0, layers[i].z);
244                 glTexCoord2f(1.0, 0.0); glVertex3f(240*1000* 1.0, 282*1000* 1.0, layers[i].z);
245                 glEnd();
246         }
247
248         glPopMatrix();
249 }