]> Pileus Git - grits/blob - src/plugin-ridge.c
Adding copyright statements and a few bug fixes (hacks) for shading radar data
[grits] / src / plugin-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 <gtk/gtk.h>
20 #include <curl/curl.h>
21 #include <GL/gl.h>
22
23 #include <stdio.h>
24
25 #include "aweather-gui.h"
26 #include "data.h"
27
28 enum {
29         LAYER_TOPO,
30         LAYER_COUNTY,
31         LAYER_RIVERS,
32         LAYER_HIGHWAYS,
33         LAYER_CITY,
34         LAYER_COUNT
35 };
36
37 typedef struct {
38         gchar *fmt;
39         float z;
40         guint tex;
41 } layer_t;
42
43 static layer_t layers[] = {
44         [LAYER_TOPO]     = { "Overlays/" "Topo/"     "Short/" "%s_Topo_Short.jpg",     1, 0 },
45         [LAYER_COUNTY]   = { "Overlays/" "County/"   "Short/" "%s_County_Short.gif",   3, 0 },
46         [LAYER_RIVERS]   = { "Overlays/" "Rivers/"   "Short/" "%s_Rivers_Short.gif",   4, 0 },
47         [LAYER_HIGHWAYS] = { "Overlays/" "Highways/" "Short/" "%s_Highways_Short.gif", 5, 0 },
48         [LAYER_CITY]     = { "Overlays/" "Cities/"   "Short/" "%s_City_Short.gif",     6, 0 },
49 };
50
51 static AWeatherGui *gui = NULL;
52
53 /**
54  * Load an image into an OpenGL texture
55  * \param  filename  Path to the image file
56  * \return The OpenGL identifier for the texture
57  */
58 void load_texture(gchar *filename, gpointer _layer)
59 {
60         layer_t *layer = _layer;
61         aweather_gui_gl_begin(gui);
62
63         /* Load image */
64         GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(filename, NULL);
65         if (!pixbuf)
66                 g_warning("Failed to load texture: %s", filename);
67         guchar    *pixels = gdk_pixbuf_get_pixels(pixbuf);
68         int        width  = gdk_pixbuf_get_width(pixbuf);
69         int        height = gdk_pixbuf_get_height(pixbuf);
70         int        format = gdk_pixbuf_get_has_alpha(pixbuf) ? GL_RGBA : GL_RGB;
71
72         /* Create Texture */
73         glGenTextures(1, &layer->tex);
74         glBindTexture(GL_TEXTURE_2D, layer->tex); // 2d texture (x and y size)
75
76         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
77         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
78                         format, GL_UNSIGNED_BYTE, pixels);
79         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
80         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
81
82         g_message("loaded image:  w=%-3d  h=%-3d  fmt=%x  px=(%02x,%02x,%02x,%02x)  img=%s",
83                 width, height, format, pixels[0], pixels[1], pixels[2], pixels[3],
84                 g_path_get_basename(filename));
85
86         aweather_gui_gl_end(gui);
87
88         /* Redraw */
89         gtk_widget_queue_draw(aweather_gui_get_widget(gui, "drawing"));
90 }
91
92 static void set_site(AWeatherView *view, gchar *site, gpointer user_data)
93 {
94         g_message("location changed to %s", site);
95         for (int i = 0; i < LAYER_COUNT; i++) {
96                 gchar *base = "http://radar.weather.gov/ridge/";
97                 gchar *path  = g_strdup_printf(layers[i].fmt, site);
98                 cache_file(base, path, load_texture, &layers[i]);
99                 g_free(path);
100         }
101 }
102
103 static gboolean expose(GtkWidget *da, GdkEventExpose *event, gpointer user_data)
104 {
105         g_message("ridge:expose");
106         glPushMatrix();
107         glEnable(GL_TEXTURE_2D);
108         glColor4f(1,1,1,1);
109
110         for (int i = 0; i < LAYER_COUNT; i++) {
111                 glBindTexture(GL_TEXTURE_2D, layers[i].tex);
112
113                 glBegin(GL_POLYGON);
114                 glTexCoord2f(0.0, 0.0); glVertex3f(250*1000*-1.0, 250*1000* 1.0, layers[i].z);
115                 glTexCoord2f(0.0, 1.0); glVertex3f(250*1000*-1.0, 250*1000*-1.0, layers[i].z);
116                 glTexCoord2f(1.0, 1.0); glVertex3f(250*1000* 1.0, 250*1000*-1.0, layers[i].z);
117                 glTexCoord2f(1.0, 0.0); glVertex3f(250*1000* 1.0, 250*1000* 1.0, layers[i].z);
118                 glEnd();
119         }
120
121         glPopMatrix();
122         return FALSE;
123 }
124
125 gboolean ridge_init(AWeatherGui *_gui)
126 {
127         gui = _gui;
128         GtkDrawingArea *drawing = GTK_DRAWING_AREA(aweather_gui_get_widget(gui, "drawing"));
129         AWeatherView   *view    = aweather_gui_get_view(gui);
130
131         /* Set up OpenGL Stuff */
132         g_signal_connect(drawing, "expose-event",     G_CALLBACK(expose),    NULL);
133         g_signal_connect(view,    "location-changed", G_CALLBACK(set_site),  NULL);
134
135         return TRUE;
136 }