4 #include <gdk/gdkkeysyms.h>
13 static Sweep *cur_sweep; // make this not global
14 static int nred, ngreen, nblue;
15 static guint8 red[256], green[256], blue[256];
17 /* Convert a sweep to an 2d array of data points */
18 static void bscan_sweep(Sweep *sweep, guint8 **data, int *width, int *height)
20 /* Calculate max number of bins */
22 for (i = 0; i < sweep->h.nrays; i++)
23 max_bins = MAX(max_bins, sweep->ray[i]->h.nbins);
25 /* Allocate buffer using max number of bins for each ray */
26 char *buf = g_malloc0(sweep->h.nrays * max_bins * 3);
30 for (ri = 0; ri < sweep->h.nrays; ri++) {
31 Ray *ray = sweep->ray[ri];
32 for (bi = 0; bi < ray->h.nbins; bi++) {
33 Range bin = ray->range[bi];
34 /* copy RGB into buffer */
35 buf[(ri*max_bins+bi)*3+0] = red[(guint8)ray->h.f(bin)];
36 buf[(ri*max_bins+bi)*3+1] = green[(guint8)ray->h.f(bin)];
37 buf[(ri*max_bins+bi)*3+2] = blue[(guint8)ray->h.f(bin)];
43 *height = sweep->h.nrays;
49 //sprintf(fname, "image-%d.ppm", fi);
50 //FILE *fp = fopen(fname, "w+");
51 //fprintf(fp, "P6\n");
52 //fprintf(fp, "# Foo\n");
53 //fprintf(fp, "%d %d\n", *width, *height);
54 //fprintf(fp, "255\n");
55 //fwrite(buf, 3, *width * *height, fp);
60 /* Load a sweep as the active texture */
61 static void load_sweep(Sweep *sweep)
66 bscan_sweep(sweep, &data, &width, &height);
67 glEnable(GL_TEXTURE_2D);
68 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
69 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
71 gdk_window_invalidate_rect(drawing->window, &drawing->allocation, FALSE);
74 /* Load the default sweep */
75 static gboolean configure(GtkWidget *da, GdkEventConfigure *event, gpointer user_data)
77 GdkGLContext *glcontext = gtk_widget_get_gl_context(da);
78 GdkGLDrawable *gldrawable = gtk_widget_get_gl_drawable(da);
80 if (!gdk_gl_drawable_gl_begin(gldrawable, glcontext))
81 g_assert_not_reached();
83 /* Load the texture */
84 load_sweep(cur_sweep);
86 gdk_gl_drawable_gl_end(gldrawable);
91 static gboolean expose(GtkWidget *da, GdkEventExpose *event, gpointer user_data)
93 Sweep *sweep = cur_sweep;
95 GdkGLContext *glcontext = gtk_widget_get_gl_context(da);
96 GdkGLDrawable *gldrawable = gtk_widget_get_gl_drawable(da);
100 glRotatef(0, 0, 0, 0);
103 glEnable(GL_TEXTURE_2D);
104 glBegin(GL_QUAD_STRIP);
105 int _ri; // not really used, creates strange fragments..
106 for (_ri = 0; _ri < sweep->h.nrays; _ri++) {
107 /* Do the first sweep twice to complete the last Quad */
108 int ri = _ri % sweep->h.nrays;
109 Ray *ray = sweep->ray[ri];
111 /* right and left looking out from radar */
112 double left = ((ray->h.azimuth - ((double)ray->h.beam_width/2.))*M_PI)/180.0;
113 double right = ((ray->h.azimuth + ((double)ray->h.beam_width/2.))*M_PI)/180.0;
115 double lx = sin(left);
116 double ly = cos(left);
118 /* TODO: change this to meters instead of 0..1 */
119 double max_dist = ray->h.nbins*ray->h.gate_size + ray->h.range_bin1;
120 double near_dist = (double)(ray->h.range_bin1) / max_dist;
121 double far_dist = (double)(ray->h.nbins*ray->h.gate_size + ray->h.range_bin1) / max_dist;
123 /* (find middle of bin) / scale for opengl */
124 glTexCoord2d(0.0, ((double)ri)/sweep->h.nrays); glVertex3f(lx*near_dist, ly*near_dist, 0.); // near left
125 glTexCoord2d(0.7, ((double)ri)/sweep->h.nrays); glVertex3f(lx*far_dist, ly*far_dist, 0.); // far left
127 g_printf("ri=%d, nr=%d, bw=%f\n", _ri, sweep->h.nrays, sweep->h.beam_width);
131 //glTexCoord2d( 0., 0.); glVertex3f(-1., 0., 0.); // bot left
132 //glTexCoord2d( 0., 1.); glVertex3f(-1., 1., 0.); // top left
133 //glTexCoord2d( 1., 1.); glVertex3f( 0., 1., 0.); // top right
134 //glTexCoord2d( 1., 0.); glVertex3f( 0., 0., 0.); // bot right
136 glDisable(GL_TEXTURE_2D);
138 /* Print the color table */
141 for (i = 0; i < nred; i++) {
142 glColor3ub(red[i], green[i], blue[i]);
143 glVertex3f(-1., (float)((i ) - nred/2)/(nred/2), 0.); // bot left
144 glVertex3f(-1., (float)((i+1) - nred/2)/(nred/2), 0.); // top left
145 glVertex3f(-.9, (float)((i+1) - nred/2)/(nred/2), 0.); // top right
146 glVertex3f(-.9, (float)((i ) - nred/2)/(nred/2), 0.); // bot right
155 gboolean radar_init(GtkDrawingArea *_drawing, GtkNotebook *config)
157 drawing = GTK_WIDGET(_drawing);
158 /* Set up OpenGL Stuff */
159 g_signal_connect(drawing, "expose-event", G_CALLBACK(expose), NULL);
160 g_signal_connect(drawing, "configure-event", G_CALLBACK(configure), NULL);
162 /* Parse hard coded file.. */
163 RSL_read_these_sweeps("all", NULL);
164 //RSL_read_these_sweeps("all", NULL);
165 Radar *radar = RSL_wsr88d_to_radar("/scratch/aweather/src/KABR_20080609_0224", "KABR");
166 //radar = RSL_wsr88d_to_radar("/scratch/aweather/src/KABX_20080622_2229", "KABX");
167 RSL_load_vel_color_table();
168 RSL_get_color_table(RSL_RED_TABLE, red, &nred);
169 RSL_get_color_table(RSL_GREEN_TABLE, green, &ngreen);
170 RSL_get_color_table(RSL_BLUE_TABLE, blue, &nblue);
171 if (radar->h.nvolumes < 1 || radar->v[0]->h.nsweeps < 1)
172 g_print("No sweeps found\n");
173 cur_sweep = radar->v[0]->sweep[0];
175 /* Add configuration tab */
176 GtkWidget *hbox = gtk_hbox_new(TRUE, 0);
177 GtkWidget *button = NULL;
179 for (vi = 0; vi < radar->h.nvolumes; vi++) {
180 Volume *vol = radar->v[vi];
181 if (vol == NULL) continue;
182 GtkWidget *vbox = gtk_vbox_new(TRUE, 0);
183 gtk_box_pack_start(GTK_BOX(hbox), vbox, TRUE, TRUE, 0);
184 for (si = 0; si < vol->h.nsweeps; si++) {
185 Sweep *sweep = vol->sweep[si];
186 if (sweep == NULL) continue;
187 char *label = g_strdup_printf("Tilt: %.2f (%s)", sweep->h.elev, vol->h.type_str);
188 button = gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(button), label);
189 g_signal_connect_swapped(button, "clicked", G_CALLBACK(load_sweep), sweep);
190 gtk_box_pack_start(GTK_BOX(vbox), button, TRUE, TRUE, 0);
194 GtkWidget *label = gtk_label_new("Radar");
195 gtk_notebook_append_page(GTK_NOTEBOOK(config), hbox, label);