qvolray

view src/volray.cc @ 14:88312413467c

adding a sidewidget
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 10 Apr 2012 06:35:34 +0300
parents 8990b5d2c7fe
children 3d05c261a2f4
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <assert.h>
5 #include <GL/glew.h>
6 #ifndef __APPLE__
7 #include <GL/glut.h>
8 #else
9 #include <GLUT/glut.h>
10 #endif
12 #include <vmath/vmath.h>
13 #include <imago2.h>
14 #include "sdr.h"
15 #include "volume.h"
16 #include "ui.h"
18 #define XFER_MAP_SZ 512
20 static void render_volume();
21 static void draw_slice();
22 static void draw_xfer_func();
24 /*
25 void keyb(unsigned char key, int x, int y);
26 void keyb_up(unsigned char key, int x, int y);
27 void mouse(int bn, int state, int x, int y);
28 void motion(int x, int y);
29 int parse_args(int argc, char **argv);
30 */
32 static void create_ray_texture(int xsz, int ysz, float vfov, Vector2 *tex_scale);
33 static Vector3 get_primary_ray_dir(int x, int y, int w, int h, float vfov_deg);
34 static int round_pow2(int x);
35 static void create_transfer_map(float mean, float sdev);
37 static float cam_theta = 0, cam_phi = 0, cam_dist = 4.0;
38 static float cam_x, cam_y, cam_z;
40 static Vector2 tex_scale;
41 static unsigned int vol_sdr, slice_sdr, ray_tex;
42 static int win_xsz, win_ysz;
43 static bool raytex_needs_recalc = true;
45 static unsigned int xfer_tex;
46 static float xfer_mean = 0.7, xfer_sdev = 0.1;
47 static bool xfertex_needs_recalc = true;
49 static float cur_z = 0.0;
50 static float ray_step = 0.01;
52 static const Volume *volume;
55 bool volray_init()
56 {
57 glewInit();
59 if(!(vol_sdr = create_program_load("sdr/volray.v.glsl", "sdr/volray.p.glsl"))) {
60 return false;
61 }
62 set_uniform_int(vol_sdr, "volume", 0);
63 set_uniform_int(vol_sdr, "ray_tex", 1);
64 set_uniform_int(vol_sdr, "xfer_tex", 2);
65 set_uniform_float(vol_sdr, "ray_step", ray_step);
66 set_uniform_float(vol_sdr, "zclip", cur_z);
68 if(!(slice_sdr = create_program_load(0, "sdr/slice.p.glsl"))) {
69 return false;
70 }
71 set_uniform_int(slice_sdr, "volume", 0);
72 set_uniform_int(slice_sdr, "xfer_tex", 1);
74 return true;
75 }
77 void volray_setvolume(const Volume *vol)
78 {
79 volume = vol;
80 }
82 void volray_draw(void)
83 {
84 /* recalculate primary ray texture if needed */
85 if(raytex_needs_recalc) {
86 create_ray_texture(win_xsz, win_ysz, 50.0, &tex_scale);
87 }
88 /* recalculate transfer function texture if needed */
89 if(xfertex_needs_recalc) {
90 create_transfer_map(xfer_mean, xfer_sdev);
91 }
93 glClear(GL_COLOR_BUFFER_BIT);
95 if(volume) {
96 render_volume();
97 draw_slice();
98 draw_xfer_func();
99 }
101 assert(glGetError() == GL_NO_ERROR);
102 }
104 static void render_volume(void)
105 {
106 /* set the camera transformation */
107 glMatrixMode(GL_MODELVIEW);
108 glPushMatrix();
109 glLoadIdentity();
110 glRotatef(-90, 1, 0, 0);
111 glTranslatef(cam_x, cam_y, -cam_z);
112 glRotatef(cam_theta, 0, 1, 0);
113 glRotatef(cam_phi, 1, 0, 0);
114 glTranslatef(0, 0, -cam_dist);
116 /* setup the texture matrix to map the useful part of the ray texture to [0,1] */
117 glMatrixMode(GL_TEXTURE);
118 glPushMatrix();
119 glLoadIdentity();
120 glScalef(tex_scale.x, tex_scale.y, 1.0);
122 /* tex unit0: volume data 3D texture */
123 glActiveTexture(GL_TEXTURE0);
124 glBindTexture(GL_TEXTURE_3D, volume->get_texture());
125 glEnable(GL_TEXTURE_3D);
127 /* tex unit1: primary rays in view space */
128 glActiveTexture(GL_TEXTURE1);
129 glBindTexture(GL_TEXTURE_2D, ray_tex);
130 glEnable(GL_TEXTURE_2D);
132 /* tex unit2: transfer function (1d) */
133 glActiveTexture(GL_TEXTURE2);
134 glBindTexture(GL_TEXTURE_1D, xfer_tex);
135 glEnable(GL_TEXTURE_1D);
137 bind_program(vol_sdr);
138 glBegin(GL_QUADS);
139 glColor3f(1, 1, 1);
140 glTexCoord2f(0, 1); glVertex2f(-1, -1);
141 glTexCoord2f(1, 1); glVertex2f(1, -1);
142 glTexCoord2f(1, 0); glVertex2f(1, 1);
143 glTexCoord2f(0, 0); glVertex2f(-1, 1);
144 glEnd();
145 bind_program(0);
147 glActiveTexture(GL_TEXTURE2);
148 glDisable(GL_TEXTURE_1D);
149 glActiveTexture(GL_TEXTURE1);
150 glDisable(GL_TEXTURE_2D);
151 glActiveTexture(GL_TEXTURE0);
152 glDisable(GL_TEXTURE_3D);
154 glMatrixMode(GL_TEXTURE);
155 glPopMatrix();
156 glMatrixMode(GL_MODELVIEW);
157 glPopMatrix();
158 }
160 static void draw_slice(void)
161 {
162 glMatrixMode(GL_MODELVIEW);
163 glPushMatrix();
164 glTranslatef(0.9, 0.9, 0);
165 glScalef(0.3, 0.3 * ((float)win_xsz / win_ysz), 1);
166 glTranslatef(-1, -1, 0);
168 glActiveTexture(GL_TEXTURE0);
169 glBindTexture(GL_TEXTURE_3D, volume->get_texture());
170 glEnable(GL_TEXTURE_3D);
172 glActiveTexture(GL_TEXTURE1);
173 glBindTexture(GL_TEXTURE_1D, xfer_tex);
174 glEnable(GL_TEXTURE_1D);
176 bind_program(slice_sdr);
178 glBegin(GL_QUADS);
179 glColor3f(1, 1, 1);
180 glTexCoord3f(0, 1, cur_z); glVertex2f(-1, -1);
181 glTexCoord3f(1, 1, cur_z); glVertex2f(1, -1);
182 glTexCoord3f(1, 0, cur_z); glVertex2f(1, 1);
183 glTexCoord3f(0, 0, cur_z); glVertex2f(-1, 1);
184 glEnd();
186 bind_program(0);
188 glActiveTexture(GL_TEXTURE1);
189 glDisable(GL_TEXTURE_1D);
190 glActiveTexture(GL_TEXTURE0);
191 glDisable(GL_TEXTURE_3D);
192 glPopMatrix();
193 }
195 static void draw_xfer_func(void)
196 {
197 glMatrixMode(GL_MODELVIEW);
198 glPushMatrix();
199 glTranslatef(-0.9, -0.9, 0);
200 glScalef(0.5, 0.1, 1);
202 glBindTexture(GL_TEXTURE_1D, xfer_tex);
203 glEnable(GL_TEXTURE_1D);
205 glBegin(GL_QUADS);
206 glColor3f(1, 1, 1);
207 glTexCoord1f(1);
208 glVertex2f(1, 0);
209 glVertex2f(1, 1);
210 glTexCoord1f(0);
211 glVertex2f(0, 1);
212 glVertex2f(0, 0);
213 glEnd();
215 glDisable(GL_TEXTURE_1D);
217 glLineWidth(2.0);
218 glBegin(GL_LINE_LOOP);
219 /*if(uimode == UIMODE_XFER) {
220 glColor3f(1, 0, 0);
221 } else {*/
222 glColor3f(0, 0, 1);
223 //}
224 glVertex2f(0, 0);
225 glVertex2f(1, 0);
226 glVertex2f(1, 1);
227 glVertex2f(0, 1);
228 glEnd();
230 glPopMatrix();
231 }
233 void volray_resize(int x, int y)
234 {
235 glViewport(0, 0, x, y);
237 if(x != win_xsz || y != win_ysz) {
238 raytex_needs_recalc = true;
239 win_xsz = x;
240 win_ysz = y;
241 }
242 }
244 #if 0
245 void keyb(unsigned char key, int x, int y)
246 {
247 switch(key) {
248 case 27:
249 exit(0);
251 case 'x':
252 uimode = UIMODE_XFER;
253 post_redisplay();
254 break;
256 case 'c':
257 uimode = UIMODE_CURSOR;
258 post_redisplay();
259 break;
261 default:
262 break;
263 }
264 }
266 void keyb_up(unsigned char key, int x, int y)
267 {
268 switch(key) {
269 case 'x':
270 if(uimode == UIMODE_XFER) {
271 uimode = UIMODE_DEFAULT;
272 post_redisplay();
273 }
274 break;
276 case 'c':
277 if(uimode == UIMODE_CURSOR) {
278 uimode = UIMODE_DEFAULT;
279 post_redisplay();
280 }
281 break;
283 default:
284 break;
285 }
286 }
287 #endif
289 static int bnstate[32];
290 static int prev_x, prev_y;
292 void volray_mouse(int bn, int state, int x, int y)
293 {
294 bnstate[bn] = state;
295 prev_x = x;
296 prev_y = y;
297 }
299 void volray_motion(int x, int y)
300 {
301 int dx = x - prev_x;
302 int dy = y - prev_y;
303 prev_x = x;
304 prev_y = y;
306 /*switch(uimode) {
307 case UIMODE_XFER:
308 if(dx || dy) {
309 xfer_mean += dx / (float)win_xsz;
310 xfer_sdev += 0.5 * dy / (float)win_ysz;
312 xfer_mean = xfer_mean < 0.0 ? 0.0 : (xfer_mean > 1.0 ? 1.0 : xfer_mean);
313 xfer_sdev = xfer_sdev < 0.0 ? 0.0 : (xfer_sdev > 1.0 ? 1.0 : xfer_sdev);
315 xfertex_needs_recalc = true;
316 post_redisplay();
317 }
318 break;
320 case UIMODE_CURSOR:
321 cur_z += 0.5 * dy / (float)win_ysz;
323 if(cur_z < 0.0)
324 cur_z = 0.0;
325 if(cur_z > 1.0)
326 cur_z = 1.0;
328 set_uniform_float(vol_sdr, "zclip", cur_z);
329 post_redisplay();
330 break;
332 default:*/
333 /* view control */
334 if(bnstate[0]) {
335 cam_theta += dx * 0.5;
336 cam_phi += dy * 0.5;
338 if(cam_phi <= -90) cam_phi = -89;
339 if(cam_phi >= 90) cam_phi = 89;
340 post_redisplay();
341 }
343 if(bnstate[1]) {
344 cam_x += dx * 0.025;
345 cam_y += dy * 0.025;
346 post_redisplay();
347 }
349 if(bnstate[2]) {
350 cam_dist += dy * 0.025;
351 if(cam_dist < 0.0) cam_dist = 0.0;
352 post_redisplay();
353 }
354 //}
355 }
357 #if 0
358 int parse_args(int argc, char **argv)
359 {
360 int i;
361 char *endp;
363 for(i=1; i<argc; i++) {
364 if(argv[i][0] == '-' && argv[i][2] == 0) {
365 switch(argv[i][1]) {
366 case 'm':
367 xfer_mean = strtod(argv[++i], &endp);
368 if(endp == argv[i]) {
369 fprintf(stderr, "-m must be followed by the transfer function mean\n");
370 return -1;
371 }
372 break;
374 case 'd':
375 xfer_sdev = strtod(argv[++i], &endp);
376 if(endp == argv[i]) {
377 fprintf(stderr, "-d must be followed by the transfer function std.deviation\n");
378 return -1;
379 }
380 break;
382 default:
383 fprintf(stderr, "unrecognized option: %s\n", argv[i]);
384 return -1;
385 }
386 } else {
387 if(fname) {
388 fprintf(stderr, "unexpected argument: %s\n", argv[i]);
389 return -1;
390 }
391 fname = argv[i];
392 }
393 }
395 if(!fname) {
396 fprintf(stderr, "pass the volume descriptor filename\n");
397 return -1;
398 }
399 return 0;
400 }
401 #endif
404 static void create_ray_texture(int xsz, int ysz, float vfov, Vector2 *tex_scale)
405 {
406 int cur_tex_xsz, cur_tex_ysz;
407 int tex_xsz = round_pow2(xsz);
408 int tex_ysz = round_pow2(ysz);
409 float *teximg, *dir;
411 teximg = new float[3 * xsz * ysz];
412 dir = teximg;
414 for(int i=0; i<ysz; i++) {
415 for(int j=0; j<xsz; j++) {
416 Vector3 rdir = get_primary_ray_dir(j, i, xsz, ysz, vfov);
417 *dir++ = rdir.x;
418 *dir++ = rdir.y;
419 *dir++ = rdir.z;
420 }
421 }
423 if(!ray_tex) {
424 glGenTextures(1, &ray_tex);
425 }
427 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &cur_tex_xsz);
428 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &cur_tex_ysz);
430 if(tex_xsz > cur_tex_xsz || tex_ysz > cur_tex_ysz) {
431 glBindTexture(GL_TEXTURE_2D, ray_tex);
432 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
433 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
434 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
435 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
436 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F_ARB, tex_xsz, tex_ysz, 0, GL_RGB, GL_FLOAT, 0);
437 }
439 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, xsz, ysz, GL_RGB, GL_FLOAT, teximg);
440 delete [] teximg;
442 if(tex_scale) {
443 tex_scale->x = (float)xsz / (float)tex_xsz;
444 tex_scale->y = (float)ysz / (float)tex_ysz;
445 }
446 raytex_needs_recalc = false;
447 }
449 static Vector3 get_primary_ray_dir(int x, int y, int w, int h, float vfov_deg)
450 {
451 float vfov = M_PI * vfov_deg / 180.0;
452 float aspect = (float)w / (float)h;
454 float ysz = 2.0;
455 float xsz = aspect * ysz;
457 float px = ((float)x / (float)w) * xsz - xsz / 2.0;
458 float py = 1.0 - ((float)y / (float)h) * ysz;
459 float pz = 1.0 / tan(0.5 * vfov);
461 float mag = sqrt(px * px + py * py + pz * pz);
462 return Vector3(px / mag, py / mag, pz / mag);
463 }
465 static int round_pow2(int x)
466 {
467 x--;
468 x = (x >> 1) | x;
469 x = (x >> 2) | x;
470 x = (x >> 4) | x;
471 x = (x >> 8) | x;
472 x = (x >> 16) | x;
473 return x + 1;
474 }
476 static void create_transfer_map(float mean, float sdev)
477 {
478 static float map[XFER_MAP_SZ];
480 if(!xfer_tex) {
481 glGenTextures(1, &xfer_tex);
482 glBindTexture(GL_TEXTURE_1D, xfer_tex);
483 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
484 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
485 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
486 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
487 glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE32F_ARB, XFER_MAP_SZ, 0, GL_LUMINANCE, GL_FLOAT, 0);
488 }
490 for(int i=0; i<XFER_MAP_SZ; i++) {
491 float x = (float)i / (float)(XFER_MAP_SZ - 1);
492 map[i] = gaussian(x, mean, sdev) - 1.0;
493 }
495 glTexSubImage1D(GL_TEXTURE_1D, 0, 0, XFER_MAP_SZ, GL_LUMINANCE, GL_FLOAT, map);
496 xfertex_needs_recalc = false;
497 }