volray
view src/volray.c @ 9:a6765984e057
moved the volume loading to volume.c
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 08 Apr 2012 07:11:54 +0300 |
parents | f40e4edfee7e |
children |
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 enum {
20 UIMODE_DEFAULT,
21 UIMODE_XFER,
22 UIMODE_CURSOR
23 };
25 int init(void);
26 void disp(void);
27 void render_volume(void);
28 void draw_slice(void);
29 void draw_xfer_func(void);
30 void reshape(int x, int y);
31 void keyb(unsigned char key, int x, int y);
32 void keyb_up(unsigned char key, int x, int y);
33 void mouse(int bn, int state, int x, int y);
34 void motion(int x, int y);
35 int parse_args(int argc, char **argv);
37 static void create_ray_texture(int xsz, int ysz, float vfov, vec2_t *tex_scale);
38 static vec3_t get_primary_ray_dir(int x, int y, int w, int h, float vfov_deg);
39 static int round_pow2(int x);
40 static void create_transfer_map(float mean, float sdev);
42 static float cam_theta = 0, cam_phi = 0, cam_dist = 4.0;
43 static float cam_x, cam_y, cam_z;
45 static vec2_t tex_scale;
46 static struct volume *volume;
47 static unsigned int vol_sdr, slice_sdr, ray_tex;
48 static int win_xsz, win_ysz;
49 static int raytex_needs_recalc = 1;
51 static unsigned int xfer_tex;
52 static float xfer_mean = 0.7, xfer_sdev = 0.1;
53 static int xfertex_needs_recalc = 1;
55 static int uimode;
56 static float cur_z = 0.0;
57 static float ray_step = 0.01;
59 static const char *fname;
62 int main(int argc, char **argv)
63 {
64 glutInit(&argc, argv);
65 glutInitWindowSize(1280, 720);
66 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
68 if(parse_args(argc, argv) == -1) {
69 return 1;
70 }
72 glutCreateWindow("Volume Raytracer");
74 glutDisplayFunc(disp);
75 glutReshapeFunc(reshape);
76 glutKeyboardFunc(keyb);
77 glutKeyboardUpFunc(keyb_up);
78 glutMouseFunc(mouse);
79 glutMotionFunc(motion);
81 glewInit();
83 if(init() == -1) {
84 return 1;
85 }
87 glutMainLoop();
88 return 0;
89 }
91 int init(void)
92 {
93 if(!(vol_sdr = create_program_load("sdr/volray.v.glsl", "sdr/volray.p.glsl"))) {
94 return -1;
95 }
96 set_uniform_int(vol_sdr, "volume", 0);
97 set_uniform_int(vol_sdr, "ray_tex", 1);
98 set_uniform_int(vol_sdr, "xfer_tex", 2);
99 set_uniform_float(vol_sdr, "ray_step", ray_step);
100 set_uniform_float(vol_sdr, "zclip", cur_z);
102 if(!(slice_sdr = create_program_load(0, "sdr/slice.p.glsl"))) {
103 return -1;
104 }
105 set_uniform_int(slice_sdr, "volume", 0);
106 set_uniform_int(slice_sdr, "xfer_tex", 1);
108 if(!(volume = load_volume(fname))) {
109 return -1;
110 }
112 return 0;
113 }
115 void disp(void)
116 {
117 /* recalculate primary ray texture if needed */
118 if(raytex_needs_recalc) {
119 create_ray_texture(win_xsz, win_ysz, 50.0, &tex_scale);
120 }
121 /* recalculate transfer function texture if needed */
122 if(xfertex_needs_recalc) {
123 create_transfer_map(xfer_mean, xfer_sdev);
124 }
126 render_volume();
127 draw_slice();
128 draw_xfer_func();
130 glutSwapBuffers();
131 assert(glGetError() == GL_NO_ERROR);
132 }
134 void render_volume(void)
135 {
136 /* set the camera transformation */
137 glMatrixMode(GL_MODELVIEW);
138 glPushMatrix();
139 glLoadIdentity();
140 glRotatef(-90, 1, 0, 0);
141 glTranslatef(cam_x, cam_y, -cam_z);
142 glRotatef(cam_theta, 0, 1, 0);
143 glRotatef(cam_phi, 1, 0, 0);
144 glTranslatef(0, 0, -cam_dist);
146 /* setup the texture matrix to map the useful part of the ray texture to [0,1] */
147 glMatrixMode(GL_TEXTURE);
148 glPushMatrix();
149 glLoadIdentity();
150 glScalef(tex_scale.x, tex_scale.y, 1.0);
152 /* tex unit0: volume data 3D texture */
153 glActiveTexture(GL_TEXTURE0);
154 glBindTexture(GL_TEXTURE_3D, volume->tex_vol);
155 glEnable(GL_TEXTURE_3D);
157 /* tex unit1: primary rays in view space */
158 glActiveTexture(GL_TEXTURE1);
159 glBindTexture(GL_TEXTURE_2D, ray_tex);
160 glEnable(GL_TEXTURE_2D);
162 /* tex unit2: transfer function (1d) */
163 glActiveTexture(GL_TEXTURE2);
164 glBindTexture(GL_TEXTURE_1D, xfer_tex);
165 glEnable(GL_TEXTURE_1D);
167 bind_program(vol_sdr);
168 glBegin(GL_QUADS);
169 glColor3f(1, 1, 1);
170 glTexCoord2f(0, 1); glVertex2f(-1, -1);
171 glTexCoord2f(1, 1); glVertex2f(1, -1);
172 glTexCoord2f(1, 0); glVertex2f(1, 1);
173 glTexCoord2f(0, 0); glVertex2f(-1, 1);
174 glEnd();
175 bind_program(0);
177 glActiveTexture(GL_TEXTURE2);
178 glDisable(GL_TEXTURE_1D);
179 glActiveTexture(GL_TEXTURE1);
180 glDisable(GL_TEXTURE_2D);
181 glActiveTexture(GL_TEXTURE0);
182 glDisable(GL_TEXTURE_3D);
184 glMatrixMode(GL_TEXTURE);
185 glPopMatrix();
186 glMatrixMode(GL_MODELVIEW);
187 glPopMatrix();
188 }
190 void draw_slice(void)
191 {
192 glMatrixMode(GL_MODELVIEW);
193 glPushMatrix();
194 glTranslatef(0.9, 0.9, 0);
195 glScalef(0.3, 0.3 * ((float)win_xsz / win_ysz), 1);
196 glTranslatef(-1, -1, 0);
198 glActiveTexture(GL_TEXTURE0);
199 glBindTexture(GL_TEXTURE_3D, volume->tex_vol);
200 glEnable(GL_TEXTURE_3D);
202 glActiveTexture(GL_TEXTURE1);
203 glBindTexture(GL_TEXTURE_1D, xfer_tex);
204 glEnable(GL_TEXTURE_1D);
206 bind_program(slice_sdr);
208 glBegin(GL_QUADS);
209 glColor3f(1, 1, 1);
210 glTexCoord3f(0, 1, cur_z); glVertex2f(-1, -1);
211 glTexCoord3f(1, 1, cur_z); glVertex2f(1, -1);
212 glTexCoord3f(1, 0, cur_z); glVertex2f(1, 1);
213 glTexCoord3f(0, 0, cur_z); glVertex2f(-1, 1);
214 glEnd();
216 bind_program(0);
218 glActiveTexture(GL_TEXTURE1);
219 glDisable(GL_TEXTURE_1D);
220 glActiveTexture(GL_TEXTURE0);
221 glDisable(GL_TEXTURE_3D);
222 glPopMatrix();
223 }
225 void draw_xfer_func(void)
226 {
227 glMatrixMode(GL_MODELVIEW);
228 glPushMatrix();
229 glTranslatef(-0.9, -0.9, 0);
230 glScalef(0.5, 0.1, 1);
232 glBindTexture(GL_TEXTURE_1D, xfer_tex);
233 glEnable(GL_TEXTURE_1D);
235 glBegin(GL_QUADS);
236 glColor3f(1, 1, 1);
237 glTexCoord1f(1);
238 glVertex2f(1, 0);
239 glVertex2f(1, 1);
240 glTexCoord1f(0);
241 glVertex2f(0, 1);
242 glVertex2f(0, 0);
243 glEnd();
245 glDisable(GL_TEXTURE_1D);
247 glLineWidth(2.0);
248 glBegin(GL_LINE_LOOP);
249 if(uimode == UIMODE_XFER) {
250 glColor3f(1, 0, 0);
251 } else {
252 glColor3f(0, 0, 1);
253 }
254 glVertex2f(0, 0);
255 glVertex2f(1, 0);
256 glVertex2f(1, 1);
257 glVertex2f(0, 1);
258 glEnd();
260 glPopMatrix();
261 }
263 void reshape(int x, int y)
264 {
265 glViewport(0, 0, x, y);
267 if(x != win_xsz || y != win_ysz) {
268 raytex_needs_recalc = 1;
269 win_xsz = x;
270 win_ysz = y;
271 }
272 }
274 void keyb(unsigned char key, int x, int y)
275 {
276 switch(key) {
277 case 27:
278 exit(0);
280 case 'x':
281 uimode = UIMODE_XFER;
282 glutPostRedisplay();
283 break;
285 case 'c':
286 uimode = UIMODE_CURSOR;
287 glutPostRedisplay();
288 break;
290 default:
291 break;
292 }
293 }
295 void keyb_up(unsigned char key, int x, int y)
296 {
297 switch(key) {
298 case 'x':
299 if(uimode == UIMODE_XFER) {
300 uimode = UIMODE_DEFAULT;
301 glutPostRedisplay();
302 }
303 break;
305 case 'c':
306 if(uimode == UIMODE_CURSOR) {
307 uimode = UIMODE_DEFAULT;
308 glutPostRedisplay();
309 }
310 break;
312 default:
313 break;
314 }
315 }
317 static int bnstate[32];
318 static int prev_x, prev_y;
320 void mouse(int bn, int state, int x, int y)
321 {
322 bnstate[bn - GLUT_LEFT_BUTTON] = state == GLUT_DOWN;
323 prev_x = x;
324 prev_y = y;
325 }
327 void motion(int x, int y)
328 {
329 int dx = x - prev_x;
330 int dy = y - prev_y;
331 prev_x = x;
332 prev_y = y;
334 switch(uimode) {
335 case UIMODE_XFER:
336 if(dx || dy) {
337 xfer_mean += dx / (float)win_xsz;
338 xfer_sdev += 0.5 * dy / (float)win_ysz;
340 xfer_mean = xfer_mean < 0.0 ? 0.0 : (xfer_mean > 1.0 ? 1.0 : xfer_mean);
341 xfer_sdev = xfer_sdev < 0.0 ? 0.0 : (xfer_sdev > 1.0 ? 1.0 : xfer_sdev);
343 xfertex_needs_recalc = 1;
344 glutPostRedisplay();
345 }
346 break;
348 case UIMODE_CURSOR:
349 cur_z += 0.5 * dy / (float)win_ysz;
351 if(cur_z < 0.0)
352 cur_z = 0.0;
353 if(cur_z > 1.0)
354 cur_z = 1.0;
356 set_uniform_float(vol_sdr, "zclip", cur_z);
357 glutPostRedisplay();
358 break;
360 default:
361 /* view control */
362 if(bnstate[0]) {
363 cam_theta += dx * 0.5;
364 cam_phi += dy * 0.5;
366 if(cam_phi <= -90) cam_phi = -89;
367 if(cam_phi >= 90) cam_phi = 89;
369 glutPostRedisplay();
370 }
372 if(bnstate[1]) {
373 cam_x += dx * 0.025;
374 cam_y += dy * 0.025;
375 glutPostRedisplay();
376 }
378 if(bnstate[2]) {
379 cam_dist += dy * 0.025;
380 if(cam_dist < 0.0) cam_dist = 0.0;
381 glutPostRedisplay();
382 }
383 }
384 }
387 int parse_args(int argc, char **argv)
388 {
389 int i;
390 char *endp;
392 for(i=1; i<argc; i++) {
393 if(argv[i][0] == '-' && argv[i][2] == 0) {
394 switch(argv[i][1]) {
395 case 'm':
396 xfer_mean = strtod(argv[++i], &endp);
397 if(endp == argv[i]) {
398 fprintf(stderr, "-m must be followed by the transfer function mean\n");
399 return -1;
400 }
401 break;
403 case 'd':
404 xfer_sdev = strtod(argv[++i], &endp);
405 if(endp == argv[i]) {
406 fprintf(stderr, "-d must be followed by the transfer function std.deviation\n");
407 return -1;
408 }
409 break;
411 default:
412 fprintf(stderr, "unrecognized option: %s\n", argv[i]);
413 return -1;
414 }
415 } else {
416 if(fname) {
417 fprintf(stderr, "unexpected argument: %s\n", argv[i]);
418 return -1;
419 }
420 fname = argv[i];
421 }
422 }
424 if(!fname) {
425 fprintf(stderr, "pass the volume descriptor filename\n");
426 return -1;
427 }
428 return 0;
429 }
432 static void create_ray_texture(int xsz, int ysz, float vfov, vec2_t *tex_scale)
433 {
434 int i, j;
435 int cur_tex_xsz, cur_tex_ysz;
436 int tex_xsz = round_pow2(xsz);
437 int tex_ysz = round_pow2(ysz);
438 float *teximg, *dir;
440 if(!(teximg = malloc(3 * xsz * ysz * sizeof *teximg))) {
441 return;
442 }
443 dir = teximg;
445 for(i=0; i<ysz; i++) {
446 for(j=0; j<xsz; j++) {
447 vec3_t rdir = get_primary_ray_dir(j, i, xsz, ysz, vfov);
448 *dir++ = rdir.x;
449 *dir++ = rdir.y;
450 *dir++ = rdir.z;
451 }
452 }
454 if(!ray_tex) {
455 glGenTextures(1, &ray_tex);
456 }
458 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &cur_tex_xsz);
459 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &cur_tex_ysz);
461 if(tex_xsz > cur_tex_xsz || tex_ysz > cur_tex_ysz) {
462 glBindTexture(GL_TEXTURE_2D, ray_tex);
463 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
464 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
465 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
466 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
467 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F_ARB, tex_xsz, tex_ysz, 0, GL_RGB, GL_FLOAT, 0);
468 }
470 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, xsz, ysz, GL_RGB, GL_FLOAT, teximg);
471 free(teximg);
473 if(tex_scale) {
474 tex_scale->x = (float)xsz / (float)tex_xsz;
475 tex_scale->y = (float)ysz / (float)tex_ysz;
476 }
477 raytex_needs_recalc = 0;
478 }
480 static vec3_t get_primary_ray_dir(int x, int y, int w, int h, float vfov_deg)
481 {
482 float vfov = M_PI * vfov_deg / 180.0;
483 float aspect = (float)w / (float)h;
485 float ysz = 2.0;
486 float xsz = aspect * ysz;
488 float px = ((float)x / (float)w) * xsz - xsz / 2.0;
489 float py = 1.0 - ((float)y / (float)h) * ysz;
490 float pz = 1.0 / tan(0.5 * vfov);
492 float mag = sqrt(px * px + py * py + pz * pz);
494 return v3_cons(px / mag, py / mag, pz / mag);
495 }
497 static int round_pow2(int x)
498 {
499 x--;
500 x = (x >> 1) | x;
501 x = (x >> 2) | x;
502 x = (x >> 4) | x;
503 x = (x >> 8) | x;
504 x = (x >> 16) | x;
505 return x + 1;
506 }
508 static void create_transfer_map(float mean, float sdev)
509 {
510 static float map[XFER_MAP_SZ];
511 int i;
513 if(!xfer_tex) {
514 glGenTextures(1, &xfer_tex);
515 glBindTexture(GL_TEXTURE_1D, xfer_tex);
516 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
517 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
518 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
519 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
520 glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE32F_ARB, XFER_MAP_SZ, 0, GL_LUMINANCE, GL_FLOAT, 0);
521 }
523 for(i=0; i<XFER_MAP_SZ; i++) {
524 float x = (float)i / (float)(XFER_MAP_SZ - 1);
525 map[i] = gaussian(x, mean, sdev) - 1.0;
526 }
527 putchar('\n');
529 glTexSubImage1D(GL_TEXTURE_1D, 0, 0, XFER_MAP_SZ, GL_LUMINANCE, GL_FLOAT, map);
530 xfertex_needs_recalc = 0;
531 }