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