volmetrics
view src/main.cc @ 35:df4a277adb82
port to macosx
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Fri, 06 Feb 2015 21:15:23 +0200 |
parents | eb87d4a12bd3 |
children | 1df14c5ffa71 |
line source
1 #include "opengl.h"
3 #include <math.h>
4 #include <stdio.h>
5 #include <assert.h>
7 #include <vector>
9 #include "mesh.h"
10 #include "sdr.h"
11 #include "volume.h"
13 static bool init(void);
14 static GLUI *create_ui(void);
15 static void display(void);
16 static void reshape(int x, int y);
17 static void keyboard(unsigned char key, int x, int y);
18 static void keyboard_up(unsigned char key, int x, int y);
19 static void mouse(int button, int state, int x, int y);
20 static void motion(int x, int y);
21 static bool init_xfer(void);
22 static void display_xfer(void);
23 static void reshape_xfer(int x, int y);
24 static void mouse_xfer(int button, int state, int x, int y);
25 static void motion_xfer(int x, int y);
26 static void volume_preview();
27 static void draw_iso();
28 static void draw_volume();
30 static int mainwin_id, xferwin_id;
31 //todo keyb esc
33 static int win_xsz, win_ysz;
34 static float cam_phi, cam_theta, cam_dist = 6;
35 static std::vector<bool> key_state(256);
37 static const char *vol_fname = "data/test1.vol";
38 static const char *vsdr_path = "data/shaders/transfer.v.glsl";
39 static const char *fsdr_path = "data/shaders/transfer.f.glsl";
40 static const char *vsdr_vol_path = "data/shaders/vol.v.glsl";
41 static const char *fsdr_vol_path = "data/shaders/vol.f.glsl";
43 static unsigned int sprog;
44 static unsigned int sprog_vol;
46 static Volume *vol;
47 static Mesh *mesh;
48 static float cur_z, thres = 0.5, thres2 = 1.0;
49 static float slice_z = 0.5;
51 static int use_orig_vol_res = 1;
52 static int vol_res[3]; // volume sampling resolution x/y/z
53 static float bound_scale = 1.42;
55 static GLUI *ui;
57 static int num_poly = 128;
59 int main(int argc, char **argv)
60 {
61 glutInitWindowSize(512, 512);
62 glutInit(&argc, argv);
63 if(argv[1])
64 vol_fname = argv[1];
66 if(!init()) {
67 fprintf(stderr, "Failed to initialize program.\n");
68 return 1;
69 }
71 init_xfer();
73 glutMainLoop();
74 return 0;
75 }
77 static bool init()
78 {
79 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
81 mainwin_id = glutCreateWindow("CT scan");
82 glutDisplayFunc(display);
83 glutReshapeFunc(reshape);
84 glutKeyboardFunc(keyboard);
85 glutKeyboardUpFunc(keyboard_up);
86 glutMouseFunc(mouse);
87 glutMotionFunc(motion);
89 glewInit();
91 glEnable(GL_DEPTH_TEST);
92 glEnable(GL_NORMALIZE);
94 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
96 //FIXME shaders setup
97 if(!(sprog = sdr_getprog(vsdr_path, fsdr_path)))
98 {
99 fprintf(stderr, "Failed to create shader program!\n");
100 return false;
101 }
103 if(!(sprog_vol = sdr_getprog(vsdr_vol_path, fsdr_vol_path)))
104 {
105 fprintf(stderr, "Failed to create shader program!\n");
106 return false;
107 }
109 vol = new Volume;
110 if(!vol->load(vol_fname)) {
111 fprintf(stderr, "Failed to load %s", vol_fname);
112 return false;
113 }
114 mesh = new Mesh;
115 cur_z = 0.5;
117 vol_res[0] = vol->get_slice(0)->get_width();
118 vol_res[1] = vol->get_slice(0)->get_height();
119 vol_res[2] = vol->get_slice_count();
121 num_poly = std::max(vol_res[0], std::max(vol_res[1], vol_res[2])) * bound_scale;
123 if(!(ui = create_ui())) {
124 return false;
125 }
127 return true;
128 }
130 static GLUI_Spinner *res_spin[3];
131 static void toggle_use_orig(int id)
132 {
133 for(int i=0; i<3; i++) {
134 if(use_orig_vol_res) {
135 res_spin[i]->disable();
136 } else {
137 res_spin[i]->enable();
138 }
139 }
140 }
141 static void thres_change(int id)
142 {
143 static float prev_thres = thres;
144 static float prev_thres2 = thres2;
146 if(prev_thres != thres || prev_thres2 != thres2) {
147 prev_thres = thres;
148 prev_thres2 = thres2;
150 mesh->clear();
152 glutSetWindow(xferwin_id);
153 glutPostRedisplay();
154 glutSetWindow(mainwin_id);
155 glutPostRedisplay();
156 }
157 }
159 static void res_change(int id)
160 {
161 static float prev_resx = vol_res[0];
162 static float prev_resy = vol_res[1];
163 static float prev_resz = vol_res[2];
165 if(prev_resx != vol_res[0] || prev_resy != vol_res[1] || prev_resz != vol_res[2]) {
166 prev_resx = vol_res[0];
167 prev_resy = vol_res[1];
168 prev_resz = vol_res[2];
169 mesh->clear();
170 }
171 }
172 static GLUI *create_ui()
173 {
174 GLUI *ui = GLUI_Master.create_glui("ui");
175 assert(ui);
177 ui->set_main_gfx_window(glutGetWindow());
179 GLUI_Panel *thres_panel = ui->add_panel("iso thresholds");
181 GLUI_Spinner *thres_spin = ui->add_spinner_to_panel(thres_panel, "T1", GLUI_SPINNER_FLOAT, &thres, 0, thres_change);
182 thres_spin->set_float_limits(0, 1);
184 GLUI_Spinner *thres2_spin = ui->add_spinner_to_panel(thres_panel, "T2", GLUI_SPINNER_FLOAT, &thres2, 0, thres_change);
185 thres2_spin->set_float_limits(0, 1);
187 #ifdef ISO
188 GLUI_Panel *res_panel = ui->add_panel("volume resolution");
190 ui->add_checkbox_to_panel(res_panel, "original resolution", &use_orig_vol_res, 0, toggle_use_orig);
192 static const char *res_spin_name[] = {"x", "y", "z"};
193 for(int i=0; i<3; i++) {
194 res_spin[i] = ui->add_spinner_to_panel(res_panel, res_spin_name[i], GLUI_SPINNER_INT, vol_res + i, 0, res_change);
195 res_spin[i]->set_int_limits(8, 256); // TODO limits as arguments or config
196 res_spin[i]->disable();
197 }
198 #endif
199 GLUI_Panel *preview_panel = ui->add_panel("volume preview");
201 GLUI_Spinner *preview_spin = ui->add_spinner_to_panel(preview_panel, "slice z", GLUI_SPINNER_FLOAT, &slice_z, 0);
202 preview_spin->set_float_limits(0, 1);
204 return ui;
205 }
207 static void volume_preview ()
208 {
209 float aspect = win_xsz / win_ysz;
211 glDisable(GL_DEPTH_TEST);
213 glUseProgram(sprog);
214 int tmin_loc = glGetUniformLocation(sprog, "tmin");
215 if(tmin_loc != -1)
216 glUniform1f(tmin_loc, std::min(thres, thres2));
217 int tmax_loc = glGetUniformLocation(sprog, "tmax");
218 if(tmax_loc != -1)
219 glUniform1f(tmax_loc, std::max(thres, thres2));
221 glMatrixMode(GL_MODELVIEW);
222 glPushMatrix();
223 glLoadIdentity();
225 glMatrixMode(GL_PROJECTION);
226 glPushMatrix();
227 glLoadIdentity();
228 glOrtho(0, aspect, 0, 1, -1, 1);
230 glBindTexture(GL_TEXTURE_3D, vol->get_texture());
231 glEnable(GL_TEXTURE_3D);
232 glBegin(GL_QUADS);
233 glColor3f(1.0, 1.0, 1.0);
234 glTexCoord3f(0, 0, slice_z); glVertex3f(0, 1, 0);
235 glTexCoord3f(0, 1, slice_z); glVertex3f(0, 0.75, 0);
236 glTexCoord3f(1, 1, slice_z); glVertex3f(0.25, 0.75, 0);
237 glTexCoord3f(1, 0, slice_z); glVertex3f(0.25, 1, 0);
238 glEnd();
239 glDisable(GL_TEXTURE_3D);
241 glUseProgram(0);
243 glLineWidth(2);
244 glBegin(GL_LINE_LOOP);
245 glColor3f(0.8, 0.3, 0.8);
246 glVertex3f(0, 1, 0);
247 glVertex3f(0, 0.75, 0);
248 glVertex3f(0.25, 0.75, 0);
249 glVertex3f(0.25, 1, 0);
250 glEnd();
252 glMatrixMode(GL_PROJECTION);
253 glPopMatrix();
255 glMatrixMode(GL_MODELVIEW);
256 glPopMatrix();
258 glEnable(GL_DEPTH_TEST);
259 }
261 static void draw_iso()
262 {
263 if(mesh->is_empty()) {
264 printf("recalculating isosurface ... ");
265 fflush(stdout);
266 vol->create_mesh(mesh, thres, thres2, vol_res[0], vol_res[1], vol_res[2]);
267 printf("done.\n");
268 }
269 mesh->draw();
270 }
272 static void draw_volume()
273 {
274 glUseProgram(sprog_vol);
276 int tmin_loc = glGetUniformLocation(sprog_vol, "tmin");
277 if(tmin_loc != -1)
278 glUniform1f(tmin_loc, std::min(thres, thres2));
279 int tmax_loc = glGetUniformLocation(sprog_vol, "tmax");
280 if(tmax_loc != -1)
281 glUniform1f(tmax_loc, std::max(thres, thres2));
283 int res_loc = glGetUniformLocation(sprog_vol, "res");
284 if(res_loc != -1)
285 glUniform3f(res_loc, (float)vol_res[0], (float)vol_res[1], (float)vol_res[2]);
287 glRotatef(-vol->get_volume_rotation(), 1, 0, 0);
289 glDisable(GL_DEPTH_TEST);
290 glBindTexture(GL_TEXTURE_3D, vol->get_texture());
291 glEnable(GL_TEXTURE_3D);
292 glEnable(GL_BLEND);
293 glBegin(GL_QUADS);
294 for(int i=0; i<num_poly; i++)
295 {
296 float tex_z = (float)i / (float)num_poly;
297 float z = 2 * tex_z - 1;
299 glTexCoord3f(0, 0, tex_z); glVertex3f(-bound_scale, -bound_scale, z * bound_scale);
300 glTexCoord3f(0, 1, tex_z); glVertex3f(-bound_scale, bound_scale, z * bound_scale);
301 glTexCoord3f(1, 1, tex_z); glVertex3f(bound_scale, bound_scale, z * bound_scale);
302 glTexCoord3f(1, 0, tex_z); glVertex3f(bound_scale, -bound_scale, z * bound_scale);
303 }
304 glEnd();
305 glDisable(GL_BLEND);
306 glDisable(GL_TEXTURE_3D);
307 glEnable(GL_DEPTH_TEST);
309 glUseProgram(0);
310 }
312 static void display(void)
313 {
314 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
316 glMatrixMode(GL_MODELVIEW);
317 glLoadIdentity();
318 glTranslatef(0, 0, -cam_dist);
319 glRotatef(cam_phi, 1, 0, 0);
320 glRotatef(cam_theta, 0, 1, 0);
322 //draw_slices();
323 //draw_iso();
324 draw_volume();
326 volume_preview();
327 glutSwapBuffers();
328 assert(glGetError() == GL_NO_ERROR);
329 }
331 static void reshape(int x, int y)
332 {
333 glViewport(0, 0, x, y);
334 glMatrixMode(GL_PROJECTION);
335 glLoadIdentity();
336 gluPerspective(45, (float)x / (float)y, 0.5, 500);
338 win_xsz = x;
339 win_ysz = y;
340 }
342 static void keyboard(unsigned char key, int x, int y)
343 {
344 key_state[(int)key] = true;
346 switch(key) {
347 case 27:
348 exit(0);
349 case 'w':
350 {
351 static bool wire;
352 wire = !wire;
353 glPolygonMode(GL_FRONT_AND_BACK, wire ? GL_LINE : GL_FILL);
354 }
355 break;
356 default:
357 break;
358 }
359 }
361 static void keyboard_up(unsigned char key, int x, int y)
362 {
363 key_state[(int) key] = false;
364 }
366 static int prev_x, prev_y;
367 static bool bn_state[32];
368 static float prev_thres, prev_thres2;
370 static void mouse(int bn, int state, int x, int y)
371 {
372 prev_x = x;
373 prev_y = y;
375 bn_state[bn - GLUT_LEFT_BUTTON] = (state == GLUT_DOWN);
377 if(state == GLUT_DOWN) {
378 prev_thres = thres;
379 prev_thres2 = thres2;
380 }
381 else {
382 if(thres != prev_thres || thres2 != prev_thres2) {
383 mesh->clear();
384 }
385 }
386 }
388 static void motion(int x, int y)
389 {
390 int dx = x - prev_x;
391 int dy = y - prev_y;
393 if(!dx && !dy)
394 return;
396 prev_x = x;
397 prev_y = y;
399 if(key_state[(int)'t']) {
400 thres = (float)x / (float)win_xsz;
401 printf("threshold: %f\n", thres);
403 glutPostRedisplay();
404 return;
405 }
407 // camera
408 if(bn_state[0]) {
409 if(key_state[(int)'z']) {
410 //zoom the camera
412 cam_dist += dy * 0.1;
414 if(cam_dist < 0)
415 cam_dist = 0;
416 }
417 else {
418 //rotate the camera
420 cam_phi += dy * 0.5;
421 cam_theta += dx * 0.5;
423 if(cam_phi > 90)
424 cam_phi = 90;
425 if(cam_phi < -90)
426 cam_phi = -90;
427 }
429 glutPostRedisplay();
430 }
431 }
433 static bool init_xfer(void)
434 {
435 glutSetWindow(mainwin_id);
436 int x = glutGet(GLUT_WINDOW_X);
437 int y = glutGet(GLUT_WINDOW_Y) + glutGet(GLUT_WINDOW_HEIGHT);
439 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
440 glutInitWindowSize(512, 100);
441 glutInitWindowPosition(x, y);
443 xferwin_id = glutCreateWindow("Transfer Function");
444 glutDisplayFunc(display_xfer);
445 glutReshapeFunc(reshape_xfer);
446 glutMouseFunc(mouse_xfer);
447 glutMotionFunc(motion_xfer);
449 return true;
450 }
452 static void display_xfer(void)
453 {
454 glClear(GL_COLOR_BUFFER_BIT);
456 int num_val = glutGet(GLUT_WINDOW_WIDTH) / 4.0;
457 float x = 0;
458 float dx = 1.0 / num_val;
459 float low, high;
460 if(thres < thres2) {
461 low = thres;
462 high = thres2;
463 }
464 else {
465 low = thres2;
466 high = thres;
467 }
470 //drawing the histogram
471 float hmax = 1 / (float)vol->max_histogram_value;
473 glBegin(GL_QUADS);
474 for (int i=0; i<HIST_SIZE; i++) {
475 float x0 = (float)i / HIST_SIZE;
476 float x1 = (float)(i + 1) / HIST_SIZE;
478 glColor3f(0.3, 0.3, 0.3);
479 glVertex3f(x0, 0, 0);
480 glVertex3f(x1, 0, 0);
481 glVertex3f(x1, hmax * vol->histogram[i + 1], 0);
482 glVertex3f(x0, hmax * vol->histogram[i], 0);
483 }
484 glEnd();
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) && fabs(xpos - thres) < 0.1) {
549 select_thres = &thres;
550 }
551 else if (fabs(xpos - thres2) < 0.1) {
552 select_thres = &thres2;
553 }
554 else
555 select_thres = 0;
557 if (select_thres)
558 prev_select_thres = *select_thres;
559 }
560 else {
561 if(!prev_select_thres || !select_thres) {
562 select_thres = 0;
563 return;
564 }
565 if(fabs(*select_thres - prev_select_thres) > 0.001) {
566 mesh->clear();
567 ui->sync_live();
568 }
569 select_thres = 0;
570 }
571 }
572 }
574 static void motion_xfer(int x, int y)
575 {
576 if(!select_thres)
577 return;
579 int width = glutGet(GLUT_WINDOW_WIDTH);
580 float xpos = (float)x / (float)width;
582 *select_thres = xpos;
583 thres_change(0);
585 glutPostRedisplay();
586 glutSetWindow(mainwin_id);
587 glutPostRedisplay();
588 }