volmetrics

view src/main.cc @ 22:4e120dcd55ec

added shaders that just draw
author Eleni Maria Stea <elene.mst@gmail.com>
date Thu, 24 Apr 2014 20:47:48 +0300
parents c22866ecb7ae
children 930c063ae346
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 }
98 glUseProgram(sprog);
99 float t1 = glGetUniformLocation(sprog, "thres1");
100 if(t1 != -1)
101 glUniform1f(t1, 0);
102 float t2 = glGetUniformLocation(sprog, "thres2");
103 glUniform1f(t2, 1);
104 glUseProgram(0);
106 vol = new Volume;
107 if(!vol->load(vol_fname)) {
108 fprintf(stderr, "Failed to load %s", vol_fname);
109 return false;
110 }
111 mesh = new Mesh;
112 cur_z = 0.5;
114 vol_res[0] = vol->get_slice(0)->get_width();
115 vol_res[1] = vol->get_slice(0)->get_height();
116 vol_res[2] = vol->get_slice_count();
118 if(!(ui = create_ui())) {
119 return false;
120 }
122 return true;
123 }
125 static GLUI_Spinner *res_spin[3];
126 static void toggle_use_orig(int id)
127 {
128 for(int i=0; i<3; i++) {
129 if(use_orig_vol_res) {
130 res_spin[i]->disable();
131 } else {
132 res_spin[i]->enable();
133 }
134 }
135 }
136 static void thres_change(int id)
137 {
138 static float prev_thres = thres;
139 static float prev_thres2 = thres2;
141 if(prev_thres != thres || prev_thres2 != thres2) {
142 prev_thres = thres;
143 prev_thres2 = thres2;
144 mesh->clear();
145 }
147 glutSetWindow(xferwin_id);
148 glutPostRedisplay();
149 glutSetWindow(mainwin_id);
150 glutPostRedisplay();
151 }
152 static void res_change(int id)
153 {
154 static float prev_resx = vol_res[0];
155 static float prev_resy = vol_res[1];
156 static float prev_resz = vol_res[2];
158 if(prev_resx != vol_res[0] || prev_resy != vol_res[1] || prev_resz != vol_res[2]) {
159 prev_resx = vol_res[0];
160 prev_resy = vol_res[1];
161 prev_resz = vol_res[2];
162 mesh->clear();
163 }
164 }
165 static GLUI *create_ui()
166 {
167 GLUI *ui = GLUI_Master.create_glui("ui");
168 assert(ui);
170 ui->set_main_gfx_window(glutGetWindow());
172 GLUI_Panel *thres_panel = ui->add_panel("iso thresholds");
174 GLUI_Spinner *thres_spin = ui->add_spinner_to_panel(thres_panel, "T1", GLUI_SPINNER_FLOAT, &thres, 0, thres_change);
175 thres_spin->set_float_limits(0, 1);
177 GLUI_Spinner *thres2_spin = ui->add_spinner_to_panel(thres_panel, "T2", GLUI_SPINNER_FLOAT, &thres2, 0, thres_change);
178 thres2_spin->set_float_limits(0, 1);
180 GLUI_Panel *res_panel = ui->add_panel("volume resolution");
182 ui->add_checkbox_to_panel(res_panel, "original resolution", &use_orig_vol_res, 0, toggle_use_orig);
184 static const char *res_spin_name[] = {"x", "y", "z"};
185 for(int i=0; i<3; i++) {
186 res_spin[i] = ui->add_spinner_to_panel(res_panel, res_spin_name[i], GLUI_SPINNER_INT, vol_res + i, 0, res_change);
187 res_spin[i]->set_int_limits(8, 256); // TODO limits as arguments or config
188 res_spin[i]->disable();
189 }
191 GLUI_Panel *preview_panel = ui->add_panel("volume preview");
193 GLUI_Spinner *preview_spin = ui->add_spinner_to_panel(preview_panel, "slice z", GLUI_SPINNER_FLOAT, &slice_z, 0);
194 preview_spin->set_float_limits(0, 1);
196 return ui;
197 }
199 static void volume_preview ()
200 {
201 float aspect = win_xsz / win_ysz;
203 glDisable(GL_LIGHTING);
204 glDisable(GL_DEPTH_TEST);
206 glUseProgram(sprog);
208 glMatrixMode(GL_MODELVIEW);
209 glPushMatrix();
210 glLoadIdentity();
212 glMatrixMode(GL_PROJECTION);
213 glPushMatrix();
214 glLoadIdentity();
215 glOrtho(-aspect, aspect, -1.0, 1.0, -1.0, 1.0);
217 glBindTexture(GL_TEXTURE_3D, vol->get_texture());
218 glEnable(GL_TEXTURE_3D);
219 glBegin(GL_QUADS);
220 glColor3f(1.0, 1.0, 1.0);
221 glTexCoord3f(0, 0, slice_z); glVertex3f(-1.0, 1.0, 0.0);
222 glTexCoord3f(0, 1, slice_z); glVertex3f(-1.0, 0.5, 0.0);
223 glTexCoord3f(1, 1, slice_z); glVertex3f(-0.5, 0.5, 0.0);
224 glTexCoord3f(1, 0, slice_z); glVertex3f(-0.5, 1.0, 0.0);
225 glEnd();
226 glDisable(GL_TEXTURE_3D);
228 glLineWidth(3);
229 glBegin(GL_LINE_LOOP);
230 glColor3f(0.4, 0.8, 0.4);
231 glVertex3f(-1.0, 1.0, 0.0);
232 glVertex3f(-1.0, 0.5, 0.0);
233 glVertex3f(-0.5, 0.5, 0.0);
234 glVertex3f(-0.5, 1.0, 0.0);
235 glEnd();
237 glMatrixMode(GL_PROJECTION);
238 glPopMatrix();
240 glMatrixMode(GL_MODELVIEW);
241 glPopMatrix();
243 glUseProgram(0);
245 glEnable(GL_DEPTH_TEST);
246 glEnable(GL_LIGHTING);
247 }
249 static void display(void)
250 {
251 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
253 glMatrixMode(GL_MODELVIEW);
254 glLoadIdentity();
255 glTranslatef(0, 0, -cam_dist);
256 glRotatef(cam_phi, 1, 0, 0);
257 glRotatef(cam_theta, 0, 1, 0);
259 /*
260 glBindTexture(GL_TEXTURE_3D, vol->get_texture());
261 glEnable(GL_TEXTURE_3D);
262 glBegin(GL_QUADS);
263 glTexCoord3f(0, 0, cur_z); glVertex3f(-1, -1, 0);
264 glTexCoord3f(0, 1, cur_z); glVertex3f(-1, 1, 0);
265 glTexCoord3f(1, 1, cur_z); glVertex3f(1, 1, 0);
266 glTexCoord3f(1, 0, cur_z); glVertex3f(1, -1, 0);
267 glEnd();
268 glDisable(GL_TEXTURE_3D);
269 */
271 if(mesh->is_empty()) {
272 printf("recalculating isosurface ... ");
273 fflush(stdout);
274 vol->create_mesh(mesh, thres, thres2, vol_res[0], vol_res[1], vol_res[2]);
275 printf("done.\n");
276 }
277 mesh->draw();
279 volume_preview();
280 glutSwapBuffers();
281 assert(glGetError() == GL_NO_ERROR);
282 }
284 static void reshape(int x, int y)
285 {
286 glViewport(0, 0, x, y);
287 glMatrixMode(GL_PROJECTION);
288 glLoadIdentity();
289 gluPerspective(45, (float)x / (float)y, 0.5, 500);
291 if(x != win_xsz || y != win_ysz) {
292 win_xsz = x;
293 win_ysz = y;
294 }
295 }
297 static void keyboard(unsigned char key, int x, int y)
298 {
299 key_state[(int)key] = true;
301 switch(key) {
302 case 27:
303 exit(0);
304 case 'w':
305 {
306 static bool wire;
307 wire = !wire;
308 glPolygonMode(GL_FRONT_AND_BACK, wire ? GL_LINE : GL_FILL);
309 }
310 break;
311 default:
312 break;
313 }
314 }
316 static void keyboard_up(unsigned char key, int x, int y)
317 {
318 key_state[(int) key] = false;
319 }
321 static int prev_x, prev_y;
322 static bool bn_state[32];
323 static float prev_thres, prev_thres2;
325 static void mouse(int bn, int state, int x, int y)
326 {
327 prev_x = x;
328 prev_y = y;
330 bn_state[bn - GLUT_LEFT_BUTTON] = (state == GLUT_DOWN);
332 if(state == GLUT_DOWN) {
333 prev_thres = thres;
334 prev_thres2 = thres2;
335 }
336 else {
337 if(thres != prev_thres || thres2 != prev_thres2) {
338 mesh->clear();
339 }
340 }
341 }
343 static void motion(int x, int y)
344 {
345 int dx = x - prev_x;
346 int dy = y - prev_y;
348 if(!dx && !dy)
349 return;
351 prev_x = x;
352 prev_y = y;
354 if(key_state[(int)'t']) {
355 thres = (float)x / (float)win_xsz;
356 printf("threshold: %f\n", thres);
358 glutPostRedisplay();
359 return;
360 }
362 // camera
363 if(bn_state[0]) {
364 if(key_state[(int)'z']) {
365 //zoom the camera
367 cam_dist += dy * 0.1;
369 if(cam_dist < 0)
370 cam_dist = 0;
371 }
372 else {
373 //rotate the camera
375 cam_phi += dy * 0.5;
376 cam_theta += dx * 0.5;
378 if(cam_phi > 90)
379 cam_phi = 90;
380 if(cam_phi < -90)
381 cam_phi = -90;
382 }
384 glutPostRedisplay();
385 }
386 }
388 static bool init_xfer(void)
389 {
390 glutSetWindow(mainwin_id);
391 int x = glutGet(GLUT_WINDOW_X);
392 int y = glutGet(GLUT_WINDOW_Y) + glutGet(GLUT_WINDOW_HEIGHT);
394 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
395 glutInitWindowSize(512, 100);
396 glutInitWindowPosition(x, y);
398 xferwin_id = glutCreateWindow("Transfer Function");
399 glutDisplayFunc(display_xfer);
400 glutReshapeFunc(reshape_xfer);
401 glutMouseFunc(mouse_xfer);
402 glutMotionFunc(motion_xfer);
404 return true;
405 }
407 static void display_xfer(void)
408 {
409 glClear(GL_COLOR_BUFFER_BIT);
411 int num_val = glutGet(GLUT_WINDOW_WIDTH) / 4.0;
412 float x = 0;
413 float dx = 1.0 / num_val;
414 float low, high;
415 if(thres < thres2) {
416 low = thres;
417 high = thres2;
418 }
419 else {
420 low = thres2;
421 high = thres;
422 }
424 //drawing the transfer function curve
426 glLineWidth(3);
427 glBegin(GL_LINE_STRIP);
428 glColor3f(0.8, 0.3, 0.8);
429 for(int i=0; i<num_val; i++) {
430 float val = transfer_function(x, low, high);
431 glVertex2f(x, val);
432 x += dx;
433 }
434 glEnd();
436 //threshold bars
438 glLineWidth(2);
439 glBegin(GL_LINES);
440 glColor3f(0.4, 0.4, 0.8);
441 glVertex2f(low, 0);
442 glVertex2f(low, 1);
443 glColor3f(0.4, 0.8, 0.4);
444 glVertex2f(high, 0);
445 glVertex2f(high, 1);
446 glEnd();
448 /*
449 * gradient
450 */
451 /* glBegin(GL_QUADS);
452 for(int i=0; i<num_val; i++) {
453 float val = transfer_function(x, low, high);
454 glColor3f(val, val, val);
455 glVertex3f(x, 1.0, 0.0);
456 glVertex3f(x, 0.0, 0.0);
458 val = transfer_function(x + dx, low, high);
459 glColor3f(val, val, val);
460 glVertex3f(x + dx, 0.0, 0.0);
461 glVertex3f(x + dx, 1.0, 0.0);
462 x += dx;
463 }
464 glEnd(); */
466 glutSwapBuffers();
467 assert(glGetError() == GL_NO_ERROR);
468 }
470 static void reshape_xfer(int x, int y)
471 {
472 glViewport(0.0, 0.0, x, y);
473 glMatrixMode(GL_PROJECTION);
474 glLoadIdentity();
475 glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
476 }
478 static float *select_thres;
479 static float prev_select_thres;
480 static void mouse_xfer(int button, int state, int x, int y)
481 {
482 if(button == GLUT_LEFT_BUTTON) {
483 if(state == GLUT_DOWN) {
484 int width = glutGet(GLUT_WINDOW_WIDTH);
485 float xpos = (float)x / (float)width;
486 if(fabs(xpos - thres) <= fabs(xpos - thres2)) {
487 select_thres = &thres;
488 }
489 else {
490 select_thres = &thres2;
491 }
492 prev_select_thres = *select_thres;
493 }
494 else {
495 if(fabs(*select_thres - prev_select_thres) > 0.001) {
496 mesh->clear();
497 ui->sync_live();
498 }
499 select_thres = 0;
500 }
501 }
502 }
504 static void motion_xfer(int x, int y)
505 {
506 if(!select_thres)
507 return;
509 int width = glutGet(GLUT_WINDOW_WIDTH);
510 float xpos = (float)x / (float)width;
512 *select_thres = xpos;
514 glutPostRedisplay();
515 glutSetWindow(mainwin_id);
516 glutPostRedisplay();
517 }