volmetrics

view src/main.cc @ 26:5ee081af59b8

volume rendering
author Eleni Maria Stea <elene.mst@gmail.com>
date Sun, 27 Apr 2014 18:25:40 +0300
parents 4b6c952a83bd
children 77cbc31cd31f
line source
1 #include <GL/glew.h>
2 #include <GL/glut.h>
3 #include <GL/glui.h>
5 #include <math.h>
6 #include <stdio.h>
7 #include <assert.h>
9 #include <vector>
11 #include "mesh.h"
12 #include "sdr.h"
13 #include "volume.h"
15 static bool init(void);
16 static GLUI *create_ui(void);
17 static void display(void);
18 static void reshape(int x, int y);
19 static void keyboard(unsigned char key, int x, int y);
20 static void keyboard_up(unsigned char key, int x, int y);
21 static void mouse(int button, int state, int x, int y);
22 static void motion(int x, int y);
23 static bool init_xfer(void);
24 static void display_xfer(void);
25 static void reshape_xfer(int x, int y);
26 static void mouse_xfer(int button, int state, int x, int y);
27 static void motion_xfer(int x, int y);
28 static void volume_preview();
29 static void draw_slices();
30 static void draw_iso();
31 static void draw_volume();
33 static int mainwin_id, xferwin_id;
34 //todo keyb esc
36 static int win_xsz, win_ysz;
37 static float cam_phi, cam_theta, cam_dist = 6;
38 static std::vector<bool> key_state(256);
40 static const char *vol_fname = "data/test1.vol";
41 static const char *vsdr_path = "data/shaders/transfer.v.glsl";
42 static const char *fsdr_path = "data/shaders/transfer.f.glsl";
43 static const char *vsdr_vol_path = "data/shaders/vol.v.glsl";
44 static const char *fsdr_vol_path = "data/shaders/vol.f.glsl";
46 static unsigned int sprog;
47 static unsigned int sprog_vol;
49 static Volume *vol;
50 static Mesh *mesh;
51 static float cur_z, thres = 0.5, thres2 = 1.0;
52 static float slice_z = 0.5;
54 static int use_orig_vol_res = 1;
55 static int vol_res[3]; // volume sampling resolution x/y/z
56 static float bound_scale = 1.42;
58 static GLUI *ui;
60 static int num_poly = 128;
62 int main(int argc, char **argv)
63 {
64 glutInit(&argc, argv);
65 if(argv[1])
66 vol_fname = argv[1];
68 if(!init()) {
69 fprintf(stderr, "Failed to initialize program.\n");
70 return 1;
71 }
73 init_xfer();
75 glutMainLoop();
76 return 0;
77 }
79 static bool init()
80 {
81 glutInitWindowSize(512, 512);
82 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
84 mainwin_id = glutCreateWindow("CT scan");
85 glutDisplayFunc(display);
86 glutReshapeFunc(reshape);
87 glutKeyboardFunc(keyboard);
88 glutKeyboardUpFunc(keyboard_up);
89 glutMouseFunc(mouse);
90 glutMotionFunc(motion);
92 glewInit();
94 glEnable(GL_DEPTH_TEST);
95 glEnable(GL_NORMALIZE);
97 glEnable(GL_LIGHTING);
98 glEnable(GL_LIGHT0);
99 glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, 1);
101 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
103 //FIXME shaders setup
104 if(!(sprog = sdr_getprog(vsdr_path, fsdr_path)))
105 {
106 fprintf(stderr, "Failed to create shader program!\n");
107 return false;
108 }
110 if(!(sprog_vol = sdr_getprog(vsdr_vol_path, fsdr_vol_path)))
111 {
112 fprintf(stderr, "Failed to create shader program!\n");
113 return false;
114 }
116 vol = new Volume;
117 if(!vol->load(vol_fname)) {
118 fprintf(stderr, "Failed to load %s", vol_fname);
119 return false;
120 }
121 mesh = new Mesh;
122 cur_z = 0.5;
124 vol_res[0] = vol->get_slice(0)->get_width();
125 vol_res[1] = vol->get_slice(0)->get_height();
126 vol_res[2] = vol->get_slice_count();
128 num_poly = std::max(vol_res[0], std::max(vol_res[1], vol_res[2])) * bound_scale;
130 if(!(ui = create_ui())) {
131 return false;
132 }
134 return true;
135 }
137 static GLUI_Spinner *res_spin[3];
138 static void toggle_use_orig(int id)
139 {
140 for(int i=0; i<3; i++) {
141 if(use_orig_vol_res) {
142 res_spin[i]->disable();
143 } else {
144 res_spin[i]->enable();
145 }
146 }
147 }
148 static void thres_change(int id)
149 {
150 static float prev_thres = thres;
151 static float prev_thres2 = thres2;
153 if(prev_thres != thres || prev_thres2 != thres2) {
154 prev_thres = thres;
155 prev_thres2 = thres2;
157 mesh->clear();
159 glutSetWindow(xferwin_id);
160 glutPostRedisplay();
161 glutSetWindow(mainwin_id);
162 glutPostRedisplay();
163 }
164 }
166 static void res_change(int id)
167 {
168 static float prev_resx = vol_res[0];
169 static float prev_resy = vol_res[1];
170 static float prev_resz = vol_res[2];
172 if(prev_resx != vol_res[0] || prev_resy != vol_res[1] || prev_resz != vol_res[2]) {
173 prev_resx = vol_res[0];
174 prev_resy = vol_res[1];
175 prev_resz = vol_res[2];
176 mesh->clear();
177 }
178 }
179 static GLUI *create_ui()
180 {
181 GLUI *ui = GLUI_Master.create_glui("ui");
182 assert(ui);
184 ui->set_main_gfx_window(glutGetWindow());
186 GLUI_Panel *thres_panel = ui->add_panel("iso thresholds");
188 GLUI_Spinner *thres_spin = ui->add_spinner_to_panel(thres_panel, "T1", GLUI_SPINNER_FLOAT, &thres, 0, thres_change);
189 thres_spin->set_float_limits(0, 1);
191 GLUI_Spinner *thres2_spin = ui->add_spinner_to_panel(thres_panel, "T2", GLUI_SPINNER_FLOAT, &thres2, 0, thres_change);
192 thres2_spin->set_float_limits(0, 1);
194 GLUI_Panel *res_panel = ui->add_panel("volume resolution");
196 ui->add_checkbox_to_panel(res_panel, "original resolution", &use_orig_vol_res, 0, toggle_use_orig);
198 static const char *res_spin_name[] = {"x", "y", "z"};
199 for(int i=0; i<3; i++) {
200 res_spin[i] = ui->add_spinner_to_panel(res_panel, res_spin_name[i], GLUI_SPINNER_INT, vol_res + i, 0, res_change);
201 res_spin[i]->set_int_limits(8, 256); // TODO limits as arguments or config
202 res_spin[i]->disable();
203 }
205 GLUI_Panel *preview_panel = ui->add_panel("volume preview");
207 GLUI_Spinner *preview_spin = ui->add_spinner_to_panel(preview_panel, "slice z", GLUI_SPINNER_FLOAT, &slice_z, 0);
208 preview_spin->set_float_limits(0, 1);
210 return ui;
211 }
213 static void volume_preview ()
214 {
215 float aspect = win_xsz / win_ysz;
217 glDisable(GL_DEPTH_TEST);
219 glUseProgram(sprog);
220 int tmin_loc = glGetUniformLocation(sprog, "tmin");
221 if(tmin_loc != -1)
222 glUniform1f(tmin_loc, std::min(thres, thres2));
223 int tmax_loc = glGetUniformLocation(sprog, "tmax");
224 if(tmax_loc != -1)
225 glUniform1f(tmax_loc, std::max(thres, thres2));
227 glMatrixMode(GL_MODELVIEW);
228 glPushMatrix();
229 glLoadIdentity();
231 glMatrixMode(GL_PROJECTION);
232 glPushMatrix();
233 glLoadIdentity();
234 glOrtho(0, aspect, 0, 1, -1, 1);
236 glBindTexture(GL_TEXTURE_3D, vol->get_texture());
237 glEnable(GL_TEXTURE_3D);
238 glBegin(GL_QUADS);
239 glColor3f(1.0, 1.0, 1.0);
240 glTexCoord3f(0, 0, slice_z); glVertex3f(0, 1, 0);
241 glTexCoord3f(0, 1, slice_z); glVertex3f(0, 0.75, 0);
242 glTexCoord3f(1, 1, slice_z); glVertex3f(0.25, 0.75, 0);
243 glTexCoord3f(1, 0, slice_z); glVertex3f(0.25, 1, 0);
244 glEnd();
245 glDisable(GL_TEXTURE_3D);
247 glLineWidth(3);
248 glBegin(GL_LINE_LOOP);
249 glColor3f(0.4, 0.8, 0.4);
250 glVertex3f(-1.0, 1.0, 0.0);
251 glVertex3f(-1.0, 0.5, 0.0);
252 glVertex3f(-0.5, 0.5, 0.0);
253 glVertex3f(-0.5, 1.0, 0.0);
254 glEnd();
256 glMatrixMode(GL_PROJECTION);
257 glPopMatrix();
259 glMatrixMode(GL_MODELVIEW);
260 glPopMatrix();
262 glUseProgram(0);
264 glEnable(GL_DEPTH_TEST);
265 }
267 static void draw_slices()
268 {
269 glBindTexture(GL_TEXTURE_3D, vol->get_texture());
270 glEnable(GL_TEXTURE_3D);
271 glBegin(GL_QUADS);
272 glTexCoord3f(0, 0, cur_z); glVertex3f(-1, -1, 0);
273 glTexCoord3f(0, 1, cur_z); glVertex3f(-1, 1, 0);
274 glTexCoord3f(1, 1, cur_z); glVertex3f(1, 1, 0);
275 glTexCoord3f(1, 0, cur_z); glVertex3f(1, -1, 0);
276 glEnd();
277 glDisable(GL_TEXTURE_3D);
278 }
280 static void draw_iso()
281 {
282 if(mesh->is_empty()) {
283 printf("recalculating isosurface ... ");
284 fflush(stdout);
285 vol->create_mesh(mesh, thres, thres2, vol_res[0], vol_res[1], vol_res[2]);
286 printf("done.\n");
287 }
288 mesh->draw();
289 }
291 static void draw_volume()
292 {
293 glUseProgram(sprog_vol);
295 int tmin_loc = glGetUniformLocation(sprog_vol, "tmin");
296 if(tmin_loc != -1)
297 glUniform1f(tmin_loc, std::min(thres, thres2));
298 int tmax_loc = glGetUniformLocation(sprog_vol, "tmax");
299 if(tmax_loc != -1)
300 glUniform1f(tmax_loc, std::max(thres, thres2));
302 int res_loc = glGetUniformLocation(sprog_vol, "res");
303 if(res_loc != -1)
304 glUniform3f(res_loc, (float)vol_res[0], (float)vol_res[1], (float)vol_res[2]);
306 glDisable(GL_DEPTH_TEST);
307 glBindTexture(GL_TEXTURE_3D, vol->get_texture());
308 glEnable(GL_TEXTURE_3D);
309 glEnable(GL_BLEND);
310 glBegin(GL_QUADS);
311 for(int i=0; i<num_poly; i++)
312 {
313 float tex_z = (float)i / (float)num_poly;
314 float z = 2 * tex_z - 1;
316 glTexCoord3f(0, 0, tex_z); glVertex3f(-bound_scale, -bound_scale, z * bound_scale);
317 glTexCoord3f(0, 1, tex_z); glVertex3f(-bound_scale, bound_scale, z * bound_scale);
318 glTexCoord3f(1, 1, tex_z); glVertex3f(bound_scale, bound_scale, z * bound_scale);
319 glTexCoord3f(1, 0, tex_z); glVertex3f(bound_scale, -bound_scale, z * bound_scale);
320 }
321 glEnd();
322 glDisable(GL_BLEND);
323 glDisable(GL_TEXTURE_3D);
324 glEnable(GL_DEPTH_TEST);
326 glUseProgram(0);
327 }
329 static void display(void)
330 {
331 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
333 glMatrixMode(GL_MODELVIEW);
334 glLoadIdentity();
335 glTranslatef(0, 0, -cam_dist);
336 glRotatef(cam_phi, 1, 0, 0);
337 glRotatef(cam_theta, 0, 1, 0);
339 //draw_slices();
340 //draw_iso();
341 draw_volume();
343 volume_preview();
344 glutSwapBuffers();
345 assert(glGetError() == GL_NO_ERROR);
346 }
348 static void reshape(int x, int y)
349 {
350 glViewport(0, 0, x, y);
351 glMatrixMode(GL_PROJECTION);
352 glLoadIdentity();
353 gluPerspective(45, (float)x / (float)y, 0.5, 500);
355 win_xsz = x;
356 win_ysz = y;
357 }
359 static void keyboard(unsigned char key, int x, int y)
360 {
361 key_state[(int)key] = true;
363 switch(key) {
364 case 27:
365 exit(0);
366 case 'w':
367 {
368 static bool wire;
369 wire = !wire;
370 glPolygonMode(GL_FRONT_AND_BACK, wire ? GL_LINE : GL_FILL);
371 }
372 break;
373 default:
374 break;
375 }
376 }
378 static void keyboard_up(unsigned char key, int x, int y)
379 {
380 key_state[(int) key] = false;
381 }
383 static int prev_x, prev_y;
384 static bool bn_state[32];
385 static float prev_thres, prev_thres2;
387 static void mouse(int bn, int state, int x, int y)
388 {
389 prev_x = x;
390 prev_y = y;
392 bn_state[bn - GLUT_LEFT_BUTTON] = (state == GLUT_DOWN);
394 if(state == GLUT_DOWN) {
395 prev_thres = thres;
396 prev_thres2 = thres2;
397 }
398 else {
399 if(thres != prev_thres || thres2 != prev_thres2) {
400 mesh->clear();
401 }
402 }
403 }
405 static void motion(int x, int y)
406 {
407 int dx = x - prev_x;
408 int dy = y - prev_y;
410 if(!dx && !dy)
411 return;
413 prev_x = x;
414 prev_y = y;
416 if(key_state[(int)'t']) {
417 thres = (float)x / (float)win_xsz;
418 printf("threshold: %f\n", thres);
420 glutPostRedisplay();
421 return;
422 }
424 // camera
425 if(bn_state[0]) {
426 if(key_state[(int)'z']) {
427 //zoom the camera
429 cam_dist += dy * 0.1;
431 if(cam_dist < 0)
432 cam_dist = 0;
433 }
434 else {
435 //rotate the camera
437 cam_phi += dy * 0.5;
438 cam_theta += dx * 0.5;
440 if(cam_phi > 90)
441 cam_phi = 90;
442 if(cam_phi < -90)
443 cam_phi = -90;
444 }
446 glutPostRedisplay();
447 }
448 }
450 static bool init_xfer(void)
451 {
452 glutSetWindow(mainwin_id);
453 int x = glutGet(GLUT_WINDOW_X);
454 int y = glutGet(GLUT_WINDOW_Y) + glutGet(GLUT_WINDOW_HEIGHT);
456 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
457 glutInitWindowSize(512, 100);
458 glutInitWindowPosition(x, y);
460 xferwin_id = glutCreateWindow("Transfer Function");
461 glutDisplayFunc(display_xfer);
462 glutReshapeFunc(reshape_xfer);
463 glutMouseFunc(mouse_xfer);
464 glutMotionFunc(motion_xfer);
466 return true;
467 }
469 static void display_xfer(void)
470 {
471 glClear(GL_COLOR_BUFFER_BIT);
473 int num_val = glutGet(GLUT_WINDOW_WIDTH) / 4.0;
474 float x = 0;
475 float dx = 1.0 / num_val;
476 float low, high;
477 if(thres < thres2) {
478 low = thres;
479 high = thres2;
480 }
481 else {
482 low = thres2;
483 high = thres;
484 }
486 //drawing the transfer function curve
488 glLineWidth(3);
489 glBegin(GL_LINE_STRIP);
490 glColor3f(0.8, 0.3, 0.8);
491 for(int i=0; i<num_val; i++) {
492 float val = transfer_function(x, low, high);
493 glVertex2f(x, val);
494 x += dx;
495 }
496 glEnd();
498 //threshold bars
500 glLineWidth(2);
501 glBegin(GL_LINES);
502 glColor3f(0.4, 0.4, 0.8);
503 glVertex2f(low, 0);
504 glVertex2f(low, 1);
505 glColor3f(0.4, 0.8, 0.4);
506 glVertex2f(high, 0);
507 glVertex2f(high, 1);
508 glEnd();
510 /*
511 * gradient
512 */
513 /* glBegin(GL_QUADS);
514 for(int i=0; i<num_val; i++) {
515 float val = transfer_function(x, low, high);
516 glColor3f(val, val, val);
517 glVertex3f(x, 1.0, 0.0);
518 glVertex3f(x, 0.0, 0.0);
520 val = transfer_function(x + dx, low, high);
521 glColor3f(val, val, val);
522 glVertex3f(x + dx, 0.0, 0.0);
523 glVertex3f(x + dx, 1.0, 0.0);
524 x += dx;
525 }
526 glEnd(); */
528 glutSwapBuffers();
529 assert(glGetError() == GL_NO_ERROR);
530 }
532 static void reshape_xfer(int x, int y)
533 {
534 glViewport(0.0, 0.0, x, y);
535 glMatrixMode(GL_PROJECTION);
536 glLoadIdentity();
537 glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
538 }
540 static float *select_thres;
541 static float prev_select_thres;
542 static void mouse_xfer(int button, int state, int x, int y)
543 {
544 if(button == GLUT_LEFT_BUTTON) {
545 if(state == GLUT_DOWN) {
546 int width = glutGet(GLUT_WINDOW_WIDTH);
547 float xpos = (float)x / (float)width;
548 if(fabs(xpos - thres) <= fabs(xpos - thres2)) {
549 select_thres = &thres;
550 }
551 else {
552 select_thres = &thres2;
553 }
554 prev_select_thres = *select_thres;
555 }
556 else {
557 if(fabs(*select_thres - prev_select_thres) > 0.001) {
558 mesh->clear();
559 ui->sync_live();
560 }
561 select_thres = 0;
562 }
563 }
564 }
566 static void motion_xfer(int x, int y)
567 {
568 if(!select_thres)
569 return;
571 int width = glutGet(GLUT_WINDOW_WIDTH);
572 float xpos = (float)x / (float)width;
574 *select_thres = xpos;
575 thres_change(0);
577 glutPostRedisplay();
578 glutSetWindow(mainwin_id);
579 glutPostRedisplay();
580 }