volmetrics

view src/main.cc @ 23:930c063ae346

preview works - transfer function on sdr
author Eleni Maria Stea <elene.mst@gmail.com>
date Thu, 24 Apr 2014 21:32:28 +0300
parents 4e120dcd55ec
children 4b6c952a83bd
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 ();
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";
41 static Volume *vol;
42 static Mesh *mesh;
43 static float cur_z, thres = 0.5, thres2 = 1.0;
44 static float slice_z;
46 static int use_orig_vol_res = 1;
47 static int vol_res[3]; // volume sampling resolution x/y/z
49 static GLUI *ui;
51 static unsigned int sprog;
53 int main(int argc, char **argv)
54 {
55 glutInit(&argc, argv);
56 if(argv[1])
57 vol_fname = argv[1];
59 if(!init()) {
60 fprintf(stderr, "Failed to initialize program.\n");
61 return 1;
62 }
64 init_xfer();
66 glutMainLoop();
67 return 0;
68 }
70 static bool init()
71 {
72 glutInitWindowSize(512, 512);
73 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
75 mainwin_id = glutCreateWindow("CT scan");
76 glutDisplayFunc(display);
77 glutReshapeFunc(reshape);
78 glutKeyboardFunc(keyboard);
79 glutKeyboardUpFunc(keyboard_up);
80 glutMouseFunc(mouse);
81 glutMotionFunc(motion);
83 glewInit();
85 glEnable(GL_DEPTH_TEST);
86 glEnable(GL_NORMALIZE);
88 glEnable(GL_LIGHTING);
89 glEnable(GL_LIGHT0);
90 glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, 1);
92 //FIXME shaders setup
93 if(!(sprog = sdr_getprog(vsdr_path, fsdr_path)))
94 {
95 fprintf(stderr, "Failed to create shader program!\n");
96 return false;
97 }
99 vol = new Volume;
100 if(!vol->load(vol_fname)) {
101 fprintf(stderr, "Failed to load %s", vol_fname);
102 return false;
103 }
104 mesh = new Mesh;
105 cur_z = 0.5;
107 vol_res[0] = vol->get_slice(0)->get_width();
108 vol_res[1] = vol->get_slice(0)->get_height();
109 vol_res[2] = vol->get_slice_count();
111 if(!(ui = create_ui())) {
112 return false;
113 }
115 return true;
116 }
118 static GLUI_Spinner *res_spin[3];
119 static void toggle_use_orig(int id)
120 {
121 for(int i=0; i<3; i++) {
122 if(use_orig_vol_res) {
123 res_spin[i]->disable();
124 } else {
125 res_spin[i]->enable();
126 }
127 }
128 }
129 static void thres_change(int id)
130 {
131 static float prev_thres = thres;
132 static float prev_thres2 = thres2;
134 if(prev_thres != thres || prev_thres2 != thres2) {
135 prev_thres = thres;
136 prev_thres2 = thres2;
138 mesh->clear();
140 glutSetWindow(xferwin_id);
141 glutPostRedisplay();
142 glutSetWindow(mainwin_id);
143 glutPostRedisplay();
144 }
145 }
147 static void res_change(int id)
148 {
149 static float prev_resx = vol_res[0];
150 static float prev_resy = vol_res[1];
151 static float prev_resz = vol_res[2];
153 if(prev_resx != vol_res[0] || prev_resy != vol_res[1] || prev_resz != vol_res[2]) {
154 prev_resx = vol_res[0];
155 prev_resy = vol_res[1];
156 prev_resz = vol_res[2];
157 mesh->clear();
158 }
159 }
160 static GLUI *create_ui()
161 {
162 GLUI *ui = GLUI_Master.create_glui("ui");
163 assert(ui);
165 ui->set_main_gfx_window(glutGetWindow());
167 GLUI_Panel *thres_panel = ui->add_panel("iso thresholds");
169 GLUI_Spinner *thres_spin = ui->add_spinner_to_panel(thres_panel, "T1", GLUI_SPINNER_FLOAT, &thres, 0, thres_change);
170 thres_spin->set_float_limits(0, 1);
172 GLUI_Spinner *thres2_spin = ui->add_spinner_to_panel(thres_panel, "T2", GLUI_SPINNER_FLOAT, &thres2, 0, thres_change);
173 thres2_spin->set_float_limits(0, 1);
175 GLUI_Panel *res_panel = ui->add_panel("volume resolution");
177 ui->add_checkbox_to_panel(res_panel, "original resolution", &use_orig_vol_res, 0, toggle_use_orig);
179 static const char *res_spin_name[] = {"x", "y", "z"};
180 for(int i=0; i<3; i++) {
181 res_spin[i] = ui->add_spinner_to_panel(res_panel, res_spin_name[i], GLUI_SPINNER_INT, vol_res + i, 0, res_change);
182 res_spin[i]->set_int_limits(8, 256); // TODO limits as arguments or config
183 res_spin[i]->disable();
184 }
186 GLUI_Panel *preview_panel = ui->add_panel("volume preview");
188 GLUI_Spinner *preview_spin = ui->add_spinner_to_panel(preview_panel, "slice z", GLUI_SPINNER_FLOAT, &slice_z, 0);
189 preview_spin->set_float_limits(0, 1);
191 return ui;
192 }
194 static void volume_preview ()
195 {
196 float aspect = win_xsz / win_ysz;
198 glDisable(GL_LIGHTING);
199 glDisable(GL_DEPTH_TEST);
201 glUseProgram(sprog);
202 int tmin_loc = glGetUniformLocation(sprog, "tmin");
203 if(tmin_loc != -1)
204 glUniform1f(tmin_loc, std::min(thres, thres2));
205 int tmax_loc = glGetUniformLocation(sprog, "tmax");
206 if(tmax_loc != -1)
207 glUniform1f(tmax_loc, std::max(thres, thres2));
209 glMatrixMode(GL_MODELVIEW);
210 glPushMatrix();
211 glLoadIdentity();
213 glMatrixMode(GL_PROJECTION);
214 glPushMatrix();
215 glLoadIdentity();
216 glOrtho(-aspect, aspect, -1.0, 1.0, -1.0, 1.0);
218 glBindTexture(GL_TEXTURE_3D, vol->get_texture());
219 glEnable(GL_TEXTURE_3D);
220 glBegin(GL_QUADS);
221 glColor3f(1.0, 1.0, 1.0);
222 glTexCoord3f(0, 0, slice_z); glVertex3f(-1.0, 1.0, 0.0);
223 glTexCoord3f(0, 1, slice_z); glVertex3f(-1.0, 0.5, 0.0);
224 glTexCoord3f(1, 1, slice_z); glVertex3f(-0.5, 0.5, 0.0);
225 glTexCoord3f(1, 0, slice_z); glVertex3f(-0.5, 1.0, 0.0);
226 glEnd();
227 glDisable(GL_TEXTURE_3D);
229 glLineWidth(3);
230 glBegin(GL_LINE_LOOP);
231 glColor3f(0.4, 0.8, 0.4);
232 glVertex3f(-1.0, 1.0, 0.0);
233 glVertex3f(-1.0, 0.5, 0.0);
234 glVertex3f(-0.5, 0.5, 0.0);
235 glVertex3f(-0.5, 1.0, 0.0);
236 glEnd();
238 glMatrixMode(GL_PROJECTION);
239 glPopMatrix();
241 glMatrixMode(GL_MODELVIEW);
242 glPopMatrix();
244 glUseProgram(0);
246 glEnable(GL_DEPTH_TEST);
247 glEnable(GL_LIGHTING);
248 }
250 static void display(void)
251 {
252 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
254 glMatrixMode(GL_MODELVIEW);
255 glLoadIdentity();
256 glTranslatef(0, 0, -cam_dist);
257 glRotatef(cam_phi, 1, 0, 0);
258 glRotatef(cam_theta, 0, 1, 0);
260 /*
261 glBindTexture(GL_TEXTURE_3D, vol->get_texture());
262 glEnable(GL_TEXTURE_3D);
263 glBegin(GL_QUADS);
264 glTexCoord3f(0, 0, cur_z); glVertex3f(-1, -1, 0);
265 glTexCoord3f(0, 1, cur_z); glVertex3f(-1, 1, 0);
266 glTexCoord3f(1, 1, cur_z); glVertex3f(1, 1, 0);
267 glTexCoord3f(1, 0, cur_z); glVertex3f(1, -1, 0);
268 glEnd();
269 glDisable(GL_TEXTURE_3D);
270 */
272 /* if(mesh->is_empty()) {
273 printf("recalculating isosurface ... ");
274 fflush(stdout);
275 vol->create_mesh(mesh, thres, thres2, vol_res[0], vol_res[1], vol_res[2]);
276 printf("done.\n");
277 }
278 mesh->draw();
279 */
281 volume_preview();
282 glutSwapBuffers();
283 assert(glGetError() == GL_NO_ERROR);
284 }
286 static void reshape(int x, int y)
287 {
288 glViewport(0, 0, x, y);
289 glMatrixMode(GL_PROJECTION);
290 glLoadIdentity();
291 gluPerspective(45, (float)x / (float)y, 0.5, 500);
293 if(x != win_xsz || y != win_ysz) {
294 win_xsz = x;
295 win_ysz = y;
296 }
297 }
299 static void keyboard(unsigned char key, int x, int y)
300 {
301 key_state[(int)key] = true;
303 switch(key) {
304 case 27:
305 exit(0);
306 case 'w':
307 {
308 static bool wire;
309 wire = !wire;
310 glPolygonMode(GL_FRONT_AND_BACK, wire ? GL_LINE : GL_FILL);
311 }
312 break;
313 default:
314 break;
315 }
316 }
318 static void keyboard_up(unsigned char key, int x, int y)
319 {
320 key_state[(int) key] = false;
321 }
323 static int prev_x, prev_y;
324 static bool bn_state[32];
325 static float prev_thres, prev_thres2;
327 static void mouse(int bn, int state, int x, int y)
328 {
329 prev_x = x;
330 prev_y = y;
332 bn_state[bn - GLUT_LEFT_BUTTON] = (state == GLUT_DOWN);
334 if(state == GLUT_DOWN) {
335 prev_thres = thres;
336 prev_thres2 = thres2;
337 }
338 else {
339 if(thres != prev_thres || thres2 != prev_thres2) {
340 mesh->clear();
341 }
342 }
343 }
345 static void motion(int x, int y)
346 {
347 int dx = x - prev_x;
348 int dy = y - prev_y;
350 if(!dx && !dy)
351 return;
353 prev_x = x;
354 prev_y = y;
356 if(key_state[(int)'t']) {
357 thres = (float)x / (float)win_xsz;
358 printf("threshold: %f\n", thres);
360 glutPostRedisplay();
361 return;
362 }
364 // camera
365 if(bn_state[0]) {
366 if(key_state[(int)'z']) {
367 //zoom the camera
369 cam_dist += dy * 0.1;
371 if(cam_dist < 0)
372 cam_dist = 0;
373 }
374 else {
375 //rotate the camera
377 cam_phi += dy * 0.5;
378 cam_theta += dx * 0.5;
380 if(cam_phi > 90)
381 cam_phi = 90;
382 if(cam_phi < -90)
383 cam_phi = -90;
384 }
386 glutPostRedisplay();
387 }
388 }
390 static bool init_xfer(void)
391 {
392 glutSetWindow(mainwin_id);
393 int x = glutGet(GLUT_WINDOW_X);
394 int y = glutGet(GLUT_WINDOW_Y) + glutGet(GLUT_WINDOW_HEIGHT);
396 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
397 glutInitWindowSize(512, 100);
398 glutInitWindowPosition(x, y);
400 xferwin_id = glutCreateWindow("Transfer Function");
401 glutDisplayFunc(display_xfer);
402 glutReshapeFunc(reshape_xfer);
403 glutMouseFunc(mouse_xfer);
404 glutMotionFunc(motion_xfer);
406 return true;
407 }
409 static void display_xfer(void)
410 {
411 glClear(GL_COLOR_BUFFER_BIT);
413 int num_val = glutGet(GLUT_WINDOW_WIDTH) / 4.0;
414 float x = 0;
415 float dx = 1.0 / num_val;
416 float low, high;
417 if(thres < thres2) {
418 low = thres;
419 high = thres2;
420 }
421 else {
422 low = thres2;
423 high = thres;
424 }
426 //drawing the transfer function curve
428 glLineWidth(3);
429 glBegin(GL_LINE_STRIP);
430 glColor3f(0.8, 0.3, 0.8);
431 for(int i=0; i<num_val; i++) {
432 float val = transfer_function(x, low, high);
433 glVertex2f(x, val);
434 x += dx;
435 }
436 glEnd();
438 //threshold bars
440 glLineWidth(2);
441 glBegin(GL_LINES);
442 glColor3f(0.4, 0.4, 0.8);
443 glVertex2f(low, 0);
444 glVertex2f(low, 1);
445 glColor3f(0.4, 0.8, 0.4);
446 glVertex2f(high, 0);
447 glVertex2f(high, 1);
448 glEnd();
450 /*
451 * gradient
452 */
453 /* glBegin(GL_QUADS);
454 for(int i=0; i<num_val; i++) {
455 float val = transfer_function(x, low, high);
456 glColor3f(val, val, val);
457 glVertex3f(x, 1.0, 0.0);
458 glVertex3f(x, 0.0, 0.0);
460 val = transfer_function(x + dx, low, high);
461 glColor3f(val, val, val);
462 glVertex3f(x + dx, 0.0, 0.0);
463 glVertex3f(x + dx, 1.0, 0.0);
464 x += dx;
465 }
466 glEnd(); */
468 glutSwapBuffers();
469 assert(glGetError() == GL_NO_ERROR);
470 }
472 static void reshape_xfer(int x, int y)
473 {
474 glViewport(0.0, 0.0, x, y);
475 glMatrixMode(GL_PROJECTION);
476 glLoadIdentity();
477 glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
478 }
480 static float *select_thres;
481 static float prev_select_thres;
482 static void mouse_xfer(int button, int state, int x, int y)
483 {
484 if(button == GLUT_LEFT_BUTTON) {
485 if(state == GLUT_DOWN) {
486 int width = glutGet(GLUT_WINDOW_WIDTH);
487 float xpos = (float)x / (float)width;
488 if(fabs(xpos - thres) <= fabs(xpos - thres2)) {
489 select_thres = &thres;
490 }
491 else {
492 select_thres = &thres2;
493 }
494 prev_select_thres = *select_thres;
495 }
496 else {
497 if(fabs(*select_thres - prev_select_thres) > 0.001) {
498 mesh->clear();
499 ui->sync_live();
500 }
501 select_thres = 0;
502 }
503 }
504 }
506 static void motion_xfer(int x, int y)
507 {
508 if(!select_thres)
509 return;
511 int width = glutGet(GLUT_WINDOW_WIDTH);
512 float xpos = (float)x / (float)width;
514 *select_thres = xpos;
515 thres_change(0);
517 glutPostRedisplay();
518 glutSetWindow(mainwin_id);
519 glutPostRedisplay();
520 }