qvolray

view src/volray.cc @ 22:2d0dfb5751dc

foo
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 11 Apr 2012 18:35:12 +0300
parents 4c62be57fc1a
children 53aca4775514
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 float ray_step = 0.01;
53 static Volume *volume;
56 bool volray_init()
57 {
58 glewInit();
60 if(!(vol_sdr = create_program_load("sdr/volray.v.glsl", "sdr/volray.p.glsl"))) {
61 return false;
62 }
63 set_uniform_int(vol_sdr, "volume", 0);
64 set_uniform_int(vol_sdr, "ray_tex", 1);
65 set_uniform_int(vol_sdr, "xfer_tex", 2);
66 set_uniform_float(vol_sdr, "ray_step", ray_step);
67 set_uniform_float(vol_sdr, "zclip", cur_z);
69 if(!(slice_sdr = create_program_load(0, "sdr/slice.p.glsl"))) {
70 return false;
71 }
72 set_uniform_int(slice_sdr, "volume", 0);
73 set_uniform_int(slice_sdr, "xfer_tex", 1);
75 init_demo();
77 return true;
78 }
80 void volray_setvolume(Volume *vol)
81 {
82 volume = vol;
83 }
85 Volume *volray_getvolume()
86 {
87 return volume;
88 }
90 void volray_setvalue(int which, float val)
91 {
92 switch(which) {
93 case VOLRAY_ZCURSOR:
94 cur_z = val;
95 set_uniform_float(vol_sdr, "zclip", cur_z);
96 post_redisplay();
97 break;
99 case VOLRAY_ZCLIP:
101 default:
102 break;
103 }
104 }
106 float volray_getvalue(int which)
107 {
108 switch(which) {
109 case VOLRAY_ZCURSOR:
110 return cur_z;
112 default:
113 break;
114 }
115 return 0.0;
116 }
118 void volray_draw(void)
119 {
120 /* recalculate primary ray texture if needed */
121 if(raytex_needs_recalc) {
122 create_ray_texture(win_xsz, win_ysz, 50.0, &tex_scale);
123 }
124 /* recalculate transfer function texture if needed */
125 if(xfertex_needs_recalc) {
126 create_transfer_map(xfer_mean, xfer_sdev);
127 }
129 draw_demo();
131 glClear(GL_COLOR_BUFFER_BIT);
133 if(volume) {
134 render_volume();
135 draw_slice();
136 draw_xfer_func();
137 }
139 assert(glGetError() == GL_NO_ERROR);
140 }
142 static void render_volume(void)
143 {
144 /* set the camera transformation */
145 glMatrixMode(GL_MODELVIEW);
146 glPushMatrix();
147 glLoadIdentity();
148 glRotatef(-90, 1, 0, 0);
149 glTranslatef(cam_x, cam_y, -cam_z);
150 glRotatef(cam_theta, 0, 1, 0);
151 glRotatef(cam_phi, 1, 0, 0);
152 glTranslatef(0, 0, -cam_dist);
154 /* setup the texture matrix to map the useful part of the ray texture to [0,1] */
155 glMatrixMode(GL_TEXTURE);
156 glPushMatrix();
157 glLoadIdentity();
158 glScalef(tex_scale.x, tex_scale.y, 1.0);
160 /* tex unit0: volume data 3D texture */
161 glActiveTexture(GL_TEXTURE0);
162 glBindTexture(GL_TEXTURE_3D, volume->get_texture());
163 glEnable(GL_TEXTURE_3D);
165 /* tex unit1: primary rays in view space */
166 glActiveTexture(GL_TEXTURE1);
167 glBindTexture(GL_TEXTURE_2D, ray_tex);
168 glEnable(GL_TEXTURE_2D);
170 /* tex unit2: transfer function (1d) */
171 glActiveTexture(GL_TEXTURE2);
172 glBindTexture(GL_TEXTURE_1D, xfer_tex);
173 glEnable(GL_TEXTURE_1D);
175 bind_program(vol_sdr);
176 glBegin(GL_QUADS);
177 glColor3f(1, 1, 1);
178 glTexCoord2f(0, 1); glVertex2f(-1, -1);
179 glTexCoord2f(1, 1); glVertex2f(1, -1);
180 glTexCoord2f(1, 0); glVertex2f(1, 1);
181 glTexCoord2f(0, 0); glVertex2f(-1, 1);
182 glEnd();
183 bind_program(0);
185 glActiveTexture(GL_TEXTURE2);
186 glDisable(GL_TEXTURE_1D);
187 glActiveTexture(GL_TEXTURE1);
188 glDisable(GL_TEXTURE_2D);
189 glActiveTexture(GL_TEXTURE0);
190 glDisable(GL_TEXTURE_3D);
192 glMatrixMode(GL_TEXTURE);
193 glPopMatrix();
194 glMatrixMode(GL_MODELVIEW);
195 glPopMatrix();
196 }
198 static void draw_slice(void)
199 {
200 glMatrixMode(GL_MODELVIEW);
201 glPushMatrix();
202 glTranslatef(0.9, 0.9, 0);
203 glScalef(0.3, 0.3 * ((float)win_xsz / win_ysz), 1);
204 glTranslatef(-1, -1, 0);
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 glPopMatrix();
231 }
233 static void draw_xfer_func(void)
234 {
235 glMatrixMode(GL_MODELVIEW);
236 glPushMatrix();
237 glTranslatef(-0.9, -0.9, 0);
238 glScalef(0.5, 0.1, 1);
240 glBindTexture(GL_TEXTURE_1D, xfer_tex);
241 glEnable(GL_TEXTURE_1D);
243 glBegin(GL_QUADS);
244 glColor3f(1, 1, 1);
245 glTexCoord1f(1);
246 glVertex2f(1, 0);
247 glVertex2f(1, 1);
248 glTexCoord1f(0);
249 glVertex2f(0, 1);
250 glVertex2f(0, 0);
251 glEnd();
253 glDisable(GL_TEXTURE_1D);
255 glLineWidth(2.0);
256 glBegin(GL_LINE_LOOP);
257 /*if(uimode == UIMODE_XFER) {
258 glColor3f(1, 0, 0);
259 } else {*/
260 glColor3f(0, 0, 1);
261 //}
262 glVertex2f(0, 0);
263 glVertex2f(1, 0);
264 glVertex2f(1, 1);
265 glVertex2f(0, 1);
266 glEnd();
268 glPopMatrix();
269 }
271 void volray_resize(int x, int y)
272 {
273 glViewport(0, 0, x, y);
275 if(x != win_xsz || y != win_ysz) {
276 raytex_needs_recalc = true;
277 win_xsz = x;
278 win_ysz = y;
279 }
280 }
282 #if 0
283 void keyb(unsigned char key, int x, int y)
284 {
285 switch(key) {
286 case 27:
287 exit(0);
289 case 'x':
290 uimode = UIMODE_XFER;
291 post_redisplay();
292 break;
294 case 'c':
295 uimode = UIMODE_CURSOR;
296 post_redisplay();
297 break;
299 default:
300 break;
301 }
302 }
304 void keyb_up(unsigned char key, int x, int y)
305 {
306 switch(key) {
307 case 'x':
308 if(uimode == UIMODE_XFER) {
309 uimode = UIMODE_DEFAULT;
310 post_redisplay();
311 }
312 break;
314 case 'c':
315 if(uimode == UIMODE_CURSOR) {
316 uimode = UIMODE_DEFAULT;
317 post_redisplay();
318 }
319 break;
321 default:
322 break;
323 }
324 }
325 #endif
327 static int bnstate[32];
328 static int prev_x, prev_y;
330 void volray_mouse(int bn, int state, int x, int y)
331 {
332 bnstate[bn] = state;
333 prev_x = x;
334 prev_y = y;
335 }
337 void volray_motion(int x, int y)
338 {
339 int dx = x - prev_x;
340 int dy = y - prev_y;
341 prev_x = x;
342 prev_y = y;
344 /*switch(uimode) {
345 case UIMODE_XFER:
346 if(dx || dy) {
347 xfer_mean += dx / (float)win_xsz;
348 xfer_sdev += 0.5 * dy / (float)win_ysz;
350 xfer_mean = xfer_mean < 0.0 ? 0.0 : (xfer_mean > 1.0 ? 1.0 : xfer_mean);
351 xfer_sdev = xfer_sdev < 0.0 ? 0.0 : (xfer_sdev > 1.0 ? 1.0 : xfer_sdev);
353 xfertex_needs_recalc = true;
354 post_redisplay();
355 }
356 break;
358 case UIMODE_CURSOR:
359 cur_z += 0.5 * dy / (float)win_ysz;
361 if(cur_z < 0.0)
362 cur_z = 0.0;
363 if(cur_z > 1.0)
364 cur_z = 1.0;
366 set_uniform_float(vol_sdr, "zclip", cur_z);
367 post_redisplay();
368 break;
370 default:*/
371 /* view control */
372 if(bnstate[0]) {
373 cam_theta += dx * 0.5;
374 cam_phi += dy * 0.5;
376 if(cam_phi <= -90) cam_phi = -89;
377 if(cam_phi >= 90) cam_phi = 89;
378 post_redisplay();
379 }
381 if(bnstate[1]) {
382 cam_x += dx * 0.025;
383 cam_y += dy * 0.025;
384 post_redisplay();
385 }
387 if(bnstate[2]) {
388 cam_dist += dy * 0.025;
389 if(cam_dist < 0.0) cam_dist = 0.0;
390 post_redisplay();
391 }
392 //}
393 }
395 #if 0
396 int parse_args(int argc, char **argv)
397 {
398 int i;
399 char *endp;
401 for(i=1; i<argc; i++) {
402 if(argv[i][0] == '-' && argv[i][2] == 0) {
403 switch(argv[i][1]) {
404 case 'm':
405 xfer_mean = strtod(argv[++i], &endp);
406 if(endp == argv[i]) {
407 fprintf(stderr, "-m must be followed by the transfer function mean\n");
408 return -1;
409 }
410 break;
412 case 'd':
413 xfer_sdev = strtod(argv[++i], &endp);
414 if(endp == argv[i]) {
415 fprintf(stderr, "-d must be followed by the transfer function std.deviation\n");
416 return -1;
417 }
418 break;
420 default:
421 fprintf(stderr, "unrecognized option: %s\n", argv[i]);
422 return -1;
423 }
424 } else {
425 if(fname) {
426 fprintf(stderr, "unexpected argument: %s\n", argv[i]);
427 return -1;
428 }
429 fname = argv[i];
430 }
431 }
433 if(!fname) {
434 fprintf(stderr, "pass the volume descriptor filename\n");
435 return -1;
436 }
437 return 0;
438 }
439 #endif
442 static void create_ray_texture(int xsz, int ysz, float vfov, Vector2 *tex_scale)
443 {
444 int cur_tex_xsz, cur_tex_ysz;
445 int tex_xsz = round_pow2(xsz);
446 int tex_ysz = round_pow2(ysz);
447 float *teximg, *dir;
449 teximg = new float[3 * xsz * ysz];
450 dir = teximg;
452 for(int i=0; i<ysz; i++) {
453 for(int j=0; j<xsz; j++) {
454 Vector3 rdir = get_primary_ray_dir(j, i, xsz, ysz, vfov);
455 *dir++ = rdir.x;
456 *dir++ = rdir.y;
457 *dir++ = rdir.z;
458 }
459 }
461 if(!ray_tex) {
462 glGenTextures(1, &ray_tex);
463 }
465 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &cur_tex_xsz);
466 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &cur_tex_ysz);
468 if(tex_xsz > cur_tex_xsz || tex_ysz > cur_tex_ysz) {
469 glBindTexture(GL_TEXTURE_2D, ray_tex);
470 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
471 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
472 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
473 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
474 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F_ARB, tex_xsz, tex_ysz, 0, GL_RGB, GL_FLOAT, 0);
475 }
477 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, xsz, ysz, GL_RGB, GL_FLOAT, teximg);
478 delete [] teximg;
480 if(tex_scale) {
481 tex_scale->x = (float)xsz / (float)tex_xsz;
482 tex_scale->y = (float)ysz / (float)tex_ysz;
483 }
484 raytex_needs_recalc = false;
485 }
487 static Vector3 get_primary_ray_dir(int x, int y, int w, int h, float vfov_deg)
488 {
489 float vfov = M_PI * vfov_deg / 180.0;
490 float aspect = (float)w / (float)h;
492 float ysz = 2.0;
493 float xsz = aspect * ysz;
495 float px = ((float)x / (float)w) * xsz - xsz / 2.0;
496 float py = 1.0 - ((float)y / (float)h) * ysz;
497 float pz = 1.0 / tan(0.5 * vfov);
499 float mag = sqrt(px * px + py * py + pz * pz);
500 return Vector3(px / mag, py / mag, pz / mag);
501 }
503 static int round_pow2(int x)
504 {
505 x--;
506 x = (x >> 1) | x;
507 x = (x >> 2) | x;
508 x = (x >> 4) | x;
509 x = (x >> 8) | x;
510 x = (x >> 16) | x;
511 return x + 1;
512 }
514 static void create_transfer_map(float mean, float sdev)
515 {
516 static float map[XFER_MAP_SZ];
518 if(!xfer_tex) {
519 glGenTextures(1, &xfer_tex);
520 glBindTexture(GL_TEXTURE_1D, xfer_tex);
521 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
522 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
523 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
524 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
525 glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE32F_ARB, XFER_MAP_SZ, 0, GL_LUMINANCE, GL_FLOAT, 0);
526 }
528 for(int i=0; i<XFER_MAP_SZ; i++) {
529 float x = (float)i / (float)(XFER_MAP_SZ - 1);
530 map[i] = gaussian(x, mean, sdev) - 1.0;
531 }
533 glTexSubImage1D(GL_TEXTURE_1D, 0, 0, XFER_MAP_SZ, GL_LUMINANCE, GL_FLOAT, map);
534 xfertex_needs_recalc = false;
535 }