volmetrics

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