qvolray

view src/volray.cc @ 30:40df2cdc6323

transfer function window
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 14 Apr 2012 22:10:30 +0300
parents 93d889a3726a
children 437e1ba9cf39
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 Volume *volume;
54 bool volray_init()
55 {
56 glewInit();
58 if(!(vol_sdr = create_program_load("sdr/volray.v.glsl", "sdr/volray.p.glsl"))) {
59 return false;
60 }
61 set_uniform_int(vol_sdr, "volume", 0);
62 set_uniform_int(vol_sdr, "ray_tex", 1);
63 set_uniform_int(vol_sdr, "xfer_tex", 2);
64 set_uniform_float(vol_sdr, "ray_step", ray_step);
65 set_uniform_float(vol_sdr, "zclip", 0.0);
67 if(!(slice_sdr = create_program_load(0, "sdr/slice.p.glsl"))) {
68 return false;
69 }
70 set_uniform_int(slice_sdr, "volume", 0);
71 set_uniform_int(slice_sdr, "xfer_tex", 1);
73 init_demo();
75 return true;
76 }
78 void volray_setvolume(Volume *vol)
79 {
80 volume = vol;
81 }
83 Volume *volray_getvolume()
84 {
85 return volume;
86 }
88 void volray_setvalue(VolRayOpt which, float val)
89 {
90 switch(which) {
91 case VolRayOpt::ZCURSOR:
92 cur_z = val;
93 if(clip_z) {
94 set_uniform_float(vol_sdr, "zclip", cur_z);
95 }
96 post_redisplay();
97 break;
99 case VolRayOpt::ZCLIP:
100 clip_z = val > 0.5;
101 set_uniform_float(vol_sdr, "zclip", clip_z ? cur_z : 0.0);
102 post_redisplay();
103 break;
105 default:
106 break;
107 }
108 }
110 float volray_getvalue(VolRayOpt which)
111 {
112 switch(which) {
113 case VolRayOpt::ZCURSOR:
114 return cur_z;
116 case VolRayOpt::ZCLIP:
117 return clip_z > 0.5 ? 1.0 : 0.0;
118 break;
120 default:
121 break;
122 }
123 return 0.0;
124 }
126 void volray_draw(void)
127 {
128 /* recalculate primary ray texture if needed */
129 if(raytex_needs_recalc) {
130 create_ray_texture(win_xsz, win_ysz, 50.0, &tex_scale);
131 }
132 /* recalculate transfer function texture if needed */
133 if(xfertex_needs_recalc) {
134 create_transfer_map(xfer_mean, xfer_sdev);
135 }
137 draw_demo();
139 glClear(GL_COLOR_BUFFER_BIT);
141 if(volume) {
142 render_volume();
143 }
145 assert(glGetError() == GL_NO_ERROR);
146 }
148 static void render_volume(void)
149 {
150 /* set the camera transformation */
151 glMatrixMode(GL_MODELVIEW);
152 glPushMatrix();
153 glLoadIdentity();
154 glRotatef(-90, 1, 0, 0);
155 glTranslatef(cam_x, cam_y, -cam_z);
156 glRotatef(cam_theta, 0, 1, 0);
157 glRotatef(cam_phi, 1, 0, 0);
158 glTranslatef(0, 0, -cam_dist);
160 /* setup the texture matrix to map the useful part of the ray texture to [0,1] */
161 glMatrixMode(GL_TEXTURE);
162 glPushMatrix();
163 glLoadIdentity();
164 glScalef(tex_scale.x, tex_scale.y, 1.0);
166 /* tex unit0: volume data 3D texture */
167 glActiveTexture(GL_TEXTURE0);
168 glBindTexture(GL_TEXTURE_3D, volume->get_texture());
169 glEnable(GL_TEXTURE_3D);
171 /* tex unit1: primary rays in view space */
172 glActiveTexture(GL_TEXTURE1);
173 glBindTexture(GL_TEXTURE_2D, ray_tex);
174 glEnable(GL_TEXTURE_2D);
176 /* tex unit2: transfer function (1d) */
177 glActiveTexture(GL_TEXTURE2);
178 glBindTexture(GL_TEXTURE_1D, xfer_tex);
179 glEnable(GL_TEXTURE_1D);
181 bind_program(vol_sdr);
182 glBegin(GL_QUADS);
183 glColor3f(1, 1, 1);
184 glTexCoord2f(0, 1); glVertex2f(-1, -1);
185 glTexCoord2f(1, 1); glVertex2f(1, -1);
186 glTexCoord2f(1, 0); glVertex2f(1, 1);
187 glTexCoord2f(0, 0); glVertex2f(-1, 1);
188 glEnd();
189 bind_program(0);
191 glActiveTexture(GL_TEXTURE2);
192 glDisable(GL_TEXTURE_1D);
193 glActiveTexture(GL_TEXTURE1);
194 glDisable(GL_TEXTURE_2D);
195 glActiveTexture(GL_TEXTURE0);
196 glDisable(GL_TEXTURE_3D);
198 glMatrixMode(GL_TEXTURE);
199 glPopMatrix();
200 glMatrixMode(GL_MODELVIEW);
201 glPopMatrix();
202 }
204 void volray_draw_slice(void)
205 {
206 glActiveTexture(GL_TEXTURE0);
207 glBindTexture(GL_TEXTURE_3D, volume->get_texture());
208 glEnable(GL_TEXTURE_3D);
210 glActiveTexture(GL_TEXTURE1);
211 glBindTexture(GL_TEXTURE_1D, xfer_tex);
212 glEnable(GL_TEXTURE_1D);
214 bind_program(slice_sdr);
216 glBegin(GL_QUADS);
217 glColor3f(1, 1, 1);
218 glTexCoord3f(0, 1, cur_z); glVertex2f(-1, -1);
219 glTexCoord3f(1, 1, cur_z); glVertex2f(1, -1);
220 glTexCoord3f(1, 0, cur_z); glVertex2f(1, 1);
221 glTexCoord3f(0, 0, cur_z); glVertex2f(-1, 1);
222 glEnd();
224 bind_program(0);
226 glActiveTexture(GL_TEXTURE1);
227 glDisable(GL_TEXTURE_1D);
228 glActiveTexture(GL_TEXTURE0);
229 glDisable(GL_TEXTURE_3D);
230 }
232 void volray_draw_xfer(void)
233 {
234 glMatrixMode(GL_MODELVIEW);
235 glPushMatrix();
236 glTranslatef(-1, -1, 0);
237 glScalef(2, 2, 1);
239 glBindTexture(GL_TEXTURE_1D, xfer_tex);
240 glEnable(GL_TEXTURE_1D);
242 glBegin(GL_QUADS);
243 glColor3f(1, 1, 1);
244 glTexCoord1f(1);
245 glVertex2f(1, 0);
246 glVertex2f(1, 1);
247 glTexCoord1f(0);
248 glVertex2f(0, 1);
249 glVertex2f(0, 0);
250 glEnd();
252 glDisable(GL_TEXTURE_1D);
254 glLineWidth(2.0);
255 glBegin(GL_LINE_LOOP);
256 /*if(uimode == UIMODE_XFER) {
257 glColor3f(1, 0, 0);
258 } else {*/
259 glColor3f(0, 0, 1);
260 //}
261 glVertex2f(0, 0);
262 glVertex2f(1, 0);
263 glVertex2f(1, 1);
264 glVertex2f(0, 1);
265 glEnd();
267 glPopMatrix();
268 }
270 void volray_resize(int x, int y)
271 {
272 glViewport(0, 0, x, y);
274 if(x != win_xsz || y != win_ysz) {
275 raytex_needs_recalc = true;
276 win_xsz = x;
277 win_ysz = y;
278 }
279 }
281 #if 0
282 void keyb(unsigned char key, int x, int y)
283 {
284 switch(key) {
285 case 27:
286 exit(0);
288 case 'x':
289 uimode = UIMODE_XFER;
290 post_redisplay();
291 break;
293 case 'c':
294 uimode = UIMODE_CURSOR;
295 post_redisplay();
296 break;
298 default:
299 break;
300 }
301 }
303 void keyb_up(unsigned char key, int x, int y)
304 {
305 switch(key) {
306 case 'x':
307 if(uimode == UIMODE_XFER) {
308 uimode = UIMODE_DEFAULT;
309 post_redisplay();
310 }
311 break;
313 case 'c':
314 if(uimode == UIMODE_CURSOR) {
315 uimode = UIMODE_DEFAULT;
316 post_redisplay();
317 }
318 break;
320 default:
321 break;
322 }
323 }
324 #endif
326 static int bnstate[32];
327 static int prev_x, prev_y;
329 void volray_mouse(int bn, int state, int x, int y)
330 {
331 bnstate[bn] = state;
332 prev_x = x;
333 prev_y = y;
334 }
336 void volray_motion(int x, int y)
337 {
338 int dx = x - prev_x;
339 int dy = y - prev_y;
340 prev_x = x;
341 prev_y = y;
343 /*switch(uimode) {
344 case UIMODE_XFER:
345 if(dx || dy) {
346 xfer_mean += dx / (float)win_xsz;
347 xfer_sdev += 0.5 * dy / (float)win_ysz;
349 xfer_mean = xfer_mean < 0.0 ? 0.0 : (xfer_mean > 1.0 ? 1.0 : xfer_mean);
350 xfer_sdev = xfer_sdev < 0.0 ? 0.0 : (xfer_sdev > 1.0 ? 1.0 : xfer_sdev);
352 xfertex_needs_recalc = true;
353 post_redisplay();
354 }
355 break;
357 case UIMODE_CURSOR:
358 cur_z += 0.5 * dy / (float)win_ysz;
360 if(cur_z < 0.0)
361 cur_z = 0.0;
362 if(cur_z > 1.0)
363 cur_z = 1.0;
365 set_uniform_float(vol_sdr, "zclip", cur_z);
366 post_redisplay();
367 break;
369 default:*/
370 /* view control */
371 if(bnstate[0]) {
372 cam_theta += dx * 0.5;
373 cam_phi += dy * 0.5;
375 if(cam_phi <= -90) cam_phi = -89;
376 if(cam_phi >= 90) cam_phi = 89;
377 post_redisplay();
378 }
380 if(bnstate[1]) {
381 cam_x += dx * 0.025;
382 cam_y += dy * 0.025;
383 post_redisplay();
384 }
386 if(bnstate[2]) {
387 cam_dist += dy * 0.025;
388 if(cam_dist < 0.0) cam_dist = 0.0;
389 post_redisplay();
390 }
391 //}
392 }
394 int parse_args(int argc, char **argv)
395 {
396 int i;
397 char *endp;
399 for(i=1; i<argc; i++) {
400 if(argv[i][0] == '-' && argv[i][2] == 0) {
401 switch(argv[i][1]) {
402 case 'm':
403 xfer_mean = strtod(argv[++i], &endp);
404 if(endp == argv[i]) {
405 fprintf(stderr, "-m must be followed by the transfer function mean\n");
406 return -1;
407 }
408 break;
410 case 'd':
411 xfer_sdev = strtod(argv[++i], &endp);
412 if(endp == argv[i]) {
413 fprintf(stderr, "-d must be followed by the transfer function std.deviation\n");
414 return -1;
415 }
416 break;
418 default:
419 fprintf(stderr, "unrecognized option: %s\n", argv[i]);
420 return -1;
421 }
422 } else {
423 fprintf(stderr, "unexpected argument: %s\n", argv[i]);
424 }
425 }
427 return 0;
428 }
431 static void create_ray_texture(int xsz, int ysz, float vfov, Vector2 *tex_scale)
432 {
433 int cur_tex_xsz, cur_tex_ysz;
434 int tex_xsz = round_pow2(xsz);
435 int tex_ysz = round_pow2(ysz);
436 float *teximg, *dir;
438 teximg = new float[3 * xsz * ysz];
439 dir = teximg;
441 for(int i=0; i<ysz; i++) {
442 for(int j=0; j<xsz; j++) {
443 Vector3 rdir = get_primary_ray_dir(j, i, xsz, ysz, vfov);
444 *dir++ = rdir.x;
445 *dir++ = rdir.y;
446 *dir++ = rdir.z;
447 }
448 }
450 if(!ray_tex) {
451 glGenTextures(1, &ray_tex);
452 }
454 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &cur_tex_xsz);
455 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &cur_tex_ysz);
457 if(tex_xsz > cur_tex_xsz || tex_ysz > cur_tex_ysz) {
458 glBindTexture(GL_TEXTURE_2D, ray_tex);
459 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
460 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
461 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
462 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
463 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F_ARB, tex_xsz, tex_ysz, 0, GL_RGB, GL_FLOAT, 0);
464 }
466 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, xsz, ysz, GL_RGB, GL_FLOAT, teximg);
467 delete [] teximg;
469 if(tex_scale) {
470 tex_scale->x = (float)xsz / (float)tex_xsz;
471 tex_scale->y = (float)ysz / (float)tex_ysz;
472 }
473 raytex_needs_recalc = false;
474 }
476 static Vector3 get_primary_ray_dir(int x, int y, int w, int h, float vfov_deg)
477 {
478 float vfov = M_PI * vfov_deg / 180.0;
479 float aspect = (float)w / (float)h;
481 float ysz = 2.0;
482 float xsz = aspect * ysz;
484 float px = ((float)x / (float)w) * xsz - xsz / 2.0;
485 float py = 1.0 - ((float)y / (float)h) * ysz;
486 float pz = 1.0 / tan(0.5 * vfov);
488 float mag = sqrt(px * px + py * py + pz * pz);
489 return Vector3(px / mag, py / mag, pz / mag);
490 }
492 static int round_pow2(int x)
493 {
494 x--;
495 x = (x >> 1) | x;
496 x = (x >> 2) | x;
497 x = (x >> 4) | x;
498 x = (x >> 8) | x;
499 x = (x >> 16) | x;
500 return x + 1;
501 }
503 static void create_transfer_map(float mean, float sdev)
504 {
505 static float map[XFER_MAP_SZ];
507 if(!xfer_tex) {
508 glGenTextures(1, &xfer_tex);
509 glBindTexture(GL_TEXTURE_1D, xfer_tex);
510 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
511 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
512 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
513 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
514 glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE32F_ARB, XFER_MAP_SZ, 0, GL_LUMINANCE, GL_FLOAT, 0);
515 }
517 for(int i=0; i<XFER_MAP_SZ; i++) {
518 float x = (float)i / (float)(XFER_MAP_SZ - 1);
519 map[i] = gaussian(x, mean, sdev) - 1.0;
520 }
522 glTexSubImage1D(GL_TEXTURE_1D, 0, 0, XFER_MAP_SZ, GL_LUMINANCE, GL_FLOAT, map);
523 xfertex_needs_recalc = false;
524 }