qvolray

view src/volray.cc @ 11:8990b5d2c7fe

moving to qt
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 09 Apr 2012 23:42:57 +0300
parents src/volray.c@a6765984e057
children 17d9dc2edc91
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"
17 #define XFER_MAP_SZ 512
19 static void render_volume();
20 static void draw_slice();
21 static void draw_xfer_func();
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 int parse_args(int argc, char **argv);
29 */
31 static void create_ray_texture(int xsz, int ysz, float vfov, Vector2 *tex_scale);
32 static Vector3 get_primary_ray_dir(int x, int y, int w, int h, float vfov_deg);
33 static int round_pow2(int x);
34 static void create_transfer_map(float mean, float sdev);
36 static float cam_theta = 0, cam_phi = 0, cam_dist = 4.0;
37 static float cam_x, cam_y, cam_z;
39 static vec2_t tex_scale;
40 static unsigned int vol_sdr, slice_sdr, ray_tex;
41 static int win_xsz, win_ysz;
42 static bool raytex_needs_recalc = true;
44 static unsigned int xfer_tex;
45 static float xfer_mean = 0.7, xfer_sdev = 0.1;
46 static bool xfertex_needs_recalc = true;
48 static float cur_z = 0.0;
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", cur_z);
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 if(!volume.load(fname)) {
74 return false;
75 }
76 return true;
77 }
79 void volray_draw(void)
80 {
81 /* recalculate primary ray texture if needed */
82 if(raytex_needs_recalc) {
83 create_ray_texture(win_xsz, win_ysz, 50.0, &tex_scale);
84 }
85 /* recalculate transfer function texture if needed */
86 if(xfertex_needs_recalc) {
87 create_transfer_map(xfer_mean, xfer_sdev);
88 }
90 render_volume();
91 draw_slice();
92 draw_xfer_func();
94 assert(glGetError() == GL_NO_ERROR);
95 }
97 static void render_volume(void)
98 {
99 /* set the camera transformation */
100 glMatrixMode(GL_MODELVIEW);
101 glPushMatrix();
102 glLoadIdentity();
103 glRotatef(-90, 1, 0, 0);
104 glTranslatef(cam_x, cam_y, -cam_z);
105 glRotatef(cam_theta, 0, 1, 0);
106 glRotatef(cam_phi, 1, 0, 0);
107 glTranslatef(0, 0, -cam_dist);
109 /* setup the texture matrix to map the useful part of the ray texture to [0,1] */
110 glMatrixMode(GL_TEXTURE);
111 glPushMatrix();
112 glLoadIdentity();
113 glScalef(tex_scale.x, tex_scale.y, 1.0);
115 /* tex unit0: volume data 3D texture */
116 glActiveTexture(GL_TEXTURE0);
117 glBindTexture(GL_TEXTURE_3D, volume.get_texture());
118 glEnable(GL_TEXTURE_3D);
120 /* tex unit1: primary rays in view space */
121 glActiveTexture(GL_TEXTURE1);
122 glBindTexture(GL_TEXTURE_2D, ray_tex);
123 glEnable(GL_TEXTURE_2D);
125 /* tex unit2: transfer function (1d) */
126 glActiveTexture(GL_TEXTURE2);
127 glBindTexture(GL_TEXTURE_1D, xfer_tex);
128 glEnable(GL_TEXTURE_1D);
130 bind_program(vol_sdr);
131 glBegin(GL_QUADS);
132 glColor3f(1, 1, 1);
133 glTexCoord2f(0, 1); glVertex2f(-1, -1);
134 glTexCoord2f(1, 1); glVertex2f(1, -1);
135 glTexCoord2f(1, 0); glVertex2f(1, 1);
136 glTexCoord2f(0, 0); glVertex2f(-1, 1);
137 glEnd();
138 bind_program(0);
140 glActiveTexture(GL_TEXTURE2);
141 glDisable(GL_TEXTURE_1D);
142 glActiveTexture(GL_TEXTURE1);
143 glDisable(GL_TEXTURE_2D);
144 glActiveTexture(GL_TEXTURE0);
145 glDisable(GL_TEXTURE_3D);
147 glMatrixMode(GL_TEXTURE);
148 glPopMatrix();
149 glMatrixMode(GL_MODELVIEW);
150 glPopMatrix();
151 }
153 static void draw_slice(void)
154 {
155 glMatrixMode(GL_MODELVIEW);
156 glPushMatrix();
157 glTranslatef(0.9, 0.9, 0);
158 glScalef(0.3, 0.3 * ((float)win_xsz / win_ysz), 1);
159 glTranslatef(-1, -1, 0);
161 glActiveTexture(GL_TEXTURE0);
162 glBindTexture(GL_TEXTURE_3D, volume.get_texture());
163 glEnable(GL_TEXTURE_3D);
165 glActiveTexture(GL_TEXTURE1);
166 glBindTexture(GL_TEXTURE_1D, xfer_tex);
167 glEnable(GL_TEXTURE_1D);
169 bind_program(slice_sdr);
171 glBegin(GL_QUADS);
172 glColor3f(1, 1, 1);
173 glTexCoord3f(0, 1, cur_z); glVertex2f(-1, -1);
174 glTexCoord3f(1, 1, cur_z); glVertex2f(1, -1);
175 glTexCoord3f(1, 0, cur_z); glVertex2f(1, 1);
176 glTexCoord3f(0, 0, cur_z); glVertex2f(-1, 1);
177 glEnd();
179 bind_program(0);
181 glActiveTexture(GL_TEXTURE1);
182 glDisable(GL_TEXTURE_1D);
183 glActiveTexture(GL_TEXTURE0);
184 glDisable(GL_TEXTURE_3D);
185 glPopMatrix();
186 }
188 static void draw_xfer_func(void)
189 {
190 glMatrixMode(GL_MODELVIEW);
191 glPushMatrix();
192 glTranslatef(-0.9, -0.9, 0);
193 glScalef(0.5, 0.1, 1);
195 glBindTexture(GL_TEXTURE_1D, xfer_tex);
196 glEnable(GL_TEXTURE_1D);
198 glBegin(GL_QUADS);
199 glColor3f(1, 1, 1);
200 glTexCoord1f(1);
201 glVertex2f(1, 0);
202 glVertex2f(1, 1);
203 glTexCoord1f(0);
204 glVertex2f(0, 1);
205 glVertex2f(0, 0);
206 glEnd();
208 glDisable(GL_TEXTURE_1D);
210 glLineWidth(2.0);
211 glBegin(GL_LINE_LOOP);
212 if(uimode == UIMODE_XFER) {
213 glColor3f(1, 0, 0);
214 } else {
215 glColor3f(0, 0, 1);
216 }
217 glVertex2f(0, 0);
218 glVertex2f(1, 0);
219 glVertex2f(1, 1);
220 glVertex2f(0, 1);
221 glEnd();
223 glPopMatrix();
224 }
226 void volray_resize(int x, int y)
227 {
228 glViewport(0, 0, x, y);
230 if(x != win_xsz || y != win_ysz) {
231 raytex_needs_recalc = true;
232 win_xsz = x;
233 win_ysz = y;
234 }
235 }
237 #if 0
238 void keyb(unsigned char key, int x, int y)
239 {
240 switch(key) {
241 case 27:
242 exit(0);
244 case 'x':
245 uimode = UIMODE_XFER;
246 glutPostRedisplay();
247 break;
249 case 'c':
250 uimode = UIMODE_CURSOR;
251 glutPostRedisplay();
252 break;
254 default:
255 break;
256 }
257 }
259 void keyb_up(unsigned char key, int x, int y)
260 {
261 switch(key) {
262 case 'x':
263 if(uimode == UIMODE_XFER) {
264 uimode = UIMODE_DEFAULT;
265 glutPostRedisplay();
266 }
267 break;
269 case 'c':
270 if(uimode == UIMODE_CURSOR) {
271 uimode = UIMODE_DEFAULT;
272 glutPostRedisplay();
273 }
274 break;
276 default:
277 break;
278 }
279 }
281 static int bnstate[32];
282 static int prev_x, prev_y;
284 void mouse(int bn, int state, int x, int y)
285 {
286 bnstate[bn - GLUT_LEFT_BUTTON] = state == GLUT_DOWN;
287 prev_x = x;
288 prev_y = y;
289 }
291 void motion(int x, int y)
292 {
293 int dx = x - prev_x;
294 int dy = y - prev_y;
295 prev_x = x;
296 prev_y = y;
298 switch(uimode) {
299 case UIMODE_XFER:
300 if(dx || dy) {
301 xfer_mean += dx / (float)win_xsz;
302 xfer_sdev += 0.5 * dy / (float)win_ysz;
304 xfer_mean = xfer_mean < 0.0 ? 0.0 : (xfer_mean > 1.0 ? 1.0 : xfer_mean);
305 xfer_sdev = xfer_sdev < 0.0 ? 0.0 : (xfer_sdev > 1.0 ? 1.0 : xfer_sdev);
307 xfertex_needs_recalc = true;
308 glutPostRedisplay();
309 }
310 break;
312 case UIMODE_CURSOR:
313 cur_z += 0.5 * dy / (float)win_ysz;
315 if(cur_z < 0.0)
316 cur_z = 0.0;
317 if(cur_z > 1.0)
318 cur_z = 1.0;
320 set_uniform_float(vol_sdr, "zclip", cur_z);
321 glutPostRedisplay();
322 break;
324 default:
325 /* view control */
326 if(bnstate[0]) {
327 cam_theta += dx * 0.5;
328 cam_phi += dy * 0.5;
330 if(cam_phi <= -90) cam_phi = -89;
331 if(cam_phi >= 90) cam_phi = 89;
333 glutPostRedisplay();
334 }
336 if(bnstate[1]) {
337 cam_x += dx * 0.025;
338 cam_y += dy * 0.025;
339 glutPostRedisplay();
340 }
342 if(bnstate[2]) {
343 cam_dist += dy * 0.025;
344 if(cam_dist < 0.0) cam_dist = 0.0;
345 glutPostRedisplay();
346 }
347 }
348 }
351 int parse_args(int argc, char **argv)
352 {
353 int i;
354 char *endp;
356 for(i=1; i<argc; i++) {
357 if(argv[i][0] == '-' && argv[i][2] == 0) {
358 switch(argv[i][1]) {
359 case 'm':
360 xfer_mean = strtod(argv[++i], &endp);
361 if(endp == argv[i]) {
362 fprintf(stderr, "-m must be followed by the transfer function mean\n");
363 return -1;
364 }
365 break;
367 case 'd':
368 xfer_sdev = strtod(argv[++i], &endp);
369 if(endp == argv[i]) {
370 fprintf(stderr, "-d must be followed by the transfer function std.deviation\n");
371 return -1;
372 }
373 break;
375 default:
376 fprintf(stderr, "unrecognized option: %s\n", argv[i]);
377 return -1;
378 }
379 } else {
380 if(fname) {
381 fprintf(stderr, "unexpected argument: %s\n", argv[i]);
382 return -1;
383 }
384 fname = argv[i];
385 }
386 }
388 if(!fname) {
389 fprintf(stderr, "pass the volume descriptor filename\n");
390 return -1;
391 }
392 return 0;
393 }
394 #endif
397 static void create_ray_texture(int xsz, int ysz, float vfov, Vector2 *tex_scale)
398 {
399 int cur_tex_xsz, cur_tex_ysz;
400 int tex_xsz = round_pow2(xsz);
401 int tex_ysz = round_pow2(ysz);
402 float *teximg, *dir;
404 teximg = new float[3 * xsz * ysz];
405 dir = teximg;
407 for(int i=0; i<ysz; i++) {
408 for(int j=0; j<xsz; j++) {
409 Vector3 rdir = get_primary_ray_dir(j, i, xsz, ysz, vfov);
410 *dir++ = rdir.x;
411 *dir++ = rdir.y;
412 *dir++ = rdir.z;
413 }
414 }
416 if(!ray_tex) {
417 glGenTextures(1, &ray_tex);
418 }
420 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &cur_tex_xsz);
421 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &cur_tex_ysz);
423 if(tex_xsz > cur_tex_xsz || tex_ysz > cur_tex_ysz) {
424 glBindTexture(GL_TEXTURE_2D, ray_tex);
425 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
426 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
427 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
428 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
429 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F_ARB, tex_xsz, tex_ysz, 0, GL_RGB, GL_FLOAT, 0);
430 }
432 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, xsz, ysz, GL_RGB, GL_FLOAT, teximg);
433 delete [] teximg;
435 if(tex_scale) {
436 tex_scale->x = (float)xsz / (float)tex_xsz;
437 tex_scale->y = (float)ysz / (float)tex_ysz;
438 }
439 raytex_needs_recalc = false;
440 }
442 static Vector3 get_primary_ray_dir(int x, int y, int w, int h, float vfov_deg)
443 {
444 float vfov = M_PI * vfov_deg / 180.0;
445 float aspect = (float)w / (float)h;
447 float ysz = 2.0;
448 float xsz = aspect * ysz;
450 float px = ((float)x / (float)w) * xsz - xsz / 2.0;
451 float py = 1.0 - ((float)y / (float)h) * ysz;
452 float pz = 1.0 / tan(0.5 * vfov);
454 float mag = sqrt(px * px + py * py + pz * pz);
455 return Vector3(px / mag, py / mag, pz / mag);
456 }
458 static int round_pow2(int x)
459 {
460 x--;
461 x = (x >> 1) | x;
462 x = (x >> 2) | x;
463 x = (x >> 4) | x;
464 x = (x >> 8) | x;
465 x = (x >> 16) | x;
466 return x + 1;
467 }
469 static void create_transfer_map(float mean, float sdev)
470 {
471 static float map[XFER_MAP_SZ];
473 if(!xfer_tex) {
474 glGenTextures(1, &xfer_tex);
475 glBindTexture(GL_TEXTURE_1D, xfer_tex);
476 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
477 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
478 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
479 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
480 glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE32F_ARB, XFER_MAP_SZ, 0, GL_LUMINANCE, GL_FLOAT, 0);
481 }
483 for(int i=0; i<XFER_MAP_SZ; i++) {
484 float x = (float)i / (float)(XFER_MAP_SZ - 1);
485 map[i] = gaussian(x, mean, sdev) - 1.0;
486 }
488 glTexSubImage1D(GL_TEXTURE_1D, 0, 0, XFER_MAP_SZ, GL_LUMINANCE, GL_FLOAT, map);
489 xfertex_needs_recalc = false;
490 }