qvolray

view src/volray.cc @ 36:70b937008134

demo :)
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 15 Apr 2012 07:19:01 +0300
parents 6ca076bf5084
children 450d4c50470f
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();
23 /*
24 void keyb(unsigned char key, int x, int y);
25 void keyb_up(unsigned char key, int x, int y);
26 void mouse(int bn, int state, int x, int y);
27 void motion(int x, int y);
28 */
30 static void create_ray_texture(int xsz, int ysz, float vfov, Vector2 *tex_scale);
31 static Vector3 get_primary_ray_dir(int x, int y, int w, int h, float vfov_deg);
32 static int round_pow2(int x);
33 static void create_transfer_map(float mean, float sdev);
35 static float cam_theta = 0, cam_phi = 0, cam_dist = 4.0;
36 static float cam_x, cam_y, cam_z;
38 static Vector2 tex_scale;
39 static unsigned int vol_sdr, slice_sdr, ray_tex;
40 static int win_xsz, win_ysz;
41 static bool raytex_needs_recalc = true;
43 static unsigned int xfer_tex;
44 static float xfer_mean = 0.7, xfer_sdev = 0.1;
45 static bool xfertex_needs_recalc = true;
47 static float cur_z = 0.5;
48 static bool clip_z;
49 static float ray_step = 0.01;
51 static Demo *demo;
52 static Volume *volume;
54 static bool dbg_noray;
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 demo = new Demo;
77 if(!demo->init()) {
78 return false;
79 }
81 return true;
82 }
84 void volray_setvolume(Volume *vol)
85 {
86 volume = vol;
87 }
89 Volume *volray_getvolume()
90 {
91 return volume;
92 }
94 void volray_setvalue(VolRayOpt which, float val)
95 {
96 switch(which) {
97 case VolRayOpt::ZCURSOR:
98 cur_z = val;
99 if(clip_z) {
100 set_uniform_float(vol_sdr, "zclip", cur_z);
101 }
102 post_redisplay();
103 break;
105 case VolRayOpt::ZCLIP:
106 clip_z = val > 0.5;
107 set_uniform_float(vol_sdr, "zclip", clip_z ? cur_z : 0.0);
108 post_redisplay();
109 break;
111 default:
112 break;
113 }
114 }
116 float volray_getvalue(VolRayOpt which)
117 {
118 switch(which) {
119 case VolRayOpt::ZCURSOR:
120 return cur_z;
122 case VolRayOpt::ZCLIP:
123 return clip_z > 0.5 ? 1.0 : 0.0;
124 break;
126 default:
127 break;
128 }
129 return 0.0;
130 }
132 void volray_draw(void)
133 {
134 /* recalculate primary ray texture if needed */
135 if(raytex_needs_recalc) {
136 create_ray_texture(win_xsz, win_ysz, 50.0, &tex_scale);
137 }
138 /* recalculate transfer function texture if needed */
139 if(xfertex_needs_recalc) {
140 create_transfer_map(xfer_mean, xfer_sdev);
141 }
143 glClear(GL_COLOR_BUFFER_BIT);
145 if(dbg_noray)
146 return;
148 demo->draw();
150 if(volume) {
151 render_volume();
152 }
154 assert(glGetError() == GL_NO_ERROR);
155 }
157 static void render_volume(void)
158 {
159 /* set the camera transformation */
160 glMatrixMode(GL_MODELVIEW);
161 glPushMatrix();
162 glLoadIdentity();
163 glRotatef(-90, 1, 0, 0);
164 glTranslatef(cam_x, cam_y, -cam_z);
165 glRotatef(cam_theta, 0, 1, 0);
166 glRotatef(cam_phi, 1, 0, 0);
167 glTranslatef(0, 0, -cam_dist);
169 /* setup the texture matrix to map the useful part of the ray texture to [0,1] */
170 glMatrixMode(GL_TEXTURE);
171 glPushMatrix();
172 glLoadIdentity();
173 glScalef(tex_scale.x, tex_scale.y, 1.0);
175 /* tex unit0: volume data 3D texture */
176 glActiveTexture(GL_TEXTURE0);
177 glBindTexture(GL_TEXTURE_3D, volume->get_texture());
178 glEnable(GL_TEXTURE_3D);
180 /* tex unit1: primary rays in view space */
181 glActiveTexture(GL_TEXTURE1);
182 glBindTexture(GL_TEXTURE_2D, ray_tex);
183 glEnable(GL_TEXTURE_2D);
185 /* tex unit2: transfer function (1d) */
186 glActiveTexture(GL_TEXTURE2);
187 glBindTexture(GL_TEXTURE_1D, xfer_tex);
188 glEnable(GL_TEXTURE_1D);
190 bind_program(vol_sdr);
191 glBegin(GL_QUADS);
192 glColor3f(1, 1, 1);
193 glTexCoord2f(0, 1); glVertex2f(-1, -1);
194 glTexCoord2f(1, 1); glVertex2f(1, -1);
195 glTexCoord2f(1, 0); glVertex2f(1, 1);
196 glTexCoord2f(0, 0); glVertex2f(-1, 1);
197 glEnd();
198 bind_program(0);
200 glActiveTexture(GL_TEXTURE2);
201 glDisable(GL_TEXTURE_1D);
202 glActiveTexture(GL_TEXTURE1);
203 glDisable(GL_TEXTURE_2D);
204 glActiveTexture(GL_TEXTURE0);
205 glDisable(GL_TEXTURE_3D);
207 glMatrixMode(GL_TEXTURE);
208 glPopMatrix();
209 glMatrixMode(GL_MODELVIEW);
210 glPopMatrix();
211 }
213 void volray_draw_slice(void)
214 {
215 glClear(GL_COLOR_BUFFER_BIT);
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 }
243 void volray_draw_xfer(void)
244 {
245 glClear(GL_COLOR_BUFFER_BIT);
247 glMatrixMode(GL_MODELVIEW);
248 glPushMatrix();
249 glTranslatef(-1, -1, 0);
250 glScalef(2, 2, 1);
252 glBindTexture(GL_TEXTURE_1D, xfer_tex);
253 glEnable(GL_TEXTURE_1D);
255 glBegin(GL_QUADS);
256 glColor3f(1, 1, 1);
257 glTexCoord1f(1);
258 glVertex2f(1, 0);
259 glVertex2f(1, 1);
260 glTexCoord1f(0);
261 glVertex2f(0, 1);
262 glVertex2f(0, 0);
263 glEnd();
265 glDisable(GL_TEXTURE_1D);
266 glPopMatrix();
267 }
269 void volray_resize(int x, int y)
270 {
271 glViewport(0, 0, x, y);
273 if(x != win_xsz || y != win_ysz) {
274 raytex_needs_recalc = true;
275 win_xsz = x;
276 win_ysz = y;
277 }
278 }
280 #if 0
281 void keyb(unsigned char key, int x, int y)
282 {
283 switch(key) {
284 case 27:
285 exit(0);
287 case 'x':
288 uimode = UIMODE_XFER;
289 post_redisplay();
290 break;
292 case 'c':
293 uimode = UIMODE_CURSOR;
294 post_redisplay();
295 break;
297 default:
298 break;
299 }
300 }
302 void keyb_up(unsigned char key, int x, int y)
303 {
304 switch(key) {
305 case 'x':
306 if(uimode == UIMODE_XFER) {
307 uimode = UIMODE_DEFAULT;
308 post_redisplay();
309 }
310 break;
312 case 'c':
313 if(uimode == UIMODE_CURSOR) {
314 uimode = UIMODE_DEFAULT;
315 post_redisplay();
316 }
317 break;
319 default:
320 break;
321 }
322 }
323 #endif
325 static int bnstate[32];
326 static int prev_x, prev_y;
328 void volray_mouse(int bn, int state, int x, int y)
329 {
330 bnstate[bn] = state;
331 prev_x = x;
332 prev_y = y;
333 }
335 void modxfer(int dx, int dy, int max_x, int max_y)
336 {
337 if(!dx && !dy)
338 return;
340 xfer_mean += dx / (float)max_x;
341 xfer_sdev += 0.5 * dy / (float)max_y;
343 xfer_mean = xfer_mean < 0.0 ? 0.0 : (xfer_mean > 1.0 ? 1.0 : xfer_mean);
344 xfer_sdev = xfer_sdev < 0.0 ? 0.0 : (xfer_sdev > 1.0 ? 1.0 : xfer_sdev);
346 xfertex_needs_recalc = true;
347 post_redisplay();
348 }
350 void volray_motion(int x, int y)
351 {
352 int dx = x - prev_x;
353 int dy = y - prev_y;
354 prev_x = x;
355 prev_y = y;
357 /*switch(uimode) {
358 case UIMODE_XFER:
359 break;
361 case UIMODE_CURSOR:
362 cur_z += 0.5 * dy / (float)win_ysz;
364 if(cur_z < 0.0)
365 cur_z = 0.0;
366 if(cur_z > 1.0)
367 cur_z = 1.0;
369 set_uniform_float(vol_sdr, "zclip", cur_z);
370 post_redisplay();
371 break;
373 default:*/
374 /* view control */
375 if(bnstate[0]) {
376 cam_theta += dx * 0.5;
377 cam_phi += dy * 0.5;
379 if(cam_phi <= -90) cam_phi = -89;
380 if(cam_phi >= 90) cam_phi = 89;
381 post_redisplay();
382 }
384 if(bnstate[1]) {
385 cam_x += dx * 0.025;
386 cam_y += dy * 0.025;
387 post_redisplay();
388 }
390 if(bnstate[2]) {
391 cam_dist += dy * 0.025;
392 if(cam_dist < 0.0) cam_dist = 0.0;
393 post_redisplay();
394 }
395 //}
396 }
398 int parse_args(int argc, char **argv)
399 {
400 int i;
401 char *endp;
403 for(i=1; i<argc; i++) {
404 if(argv[i][0] == '-' && argv[i][2] == 0) {
405 switch(argv[i][1]) {
406 case 'm':
407 xfer_mean = strtod(argv[++i], &endp);
408 if(endp == argv[i]) {
409 fprintf(stderr, "-m must be followed by the transfer function mean\n");
410 return -1;
411 }
412 break;
414 case 'd':
415 xfer_sdev = strtod(argv[++i], &endp);
416 if(endp == argv[i]) {
417 fprintf(stderr, "-d must be followed by the transfer function std.deviation\n");
418 return -1;
419 }
420 break;
422 default:
423 fprintf(stderr, "unrecognized option: %s\n", argv[i]);
424 return -1;
425 }
426 } else {
427 fprintf(stderr, "unexpected argument: %s\n", argv[i]);
428 }
429 }
431 if(getenv("DBG_NORAY")) {
432 dbg_noray = true;
433 }
434 return 0;
435 }
438 static void create_ray_texture(int xsz, int ysz, float vfov, Vector2 *tex_scale)
439 {
440 int cur_tex_xsz, cur_tex_ysz;
441 int tex_xsz = round_pow2(xsz);
442 int tex_ysz = round_pow2(ysz);
443 float *teximg, *dir;
445 teximg = new float[3 * xsz * ysz];
446 dir = teximg;
448 for(int i=0; i<ysz; i++) {
449 for(int j=0; j<xsz; j++) {
450 Vector3 rdir = get_primary_ray_dir(j, i, xsz, ysz, vfov);
451 *dir++ = rdir.x;
452 *dir++ = rdir.y;
453 *dir++ = rdir.z;
454 }
455 }
457 if(!ray_tex) {
458 glGenTextures(1, &ray_tex);
459 }
461 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &cur_tex_xsz);
462 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &cur_tex_ysz);
464 if(tex_xsz > cur_tex_xsz || tex_ysz > cur_tex_ysz) {
465 glBindTexture(GL_TEXTURE_2D, ray_tex);
466 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
467 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
468 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
469 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
470 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F_ARB, tex_xsz, tex_ysz, 0, GL_RGB, GL_FLOAT, 0);
471 }
473 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, xsz, ysz, GL_RGB, GL_FLOAT, teximg);
474 delete [] teximg;
476 if(tex_scale) {
477 tex_scale->x = (float)xsz / (float)tex_xsz;
478 tex_scale->y = (float)ysz / (float)tex_ysz;
479 }
480 raytex_needs_recalc = false;
481 }
483 static Vector3 get_primary_ray_dir(int x, int y, int w, int h, float vfov_deg)
484 {
485 float vfov = M_PI * vfov_deg / 180.0;
486 float aspect = (float)w / (float)h;
488 float ysz = 2.0;
489 float xsz = aspect * ysz;
491 float px = ((float)x / (float)w) * xsz - xsz / 2.0;
492 float py = 1.0 - ((float)y / (float)h) * ysz;
493 float pz = 1.0 / tan(0.5 * vfov);
495 float mag = sqrt(px * px + py * py + pz * pz);
496 return Vector3(px / mag, py / mag, pz / mag);
497 }
499 static int round_pow2(int x)
500 {
501 x--;
502 x = (x >> 1) | x;
503 x = (x >> 2) | x;
504 x = (x >> 4) | x;
505 x = (x >> 8) | x;
506 x = (x >> 16) | x;
507 return x + 1;
508 }
510 static void create_transfer_map(float mean, float sdev)
511 {
512 static float map[XFER_MAP_SZ];
514 if(!xfer_tex) {
515 glGenTextures(1, &xfer_tex);
516 glBindTexture(GL_TEXTURE_1D, xfer_tex);
517 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
518 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
519 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
520 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
521 glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE32F_ARB, XFER_MAP_SZ, 0, GL_LUMINANCE, GL_FLOAT, 0);
522 }
524 for(int i=0; i<XFER_MAP_SZ; i++) {
525 float x = (float)i / (float)(XFER_MAP_SZ - 1);
526 map[i] = gaussian(x, mean, sdev) - 1.0;
527 }
529 glTexSubImage1D(GL_TEXTURE_1D, 0, 0, XFER_MAP_SZ, GL_LUMINANCE, GL_FLOAT, map);
530 xfertex_needs_recalc = false;
531 }