8 #include "aweather-gui.h"
24 [LAYER_TOPO] = { "Overlays/" "Topo/" "Short/" "%s_Topo_Short.jpg", 1, 0 },
25 [LAYER_COUNTY] = { "Overlays/" "County/" "Short/" "%s_County_Short.gif", 3, 0 },
26 [LAYER_RIVERS] = { "Overlays/" "Rivers/" "Short/" "%s_Rivers_Short.gif", 4, 0 },
27 [LAYER_HIGHWAYS] = { "Overlays/" "Highways/" "Short/" "%s_Highways_Short.gif", 5, 0 },
28 [LAYER_CITY] = { "Overlays/" "Cities/" "Short/" "%s_City_Short.gif", 6, 0 },
31 static CURL *curl_handle;
35 * Cache a image from Ridge to the local disk
36 * \param path Path to the Ridge file, starting after /ridge/
37 * \return The local path to the cached image
39 char *cache_image(char *path)
41 gchar base[] = "http://radar.weather.gov/ridge/";
42 gchar *url = g_strconcat(base, path, NULL);
43 gchar *local = g_build_filename(g_get_user_cache_dir(), PACKAGE, path, NULL);
44 if (!g_file_test(local, G_FILE_TEST_EXISTS)) {
45 if (!g_file_test(g_path_get_dirname(local), G_FILE_TEST_IS_DIR)) {
46 //g_printerr("Making directory %s\n", g_path_get_dirname(local));
47 g_mkdir_with_parents(g_path_get_dirname(local), 0755);
49 //g_printerr("Fetching image %s -> %s\n", url, local);
51 FILE *cached_image = fopen(local, "w+");
52 curl_easy_setopt(curl_handle, CURLOPT_URL, url);
53 curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, cached_image);
54 curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, NULL);
55 curl_easy_perform(curl_handle);
56 curl_easy_getinfo(curl_handle, CURLINFO_RESPONSE_CODE, &http_code);
58 if (http_code != 200) {
59 g_message("http %ld while fetching %s", http_code, url);
68 * Load an image into an OpenGL texture
69 * \param filename Path to the image file
70 * \return The OpenGL identifier for the texture
72 guint load_texture(char *filename)
75 GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(filename, NULL);
76 guchar *pixels = gdk_pixbuf_get_pixels(pixbuf);
77 int width = gdk_pixbuf_get_width(pixbuf);
78 int height = gdk_pixbuf_get_height(pixbuf);
79 int format = gdk_pixbuf_get_has_alpha(pixbuf) ? GL_RGBA : GL_RGB;
83 glGenTextures(1, &id);
84 glBindTexture(GL_TEXTURE_2D, id); // 2d texture (x and y size)
86 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
87 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
88 format, GL_UNSIGNED_BYTE, pixels);
89 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
90 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
92 g_message("loaded image: w=%-3d h=%-3d fmt=%x px=(%02x,%02x,%02x,%02x) img=%s",
93 width, height, format, pixels[0], pixels[1], pixels[2], pixels[3],
94 g_path_get_basename(filename));
98 static gboolean expose(GtkWidget *da, GdkEventExpose *event, gpointer user_data)
100 g_message("ridge:expose");
102 glEnable(GL_TEXTURE_2D);
105 for (int i = 0; i < LAYER_COUNT; i++) {
106 glBindTexture(GL_TEXTURE_2D, layers[i].tex);
109 glTexCoord2f(0.0, 0.0); glVertex3f(500*1000*-1.0, 500*1000* 1.0, layers[i].z);
110 glTexCoord2f(0.0, 1.0); glVertex3f(500*1000*-1.0, 500*1000*-1.0, layers[i].z);
111 glTexCoord2f(1.0, 1.0); glVertex3f(500*1000* 1.0, 500*1000*-1.0, layers[i].z);
112 glTexCoord2f(1.0, 0.0); glVertex3f(500*1000* 1.0, 500*1000* 1.0, layers[i].z);
120 static gboolean configure(GtkWidget *da, GdkEventConfigure *event, gpointer user_data)
122 for (int i = 0; i < LAYER_COUNT; i++) {
123 if (layers[i].tex != 0)
125 char *path = g_strdup_printf(layers[i].fmt, "IND");
126 char *local = cache_image(path);
127 layers[i].tex = load_texture(local);
134 gboolean ridge_init(AWeatherGui *gui)
136 GtkDrawingArea *drawing = aweather_gui_get_drawing(gui);
138 /* Set up OpenGL Stuff */
139 g_signal_connect(drawing, "expose-event", G_CALLBACK(expose), NULL);
140 g_signal_connect(drawing, "configure-event", G_CALLBACK(configure), NULL);
142 curl_global_init(CURL_GLOBAL_ALL);
143 curl_handle = curl_easy_init();