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 },
29 static CURL *curl_handle;
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
37 char *cache_image(char *path)
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);
47 //g_printerr("Fetching image %s -> %s\n", url, local);
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);
56 if (http_code != 200) {
57 g_message("http %ld while fetching %s", http_code, url);
66 * Load an image into an OpenGL texture
67 * \param filename Path to the image file
68 * \return The OpenGL identifier for the texture
70 guint load_texture(char *filename)
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;
81 glGenTextures(1, &id);
82 glBindTexture(GL_TEXTURE_2D, id); // 2d texture (x and y size)
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);
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));
96 static gboolean expose(GtkWidget *da, GdkEventExpose *event, gpointer user_data)
98 g_message("ridge:expose");
100 glEnable(GL_TEXTURE_2D);
103 for (int i = 0; i < LAYER_COUNT; i++) {
104 glBindTexture(GL_TEXTURE_2D, layers[i].tex);
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);
118 static gboolean configure(GtkWidget *da, GdkEventConfigure *event, gpointer user_data)
120 for (int i = 0; i < LAYER_COUNT; i++) {
121 if (layers[i].tex != 0)
123 char *path = g_strdup_printf(layers[i].fmt, "IND");
124 char *local = cache_image(path);
125 layers[i].tex = load_texture(local);
132 gboolean ridge_init(GtkDrawingArea *drawing, GtkNotebook *config)
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);
138 curl_global_init(CURL_GLOBAL_ALL);
139 curl_handle = curl_easy_init();