2 * Copyright (C) 2009 Andy Spencer <spenceal@rose-hulman.edu>
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/>.
21 #include <curl/curl.h>
29 /*********************
31 *********************/
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},
62 * Load an image into an OpenGL texture
63 * \param filename Path to the image file
64 * \return The OpenGL identifier for the texture
66 void load_texture(GisPluginRidge *self, layer_t *layer, gchar *filename)
68 gis_opengl_begin(self->opengl);
72 GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(filename, &error);
73 if (!pixbuf || error) {
74 g_warning("Failed to load texture: %s", filename);
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;
83 glDeleteTextures(1, &layer->tex);
84 glGenTextures(1, &layer->tex);
85 glBindTexture(GL_TEXTURE_2D, layer->tex); // 2d texture (x and y size)
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);
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],
100 gis_opengl_end(self->opengl);
102 g_object_unref(pixbuf);
105 gis_opengl_redraw(self->opengl);
113 GisPluginRidge *self;
116 void cached_cb(gchar *filename, gboolean updated, gpointer _udata)
118 cached_t *udata = _udata;
119 load_texture(udata->self, udata->layer, filename);
127 static void on_site_changed(GisView *view, gchar *site, GisPluginRidge *self)
129 g_debug("GisPluginRidge: on_site_changed - site=%s", site);
130 if (site == NULL || site[0] == '\0')
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));
137 udata->layer = &layers[i];
138 cache_file(base, path, GIS_ONCE, NULL, cached_cb, udata);
143 void toggle_layer(GtkToggleButton *check, GisPluginRidge *self)
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);
154 GisPluginRidge *gis_plugin_ridge_new(GisWorld *world, GisView *view, GisOpenGL *opengl)
156 GisPluginRidge *self = g_object_new(GIS_TYPE_PLUGIN_RIDGE, NULL);
159 self->opengl = opengl;
161 g_signal_connect(view, "site-changed", G_CALLBACK(on_site_changed), self);
162 on_site_changed(view, gis_view_get_site(view), self);
167 static GtkWidget *gis_plugin_ridge_get_config(GisPlugin *_self)
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);
179 gtk_container_add(GTK_CONTAINER(body), hbox);
183 static void gis_plugin_ridge_expose(GisPlugin *_self)
185 GisPluginRidge *self = GIS_PLUGIN_RIDGE(_self);
187 g_debug("GisPluginRidge: expose");
188 glEnable(GL_TEXTURE_2D);
191 for (int i = 0; i < LAYER_COUNT; i++) {
192 if (!layers[i].enabled)
194 glBindTexture(GL_TEXTURE_2D, layers[i].tex);
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);
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)
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;
220 /* Class/Object init */
221 static void gis_plugin_ridge_init(GisPluginRidge *self)
223 g_debug("GisPluginRidge: init");
226 static void gis_plugin_ridge_dispose(GObject *gobject)
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);
233 static void gis_plugin_ridge_finalize(GObject *gobject)
235 g_debug("GisPluginRidge: finalize");
236 GisPluginRidge *self = GIS_PLUGIN_RIDGE(gobject);
238 G_OBJECT_CLASS(gis_plugin_ridge_parent_class)->finalize(gobject);
241 static void gis_plugin_ridge_class_init(GisPluginRidgeClass *klass)
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;