qvolray
view src/volray.cc @ 23:53aca4775514
clip checkbox
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Fri, 13 Apr 2012 15:43:45 +0300 |
parents | 2d0dfb5751dc |
children | aeef3c2ae472 |
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 static void draw_slice(void)
210 {
211 glMatrixMode(GL_MODELVIEW);
212 glPushMatrix();
213 glTranslatef(0.9, 0.9, 0);
214 glScalef(0.3, 0.3 * ((float)win_xsz / win_ysz), 1);
215 glTranslatef(-1, -1, 0);
217 glActiveTexture(GL_TEXTURE0);
218 glBindTexture(GL_TEXTURE_3D, volume->get_texture());
219 glEnable(GL_TEXTURE_3D);
221 glActiveTexture(GL_TEXTURE1);
222 glBindTexture(GL_TEXTURE_1D, xfer_tex);
223 glEnable(GL_TEXTURE_1D);
225 bind_program(slice_sdr);
227 glBegin(GL_QUADS);
228 glColor3f(1, 1, 1);
229 glTexCoord3f(0, 1, cur_z); glVertex2f(-1, -1);
230 glTexCoord3f(1, 1, cur_z); glVertex2f(1, -1);
231 glTexCoord3f(1, 0, cur_z); glVertex2f(1, 1);
232 glTexCoord3f(0, 0, cur_z); glVertex2f(-1, 1);
233 glEnd();
235 bind_program(0);
237 glActiveTexture(GL_TEXTURE1);
238 glDisable(GL_TEXTURE_1D);
239 glActiveTexture(GL_TEXTURE0);
240 glDisable(GL_TEXTURE_3D);
241 glPopMatrix();
242 }
244 static void draw_xfer_func(void)
245 {
246 glMatrixMode(GL_MODELVIEW);
247 glPushMatrix();
248 glTranslatef(-0.9, -0.9, 0);
249 glScalef(0.5, 0.1, 1);
251 glBindTexture(GL_TEXTURE_1D, xfer_tex);
252 glEnable(GL_TEXTURE_1D);
254 glBegin(GL_QUADS);
255 glColor3f(1, 1, 1);
256 glTexCoord1f(1);
257 glVertex2f(1, 0);
258 glVertex2f(1, 1);
259 glTexCoord1f(0);
260 glVertex2f(0, 1);
261 glVertex2f(0, 0);
262 glEnd();
264 glDisable(GL_TEXTURE_1D);
266 glLineWidth(2.0);
267 glBegin(GL_LINE_LOOP);
268 /*if(uimode == UIMODE_XFER) {
269 glColor3f(1, 0, 0);
270 } else {*/
271 glColor3f(0, 0, 1);
272 //}
273 glVertex2f(0, 0);
274 glVertex2f(1, 0);
275 glVertex2f(1, 1);
276 glVertex2f(0, 1);
277 glEnd();
279 glPopMatrix();
280 }
282 void volray_resize(int x, int y)
283 {
284 glViewport(0, 0, x, y);
286 if(x != win_xsz || y != win_ysz) {
287 raytex_needs_recalc = true;
288 win_xsz = x;
289 win_ysz = y;
290 }
291 }
293 #if 0
294 void keyb(unsigned char key, int x, int y)
295 {
296 switch(key) {
297 case 27:
298 exit(0);
300 case 'x':
301 uimode = UIMODE_XFER;
302 post_redisplay();
303 break;
305 case 'c':
306 uimode = UIMODE_CURSOR;
307 post_redisplay();
308 break;
310 default:
311 break;
312 }
313 }
315 void keyb_up(unsigned char key, int x, int y)
316 {
317 switch(key) {
318 case 'x':
319 if(uimode == UIMODE_XFER) {
320 uimode = UIMODE_DEFAULT;
321 post_redisplay();
322 }
323 break;
325 case 'c':
326 if(uimode == UIMODE_CURSOR) {
327 uimode = UIMODE_DEFAULT;
328 post_redisplay();
329 }
330 break;
332 default:
333 break;
334 }
335 }
336 #endif
338 static int bnstate[32];
339 static int prev_x, prev_y;
341 void volray_mouse(int bn, int state, int x, int y)
342 {
343 bnstate[bn] = state;
344 prev_x = x;
345 prev_y = y;
346 }
348 void volray_motion(int x, int y)
349 {
350 int dx = x - prev_x;
351 int dy = y - prev_y;
352 prev_x = x;
353 prev_y = y;
355 /*switch(uimode) {
356 case UIMODE_XFER:
357 if(dx || dy) {
358 xfer_mean += dx / (float)win_xsz;
359 xfer_sdev += 0.5 * dy / (float)win_ysz;
361 xfer_mean = xfer_mean < 0.0 ? 0.0 : (xfer_mean > 1.0 ? 1.0 : xfer_mean);
362 xfer_sdev = xfer_sdev < 0.0 ? 0.0 : (xfer_sdev > 1.0 ? 1.0 : xfer_sdev);
364 xfertex_needs_recalc = true;
365 post_redisplay();
366 }
367 break;
369 case UIMODE_CURSOR:
370 cur_z += 0.5 * dy / (float)win_ysz;
372 if(cur_z < 0.0)
373 cur_z = 0.0;
374 if(cur_z > 1.0)
375 cur_z = 1.0;
377 set_uniform_float(vol_sdr, "zclip", cur_z);
378 post_redisplay();
379 break;
381 default:*/
382 /* view control */
383 if(bnstate[0]) {
384 cam_theta += dx * 0.5;
385 cam_phi += dy * 0.5;
387 if(cam_phi <= -90) cam_phi = -89;
388 if(cam_phi >= 90) cam_phi = 89;
389 post_redisplay();
390 }
392 if(bnstate[1]) {
393 cam_x += dx * 0.025;
394 cam_y += dy * 0.025;
395 post_redisplay();
396 }
398 if(bnstate[2]) {
399 cam_dist += dy * 0.025;
400 if(cam_dist < 0.0) cam_dist = 0.0;
401 post_redisplay();
402 }
403 //}
404 }
406 #if 0
407 int parse_args(int argc, char **argv)
408 {
409 int i;
410 char *endp;
412 for(i=1; i<argc; i++) {
413 if(argv[i][0] == '-' && argv[i][2] == 0) {
414 switch(argv[i][1]) {
415 case 'm':
416 xfer_mean = strtod(argv[++i], &endp);
417 if(endp == argv[i]) {
418 fprintf(stderr, "-m must be followed by the transfer function mean\n");
419 return -1;
420 }
421 break;
423 case 'd':
424 xfer_sdev = strtod(argv[++i], &endp);
425 if(endp == argv[i]) {
426 fprintf(stderr, "-d must be followed by the transfer function std.deviation\n");
427 return -1;
428 }
429 break;
431 default:
432 fprintf(stderr, "unrecognized option: %s\n", argv[i]);
433 return -1;
434 }
435 } else {
436 if(fname) {
437 fprintf(stderr, "unexpected argument: %s\n", argv[i]);
438 return -1;
439 }
440 fname = argv[i];
441 }
442 }
444 if(!fname) {
445 fprintf(stderr, "pass the volume descriptor filename\n");
446 return -1;
447 }
448 return 0;
449 }
450 #endif
453 static void create_ray_texture(int xsz, int ysz, float vfov, Vector2 *tex_scale)
454 {
455 int cur_tex_xsz, cur_tex_ysz;
456 int tex_xsz = round_pow2(xsz);
457 int tex_ysz = round_pow2(ysz);
458 float *teximg, *dir;
460 teximg = new float[3 * xsz * ysz];
461 dir = teximg;
463 for(int i=0; i<ysz; i++) {
464 for(int j=0; j<xsz; j++) {
465 Vector3 rdir = get_primary_ray_dir(j, i, xsz, ysz, vfov);
466 *dir++ = rdir.x;
467 *dir++ = rdir.y;
468 *dir++ = rdir.z;
469 }
470 }
472 if(!ray_tex) {
473 glGenTextures(1, &ray_tex);
474 }
476 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &cur_tex_xsz);
477 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &cur_tex_ysz);
479 if(tex_xsz > cur_tex_xsz || tex_ysz > cur_tex_ysz) {
480 glBindTexture(GL_TEXTURE_2D, ray_tex);
481 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
482 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
483 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
484 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
485 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F_ARB, tex_xsz, tex_ysz, 0, GL_RGB, GL_FLOAT, 0);
486 }
488 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, xsz, ysz, GL_RGB, GL_FLOAT, teximg);
489 delete [] teximg;
491 if(tex_scale) {
492 tex_scale->x = (float)xsz / (float)tex_xsz;
493 tex_scale->y = (float)ysz / (float)tex_ysz;
494 }
495 raytex_needs_recalc = false;
496 }
498 static Vector3 get_primary_ray_dir(int x, int y, int w, int h, float vfov_deg)
499 {
500 float vfov = M_PI * vfov_deg / 180.0;
501 float aspect = (float)w / (float)h;
503 float ysz = 2.0;
504 float xsz = aspect * ysz;
506 float px = ((float)x / (float)w) * xsz - xsz / 2.0;
507 float py = 1.0 - ((float)y / (float)h) * ysz;
508 float pz = 1.0 / tan(0.5 * vfov);
510 float mag = sqrt(px * px + py * py + pz * pz);
511 return Vector3(px / mag, py / mag, pz / mag);
512 }
514 static int round_pow2(int x)
515 {
516 x--;
517 x = (x >> 1) | x;
518 x = (x >> 2) | x;
519 x = (x >> 4) | x;
520 x = (x >> 8) | x;
521 x = (x >> 16) | x;
522 return x + 1;
523 }
525 static void create_transfer_map(float mean, float sdev)
526 {
527 static float map[XFER_MAP_SZ];
529 if(!xfer_tex) {
530 glGenTextures(1, &xfer_tex);
531 glBindTexture(GL_TEXTURE_1D, xfer_tex);
532 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
533 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
534 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
535 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
536 glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE32F_ARB, XFER_MAP_SZ, 0, GL_LUMINANCE, GL_FLOAT, 0);
537 }
539 for(int i=0; i<XFER_MAP_SZ; i++) {
540 float x = (float)i / (float)(XFER_MAP_SZ - 1);
541 map[i] = gaussian(x, mean, sdev) - 1.0;
542 }
544 glTexSubImage1D(GL_TEXTURE_1D, 0, 0, XFER_MAP_SZ, GL_LUMINANCE, GL_FLOAT, map);
545 xfertex_needs_recalc = false;
546 }