volmetrics

view src/main.cc @ 29:77cbc31cd31f

foo
author Eleni Maria Stea <elene.mst@gmail.com>
date Sun, 27 Apr 2014 21:07:10 +0300
parents 5ee081af59b8
children e548d95e0667
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 #ifdef ISO
195 GLUI_Panel *res_panel = ui->add_panel("volume resolution");
197 ui->add_checkbox_to_panel(res_panel, "original resolution", &use_orig_vol_res, 0, toggle_use_orig);
199 static const char *res_spin_name[] = {"x", "y", "z"};
200 for(int i=0; i<3; i++) {
201 res_spin[i] = ui->add_spinner_to_panel(res_panel, res_spin_name[i], GLUI_SPINNER_INT, vol_res + i, 0, res_change);
202 res_spin[i]->set_int_limits(8, 256); // TODO limits as arguments or config
203 res_spin[i]->disable();
204 }
205 #endif
206 GLUI_Panel *preview_panel = ui->add_panel("volume preview");
208 GLUI_Spinner *preview_spin = ui->add_spinner_to_panel(preview_panel, "slice z", GLUI_SPINNER_FLOAT, &slice_z, 0);
209 preview_spin->set_float_limits(0, 1);
211 return ui;
212 }
214 static void volume_preview ()
215 {
216 float aspect = win_xsz / win_ysz;
218 glDisable(GL_DEPTH_TEST);
220 glUseProgram(sprog);
221 int tmin_loc = glGetUniformLocation(sprog, "tmin");
222 if(tmin_loc != -1)
223 glUniform1f(tmin_loc, std::min(thres, thres2));
224 int tmax_loc = glGetUniformLocation(sprog, "tmax");
225 if(tmax_loc != -1)
226 glUniform1f(tmax_loc, std::max(thres, thres2));
228 glMatrixMode(GL_MODELVIEW);
229 glPushMatrix();
230 glLoadIdentity();
232 glMatrixMode(GL_PROJECTION);
233 glPushMatrix();
234 glLoadIdentity();
235 glOrtho(0, aspect, 0, 1, -1, 1);
237 glBindTexture(GL_TEXTURE_3D, vol->get_texture());
238 glEnable(GL_TEXTURE_3D);
239 glBegin(GL_QUADS);
240 glColor3f(1.0, 1.0, 1.0);
241 glTexCoord3f(0, 0, slice_z); glVertex3f(0, 1, 0);
242 glTexCoord3f(0, 1, slice_z); glVertex3f(0, 0.75, 0);
243 glTexCoord3f(1, 1, slice_z); glVertex3f(0.25, 0.75, 0);
244 glTexCoord3f(1, 0, slice_z); glVertex3f(0.25, 1, 0);
245 glEnd();
246 glDisable(GL_TEXTURE_3D);
248 glLineWidth(3);
249 glBegin(GL_LINE_LOOP);
250 glColor3f(0.4, 0.8, 0.4);
251 glVertex3f(-1.0, 1.0, 0.0);
252 glVertex3f(-1.0, 0.5, 0.0);
253 glVertex3f(-0.5, 0.5, 0.0);
254 glVertex3f(-0.5, 1.0, 0.0);
255 glEnd();
257 glMatrixMode(GL_PROJECTION);
258 glPopMatrix();
260 glMatrixMode(GL_MODELVIEW);
261 glPopMatrix();
263 glUseProgram(0);
265 glEnable(GL_DEPTH_TEST);
266 }
268 static void draw_slices()
269 {
270 glBindTexture(GL_TEXTURE_3D, vol->get_texture());
271 glEnable(GL_TEXTURE_3D);
272 glBegin(GL_QUADS);
273 glTexCoord3f(0, 0, cur_z); glVertex3f(-1, -1, 0);
274 glTexCoord3f(0, 1, cur_z); glVertex3f(-1, 1, 0);
275 glTexCoord3f(1, 1, cur_z); glVertex3f(1, 1, 0);
276 glTexCoord3f(1, 0, cur_z); glVertex3f(1, -1, 0);
277 glEnd();
278 glDisable(GL_TEXTURE_3D);
279 }
281 static void draw_iso()
282 {
283 if(mesh->is_empty()) {
284 printf("recalculating isosurface ... ");
285 fflush(stdout);
286 vol->create_mesh(mesh, thres, thres2, vol_res[0], vol_res[1], vol_res[2]);
287 printf("done.\n");
288 }
289 mesh->draw();
290 }
292 static void draw_volume()
293 {
294 glUseProgram(sprog_vol);
296 int tmin_loc = glGetUniformLocation(sprog_vol, "tmin");
297 if(tmin_loc != -1)
298 glUniform1f(tmin_loc, std::min(thres, thres2));
299 int tmax_loc = glGetUniformLocation(sprog_vol, "tmax");
300 if(tmax_loc != -1)
301 glUniform1f(tmax_loc, std::max(thres, thres2));
303 int res_loc = glGetUniformLocation(sprog_vol, "res");
304 if(res_loc != -1)
305 glUniform3f(res_loc, (float)vol_res[0], (float)vol_res[1], (float)vol_res[2]);
307 glDisable(GL_DEPTH_TEST);
308 glBindTexture(GL_TEXTURE_3D, vol->get_texture());
309 glEnable(GL_TEXTURE_3D);
310 glEnable(GL_BLEND);
311 glBegin(GL_QUADS);
312 for(int i=0; i<num_poly; i++)
313 {
314 float tex_z = (float)i / (float)num_poly;
315 float z = 2 * tex_z - 1;
317 glTexCoord3f(0, 0, tex_z); glVertex3f(-bound_scale, -bound_scale, z * bound_scale);
318 glTexCoord3f(0, 1, tex_z); glVertex3f(-bound_scale, bound_scale, z * bound_scale);
319 glTexCoord3f(1, 1, tex_z); glVertex3f(bound_scale, bound_scale, z * bound_scale);
320 glTexCoord3f(1, 0, tex_z); glVertex3f(bound_scale, -bound_scale, z * bound_scale);
321 }
322 glEnd();
323 glDisable(GL_BLEND);
324 glDisable(GL_TEXTURE_3D);
325 glEnable(GL_DEPTH_TEST);
327 glUseProgram(0);
328 }
330 static void display(void)
331 {
332 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
334 glMatrixMode(GL_MODELVIEW);
335 glLoadIdentity();
336 glTranslatef(0, 0, -cam_dist);
337 glRotatef(cam_phi, 1, 0, 0);
338 glRotatef(cam_theta, 0, 1, 0);
340 //draw_slices();
341 //draw_iso();
342 draw_volume();
344 volume_preview();
345 glutSwapBuffers();
346 assert(glGetError() == GL_NO_ERROR);
347 }
349 static void reshape(int x, int y)
350 {
351 glViewport(0, 0, x, y);
352 glMatrixMode(GL_PROJECTION);
353 glLoadIdentity();
354 gluPerspective(45, (float)x / (float)y, 0.5, 500);
356 win_xsz = x;
357 win_ysz = y;
358 }
360 static void keyboard(unsigned char key, int x, int y)
361 {
362 key_state[(int)key] = true;
364 switch(key) {
365 case 27:
366 exit(0);
367 case 'w':
368 {
369 static bool wire;
370 wire = !wire;
371 glPolygonMode(GL_FRONT_AND_BACK, wire ? GL_LINE : GL_FILL);
372 }
373 break;
374 default:
375 break;
376 }
377 }
379 static void keyboard_up(unsigned char key, int x, int y)
380 {
381 key_state[(int) key] = false;
382 }
384 static int prev_x, prev_y;
385 static bool bn_state[32];
386 static float prev_thres, prev_thres2;
388 static void mouse(int bn, int state, int x, int y)
389 {
390 prev_x = x;
391 prev_y = y;
393 bn_state[bn - GLUT_LEFT_BUTTON] = (state == GLUT_DOWN);
395 if(state == GLUT_DOWN) {
396 prev_thres = thres;
397 prev_thres2 = thres2;
398 }
399 else {
400 if(thres != prev_thres || thres2 != prev_thres2) {
401 mesh->clear();
402 }
403 }
404 }
406 static void motion(int x, int y)
407 {
408 int dx = x - prev_x;
409 int dy = y - prev_y;
411 if(!dx && !dy)
412 return;
414 prev_x = x;
415 prev_y = y;
417 if(key_state[(int)'t']) {
418 thres = (float)x / (float)win_xsz;
419 printf("threshold: %f\n", thres);
421 glutPostRedisplay();
422 return;
423 }
425 // camera
426 if(bn_state[0]) {
427 if(key_state[(int)'z']) {
428 //zoom the camera
430 cam_dist += dy * 0.1;
432 if(cam_dist < 0)
433 cam_dist = 0;
434 }
435 else {
436 //rotate the camera
438 cam_phi += dy * 0.5;
439 cam_theta += dx * 0.5;
441 if(cam_phi > 90)
442 cam_phi = 90;
443 if(cam_phi < -90)
444 cam_phi = -90;
445 }
447 glutPostRedisplay();
448 }
449 }
451 static bool init_xfer(void)
452 {
453 glutSetWindow(mainwin_id);
454 int x = glutGet(GLUT_WINDOW_X);
455 int y = glutGet(GLUT_WINDOW_Y) + glutGet(GLUT_WINDOW_HEIGHT);
457 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
458 glutInitWindowSize(512, 100);
459 glutInitWindowPosition(x, y);
461 xferwin_id = glutCreateWindow("Transfer Function");
462 glutDisplayFunc(display_xfer);
463 glutReshapeFunc(reshape_xfer);
464 glutMouseFunc(mouse_xfer);
465 glutMotionFunc(motion_xfer);
467 return true;
468 }
470 static void display_xfer(void)
471 {
472 glClear(GL_COLOR_BUFFER_BIT);
474 int num_val = glutGet(GLUT_WINDOW_WIDTH) / 4.0;
475 float x = 0;
476 float dx = 1.0 / num_val;
477 float low, high;
478 if(thres < thres2) {
479 low = thres;
480 high = thres2;
481 }
482 else {
483 low = thres2;
484 high = thres;
485 }
487 //drawing the transfer function curve
489 glLineWidth(3);
490 glBegin(GL_LINE_STRIP);
491 glColor3f(0.8, 0.3, 0.8);
492 for(int i=0; i<num_val; i++) {
493 float val = transfer_function(x, low, high);
494 glVertex2f(x, val);
495 x += dx;
496 }
497 glEnd();
499 //threshold bars
501 glLineWidth(2);
502 glBegin(GL_LINES);
503 glColor3f(0.4, 0.4, 0.8);
504 glVertex2f(low, 0);
505 glVertex2f(low, 1);
506 glColor3f(0.4, 0.8, 0.4);
507 glVertex2f(high, 0);
508 glVertex2f(high, 1);
509 glEnd();
511 /*
512 * gradient
513 */
514 /* glBegin(GL_QUADS);
515 for(int i=0; i<num_val; i++) {
516 float val = transfer_function(x, low, high);
517 glColor3f(val, val, val);
518 glVertex3f(x, 1.0, 0.0);
519 glVertex3f(x, 0.0, 0.0);
521 val = transfer_function(x + dx, low, high);
522 glColor3f(val, val, val);
523 glVertex3f(x + dx, 0.0, 0.0);
524 glVertex3f(x + dx, 1.0, 0.0);
525 x += dx;
526 }
527 glEnd(); */
529 glutSwapBuffers();
530 assert(glGetError() == GL_NO_ERROR);
531 }
533 static void reshape_xfer(int x, int y)
534 {
535 glViewport(0.0, 0.0, x, y);
536 glMatrixMode(GL_PROJECTION);
537 glLoadIdentity();
538 glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
539 }
541 static float *select_thres;
542 static float prev_select_thres;
543 static void mouse_xfer(int button, int state, int x, int y)
544 {
545 if(button == GLUT_LEFT_BUTTON) {
546 if(state == GLUT_DOWN) {
547 int width = glutGet(GLUT_WINDOW_WIDTH);
548 float xpos = (float)x / (float)width;
549 if(fabs(xpos - thres) <= fabs(xpos - thres2)) {
550 select_thres = &thres;
551 }
552 else {
553 select_thres = &thres2;
554 }
555 prev_select_thres = *select_thres;
556 }
557 else {
558 if(fabs(*select_thres - prev_select_thres) > 0.001) {
559 mesh->clear();
560 ui->sync_live();
561 }
562 select_thres = 0;
563 }
564 }
565 }
567 static void motion_xfer(int x, int y)
568 {
569 if(!select_thres)
570 return;
572 int width = glutGet(GLUT_WINDOW_WIDTH);
573 float xpos = (float)x / (float)width;
575 *select_thres = xpos;
576 thres_change(0);
578 glutPostRedisplay();
579 glutSetWindow(mainwin_id);
580 glutPostRedisplay();
581 }