qvolray
view src/volray.cc @ 21:4c62be57fc1a
foo
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Wed, 11 Apr 2012 16:59:45 +0300 |
parents | 784d3d321caa |
children | 2d0dfb5751dc |
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 default:
100 break;
101 }
102 }
104 float volray_getvalue(int which)
105 {
106 switch(which) {
107 case VOLRAY_ZCURSOR:
108 return cur_z;
110 default:
111 break;
112 }
113 return 0.0;
114 }
116 void volray_draw(void)
117 {
118 /* recalculate primary ray texture if needed */
119 if(raytex_needs_recalc) {
120 create_ray_texture(win_xsz, win_ysz, 50.0, &tex_scale);
121 }
122 /* recalculate transfer function texture if needed */
123 if(xfertex_needs_recalc) {
124 create_transfer_map(xfer_mean, xfer_sdev);
125 }
127 draw_demo();
129 glClear(GL_COLOR_BUFFER_BIT);
131 if(volume) {
132 render_volume();
133 draw_slice();
134 draw_xfer_func();
135 }
137 assert(glGetError() == GL_NO_ERROR);
138 }
140 static void render_volume(void)
141 {
142 /* set the camera transformation */
143 glMatrixMode(GL_MODELVIEW);
144 glPushMatrix();
145 glLoadIdentity();
146 glRotatef(-90, 1, 0, 0);
147 glTranslatef(cam_x, cam_y, -cam_z);
148 glRotatef(cam_theta, 0, 1, 0);
149 glRotatef(cam_phi, 1, 0, 0);
150 glTranslatef(0, 0, -cam_dist);
152 /* setup the texture matrix to map the useful part of the ray texture to [0,1] */
153 glMatrixMode(GL_TEXTURE);
154 glPushMatrix();
155 glLoadIdentity();
156 glScalef(tex_scale.x, tex_scale.y, 1.0);
158 /* tex unit0: volume data 3D texture */
159 glActiveTexture(GL_TEXTURE0);
160 glBindTexture(GL_TEXTURE_3D, volume->get_texture());
161 glEnable(GL_TEXTURE_3D);
163 /* tex unit1: primary rays in view space */
164 glActiveTexture(GL_TEXTURE1);
165 glBindTexture(GL_TEXTURE_2D, ray_tex);
166 glEnable(GL_TEXTURE_2D);
168 /* tex unit2: transfer function (1d) */
169 glActiveTexture(GL_TEXTURE2);
170 glBindTexture(GL_TEXTURE_1D, xfer_tex);
171 glEnable(GL_TEXTURE_1D);
173 bind_program(vol_sdr);
174 glBegin(GL_QUADS);
175 glColor3f(1, 1, 1);
176 glTexCoord2f(0, 1); glVertex2f(-1, -1);
177 glTexCoord2f(1, 1); glVertex2f(1, -1);
178 glTexCoord2f(1, 0); glVertex2f(1, 1);
179 glTexCoord2f(0, 0); glVertex2f(-1, 1);
180 glEnd();
181 bind_program(0);
183 glActiveTexture(GL_TEXTURE2);
184 glDisable(GL_TEXTURE_1D);
185 glActiveTexture(GL_TEXTURE1);
186 glDisable(GL_TEXTURE_2D);
187 glActiveTexture(GL_TEXTURE0);
188 glDisable(GL_TEXTURE_3D);
190 glMatrixMode(GL_TEXTURE);
191 glPopMatrix();
192 glMatrixMode(GL_MODELVIEW);
193 glPopMatrix();
194 }
196 static void draw_slice(void)
197 {
198 glMatrixMode(GL_MODELVIEW);
199 glPushMatrix();
200 glTranslatef(0.9, 0.9, 0);
201 glScalef(0.3, 0.3 * ((float)win_xsz / win_ysz), 1);
202 glTranslatef(-1, -1, 0);
204 glActiveTexture(GL_TEXTURE0);
205 glBindTexture(GL_TEXTURE_3D, volume->get_texture());
206 glEnable(GL_TEXTURE_3D);
208 glActiveTexture(GL_TEXTURE1);
209 glBindTexture(GL_TEXTURE_1D, xfer_tex);
210 glEnable(GL_TEXTURE_1D);
212 bind_program(slice_sdr);
214 glBegin(GL_QUADS);
215 glColor3f(1, 1, 1);
216 glTexCoord3f(0, 1, cur_z); glVertex2f(-1, -1);
217 glTexCoord3f(1, 1, cur_z); glVertex2f(1, -1);
218 glTexCoord3f(1, 0, cur_z); glVertex2f(1, 1);
219 glTexCoord3f(0, 0, cur_z); glVertex2f(-1, 1);
220 glEnd();
222 bind_program(0);
224 glActiveTexture(GL_TEXTURE1);
225 glDisable(GL_TEXTURE_1D);
226 glActiveTexture(GL_TEXTURE0);
227 glDisable(GL_TEXTURE_3D);
228 glPopMatrix();
229 }
231 static void draw_xfer_func(void)
232 {
233 glMatrixMode(GL_MODELVIEW);
234 glPushMatrix();
235 glTranslatef(-0.9, -0.9, 0);
236 glScalef(0.5, 0.1, 1);
238 glBindTexture(GL_TEXTURE_1D, xfer_tex);
239 glEnable(GL_TEXTURE_1D);
241 glBegin(GL_QUADS);
242 glColor3f(1, 1, 1);
243 glTexCoord1f(1);
244 glVertex2f(1, 0);
245 glVertex2f(1, 1);
246 glTexCoord1f(0);
247 glVertex2f(0, 1);
248 glVertex2f(0, 0);
249 glEnd();
251 glDisable(GL_TEXTURE_1D);
253 glLineWidth(2.0);
254 glBegin(GL_LINE_LOOP);
255 /*if(uimode == UIMODE_XFER) {
256 glColor3f(1, 0, 0);
257 } else {*/
258 glColor3f(0, 0, 1);
259 //}
260 glVertex2f(0, 0);
261 glVertex2f(1, 0);
262 glVertex2f(1, 1);
263 glVertex2f(0, 1);
264 glEnd();
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 volray_motion(int x, int y)
336 {
337 int dx = x - prev_x;
338 int dy = y - prev_y;
339 prev_x = x;
340 prev_y = y;
342 /*switch(uimode) {
343 case UIMODE_XFER:
344 if(dx || dy) {
345 xfer_mean += dx / (float)win_xsz;
346 xfer_sdev += 0.5 * dy / (float)win_ysz;
348 xfer_mean = xfer_mean < 0.0 ? 0.0 : (xfer_mean > 1.0 ? 1.0 : xfer_mean);
349 xfer_sdev = xfer_sdev < 0.0 ? 0.0 : (xfer_sdev > 1.0 ? 1.0 : xfer_sdev);
351 xfertex_needs_recalc = true;
352 post_redisplay();
353 }
354 break;
356 case UIMODE_CURSOR:
357 cur_z += 0.5 * dy / (float)win_ysz;
359 if(cur_z < 0.0)
360 cur_z = 0.0;
361 if(cur_z > 1.0)
362 cur_z = 1.0;
364 set_uniform_float(vol_sdr, "zclip", cur_z);
365 post_redisplay();
366 break;
368 default:*/
369 /* view control */
370 if(bnstate[0]) {
371 cam_theta += dx * 0.5;
372 cam_phi += dy * 0.5;
374 if(cam_phi <= -90) cam_phi = -89;
375 if(cam_phi >= 90) cam_phi = 89;
376 post_redisplay();
377 }
379 if(bnstate[1]) {
380 cam_x += dx * 0.025;
381 cam_y += dy * 0.025;
382 post_redisplay();
383 }
385 if(bnstate[2]) {
386 cam_dist += dy * 0.025;
387 if(cam_dist < 0.0) cam_dist = 0.0;
388 post_redisplay();
389 }
390 //}
391 }
393 #if 0
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 if(fname) {
424 fprintf(stderr, "unexpected argument: %s\n", argv[i]);
425 return -1;
426 }
427 fname = argv[i];
428 }
429 }
431 if(!fname) {
432 fprintf(stderr, "pass the volume descriptor filename\n");
433 return -1;
434 }
435 return 0;
436 }
437 #endif
440 static void create_ray_texture(int xsz, int ysz, float vfov, Vector2 *tex_scale)
441 {
442 int cur_tex_xsz, cur_tex_ysz;
443 int tex_xsz = round_pow2(xsz);
444 int tex_ysz = round_pow2(ysz);
445 float *teximg, *dir;
447 teximg = new float[3 * xsz * ysz];
448 dir = teximg;
450 for(int i=0; i<ysz; i++) {
451 for(int j=0; j<xsz; j++) {
452 Vector3 rdir = get_primary_ray_dir(j, i, xsz, ysz, vfov);
453 *dir++ = rdir.x;
454 *dir++ = rdir.y;
455 *dir++ = rdir.z;
456 }
457 }
459 if(!ray_tex) {
460 glGenTextures(1, &ray_tex);
461 }
463 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &cur_tex_xsz);
464 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &cur_tex_ysz);
466 if(tex_xsz > cur_tex_xsz || tex_ysz > cur_tex_ysz) {
467 glBindTexture(GL_TEXTURE_2D, ray_tex);
468 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
469 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
470 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
471 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
472 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F_ARB, tex_xsz, tex_ysz, 0, GL_RGB, GL_FLOAT, 0);
473 }
475 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, xsz, ysz, GL_RGB, GL_FLOAT, teximg);
476 delete [] teximg;
478 if(tex_scale) {
479 tex_scale->x = (float)xsz / (float)tex_xsz;
480 tex_scale->y = (float)ysz / (float)tex_ysz;
481 }
482 raytex_needs_recalc = false;
483 }
485 static Vector3 get_primary_ray_dir(int x, int y, int w, int h, float vfov_deg)
486 {
487 float vfov = M_PI * vfov_deg / 180.0;
488 float aspect = (float)w / (float)h;
490 float ysz = 2.0;
491 float xsz = aspect * ysz;
493 float px = ((float)x / (float)w) * xsz - xsz / 2.0;
494 float py = 1.0 - ((float)y / (float)h) * ysz;
495 float pz = 1.0 / tan(0.5 * vfov);
497 float mag = sqrt(px * px + py * py + pz * pz);
498 return Vector3(px / mag, py / mag, pz / mag);
499 }
501 static int round_pow2(int x)
502 {
503 x--;
504 x = (x >> 1) | x;
505 x = (x >> 2) | x;
506 x = (x >> 4) | x;
507 x = (x >> 8) | x;
508 x = (x >> 16) | x;
509 return x + 1;
510 }
512 static void create_transfer_map(float mean, float sdev)
513 {
514 static float map[XFER_MAP_SZ];
516 if(!xfer_tex) {
517 glGenTextures(1, &xfer_tex);
518 glBindTexture(GL_TEXTURE_1D, xfer_tex);
519 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
520 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
521 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
522 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
523 glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE32F_ARB, XFER_MAP_SZ, 0, GL_LUMINANCE, GL_FLOAT, 0);
524 }
526 for(int i=0; i<XFER_MAP_SZ; i++) {
527 float x = (float)i / (float)(XFER_MAP_SZ - 1);
528 map[i] = gaussian(x, mean, sdev) - 1.0;
529 }
531 glTexSubImage1D(GL_TEXTURE_1D, 0, 0, XFER_MAP_SZ, GL_LUMINANCE, GL_FLOAT, map);
532 xfertex_needs_recalc = false;
533 }