4 #include <gdk/gdkkeysyms.h>
13 static Sweep *cur_sweep; // make this not global
14 static int nred, ngreen, nblue;
15 static char 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 guint8 *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[(gint8)ray->h.f(bin)];
36 buf[(ri*max_bins+bi)*3+1] = green[(gint8)ray->h.f(bin)];
37 buf[(ri*max_bins+bi)*3+2] = blue[(gint8)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 glPixelStorei(GL_PACK_ALIGNMENT, 1);
68 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
69 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
70 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
72 glEnable(GL_TEXTURE_2D);
73 gdk_window_invalidate_rect(drawing->window, &drawing->allocation, FALSE);
76 /* Load the default sweep */
77 static gboolean configure(GtkWidget *da, GdkEventConfigure *event, gpointer user_data)
79 GdkGLContext *glcontext = gtk_widget_get_gl_context(da);
80 GdkGLDrawable *gldrawable = gtk_widget_get_gl_drawable(da);
82 if (!gdk_gl_drawable_gl_begin(gldrawable, glcontext))
83 g_assert_not_reached();
85 /* Load the texture */
86 load_sweep(cur_sweep);
88 gdk_gl_drawable_gl_end(gldrawable);
93 static gboolean expose(GtkWidget *da, GdkEventExpose *event, gpointer user_data)
95 Sweep *sweep = cur_sweep;
97 //GdkGLContext *glcontext = gtk_widget_get_gl_context(da);
98 //GdkGLDrawable *gldrawable = gtk_widget_get_gl_drawable(da);
102 glRotatef(0, 0, 0, 0);
105 glEnable(GL_TEXTURE_2D);
106 glBegin(GL_QUAD_STRIP);
107 int _ri; // not really used, creates strange fragments..
108 for (_ri = 0; _ri < sweep->h.nrays; _ri++) {
109 /* Do the first sweep twice to complete the last Quad */
110 int ri = _ri % sweep->h.nrays;
111 Ray *ray = sweep->ray[ri];
113 /* right and left looking out from radar */
114 double left = ((ray->h.azimuth - ((double)ray->h.beam_width/2.))*M_PI)/180.0;
115 //double right = ((ray->h.azimuth + ((double)ray->h.beam_width/2.))*M_PI)/180.0;
117 double lx = sin(left);
118 double ly = cos(left);
120 /* TODO: change this to meters instead of 0..1 */
121 double max_dist = ray->h.nbins*ray->h.gate_size + ray->h.range_bin1;
122 double near_dist = (double)(ray->h.range_bin1) / max_dist;
123 double far_dist = (double)(ray->h.nbins*ray->h.gate_size + ray->h.range_bin1) / max_dist;
125 /* (find middle of bin) / scale for opengl */
126 glTexCoord2d(0.0, ((double)ri)/sweep->h.nrays); glVertex3f(lx*near_dist, ly*near_dist, 0.); // near left
127 glTexCoord2d(0.7, ((double)ri)/sweep->h.nrays); glVertex3f(lx*far_dist, ly*far_dist, 0.); // far left
129 //g_print("ri=%d, nr=%d, bw=%f\n", _ri, sweep->h.nrays, sweep->h.beam_width);
133 //glTexCoord2d( 0., 0.); glVertex3f(-1., 0., 0.); // bot left
134 //glTexCoord2d( 0., 1.); glVertex3f(-1., 1., 0.); // top left
135 //glTexCoord2d( 1., 1.); glVertex3f( 0., 1., 0.); // top right
136 //glTexCoord2d( 1., 0.); glVertex3f( 0., 0., 0.); // bot right
138 glDisable(GL_TEXTURE_2D);
140 /* Print the color table */
143 for (i = 0; i < nred; i++) {
144 glColor3ub(red[i], green[i], blue[i]);
145 glVertex3f(-1., (float)((i ) - nred/2)/(nred/2), 0.); // bot left
146 glVertex3f(-1., (float)((i+1) - nred/2)/(nred/2), 0.); // top left
147 glVertex3f(-.9, (float)((i+1) - nred/2)/(nred/2), 0.); // top right
148 glVertex3f(-.9, (float)((i ) - nred/2)/(nred/2), 0.); // bot right
157 gboolean radar_init(GtkDrawingArea *_drawing, GtkNotebook *config)
159 drawing = GTK_WIDGET(_drawing);
160 /* Set up OpenGL Stuff */
161 g_signal_connect(drawing, "expose-event", G_CALLBACK(expose), NULL);
162 g_signal_connect(drawing, "configure-event", G_CALLBACK(configure), NULL);
164 /* Parse hard coded file.. */
165 RSL_read_these_sweeps("all", NULL);
166 //RSL_read_these_sweeps("all", NULL);
167 Radar *radar = RSL_wsr88d_to_radar("/scratch/aweather/data/KNQA_20090501_1925.raw", "KNQA");
168 RSL_load_refl_color_table();
169 RSL_get_color_table(RSL_RED_TABLE, red, &nred);
170 RSL_get_color_table(RSL_GREEN_TABLE, green, &ngreen);
171 RSL_get_color_table(RSL_BLUE_TABLE, blue, &nblue);
172 if (radar->h.nvolumes < 1 || radar->v[0]->h.nsweeps < 1)
173 g_print("No sweeps found\n");
174 cur_sweep = radar->v[0]->sweep[6];
176 /* Add configuration tab */
177 GtkWidget *hbox = gtk_hbox_new(TRUE, 0);
178 GtkWidget *button = NULL;
180 for (vi = 0; vi < radar->h.nvolumes; vi++) {
181 Volume *vol = radar->v[vi];
182 if (vol == NULL) continue;
183 GtkWidget *vbox = gtk_vbox_new(TRUE, 0);
184 gtk_box_pack_start(GTK_BOX(hbox), vbox, TRUE, TRUE, 0);
185 for (si = 0; si < vol->h.nsweeps; si++) {
186 Sweep *sweep = vol->sweep[si];
187 if (sweep == NULL) continue;
188 char *label = g_strdup_printf("Tilt: %.2f (%s)", sweep->h.elev, vol->h.type_str);
189 button = gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(button), label);
190 g_signal_connect_swapped(button, "clicked", G_CALLBACK(load_sweep), sweep);
191 gtk_box_pack_start(GTK_BOX(vbox), button, TRUE, TRUE, 0);
195 GtkWidget *label = gtk_label_new("Radar");
196 gtk_notebook_append_page(GTK_NOTEBOOK(config), hbox, label);