qvolray

view src/volray.cc @ 28:aeef3c2ae472

the slice widget works fine
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 14 Apr 2012 16:35:30 +0300
parents 53aca4775514
children 93d889a3726a
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"
17 #include "demo.h"
19 #define XFER_MAP_SZ 512
21 static void render_volume();
22 static void draw_slice();
23 static void draw_xfer_func();
25 /*
26 void keyb(unsigned char key, int x, int y);
27 void keyb_up(unsigned char key, int x, int y);
28 void mouse(int bn, int state, int x, int y);
29 void motion(int x, int y);
30 int parse_args(int argc, char **argv);
31 */
33 static void create_ray_texture(int xsz, int ysz, float vfov, Vector2 *tex_scale);
34 static Vector3 get_primary_ray_dir(int x, int y, int w, int h, float vfov_deg);
35 static int round_pow2(int x);
36 static void create_transfer_map(float mean, float sdev);
38 static float cam_theta = 0, cam_phi = 0, cam_dist = 4.0;
39 static float cam_x, cam_y, cam_z;
41 static Vector2 tex_scale;
42 static unsigned int vol_sdr, slice_sdr, ray_tex;
43 static int win_xsz, win_ysz;
44 static bool raytex_needs_recalc = true;
46 static unsigned int xfer_tex;
47 static float xfer_mean = 0.7, xfer_sdev = 0.1;
48 static bool xfertex_needs_recalc = true;
50 static float cur_z = 0.5;
51 static bool clip_z;
52 static float ray_step = 0.01;
54 static Volume *volume;
57 bool volray_init()
58 {
59 glewInit();
61 if(!(vol_sdr = create_program_load("sdr/volray.v.glsl", "sdr/volray.p.glsl"))) {
62 return false;
63 }
64 set_uniform_int(vol_sdr, "volume", 0);
65 set_uniform_int(vol_sdr, "ray_tex", 1);
66 set_uniform_int(vol_sdr, "xfer_tex", 2);
67 set_uniform_float(vol_sdr, "ray_step", ray_step);
68 set_uniform_float(vol_sdr, "zclip", 0.0);
70 if(!(slice_sdr = create_program_load(0, "sdr/slice.p.glsl"))) {
71 return false;
72 }
73 set_uniform_int(slice_sdr, "volume", 0);
74 set_uniform_int(slice_sdr, "xfer_tex", 1);
76 init_demo();
78 return true;
79 }
81 void volray_setvolume(Volume *vol)
82 {
83 volume = vol;
84 }
86 Volume *volray_getvolume()
87 {
88 return volume;
89 }
91 void volray_setvalue(int which, float val)
92 {
93 switch(which) {
94 case VOLRAY_ZCURSOR:
95 cur_z = val;
96 if(clip_z) {
97 set_uniform_float(vol_sdr, "zclip", cur_z);
98 }
99 post_redisplay();
100 break;
102 case VOLRAY_ZCLIP:
103 clip_z = val > 0.5;
104 set_uniform_float(vol_sdr, "zclip", clip_z ? cur_z : 0.0);
105 post_redisplay();
106 break;
108 default:
109 break;
110 }
111 }
113 float volray_getvalue(int which)
114 {
115 switch(which) {
116 case VOLRAY_ZCURSOR:
117 return cur_z;
119 case VOLRAY_ZCLIP:
120 return clip_z > 0.5 ? 1.0 : 0.0;
121 break;
123 default:
124 break;
125 }
126 return 0.0;
127 }
129 void volray_draw(void)
130 {
131 /* recalculate primary ray texture if needed */
132 if(raytex_needs_recalc) {
133 create_ray_texture(win_xsz, win_ysz, 50.0, &tex_scale);
134 }
135 /* recalculate transfer function texture if needed */
136 if(xfertex_needs_recalc) {
137 create_transfer_map(xfer_mean, xfer_sdev);
138 }
140 draw_demo();
142 glClear(GL_COLOR_BUFFER_BIT);
144 if(volume) {
145 render_volume();
146 //draw_slice();
147 draw_xfer_func();
148 }
150 assert(glGetError() == GL_NO_ERROR);
151 }
153 static void render_volume(void)
154 {
155 /* set the camera transformation */
156 glMatrixMode(GL_MODELVIEW);
157 glPushMatrix();
158 glLoadIdentity();
159 glRotatef(-90, 1, 0, 0);
160 glTranslatef(cam_x, cam_y, -cam_z);
161 glRotatef(cam_theta, 0, 1, 0);
162 glRotatef(cam_phi, 1, 0, 0);
163 glTranslatef(0, 0, -cam_dist);
165 /* setup the texture matrix to map the useful part of the ray texture to [0,1] */
166 glMatrixMode(GL_TEXTURE);
167 glPushMatrix();
168 glLoadIdentity();
169 glScalef(tex_scale.x, tex_scale.y, 1.0);
171 /* tex unit0: volume data 3D texture */
172 glActiveTexture(GL_TEXTURE0);
173 glBindTexture(GL_TEXTURE_3D, volume->get_texture());
174 glEnable(GL_TEXTURE_3D);
176 /* tex unit1: primary rays in view space */
177 glActiveTexture(GL_TEXTURE1);
178 glBindTexture(GL_TEXTURE_2D, ray_tex);
179 glEnable(GL_TEXTURE_2D);
181 /* tex unit2: transfer function (1d) */
182 glActiveTexture(GL_TEXTURE2);
183 glBindTexture(GL_TEXTURE_1D, xfer_tex);
184 glEnable(GL_TEXTURE_1D);
186 bind_program(vol_sdr);
187 glBegin(GL_QUADS);
188 glColor3f(1, 1, 1);
189 glTexCoord2f(0, 1); glVertex2f(-1, -1);
190 glTexCoord2f(1, 1); glVertex2f(1, -1);
191 glTexCoord2f(1, 0); glVertex2f(1, 1);
192 glTexCoord2f(0, 0); glVertex2f(-1, 1);
193 glEnd();
194 bind_program(0);
196 glActiveTexture(GL_TEXTURE2);
197 glDisable(GL_TEXTURE_1D);
198 glActiveTexture(GL_TEXTURE1);
199 glDisable(GL_TEXTURE_2D);
200 glActiveTexture(GL_TEXTURE0);
201 glDisable(GL_TEXTURE_3D);
203 glMatrixMode(GL_TEXTURE);
204 glPopMatrix();
205 glMatrixMode(GL_MODELVIEW);
206 glPopMatrix();
207 }
209 void volray_draw_slice(void)
210 {
211 glActiveTexture(GL_TEXTURE0);
212 glBindTexture(GL_TEXTURE_3D, volume->get_texture());
213 glEnable(GL_TEXTURE_3D);
215 glActiveTexture(GL_TEXTURE1);
216 glBindTexture(GL_TEXTURE_1D, xfer_tex);
217 glEnable(GL_TEXTURE_1D);
219 bind_program(slice_sdr);
221 glBegin(GL_QUADS);
222 glColor3f(1, 1, 1);
223 glTexCoord3f(0, 1, cur_z); glVertex2f(-1, -1);
224 glTexCoord3f(1, 1, cur_z); glVertex2f(1, -1);
225 glTexCoord3f(1, 0, cur_z); glVertex2f(1, 1);
226 glTexCoord3f(0, 0, cur_z); glVertex2f(-1, 1);
227 glEnd();
229 bind_program(0);
231 glActiveTexture(GL_TEXTURE1);
232 glDisable(GL_TEXTURE_1D);
233 glActiveTexture(GL_TEXTURE0);
234 glDisable(GL_TEXTURE_3D);
235 }
237 static void draw_xfer_func(void)
238 {
239 glMatrixMode(GL_MODELVIEW);
240 glPushMatrix();
241 glTranslatef(-0.9, -0.9, 0);
242 glScalef(0.5, 0.1, 1);
244 glBindTexture(GL_TEXTURE_1D, xfer_tex);
245 glEnable(GL_TEXTURE_1D);
247 glBegin(GL_QUADS);
248 glColor3f(1, 1, 1);
249 glTexCoord1f(1);
250 glVertex2f(1, 0);
251 glVertex2f(1, 1);
252 glTexCoord1f(0);
253 glVertex2f(0, 1);
254 glVertex2f(0, 0);
255 glEnd();
257 glDisable(GL_TEXTURE_1D);
259 glLineWidth(2.0);
260 glBegin(GL_LINE_LOOP);
261 /*if(uimode == UIMODE_XFER) {
262 glColor3f(1, 0, 0);
263 } else {*/
264 glColor3f(0, 0, 1);
265 //}
266 glVertex2f(0, 0);
267 glVertex2f(1, 0);
268 glVertex2f(1, 1);
269 glVertex2f(0, 1);
270 glEnd();
272 glPopMatrix();
273 }
275 void volray_resize(int x, int y)
276 {
277 glViewport(0, 0, x, y);
279 if(x != win_xsz || y != win_ysz) {
280 raytex_needs_recalc = true;
281 win_xsz = x;
282 win_ysz = y;
283 }
284 }
286 #if 0
287 void keyb(unsigned char key, int x, int y)
288 {
289 switch(key) {
290 case 27:
291 exit(0);
293 case 'x':
294 uimode = UIMODE_XFER;
295 post_redisplay();
296 break;
298 case 'c':
299 uimode = UIMODE_CURSOR;
300 post_redisplay();
301 break;
303 default:
304 break;
305 }
306 }
308 void keyb_up(unsigned char key, int x, int y)
309 {
310 switch(key) {
311 case 'x':
312 if(uimode == UIMODE_XFER) {
313 uimode = UIMODE_DEFAULT;
314 post_redisplay();
315 }
316 break;
318 case 'c':
319 if(uimode == UIMODE_CURSOR) {
320 uimode = UIMODE_DEFAULT;
321 post_redisplay();
322 }
323 break;
325 default:
326 break;
327 }
328 }
329 #endif
331 static int bnstate[32];
332 static int prev_x, prev_y;
334 void volray_mouse(int bn, int state, int x, int y)
335 {
336 bnstate[bn] = state;
337 prev_x = x;
338 prev_y = y;
339 }
341 void volray_motion(int x, int y)
342 {
343 int dx = x - prev_x;
344 int dy = y - prev_y;
345 prev_x = x;
346 prev_y = y;
348 /*switch(uimode) {
349 case UIMODE_XFER:
350 if(dx || dy) {
351 xfer_mean += dx / (float)win_xsz;
352 xfer_sdev += 0.5 * dy / (float)win_ysz;
354 xfer_mean = xfer_mean < 0.0 ? 0.0 : (xfer_mean > 1.0 ? 1.0 : xfer_mean);
355 xfer_sdev = xfer_sdev < 0.0 ? 0.0 : (xfer_sdev > 1.0 ? 1.0 : xfer_sdev);
357 xfertex_needs_recalc = true;
358 post_redisplay();
359 }
360 break;
362 case UIMODE_CURSOR:
363 cur_z += 0.5 * dy / (float)win_ysz;
365 if(cur_z < 0.0)
366 cur_z = 0.0;
367 if(cur_z > 1.0)
368 cur_z = 1.0;
370 set_uniform_float(vol_sdr, "zclip", cur_z);
371 post_redisplay();
372 break;
374 default:*/
375 /* view control */
376 if(bnstate[0]) {
377 cam_theta += dx * 0.5;
378 cam_phi += dy * 0.5;
380 if(cam_phi <= -90) cam_phi = -89;
381 if(cam_phi >= 90) cam_phi = 89;
382 post_redisplay();
383 }
385 if(bnstate[1]) {
386 cam_x += dx * 0.025;
387 cam_y += dy * 0.025;
388 post_redisplay();
389 }
391 if(bnstate[2]) {
392 cam_dist += dy * 0.025;
393 if(cam_dist < 0.0) cam_dist = 0.0;
394 post_redisplay();
395 }
396 //}
397 }
399 #if 0
400 int parse_args(int argc, char **argv)
401 {
402 int i;
403 char *endp;
405 for(i=1; i<argc; i++) {
406 if(argv[i][0] == '-' && argv[i][2] == 0) {
407 switch(argv[i][1]) {
408 case 'm':
409 xfer_mean = strtod(argv[++i], &endp);
410 if(endp == argv[i]) {
411 fprintf(stderr, "-m must be followed by the transfer function mean\n");
412 return -1;
413 }
414 break;
416 case 'd':
417 xfer_sdev = strtod(argv[++i], &endp);
418 if(endp == argv[i]) {
419 fprintf(stderr, "-d must be followed by the transfer function std.deviation\n");
420 return -1;
421 }
422 break;
424 default:
425 fprintf(stderr, "unrecognized option: %s\n", argv[i]);
426 return -1;
427 }
428 } else {
429 if(fname) {
430 fprintf(stderr, "unexpected argument: %s\n", argv[i]);
431 return -1;
432 }
433 fname = argv[i];
434 }
435 }
437 if(!fname) {
438 fprintf(stderr, "pass the volume descriptor filename\n");
439 return -1;
440 }
441 return 0;
442 }
443 #endif
446 static void create_ray_texture(int xsz, int ysz, float vfov, Vector2 *tex_scale)
447 {
448 int cur_tex_xsz, cur_tex_ysz;
449 int tex_xsz = round_pow2(xsz);
450 int tex_ysz = round_pow2(ysz);
451 float *teximg, *dir;
453 teximg = new float[3 * xsz * ysz];
454 dir = teximg;
456 for(int i=0; i<ysz; i++) {
457 for(int j=0; j<xsz; j++) {
458 Vector3 rdir = get_primary_ray_dir(j, i, xsz, ysz, vfov);
459 *dir++ = rdir.x;
460 *dir++ = rdir.y;
461 *dir++ = rdir.z;
462 }
463 }
465 if(!ray_tex) {
466 glGenTextures(1, &ray_tex);
467 }
469 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &cur_tex_xsz);
470 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &cur_tex_ysz);
472 if(tex_xsz > cur_tex_xsz || tex_ysz > cur_tex_ysz) {
473 glBindTexture(GL_TEXTURE_2D, ray_tex);
474 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
475 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
476 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
477 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
478 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F_ARB, tex_xsz, tex_ysz, 0, GL_RGB, GL_FLOAT, 0);
479 }
481 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, xsz, ysz, GL_RGB, GL_FLOAT, teximg);
482 delete [] teximg;
484 if(tex_scale) {
485 tex_scale->x = (float)xsz / (float)tex_xsz;
486 tex_scale->y = (float)ysz / (float)tex_ysz;
487 }
488 raytex_needs_recalc = false;
489 }
491 static Vector3 get_primary_ray_dir(int x, int y, int w, int h, float vfov_deg)
492 {
493 float vfov = M_PI * vfov_deg / 180.0;
494 float aspect = (float)w / (float)h;
496 float ysz = 2.0;
497 float xsz = aspect * ysz;
499 float px = ((float)x / (float)w) * xsz - xsz / 2.0;
500 float py = 1.0 - ((float)y / (float)h) * ysz;
501 float pz = 1.0 / tan(0.5 * vfov);
503 float mag = sqrt(px * px + py * py + pz * pz);
504 return Vector3(px / mag, py / mag, pz / mag);
505 }
507 static int round_pow2(int x)
508 {
509 x--;
510 x = (x >> 1) | x;
511 x = (x >> 2) | x;
512 x = (x >> 4) | x;
513 x = (x >> 8) | x;
514 x = (x >> 16) | x;
515 return x + 1;
516 }
518 static void create_transfer_map(float mean, float sdev)
519 {
520 static float map[XFER_MAP_SZ];
522 if(!xfer_tex) {
523 glGenTextures(1, &xfer_tex);
524 glBindTexture(GL_TEXTURE_1D, xfer_tex);
525 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
526 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
527 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
528 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
529 glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE32F_ARB, XFER_MAP_SZ, 0, GL_LUMINANCE, GL_FLOAT, 0);
530 }
532 for(int i=0; i<XFER_MAP_SZ; i++) {
533 float x = (float)i / (float)(XFER_MAP_SZ - 1);
534 map[i] = gaussian(x, mean, sdev) - 1.0;
535 }
537 glTexSubImage1D(GL_TEXTURE_1D, 0, 0, XFER_MAP_SZ, GL_LUMINANCE, GL_FLOAT, map);
538 xfertex_needs_recalc = false;
539 }