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/>.
31 static void gis_plugin_ridge_plugin_init(GisPluginInterface *iface);
32 static void gis_plugin_ridge_expose(GisPlugin *_self);
33 static GtkWidget *gis_plugin_ridge_get_config(GisPlugin *_self);
34 G_DEFINE_TYPE_WITH_CODE(GisPluginRidge, gis_plugin_ridge, G_TYPE_OBJECT,
35 G_IMPLEMENT_INTERFACE(GIS_TYPE_PLUGIN,
36 gis_plugin_ridge_plugin_init));
37 static void gis_plugin_ridge_plugin_init(GisPluginInterface *iface)
39 g_debug("GisPluginRidge: plugin_init");
40 /* Add methods to the interface */
41 iface->expose = gis_plugin_ridge_expose;
42 iface->get_config = gis_plugin_ridge_get_config;
44 /* Class/Object init */
45 static void gis_plugin_ridge_init(GisPluginRidge *self)
47 g_debug("GisPluginRidge: init");
50 static void gis_plugin_ridge_dispose(GObject *gobject)
52 g_debug("GisPluginRidge: dispose");
53 GisPluginRidge *self = GIS_PLUGIN_RIDGE(gobject);
55 G_OBJECT_CLASS(gis_plugin_ridge_parent_class)->dispose(gobject);
57 static void gis_plugin_ridge_finalize(GObject *gobject)
59 g_debug("GisPluginRidge: finalize");
60 GisPluginRidge *self = GIS_PLUGIN_RIDGE(gobject);
62 G_OBJECT_CLASS(gis_plugin_ridge_parent_class)->finalize(gobject);
65 static void gis_plugin_ridge_class_init(GisPluginRidgeClass *klass)
67 g_debug("GisPluginRidge: class_init");
68 GObjectClass *gobject_class = (GObjectClass*)klass;
69 gobject_class->dispose = gis_plugin_ridge_dispose;
70 gobject_class->finalize = gis_plugin_ridge_finalize;
73 /*********************
75 *********************/
93 static layer_t layers[] = {
94 [LAYER_TOPO] = {"Topo", "Overlays/Topo/Short/%s_Topo_Short.jpg", TRUE, 1, 0},
95 [LAYER_COUNTY] = {"Counties", "Overlays/County/Short/%s_County_Short.gif", TRUE, 3, 0},
96 [LAYER_RIVERS] = {"Rivers", "Overlays/Rivers/Short/%s_Rivers_Short.gif", FALSE, 4, 0},
97 [LAYER_HIGHWAYS] = {"Highways", "Overlays/Highways/Short/%s_Highways_Short.gif", FALSE, 5, 0},
98 [LAYER_CITY] = {"Cities", "Overlays/Cities/Short/%s_City_Short.gif", TRUE, 6, 0},
106 * Load an image into an OpenGL texture
107 * \param filename Path to the image file
108 * \return The OpenGL identifier for the texture
110 void load_texture(GisPluginRidge *self, layer_t *layer, gchar *filename)
112 gis_opengl_begin(self->opengl);
115 GError *error = NULL;
116 GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(filename, &error);
117 if (!pixbuf || error) {
118 g_warning("Failed to load texture: %s", filename);
121 guchar *pixels = gdk_pixbuf_get_pixels(pixbuf);
122 int width = gdk_pixbuf_get_width(pixbuf);
123 int height = gdk_pixbuf_get_height(pixbuf);
124 int format = gdk_pixbuf_get_has_alpha(pixbuf) ? GL_RGBA : GL_RGB;
127 glDeleteTextures(1, &layer->tex);
128 glGenTextures(1, &layer->tex);
129 glBindTexture(GL_TEXTURE_2D, layer->tex); // 2d texture (x and y size)
131 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
132 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
133 format, GL_UNSIGNED_BYTE, pixels);
134 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
135 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
137 char *base = g_path_get_basename(filename);
138 g_debug("GisPluginRidge: load_texture - "
139 "w=%-3d h=%-3d fmt=%x px=(%02x,%02x,%02x,%02x) img=%s",
140 width, height, format, pixels[0], pixels[1], pixels[2], pixels[3],
144 gis_opengl_end(self->opengl);
146 g_object_unref(pixbuf);
149 gis_opengl_redraw(self->opengl);
157 GisPluginRidge *self;
160 void cached_cb(gchar *filename, gboolean updated, gpointer _udata)
162 cached_t *udata = _udata;
163 load_texture(udata->self, udata->layer, filename);
170 static void on_site_changed(GisView *view, gchar *site, GisPluginRidge *self)
172 g_debug("GisPluginRidge: on_site_changed - site=%s", site);
173 if (site == NULL || site[0] == '\0')
175 for (int i = 0; i < LAYER_COUNT; i++) {
176 gchar *base = "http://radar.weather.gov/ridge/";
177 gchar *path = g_strdup_printf(layers[i].fmt, site+1);
178 cached_t *udata = g_malloc(sizeof(cached_t));
180 udata->layer = &layers[i];
181 cache_file(base, path, GIS_ONCE, NULL, cached_cb, udata);
186 void toggle_layer(GtkToggleButton *check, GisPluginRidge *self)
188 layer_t *layer = g_object_get_data(G_OBJECT(check), "layer");
189 layer->enabled = gtk_toggle_button_get_active(check);
190 gis_opengl_redraw(self->opengl);
196 GisPluginRidge *gis_plugin_ridge_new(GisWorld *world, GisView *view, GisOpenGL *opengl)
198 GisPluginRidge *self = g_object_new(GIS_TYPE_PLUGIN_RIDGE, NULL);
201 self->opengl = opengl;
203 g_signal_connect(view, "site-changed", G_CALLBACK(on_site_changed), self);
204 on_site_changed(view, gis_view_get_site(view), self);
209 static GtkWidget *gis_plugin_ridge_get_config(GisPlugin *_self)
211 GisPluginRidge *self = GIS_PLUGIN_RIDGE(_self);
212 GtkWidget *body = gtk_alignment_new(0.5, 0, 0, 0);
213 GtkWidget *hbox = gtk_hbox_new(FALSE, 10);
214 for (int i = 0; i < LAYER_COUNT; i++) {
215 GtkWidget *check = gtk_check_button_new_with_label(layers[i].name);
216 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check), layers[i].enabled);
217 gtk_box_pack_start(GTK_BOX(hbox), check, FALSE, TRUE, 0);
218 g_object_set_data(G_OBJECT(check), "layer", &layers[i]);
219 g_signal_connect(check, "toggled", G_CALLBACK(toggle_layer), self);
221 gtk_container_add(GTK_CONTAINER(body), hbox);
225 static void gis_plugin_ridge_expose(GisPlugin *_self)
227 GisPluginRidge *self = GIS_PLUGIN_RIDGE(_self);
229 g_debug("GisPluginRidge: expose");
231 glEnable(GL_TEXTURE_2D);
234 for (int i = 0; i < LAYER_COUNT; i++) {
235 if (!layers[i].enabled)
237 glBindTexture(GL_TEXTURE_2D, layers[i].tex);
239 glTexCoord2f(0.0, 0.0); glVertex3f(240*1000*-1.0, 282*1000* 1.0, layers[i].z);
240 glTexCoord2f(0.0, 1.0); glVertex3f(240*1000*-1.0, 282*1000*-1.0, layers[i].z);
241 glTexCoord2f(1.0, 1.0); glVertex3f(240*1000* 1.0, 282*1000*-1.0, layers[i].z);
242 glTexCoord2f(1.0, 0.0); glVertex3f(240*1000* 1.0, 282*1000* 1.0, layers[i].z);