volmetrics
view src/main.cc @ 25:4b6c952a83bd
works
todo:
1- ta quads na nai to res tou volume * 2
2- conservative quad
3- to aspect ratio tou preview
author | Eleni Maria Stea <elene.mst@gmail.com> |
---|---|
date | Sun, 27 Apr 2014 18:13:44 +0300 |
parents | 930c063ae346 |
children | 5ee081af59b8 |
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
57 static GLUI *ui;
59 static int num_poly = 128;
61 int main(int argc, char **argv)
62 {
63 glutInit(&argc, argv);
64 if(argv[1])
65 vol_fname = argv[1];
67 if(!init()) {
68 fprintf(stderr, "Failed to initialize program.\n");
69 return 1;
70 }
72 init_xfer();
74 glutMainLoop();
75 return 0;
76 }
78 static bool init()
79 {
80 glutInitWindowSize(512, 512);
81 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
83 mainwin_id = glutCreateWindow("CT scan");
84 glutDisplayFunc(display);
85 glutReshapeFunc(reshape);
86 glutKeyboardFunc(keyboard);
87 glutKeyboardUpFunc(keyboard_up);
88 glutMouseFunc(mouse);
89 glutMotionFunc(motion);
91 glewInit();
93 glEnable(GL_DEPTH_TEST);
94 glEnable(GL_NORMALIZE);
96 glEnable(GL_LIGHTING);
97 glEnable(GL_LIGHT0);
98 glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, 1);
100 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
102 //FIXME shaders setup
103 if(!(sprog = sdr_getprog(vsdr_path, fsdr_path)))
104 {
105 fprintf(stderr, "Failed to create shader program!\n");
106 return false;
107 }
109 if(!(sprog_vol = sdr_getprog(vsdr_vol_path, fsdr_vol_path)))
110 {
111 fprintf(stderr, "Failed to create shader program!\n");
112 return false;
113 }
115 vol = new Volume;
116 if(!vol->load(vol_fname)) {
117 fprintf(stderr, "Failed to load %s", vol_fname);
118 return false;
119 }
120 mesh = new Mesh;
121 cur_z = 0.5;
123 vol_res[0] = vol->get_slice(0)->get_width();
124 vol_res[1] = vol->get_slice(0)->get_height();
125 vol_res[2] = vol->get_slice_count();
127 if(!(ui = create_ui())) {
128 return false;
129 }
131 return true;
132 }
134 static GLUI_Spinner *res_spin[3];
135 static void toggle_use_orig(int id)
136 {
137 for(int i=0; i<3; i++) {
138 if(use_orig_vol_res) {
139 res_spin[i]->disable();
140 } else {
141 res_spin[i]->enable();
142 }
143 }
144 }
145 static void thres_change(int id)
146 {
147 static float prev_thres = thres;
148 static float prev_thres2 = thres2;
150 if(prev_thres != thres || prev_thres2 != thres2) {
151 prev_thres = thres;
152 prev_thres2 = thres2;
154 mesh->clear();
156 glutSetWindow(xferwin_id);
157 glutPostRedisplay();
158 glutSetWindow(mainwin_id);
159 glutPostRedisplay();
160 }
161 }
163 static void res_change(int id)
164 {
165 static float prev_resx = vol_res[0];
166 static float prev_resy = vol_res[1];
167 static float prev_resz = vol_res[2];
169 if(prev_resx != vol_res[0] || prev_resy != vol_res[1] || prev_resz != vol_res[2]) {
170 prev_resx = vol_res[0];
171 prev_resy = vol_res[1];
172 prev_resz = vol_res[2];
173 mesh->clear();
174 }
175 }
176 static GLUI *create_ui()
177 {
178 GLUI *ui = GLUI_Master.create_glui("ui");
179 assert(ui);
181 ui->set_main_gfx_window(glutGetWindow());
183 GLUI_Panel *thres_panel = ui->add_panel("iso thresholds");
185 GLUI_Spinner *thres_spin = ui->add_spinner_to_panel(thres_panel, "T1", GLUI_SPINNER_FLOAT, &thres, 0, thres_change);
186 thres_spin->set_float_limits(0, 1);
188 GLUI_Spinner *thres2_spin = ui->add_spinner_to_panel(thres_panel, "T2", GLUI_SPINNER_FLOAT, &thres2, 0, thres_change);
189 thres2_spin->set_float_limits(0, 1);
191 GLUI_Panel *res_panel = ui->add_panel("volume resolution");
193 ui->add_checkbox_to_panel(res_panel, "original resolution", &use_orig_vol_res, 0, toggle_use_orig);
195 static const char *res_spin_name[] = {"x", "y", "z"};
196 for(int i=0; i<3; i++) {
197 res_spin[i] = ui->add_spinner_to_panel(res_panel, res_spin_name[i], GLUI_SPINNER_INT, vol_res + i, 0, res_change);
198 res_spin[i]->set_int_limits(8, 256); // TODO limits as arguments or config
199 res_spin[i]->disable();
200 }
202 GLUI_Panel *preview_panel = ui->add_panel("volume preview");
204 GLUI_Spinner *preview_spin = ui->add_spinner_to_panel(preview_panel, "slice z", GLUI_SPINNER_FLOAT, &slice_z, 0);
205 preview_spin->set_float_limits(0, 1);
207 return ui;
208 }
210 static void volume_preview ()
211 {
212 float aspect = win_xsz / win_ysz;
214 glDisable(GL_DEPTH_TEST);
216 glUseProgram(sprog);
217 int tmin_loc = glGetUniformLocation(sprog, "tmin");
218 if(tmin_loc != -1)
219 glUniform1f(tmin_loc, std::min(thres, thres2));
220 int tmax_loc = glGetUniformLocation(sprog, "tmax");
221 if(tmax_loc != -1)
222 glUniform1f(tmax_loc, std::max(thres, thres2));
224 glMatrixMode(GL_MODELVIEW);
225 glPushMatrix();
226 glLoadIdentity();
228 glMatrixMode(GL_PROJECTION);
229 glPushMatrix();
230 glLoadIdentity();
231 glOrtho(0, aspect, 0, 1, -1, 1);
233 glBindTexture(GL_TEXTURE_3D, vol->get_texture());
234 glEnable(GL_TEXTURE_3D);
235 glBegin(GL_QUADS);
236 glColor3f(1.0, 1.0, 1.0);
237 glTexCoord3f(0, 0, slice_z); glVertex3f(0, 1, 0);
238 glTexCoord3f(0, 1, slice_z); glVertex3f(0, 0.75, 0);
239 glTexCoord3f(1, 1, slice_z); glVertex3f(0.25, 0.75, 0);
240 glTexCoord3f(1, 0, slice_z); glVertex3f(0.25, 1, 0);
241 glEnd();
242 glDisable(GL_TEXTURE_3D);
244 glLineWidth(3);
245 glBegin(GL_LINE_LOOP);
246 glColor3f(0.4, 0.8, 0.4);
247 glVertex3f(-1.0, 1.0, 0.0);
248 glVertex3f(-1.0, 0.5, 0.0);
249 glVertex3f(-0.5, 0.5, 0.0);
250 glVertex3f(-0.5, 1.0, 0.0);
251 glEnd();
253 glMatrixMode(GL_PROJECTION);
254 glPopMatrix();
256 glMatrixMode(GL_MODELVIEW);
257 glPopMatrix();
259 glUseProgram(0);
261 glEnable(GL_DEPTH_TEST);
262 }
264 static void draw_slices()
265 {
266 glBindTexture(GL_TEXTURE_3D, vol->get_texture());
267 glEnable(GL_TEXTURE_3D);
268 glBegin(GL_QUADS);
269 glTexCoord3f(0, 0, cur_z); glVertex3f(-1, -1, 0);
270 glTexCoord3f(0, 1, cur_z); glVertex3f(-1, 1, 0);
271 glTexCoord3f(1, 1, cur_z); glVertex3f(1, 1, 0);
272 glTexCoord3f(1, 0, cur_z); glVertex3f(1, -1, 0);
273 glEnd();
274 glDisable(GL_TEXTURE_3D);
275 }
277 static void draw_iso()
278 {
279 if(mesh->is_empty()) {
280 printf("recalculating isosurface ... ");
281 fflush(stdout);
282 vol->create_mesh(mesh, thres, thres2, vol_res[0], vol_res[1], vol_res[2]);
283 printf("done.\n");
284 }
285 mesh->draw();
286 }
288 static void draw_volume()
289 {
290 glUseProgram(sprog_vol);
292 int tmin_loc = glGetUniformLocation(sprog_vol, "tmin");
293 if(tmin_loc != -1)
294 glUniform1f(tmin_loc, std::min(thres, thres2));
295 int tmax_loc = glGetUniformLocation(sprog_vol, "tmax");
296 if(tmax_loc != -1)
297 glUniform1f(tmax_loc, std::max(thres, thres2));
299 const Image *img = vol->get_slice(0);
300 int res_x = img->get_width();
301 int res_y = img->get_height();
302 int res_z = vol->get_slice_count();
303 int res_loc = glGetUniformLocation(sprog_vol, "res");
304 if(res_loc != -1)
305 glUniform3f(res_loc, (float)res_x, (float)res_y, (float)res_z);
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(-2, -2, z * 2);
318 glTexCoord3f(0, 1, tex_z); glVertex3f(-2, 2, z * 2);
319 glTexCoord3f(1, 1, tex_z); glVertex3f(2, 2, z * 2);
320 glTexCoord3f(1, 0, tex_z); glVertex3f(2, -2, z * 2);
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 }