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