]> Pileus Git - grits/blob - src/plugin-ridge.c
(no commit message)
[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 enum {
9         LAYER_TOPO,
10         LAYER_COUNTY,
11         LAYER_RIVERS,
12         LAYER_HIGHWAYS,
13         LAYER_CITY,
14         LAYER_COUNT
15 };
16
17 static struct {
18         char *fmt;
19         float z;
20         guint tex;
21 } layers[] = {
22         [LAYER_TOPO]     = { "Overlays/" "Topo/"     "Short/" "%s_Topo_Short.jpg",     1, 0 },
23         [LAYER_COUNTY]   = { "Overlays/" "County/"   "Short/" "%s_County_Short.gif",   3, 0 },
24         [LAYER_RIVERS]   = { "Overlays/" "Rivers/"   "Short/" "%s_Rivers_Short.gif",   4, 0 },
25         [LAYER_HIGHWAYS] = { "Overlays/" "Highways/" "Short/" "%s_Highways_Short.gif", 5, 0 },
26         [LAYER_CITY]     = { "Overlays/" "Cities/"   "Short/" "%s_City_Short.gif",     6, 0 },
27 };
28
29 static CURL *curl_handle;
30
31
32 /**
33  * Cache a image from Ridge to the local disk
34  * \param  path  Path to the Ridge file, starting after /ridge/
35  * \return The local path to the cached image
36  */
37 char *cache_image(char *path)
38 {
39         gchar base[] = "http://radar.weather.gov/ridge/";
40         gchar *url   = g_strconcat(base, path, NULL);
41         gchar *local = g_build_filename(g_get_user_cache_dir(), PACKAGE, path, NULL);
42         if (!g_file_test(local, G_FILE_TEST_EXISTS)) {
43                 if (!g_file_test(g_path_get_dirname(local), G_FILE_TEST_IS_DIR)) {
44                         //g_printerr("Making directory %s\n", g_path_get_dirname(local));
45                         g_mkdir_with_parents(g_path_get_dirname(local), 0755);
46                 }
47                 //g_printerr("Fetching image %s -> %s\n", url, local);
48                 long http_code;
49                 FILE *cached_image = fopen(local, "w+");
50                 curl_easy_setopt(curl_handle, CURLOPT_URL, url);
51                 curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, cached_image);
52                 curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, NULL);
53                 curl_easy_perform(curl_handle);
54                 curl_easy_getinfo(curl_handle, CURLINFO_RESPONSE_CODE, &http_code);
55                 fflush(cached_image);
56                 if (http_code != 200) {
57                         g_message("http %ld while fetching %s", http_code, url);
58                         remove(local);
59                         return NULL;
60                 }
61         }
62         return local;
63 }
64
65 /**
66  * Load an image into an OpenGL texture
67  * \param  filename  Path to the image file
68  * \return The OpenGL identifier for the texture
69  */
70 guint load_texture(char *filename)
71 {
72         /* Load image */
73         GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(filename, NULL);
74         guchar    *pixels = gdk_pixbuf_get_pixels(pixbuf);
75         int        width  = gdk_pixbuf_get_width(pixbuf);
76         int        height = gdk_pixbuf_get_height(pixbuf);
77         int        format = gdk_pixbuf_get_has_alpha(pixbuf) ? GL_RGBA : GL_RGB;
78
79         /* Create Texture */
80         guint id;
81         glGenTextures(1, &id);
82         glBindTexture(GL_TEXTURE_2D, id);   // 2d texture (x and y size)
83
84         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
85         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
86                         format, GL_UNSIGNED_BYTE, pixels);
87         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
88         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
89
90         g_message("loaded image:  w=%-3d  h=%-3d  fmt=%x  px=(%02x,%02x,%02x,%02x)  img=%s",
91                 width, height, format, pixels[0], pixels[1], pixels[2], pixels[3],
92                 g_path_get_basename(filename));
93         return id;
94 }
95
96 static gboolean expose(GtkWidget *da, GdkEventExpose *event, gpointer user_data)
97 {
98         g_message("ridge:expose");
99         glPushMatrix();
100         glEnable(GL_TEXTURE_2D);
101         glColor4f(1,1,1,1);
102
103         for (int i = 0; i < LAYER_COUNT; i++) {
104                 glBindTexture(GL_TEXTURE_2D, layers[i].tex);
105
106                 glBegin(GL_POLYGON);
107                 glTexCoord2f(0.0, 0.0); glVertex3f(500*1000*-1.0, 500*1000* 1.0, layers[i].z);
108                 glTexCoord2f(0.0, 1.0); glVertex3f(500*1000*-1.0, 500*1000*-1.0, layers[i].z);
109                 glTexCoord2f(1.0, 1.0); glVertex3f(500*1000* 1.0, 500*1000*-1.0, layers[i].z);
110                 glTexCoord2f(1.0, 0.0); glVertex3f(500*1000* 1.0, 500*1000* 1.0, layers[i].z);
111                 glEnd();
112         }
113
114         glPopMatrix();
115         return FALSE;
116 }
117
118 static gboolean configure(GtkWidget *da, GdkEventConfigure *event, gpointer user_data)
119 {
120         for (int i = 0; i < LAYER_COUNT; i++) {
121                 if (layers[i].tex != 0)
122                         continue;
123                 char *path  = g_strdup_printf(layers[i].fmt, "IND");
124                 char *local = cache_image(path);
125                 layers[i].tex = load_texture(local);
126                 g_free(local);
127                 g_free(path);
128         }
129         return FALSE;
130 }
131
132 gboolean ridge_init(GtkDrawingArea *drawing, GtkNotebook *config)
133 {
134         /* Set up OpenGL Stuff */
135         g_signal_connect(drawing, "expose-event",    G_CALLBACK(expose),    NULL);
136         g_signal_connect(drawing, "configure-event", G_CALLBACK(configure), NULL);
137
138         curl_global_init(CURL_GLOBAL_ALL);
139         curl_handle = curl_easy_init();
140
141         return TRUE;
142 }