qvolray

view src/volray.cc @ 33:437e1ba9cf39

fixed on osx
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 14 Apr 2012 23:31:17 +0300
parents 40df2cdc6323
children 6ca076bf5084
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;
53 static bool dbg_noray;
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", 0.0);
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(VolRayOpt which, float val)
91 {
92 switch(which) {
93 case VolRayOpt::ZCURSOR:
94 cur_z = val;
95 if(clip_z) {
96 set_uniform_float(vol_sdr, "zclip", cur_z);
97 }
98 post_redisplay();
99 break;
101 case VolRayOpt::ZCLIP:
102 clip_z = val > 0.5;
103 set_uniform_float(vol_sdr, "zclip", clip_z ? cur_z : 0.0);
104 post_redisplay();
105 break;
107 default:
108 break;
109 }
110 }
112 float volray_getvalue(VolRayOpt which)
113 {
114 switch(which) {
115 case VolRayOpt::ZCURSOR:
116 return cur_z;
118 case VolRayOpt::ZCLIP:
119 return clip_z > 0.5 ? 1.0 : 0.0;
120 break;
122 default:
123 break;
124 }
125 return 0.0;
126 }
128 void volray_draw(void)
129 {
130 /* recalculate primary ray texture if needed */
131 if(raytex_needs_recalc) {
132 create_ray_texture(win_xsz, win_ysz, 50.0, &tex_scale);
133 }
134 /* recalculate transfer function texture if needed */
135 if(xfertex_needs_recalc) {
136 create_transfer_map(xfer_mean, xfer_sdev);
137 }
139 glClear(GL_COLOR_BUFFER_BIT);
141 if(dbg_noray)
142 return;
144 draw_demo();
146 if(volume) {
147 render_volume();
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 glClear(GL_COLOR_BUFFER_BIT);
213 glActiveTexture(GL_TEXTURE0);
214 glBindTexture(GL_TEXTURE_3D, volume->get_texture());
215 glEnable(GL_TEXTURE_3D);
217 glActiveTexture(GL_TEXTURE1);
218 glBindTexture(GL_TEXTURE_1D, xfer_tex);
219 glEnable(GL_TEXTURE_1D);
221 bind_program(slice_sdr);
223 glBegin(GL_QUADS);
224 glColor3f(1, 1, 1);
225 glTexCoord3f(0, 1, cur_z); glVertex2f(-1, -1);
226 glTexCoord3f(1, 1, cur_z); glVertex2f(1, -1);
227 glTexCoord3f(1, 0, cur_z); glVertex2f(1, 1);
228 glTexCoord3f(0, 0, cur_z); glVertex2f(-1, 1);
229 glEnd();
231 bind_program(0);
233 glActiveTexture(GL_TEXTURE1);
234 glDisable(GL_TEXTURE_1D);
235 glActiveTexture(GL_TEXTURE0);
236 glDisable(GL_TEXTURE_3D);
237 }
239 void volray_draw_xfer(void)
240 {
241 glClear(GL_COLOR_BUFFER_BIT);
243 glMatrixMode(GL_MODELVIEW);
244 glPushMatrix();
245 glTranslatef(-1, -1, 0);
246 glScalef(2, 2, 1);
248 glBindTexture(GL_TEXTURE_1D, xfer_tex);
249 glEnable(GL_TEXTURE_1D);
251 glBegin(GL_QUADS);
252 glColor3f(1, 1, 1);
253 glTexCoord1f(1);
254 glVertex2f(1, 0);
255 glVertex2f(1, 1);
256 glTexCoord1f(0);
257 glVertex2f(0, 1);
258 glVertex2f(0, 0);
259 glEnd();
261 glDisable(GL_TEXTURE_1D);
263 glLineWidth(2.0);
264 glBegin(GL_LINE_LOOP);
265 /*if(uimode == UIMODE_XFER) {
266 glColor3f(1, 0, 0);
267 } else {*/
268 glColor3f(0, 0, 1);
269 //}
270 glVertex2f(0, 0);
271 glVertex2f(1, 0);
272 glVertex2f(1, 1);
273 glVertex2f(0, 1);
274 glEnd();
276 glPopMatrix();
277 }
279 void volray_resize(int x, int y)
280 {
281 glViewport(0, 0, x, y);
283 if(x != win_xsz || y != win_ysz) {
284 raytex_needs_recalc = true;
285 win_xsz = x;
286 win_ysz = y;
287 }
288 }
290 #if 0
291 void keyb(unsigned char key, int x, int y)
292 {
293 switch(key) {
294 case 27:
295 exit(0);
297 case 'x':
298 uimode = UIMODE_XFER;
299 post_redisplay();
300 break;
302 case 'c':
303 uimode = UIMODE_CURSOR;
304 post_redisplay();
305 break;
307 default:
308 break;
309 }
310 }
312 void keyb_up(unsigned char key, int x, int y)
313 {
314 switch(key) {
315 case 'x':
316 if(uimode == UIMODE_XFER) {
317 uimode = UIMODE_DEFAULT;
318 post_redisplay();
319 }
320 break;
322 case 'c':
323 if(uimode == UIMODE_CURSOR) {
324 uimode = UIMODE_DEFAULT;
325 post_redisplay();
326 }
327 break;
329 default:
330 break;
331 }
332 }
333 #endif
335 static int bnstate[32];
336 static int prev_x, prev_y;
338 void volray_mouse(int bn, int state, int x, int y)
339 {
340 bnstate[bn] = state;
341 prev_x = x;
342 prev_y = y;
343 }
345 void volray_motion(int x, int y)
346 {
347 int dx = x - prev_x;
348 int dy = y - prev_y;
349 prev_x = x;
350 prev_y = y;
352 /*switch(uimode) {
353 case UIMODE_XFER:
354 if(dx || dy) {
355 xfer_mean += dx / (float)win_xsz;
356 xfer_sdev += 0.5 * dy / (float)win_ysz;
358 xfer_mean = xfer_mean < 0.0 ? 0.0 : (xfer_mean > 1.0 ? 1.0 : xfer_mean);
359 xfer_sdev = xfer_sdev < 0.0 ? 0.0 : (xfer_sdev > 1.0 ? 1.0 : xfer_sdev);
361 xfertex_needs_recalc = true;
362 post_redisplay();
363 }
364 break;
366 case UIMODE_CURSOR:
367 cur_z += 0.5 * dy / (float)win_ysz;
369 if(cur_z < 0.0)
370 cur_z = 0.0;
371 if(cur_z > 1.0)
372 cur_z = 1.0;
374 set_uniform_float(vol_sdr, "zclip", cur_z);
375 post_redisplay();
376 break;
378 default:*/
379 /* view control */
380 if(bnstate[0]) {
381 cam_theta += dx * 0.5;
382 cam_phi += dy * 0.5;
384 if(cam_phi <= -90) cam_phi = -89;
385 if(cam_phi >= 90) cam_phi = 89;
386 post_redisplay();
387 }
389 if(bnstate[1]) {
390 cam_x += dx * 0.025;
391 cam_y += dy * 0.025;
392 post_redisplay();
393 }
395 if(bnstate[2]) {
396 cam_dist += dy * 0.025;
397 if(cam_dist < 0.0) cam_dist = 0.0;
398 post_redisplay();
399 }
400 //}
401 }
403 int parse_args(int argc, char **argv)
404 {
405 int i;
406 char *endp;
408 for(i=1; i<argc; i++) {
409 if(argv[i][0] == '-' && argv[i][2] == 0) {
410 switch(argv[i][1]) {
411 case 'm':
412 xfer_mean = strtod(argv[++i], &endp);
413 if(endp == argv[i]) {
414 fprintf(stderr, "-m must be followed by the transfer function mean\n");
415 return -1;
416 }
417 break;
419 case 'd':
420 xfer_sdev = strtod(argv[++i], &endp);
421 if(endp == argv[i]) {
422 fprintf(stderr, "-d must be followed by the transfer function std.deviation\n");
423 return -1;
424 }
425 break;
427 default:
428 fprintf(stderr, "unrecognized option: %s\n", argv[i]);
429 return -1;
430 }
431 } else {
432 fprintf(stderr, "unexpected argument: %s\n", argv[i]);
433 }
434 }
436 if(getenv("DBG_NORAY")) {
437 dbg_noray = true;
438 }
439 return 0;
440 }
443 static void create_ray_texture(int xsz, int ysz, float vfov, Vector2 *tex_scale)
444 {
445 int cur_tex_xsz, cur_tex_ysz;
446 int tex_xsz = round_pow2(xsz);
447 int tex_ysz = round_pow2(ysz);
448 float *teximg, *dir;
450 teximg = new float[3 * xsz * ysz];
451 dir = teximg;
453 for(int i=0; i<ysz; i++) {
454 for(int j=0; j<xsz; j++) {
455 Vector3 rdir = get_primary_ray_dir(j, i, xsz, ysz, vfov);
456 *dir++ = rdir.x;
457 *dir++ = rdir.y;
458 *dir++ = rdir.z;
459 }
460 }
462 if(!ray_tex) {
463 glGenTextures(1, &ray_tex);
464 }
466 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &cur_tex_xsz);
467 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &cur_tex_ysz);
469 if(tex_xsz > cur_tex_xsz || tex_ysz > cur_tex_ysz) {
470 glBindTexture(GL_TEXTURE_2D, ray_tex);
471 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
472 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
473 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
474 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
475 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F_ARB, tex_xsz, tex_ysz, 0, GL_RGB, GL_FLOAT, 0);
476 }
478 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, xsz, ysz, GL_RGB, GL_FLOAT, teximg);
479 delete [] teximg;
481 if(tex_scale) {
482 tex_scale->x = (float)xsz / (float)tex_xsz;
483 tex_scale->y = (float)ysz / (float)tex_ysz;
484 }
485 raytex_needs_recalc = false;
486 }
488 static Vector3 get_primary_ray_dir(int x, int y, int w, int h, float vfov_deg)
489 {
490 float vfov = M_PI * vfov_deg / 180.0;
491 float aspect = (float)w / (float)h;
493 float ysz = 2.0;
494 float xsz = aspect * ysz;
496 float px = ((float)x / (float)w) * xsz - xsz / 2.0;
497 float py = 1.0 - ((float)y / (float)h) * ysz;
498 float pz = 1.0 / tan(0.5 * vfov);
500 float mag = sqrt(px * px + py * py + pz * pz);
501 return Vector3(px / mag, py / mag, pz / mag);
502 }
504 static int round_pow2(int x)
505 {
506 x--;
507 x = (x >> 1) | x;
508 x = (x >> 2) | x;
509 x = (x >> 4) | x;
510 x = (x >> 8) | x;
511 x = (x >> 16) | x;
512 return x + 1;
513 }
515 static void create_transfer_map(float mean, float sdev)
516 {
517 static float map[XFER_MAP_SZ];
519 if(!xfer_tex) {
520 glGenTextures(1, &xfer_tex);
521 glBindTexture(GL_TEXTURE_1D, xfer_tex);
522 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
523 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
524 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
525 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
526 glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE32F_ARB, XFER_MAP_SZ, 0, GL_LUMINANCE, GL_FLOAT, 0);
527 }
529 for(int i=0; i<XFER_MAP_SZ; i++) {
530 float x = (float)i / (float)(XFER_MAP_SZ - 1);
531 map[i] = gaussian(x, mean, sdev) - 1.0;
532 }
534 glTexSubImage1D(GL_TEXTURE_1D, 0, 0, XFER_MAP_SZ, GL_LUMINANCE, GL_FLOAT, map);
535 xfertex_needs_recalc = false;
536 }