]> Pileus Git - aweather/blob - src/plugins/radar.c
packaging fixes
[aweather] / src / plugins / radar.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 <gtk/gtkgl.h>
21 #include <gio/gio.h>
22 #include <GL/gl.h>
23 #include <math.h>
24 #include <rsl.h>
25
26 #include <gis/gis.h>
27
28 #include "radar.h"
29 #include "marching.h"
30
31 /****************
32  * GObject code *
33  ****************/
34 /* Plugin init */
35 static void gis_plugin_radar_plugin_init(GisPluginInterface *iface);
36 static void gis_plugin_radar_expose(GisPlugin *_radar);
37 static GtkWidget *gis_plugin_radar_get_config(GisPlugin *_self);
38 G_DEFINE_TYPE_WITH_CODE(GisPluginRadar, gis_plugin_radar, G_TYPE_OBJECT,
39                 G_IMPLEMENT_INTERFACE(GIS_TYPE_PLUGIN,
40                         gis_plugin_radar_plugin_init));
41 static void gis_plugin_radar_plugin_init(GisPluginInterface *iface)
42 {
43         g_debug("GisPluginRadar: plugin_init");
44         /* Add methods to the interface */
45         iface->expose     = gis_plugin_radar_expose;
46         iface->get_config = gis_plugin_radar_get_config;
47 }
48 /* Class/Object init */
49 static void gis_plugin_radar_init(GisPluginRadar *self)
50 {
51         g_debug("GisPluginRadar: class_init");
52         /* Set defaults */
53         self->soup          = NULL;
54         self->cur_triangles = NULL;
55         self->cur_num_triangles = 0;
56
57         self->config_body = gtk_alignment_new(0, 0, 1, 1);
58         gtk_container_set_border_width(GTK_CONTAINER(self->config_body), 5);
59         gtk_container_add(GTK_CONTAINER(self->config_body), gtk_label_new("No radar loaded"));
60 }
61 static void gis_plugin_radar_dispose(GObject *gobject)
62 {
63         g_debug("GisPluginRadar: dispose");
64         GisPluginRadar *self = GIS_PLUGIN_RADAR(gobject);
65         g_signal_handler_disconnect(self->view, self->time_changed_id);
66         /* Drop references */
67         G_OBJECT_CLASS(gis_plugin_radar_parent_class)->dispose(gobject);
68 }
69 static void gis_plugin_radar_finalize(GObject *gobject)
70 {
71         g_debug("GisPluginRadar: finalize");
72         GisPluginRadar *self = GIS_PLUGIN_RADAR(gobject);
73         /* Free data */
74         G_OBJECT_CLASS(gis_plugin_radar_parent_class)->finalize(gobject);
75
76 }
77 static void gis_plugin_radar_class_init(GisPluginRadarClass *klass)
78 {
79         g_debug("GisPluginRadar: class_init");
80         GObjectClass *gobject_class = (GObjectClass*)klass;
81         gobject_class->dispose  = gis_plugin_radar_dispose;
82         gobject_class->finalize = gis_plugin_radar_finalize;
83 }
84
85 /**************************
86  * Data loading functions *
87  **************************/
88 /* Convert a sweep to an 2d array of data points */
89 static void bscan_sweep(GisPluginRadar *self, Sweep *sweep, colormap_t *colormap,
90                 guint8 **data, int *width, int *height)
91 {
92         /* Calculate max number of bins */
93         int max_bins = 0;
94         for (int i = 0; i < sweep->h.nrays; i++)
95                 max_bins = MAX(max_bins, sweep->ray[i]->h.nbins);
96
97         /* Allocate buffer using max number of bins for each ray */
98         guint8 *buf = g_malloc0(sweep->h.nrays * max_bins * 4);
99
100         /* Fill the data */
101         for (int ri = 0; ri < sweep->h.nrays; ri++) {
102                 Ray *ray  = sweep->ray[ri];
103                 for (int bi = 0; bi < ray->h.nbins; bi++) {
104                         /* copy RGBA into buffer */
105                         //guint val   = dz_f(ray->range[bi]);
106                         guint8 val   = (guint8)ray->h.f(ray->range[bi]);
107                         guint  buf_i = (ri*max_bins+bi)*4;
108                         buf[buf_i+0] = colormap->data[val][0];
109                         buf[buf_i+1] = colormap->data[val][1];
110                         buf[buf_i+2] = colormap->data[val][2];
111                         buf[buf_i+3] = colormap->data[val][3]; // TESTING
112                         if (val == BADVAL     || val == RFVAL      || val == APFLAG ||
113                             val == NOTFOUND_H || val == NOTFOUND_V || val == NOECHO) {
114                                 buf[buf_i+3] = 0x00; // transparent
115                         }
116                 }
117         }
118
119         /* set output */
120         *width  = max_bins;
121         *height = sweep->h.nrays;
122         *data   = buf;
123 }
124
125 /* Load a sweep as the active texture */
126 static void load_sweep(GisPluginRadar *self, Sweep *sweep)
127 {
128         GisOpenGL *opengl = self->opengl;
129         gis_opengl_begin(opengl);
130         self->cur_sweep = sweep;
131         int height, width;
132         guint8 *data;
133         bscan_sweep(self, sweep, self->cur_colormap, &data, &width, &height);
134         glDeleteTextures(1, &self->cur_sweep_tex);
135         glGenTextures(1, &self->cur_sweep_tex);
136         glBindTexture(GL_TEXTURE_2D, self->cur_sweep_tex);
137         glPixelStorei(GL_PACK_ALIGNMENT, 1);
138         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
139         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
140         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
141         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0,
142                         GL_RGBA, GL_UNSIGNED_BYTE, data);
143         g_free(data);
144         gis_opengl_redraw(opengl);
145         gis_opengl_end(opengl);
146 }
147
148 static void load_colormap(GisPluginRadar *self, gchar *table)
149 {
150         /* Set colormap so we can draw it on expose */
151         for (int i = 0; colormaps[i].name; i++)
152                 if (g_str_equal(colormaps[i].name, table))
153                         self->cur_colormap = &colormaps[i];
154 }
155
156 /* Add selectors to the config area for the sweeps */
157 static void on_sweep_clicked(GtkRadioButton *button, gpointer _self);
158 static void load_radar_gui(GisPluginRadar *self, Radar *radar)
159 {
160         /* Clear existing items */
161         GtkWidget *child = gtk_bin_get_child(GTK_BIN(self->config_body));
162         if (child)
163                 gtk_widget_destroy(child);
164
165         gdouble elev;
166         guint rows = 1, cols = 1, cur_cols;
167         gchar row_label_str[64], col_label_str[64], button_str[64];
168         GtkWidget *row_label, *col_label, *button = NULL, *elev_box = NULL;
169         GtkWidget *table = gtk_table_new(rows, cols, FALSE);
170
171         for (guint vi = 0; vi < radar->h.nvolumes; vi++) {
172                 Volume *vol = radar->v[vi];
173                 if (vol == NULL) continue;
174                 rows++; cols = 1; elev = 0;
175
176                 /* Row label */
177                 g_snprintf(row_label_str, 64, "<b>%s:</b>", vol->h.type_str);
178                 row_label = gtk_label_new(row_label_str);
179                 gtk_label_set_use_markup(GTK_LABEL(row_label), TRUE);
180                 gtk_misc_set_alignment(GTK_MISC(row_label), 1, 0.5);
181                 gtk_table_attach(GTK_TABLE(table), row_label,
182                                 0,1, rows-1,rows, GTK_FILL,GTK_FILL, 5,0);
183
184                 for (guint si = 0; si < vol->h.nsweeps; si++) {
185                         Sweep *sweep = vol->sweep[si];
186                         if (sweep == NULL || sweep->h.elev == 0) continue;
187                         if (sweep->h.elev != elev) {
188                                 cols++;
189                                 elev = sweep->h.elev;
190
191                                 /* Column label */
192                                 g_object_get(table, "n-columns", &cur_cols, NULL);
193                                 if (cols >  cur_cols) {
194                                         g_snprintf(col_label_str, 64, "<b>%.2f°</b>", elev);
195                                         col_label = gtk_label_new(col_label_str);
196                                         gtk_label_set_use_markup(GTK_LABEL(col_label), TRUE);
197                                         gtk_widget_set_size_request(col_label, 50, -1);
198                                         gtk_table_attach(GTK_TABLE(table), col_label,
199                                                         cols-1,cols, 0,1, GTK_FILL,GTK_FILL, 0,0);
200                                 }
201
202                                 elev_box = gtk_hbox_new(TRUE, 0);
203                                 gtk_table_attach(GTK_TABLE(table), elev_box,
204                                                 cols-1,cols, rows-1,rows, GTK_FILL,GTK_FILL, 0,0);
205                         }
206
207
208                         /* Button */
209                         g_snprintf(button_str, 64, "%3.2f", elev);
210                         button = gtk_radio_button_new_with_label_from_widget(
211                                         GTK_RADIO_BUTTON(button), button_str);
212                         gtk_widget_set_size_request(button, -1, 26);
213                         //button = gtk_radio_button_new_from_widget(GTK_RADIO_BUTTON(button));
214                         //gtk_widget_set_size_request(button, -1, 22);
215                         g_object_set(button, "draw-indicator", FALSE, NULL);
216                         gtk_box_pack_end(GTK_BOX(elev_box), button, TRUE, TRUE, 0);
217
218                         g_object_set_data(G_OBJECT(button), "type",  vol->h.type_str);
219                         g_object_set_data(G_OBJECT(button), "sweep", sweep);
220                         g_signal_connect(button, "clicked", G_CALLBACK(on_sweep_clicked), self);
221                 }
222         }
223         gtk_container_add(GTK_CONTAINER(self->config_body), table);
224         gtk_widget_show_all(table);
225 }
226
227 static void _gis_plugin_radar_grid_set(GRIDCELL *grid, int gi, Ray *ray, int bi)
228 {
229         Range range = ray->range[bi];
230
231         double angle = d2r(ray->h.azimuth);
232         double tilt  = d2r(ray->h.elev);
233
234         double lx    = sin(angle);
235         double ly    = cos(angle);
236         double lz    = sin(tilt);
237
238         double dist   = bi*ray->h.gate_size + ray->h.range_bin1;
239                 
240         grid->p[gi].x = lx*dist;
241         grid->p[gi].y = ly*dist;
242         grid->p[gi].z = lz*dist;
243
244         guint8 val = (guint8)ray->h.f(ray->range[bi]);
245         if (val == BADVAL     || val == RFVAL      || val == APFLAG ||
246             val == NOTFOUND_H || val == NOTFOUND_V || val == NOECHO ||
247             val > 80)
248                 val = 0;
249         grid->val[gi] = (float)val;
250         //g_debug("(%.2f,%.2f,%.2f) - (%.0f,%.0f,%.0f) = %d", 
251         //      angle, tilt, dist,
252         //      grid->p[gi].x,
253         //      grid->p[gi].y,
254         //      grid->p[gi].z,
255         //      val);
256 }
257
258 /* Load a radar from a decompressed file */
259 static void load_radar(GisPluginRadar *self, gchar *radar_file)
260 {
261         char *dir  = g_path_get_dirname(radar_file);
262         char *site = g_path_get_basename(dir);
263         g_free(dir);
264         g_debug("GisPluginRadar: load_radar - Loading new radar");
265         RSL_read_these_sweeps("all", NULL);
266         Radar *radar = self->cur_radar = RSL_wsr88d_to_radar(radar_file, site);
267         if (radar == NULL) {
268                 g_warning("fail to load radar: path=%s, site=%s", radar_file, site);
269                 g_free(site);
270                 return;
271         }
272         g_free(site);
273
274 #ifdef MARCHING
275         /* Load the surface */
276         if (self->cur_triangles) {
277                 g_free(self->cur_triangles);
278                 self->cur_triangles = NULL;
279         }
280         self->cur_num_triangles = 0;
281         int x = 1;
282         for (guint vi = 0; vi < radar->h.nvolumes; vi++) {
283                 if (radar->v[vi] == NULL) continue;
284
285                 for (guint si = 0; si+1 < radar->v[vi]->h.nsweeps; si++) {
286                         Sweep *sweep0 = radar->v[vi]->sweep[si+0];
287                         Sweep *sweep1 = radar->v[vi]->sweep[si+1];
288
289                         //g_debug("_gis_plugin_radar_expose: sweep[%3d-%3d] -- nrays = %d, %d",
290                         //      si, si+1,sweep0->h.nrays, sweep1->h.nrays);
291
292                         /* Skip super->regular resolution switch for now */
293                         if (sweep0 == NULL || sweep0->h.elev == 0 ||
294                             sweep1 == NULL || sweep1->h.elev == 0 ||
295                             sweep0->h.nrays != sweep1->h.nrays)
296                                 continue;
297
298                         /* We repack the arrays so that raysX[0] is always north, etc */
299                         Ray **rays0 = g_malloc0(sizeof(Ray*)*sweep0->h.nrays);
300                         Ray **rays1 = g_malloc0(sizeof(Ray*)*sweep1->h.nrays);
301
302                         for (guint ri = 0; ri < sweep0->h.nrays; ri++)
303                                 rays0[(guint)(sweep0->ray[ri]->h.azimuth * sweep0->h.nrays / 360)] =
304                                         sweep0->ray[ri];
305                         for (guint ri = 0; ri < sweep1->h.nrays; ri++)
306                                 rays1[(guint)(sweep1->ray[ri]->h.azimuth * sweep1->h.nrays / 360)] =
307                                         sweep1->ray[ri];
308
309                         for (guint ri = 0; ri+x < sweep0->h.nrays; ri+=x) {
310                                 //g_debug("_gis_plugin_radar_expose: ray[%3d-%3d] -- nbins = %d, %d, %d, %d",
311                                 //      ri, ri+x,
312                                 //      rays0[ri  ]->h.nbins, 
313                                 //      rays0[ri+1]->h.nbins, 
314                                 //      rays1[ri  ]->h.nbins, 
315                                 //      rays1[ri+1]->h.nbins);
316
317                                 for (guint bi = 0; bi+x < rays1[ri]->h.nbins; bi+=x) {
318                                         GRIDCELL grid = {};
319                                         _gis_plugin_radar_grid_set(&grid, 7, rays0[(ri  )%sweep0->h.nrays], bi+x);
320                                         _gis_plugin_radar_grid_set(&grid, 6, rays0[(ri+x)%sweep0->h.nrays], bi+x);
321                                         _gis_plugin_radar_grid_set(&grid, 5, rays0[(ri+x)%sweep0->h.nrays], bi  );
322                                         _gis_plugin_radar_grid_set(&grid, 4, rays0[(ri  )%sweep0->h.nrays], bi  );
323                                         _gis_plugin_radar_grid_set(&grid, 3, rays1[(ri  )%sweep0->h.nrays], bi+x);
324                                         _gis_plugin_radar_grid_set(&grid, 2, rays1[(ri+x)%sweep0->h.nrays], bi+x);
325                                         _gis_plugin_radar_grid_set(&grid, 1, rays1[(ri+x)%sweep0->h.nrays], bi  );
326                                         _gis_plugin_radar_grid_set(&grid, 0, rays1[(ri  )%sweep0->h.nrays], bi  );
327                                         
328                                         TRIANGLE tris[10];
329                                         int n = march_one_cube(grid, 40, tris);
330
331                                         self->cur_triangles = g_realloc(self->cur_triangles,
332                                                 (self->cur_num_triangles+n)*sizeof(TRIANGLE));
333                                         for (int i = 0; i < n; i++) {
334                                                 //g_debug("triangle: ");
335                                                 //g_debug("\t(%f,%f,%f)", tris[i].p[0].x, tris[i].p[0].y, tris[i].p[0].z);
336                                                 //g_debug("\t(%f,%f,%f)", tris[i].p[1].x, tris[i].p[1].y, tris[i].p[1].z);
337                                                 //g_debug("\t(%f,%f,%f)", tris[i].p[2].x, tris[i].p[2].y, tris[i].p[2].z);
338                                                 self->cur_triangles[self->cur_num_triangles+i] = tris[i];
339                                         }
340                                         self->cur_num_triangles += n;
341                                         //g_debug(" ");
342                                 }
343                         }
344                 }
345                 break; // Exit after first volume (reflectivity)
346         }
347 #endif
348
349         /* Load the first sweep by default */
350         if (radar->h.nvolumes < 1 || radar->v[0]->h.nsweeps < 1) {
351                 g_warning("No sweeps found\n");
352         } else {
353                 /* load first available sweep */
354                 for (int vi = 0; vi < radar->h.nvolumes; vi++) {
355                         if (radar->v[vi]== NULL) continue;
356                         for (int si = 0; si < radar->v[vi]->h.nsweeps; si++) {
357                                 if (radar->v[vi]->sweep[si]== NULL) continue;
358                                 load_colormap(self, radar->v[vi]->h.type_str);
359                                 load_sweep(self, radar->v[vi]->sweep[si]);
360                                 break;
361                         }
362                         break;
363                 }
364         }
365
366         load_radar_gui(self, radar);
367 }
368
369 /*****************
370  * ASync helpers *
371  *****************/
372 typedef struct {
373         GisPluginRadar *self;
374         gchar *radar_file;
375 } decompressed_t;
376
377 static void decompressed_cb(GPid pid, gint status, gpointer _udata)
378 {
379         g_debug("GisPluginRadar: decompressed_cb");
380         decompressed_t *udata = _udata;
381         if (status != 0) {
382                 g_warning("wsr88ddec exited with status %d", status);
383                 return;
384         }
385         load_radar(udata->self, udata->radar_file);
386         g_spawn_close_pid(pid);
387         g_free(udata->radar_file);
388         g_free(udata);
389 }
390
391 static void cache_chunk_cb(char *path, goffset cur, goffset total, gpointer _self)
392 {
393         GisPluginRadar *self = GIS_PLUGIN_RADAR(_self);
394         double percent = (double)cur/total;
395
396         //g_message("GisPluginRadar: cache_chunk_cb - %lld/%lld = %.2f%%",
397         //              cur, total, percent*100);
398
399         gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(self->progress_bar), MIN(percent, 1.0));
400
401         gchar *msg = g_strdup_printf("Loading radar... %5.1f%% (%.2f/%.2f MB)",
402                         percent*100, (double)cur/1000000, (double)total/1000000);
403         gtk_label_set_text(GTK_LABEL(self->progress_label), msg);
404         g_free(msg);
405 }
406
407 static void cache_done_cb(char *path, gboolean updated, gpointer _self)
408 {
409         g_debug("GisPluginRadar: cache_done_cb - updated = %d", updated);
410         GisPluginRadar *self = GIS_PLUGIN_RADAR(_self);
411         char *decompressed = g_strconcat(path, ".raw", NULL);
412         if (!updated && g_file_test(decompressed, G_FILE_TEST_EXISTS)) {
413                 load_radar(self, decompressed);
414                 return;
415         }
416
417         decompressed_t *udata = g_malloc(sizeof(decompressed_t));
418         udata->self       = self;
419         udata->radar_file = decompressed;
420         g_debug("GisPluginRadar: cache_done_cb - File updated, decompressing..");
421         char *argv[] = {"wsr88ddec", path, decompressed, NULL};
422         GPid pid;
423         GError *error = NULL;
424         g_spawn_async(
425                 NULL,    // const gchar *working_directory,
426                 argv,    // gchar **argv,
427                 NULL,    // gchar **envp,
428                 G_SPAWN_SEARCH_PATH|
429                 G_SPAWN_DO_NOT_REAP_CHILD, 
430                          // GSpawnFlags flags,
431                 NULL,    // GSpawnChildSetupFunc child_setup,
432                 NULL,    // gpointer user_data,
433                 &pid,    // GPid *child_pid,
434                 &error); // GError **error
435         if (error) {
436                 g_warning("failed to decompress WSR88D data: %s",
437                                 error->message);
438                 g_error_free(error);
439         }
440         g_child_watch_add(pid, decompressed_cb, udata);
441         self->soup = NULL;
442 }
443
444 /*************
445  * Callbacks *
446  *************/
447 static void on_sweep_clicked(GtkRadioButton *button, gpointer _self)
448 {
449         GisPluginRadar *self = GIS_PLUGIN_RADAR(_self);
450         load_colormap(self, g_object_get_data(G_OBJECT(button), "type" ));
451         load_sweep   (self, g_object_get_data(G_OBJECT(button), "sweep"));
452 }
453
454 static void on_time_changed(GisView *view, const char *time, gpointer _self)
455 {
456         GisPluginRadar *self = GIS_PLUGIN_RADAR(_self);
457         g_debug("GisPluginRadar: on_time_changed - setting time=%s", time);
458         // format: http://mesonet.agron.iastate.edu/data/nexrd2/raw/KABR/KABR_20090510_0323
459         char *site = gis_view_get_site(view);
460         char *path = g_strdup_printf("nexrd2/raw/%s/%s_%s", site, site, time);
461
462         /* Set up progress bar */
463         GtkWidget *child = gtk_bin_get_child(GTK_BIN(self->config_body));
464         if (child) gtk_widget_destroy(child);
465
466         GtkWidget *vbox = gtk_vbox_new(FALSE, 10);
467         gtk_container_set_border_width(GTK_CONTAINER(vbox), 10);
468         self->progress_bar   = gtk_progress_bar_new();
469         self->progress_label = gtk_label_new("Loading radar...");
470         gtk_box_pack_start(GTK_BOX(vbox), self->progress_bar,   FALSE, FALSE, 0);
471         gtk_box_pack_start(GTK_BOX(vbox), self->progress_label, FALSE, FALSE, 0);
472         gtk_container_add(GTK_CONTAINER(self->config_body), vbox);
473         gtk_widget_show_all(self->config_body);
474
475         /* Clear radar */
476         if (self->cur_radar)
477                 RSL_free_radar(self->cur_radar);
478         self->cur_radar = NULL;
479         self->cur_sweep = NULL;
480         gis_opengl_redraw(self->opengl);
481
482         /* Start loading the new radar */
483         if (self->soup) {
484                 soup_session_abort(self->soup);
485                 self->soup = NULL;
486         }
487         gchar *base = gis_prefs_get_string(self->prefs, "aweather/nexrad_url");
488         if (gis_world_get_offline(self->world)) 
489                 self->soup = cache_file(base, path, GIS_ONCE,
490                                 cache_chunk_cb, cache_done_cb, self);
491         else 
492                 self->soup = cache_file(base, path, GIS_UPDATE,
493                                 cache_chunk_cb, cache_done_cb, self);
494         g_free(path);
495 }
496
497 /***********
498  * Methods *
499  ***********/
500 GisPluginRadar *gis_plugin_radar_new(GisWorld *world, GisView *view, GisOpenGL *opengl, GisPrefs *prefs)
501 {
502         /* TODO: move to constructor if possible */
503         g_debug("GisPluginRadar: new");
504         GisPluginRadar *self = g_object_new(GIS_TYPE_PLUGIN_RADAR, NULL);
505         self->world  = world;
506         self->view   = view;
507         self->opengl = opengl;
508         self->prefs  = prefs;
509         self->time_changed_id = g_signal_connect(view, "time-changed",
510                         G_CALLBACK(on_time_changed), self);
511         return self;
512 }
513
514 static GtkWidget *gis_plugin_radar_get_config(GisPlugin *_self)
515 {
516         GisPluginRadar *self = GIS_PLUGIN_RADAR(_self);
517         return self->config_body;
518 }
519
520 static void gis_plugin_radar_expose(GisPlugin *_self)
521 {
522         GisPluginRadar *self = GIS_PLUGIN_RADAR(_self);
523         g_debug("GisPluginRadar: expose");
524         if (self->cur_sweep == NULL)
525                 return;
526         Sweep *sweep = self->cur_sweep;
527
528 #ifdef MARCHING
529         /* Draw the surface */
530         glMatrixMode(GL_MODELVIEW);
531         glPushMatrix();
532         glDisable(GL_TEXTURE_2D);
533         float light_ambient[]  = {0.1f, 0.1f, 0.0f};
534         float light_diffuse[]  = {0.9f, 0.9f, 0.9f};
535         float light_position[] = {-300000.0f, 500000.0f, 400000.0f, 1.0f};
536         glLightfv(GL_LIGHT0, GL_AMBIENT,  light_ambient);
537         glLightfv(GL_LIGHT0, GL_DIFFUSE,  light_diffuse);
538         glLightfv(GL_LIGHT0, GL_POSITION, light_position);
539         glEnable(GL_LIGHT0);
540         glEnable(GL_LIGHTING);
541         glEnable(GL_COLOR_MATERIAL);
542         glColor4f(1,1,1,0.75);
543         g_debug("ntri=%d", self->cur_num_triangles);
544         glBegin(GL_TRIANGLES);
545         for (int i = 0; i < self->cur_num_triangles; i++) {
546                 TRIANGLE t = self->cur_triangles[i];
547                 do_normal(t.p[0].x, t.p[0].y, t.p[0].z,
548                           t.p[1].x, t.p[1].y, t.p[1].z,
549                           t.p[2].x, t.p[2].y, t.p[2].z);
550                 glVertex3f(t.p[0].x, t.p[0].y, t.p[0].z);
551                 glVertex3f(t.p[1].x, t.p[1].y, t.p[1].z);
552                 glVertex3f(t.p[2].x, t.p[2].y, t.p[2].z);
553         }
554         glEnd();
555         glPopMatrix();
556 #endif
557
558         /* Draw the rays */
559         glDisable(GL_LIGHTING);
560         glDisable(GL_COLOR_MATERIAL);
561         glMatrixMode(GL_MODELVIEW);
562         glPushMatrix();
563         glBindTexture(GL_TEXTURE_2D, self->cur_sweep_tex);
564         glEnable(GL_TEXTURE_2D);
565         glDisable(GL_ALPHA_TEST);
566         glColor4f(1,1,1,1);
567         glBegin(GL_QUAD_STRIP);
568         for (int ri = 0; ri <= sweep->h.nrays; ri++) {
569                 Ray  *ray = NULL;
570                 double angle = 0;
571                 if (ri < sweep->h.nrays) {
572                         ray = sweep->ray[ri];
573                         angle = d2r(ray->h.azimuth - ((double)ray->h.beam_width/2.));
574                 } else {
575                         /* Do the right side of the last sweep */
576                         ray = sweep->ray[ri-1];
577                         angle = d2r(ray->h.azimuth + ((double)ray->h.beam_width/2.));
578                 }
579
580                 double lx = sin(angle);
581                 double ly = cos(angle);
582
583                 double near_dist = ray->h.range_bin1;
584                 double far_dist  = ray->h.nbins*ray->h.gate_size + ray->h.range_bin1;
585
586                 /* (find middle of bin) / scale for opengl */
587                 // near left
588                 glTexCoord2f(0.0, (double)ri/sweep->h.nrays-0.01);
589                 glVertex3f(lx*near_dist, ly*near_dist, 2.0);
590
591                 // far  left
592                 // todo: correct range-height function
593                 double height = sin(d2r(ray->h.elev)) * far_dist;
594                 glTexCoord2f(1.0, (double)ri/sweep->h.nrays-0.01);
595                 glVertex3f(lx*far_dist,  ly*far_dist, height);
596         }
597         //g_print("ri=%d, nr=%d, bw=%f\n", _ri, sweep->h.nrays, sweep->h.beam_width);
598         glEnd();
599         glPopMatrix();
600
601         /* Texture debug */
602         //glBegin(GL_QUADS);
603         //glTexCoord2d( 0.,  0.); glVertex3f(-500.,   0., 0.); // bot left
604         //glTexCoord2d( 0.,  1.); glVertex3f(-500., 500., 0.); // top left
605         //glTexCoord2d( 1.,  1.); glVertex3f( 0.,   500., 3.); // top right
606         //glTexCoord2d( 1.,  0.); glVertex3f( 0.,     0., 3.); // bot right
607         //glEnd();
608
609         /* Print the color table */
610         glDisable(GL_TEXTURE_2D);
611         glDisable(GL_DEPTH_TEST);
612         glMatrixMode(GL_MODELVIEW ); glPushMatrix(); glLoadIdentity();
613         glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity();
614         glBegin(GL_QUADS);
615         int i;
616         for (i = 0; i < 256; i++) {
617                 glColor4ub(self->cur_colormap->data[i][0],
618                            self->cur_colormap->data[i][1],
619                            self->cur_colormap->data[i][2],
620                            self->cur_colormap->data[i][3]);
621                 glVertex3f(-1.0, (float)((i  ) - 256/2)/(256/2), 0.0); // bot left
622                 glVertex3f(-1.0, (float)((i+1) - 256/2)/(256/2), 0.0); // top left
623                 glVertex3f(-0.9, (float)((i+1) - 256/2)/(256/2), 0.0); // top right
624                 glVertex3f(-0.9, (float)((i  ) - 256/2)/(256/2), 0.0); // bot right
625         }
626         glEnd();
627         glEnable(GL_DEPTH_TEST);
628         glEnable(GL_ALPHA_TEST);
629         glMatrixMode(GL_PROJECTION); glPopMatrix(); 
630         glMatrixMode(GL_MODELVIEW ); glPopMatrix();
631 }