volmetrics

annotate src/main.cc @ 10:8ffa6d61eb56

changed main.cc functions to static :p
author Eleni Maria Stea <elene.mst@gmail.com>
date Sun, 26 Jan 2014 23:18:13 +0200
parents 40febfed6cff
children c5af275b6a60
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
eleni@0 5 #include <stdio.h>
eleni@0 6 #include <assert.h>
eleni@0 7
elene@9 8 #include <vector>
elene@9 9
elene@9 10 #include "mesh.h"
elene@6 11 #include "volume.h"
eleni@1 12
elene@10 13 static bool init(void);
elene@10 14 static GLUI *create_ui(void);
elene@10 15 static void display(void);
elene@10 16 static void reshape(int x, int y);
elene@10 17 static void keyboard(unsigned char key, int x, int y);
elene@10 18 static void keyboard_up(unsigned char key, int x, int y);
elene@10 19 static void mouse(int button, int state, int x, int y);
elene@10 20 static void motion(int x, int y);
eleni@0 21
eleni@0 22 static int win_xsz, win_ysz;
elene@9 23 static float cam_phi, cam_theta, cam_dist = 6;
elene@9 24 static std::vector<bool> key_state(256);
eleni@0 25
eleni@1 26 /////////////////////////////
eleni@1 27 // debug TODO remove
eleni@1 28 ////////////////////////////
elene@6 29
elene@6 30 static Volume *vol;
elene@9 31 static Mesh *mesh;
elene@9 32 static float cur_z, thres = 0.5;
elene@9 33
elene@9 34 static int use_orig_vol_res = 1;
elene@9 35 static int vol_res[3]; // volume sampling resolution x/y/z
elene@9 36
elene@9 37 static GLUI *ui;
eleni@1 38
eleni@0 39 int main(int argc, char **argv)
eleni@0 40 {
eleni@0 41 glutInit(&argc, argv);
elene@7 42 glutInitWindowSize(512, 512);
eleni@0 43 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
eleni@0 44
eleni@0 45 //TODO parse arguments
eleni@0 46
eleni@0 47 glutCreateWindow("My Colonoscopie OEO!");
eleni@0 48
eleni@0 49 glutDisplayFunc(display);
eleni@0 50 glutReshapeFunc(reshape);
eleni@0 51 glutKeyboardFunc(keyboard);
elene@9 52 glutKeyboardUpFunc(keyboard_up);
eleni@0 53 glutMouseFunc(mouse);
eleni@0 54 glutMotionFunc(motion);
eleni@0 55
eleni@0 56 glewInit();
eleni@1 57 if(!init()) {
eleni@1 58 fprintf(stderr, "Failed to initialize La votre Colonoscopie\n");
eleni@1 59 return 1;
eleni@1 60 }
eleni@0 61
eleni@0 62 //call init
eleni@0 63
eleni@0 64 glutMainLoop();
eleni@0 65 return 0;
eleni@0 66 }
eleni@0 67
elene@10 68 static bool init()
elene@9 69 {
elene@9 70 vol = new Volume;
elene@9 71 if(!vol->load("data/test1.vol")) {
elene@9 72 fprintf(stderr, "Failed to load test1.vol");
elene@9 73 return false;
elene@9 74 }
elene@9 75 mesh = new Mesh;
elene@9 76 cur_z = 0.5;
elene@9 77
elene@9 78 vol_res[0] = vol->get_slice(0)->get_width();
elene@9 79 vol_res[1] = vol->get_slice(0)->get_height();
elene@9 80 vol_res[2] = vol->get_slice_count();
elene@9 81
elene@9 82 if(!(ui = create_ui())) {
elene@9 83 return false;
elene@9 84 }
elene@9 85
elene@9 86 return true;
elene@9 87 }
elene@9 88
elene@9 89 static GLUI_Spinner *res_spin[3];
elene@9 90 static void toggle_use_orig(int id)
elene@9 91 {
elene@9 92 for(int i=0; i<3; i++) {
elene@9 93 if(use_orig_vol_res) {
elene@9 94 res_spin[i]->disable();
elene@9 95 } else {
elene@9 96 res_spin[i]->enable();
elene@9 97 }
elene@9 98 }
elene@9 99 }
elene@9 100 static void thres_change(int id)
elene@9 101 {
elene@9 102 static float prev_thres = thres;
elene@9 103
elene@9 104 if(prev_thres != thres) {
elene@9 105 prev_thres = thres;
elene@9 106 mesh->clear();
elene@9 107 }
elene@9 108 }
elene@10 109 static GLUI *create_ui()
elene@9 110 {
elene@9 111 GLUI *ui = GLUI_Master.create_glui("ui");
elene@9 112 assert(ui);
elene@9 113
elene@9 114 ui->set_main_gfx_window(glutGetWindow());
elene@9 115
elene@9 116 GLUI_Spinner *thres_spin = ui->add_spinner("iso threshold", GLUI_SPINNER_FLOAT, &thres, 0, thres_change);
elene@9 117 thres_spin->set_float_limits(0, 1);
elene@9 118
elene@9 119 GLUI_Panel *res_panel = ui->add_panel("volume resolution");
elene@9 120
elene@9 121 ui->add_checkbox_to_panel(res_panel, "original resolution", &use_orig_vol_res, 0, toggle_use_orig);
elene@9 122
elene@9 123 static const char *res_spin_name[] = {"x", "y", "z"};
elene@9 124 for(int i=0; i<3; i++) {
elene@9 125 res_spin[i] = ui->add_spinner_to_panel(res_panel, res_spin_name[i], GLUI_SPINNER_INT, vol_res + i);
elene@9 126 res_spin[i]->set_int_limits(8, 256); // TODO limits as arguments or config
elene@9 127 res_spin[i]->disable();
elene@9 128 }
elene@9 129
elene@9 130 return ui;
elene@9 131 }
elene@9 132
elene@10 133 static void display(void)
eleni@0 134 {
elene@9 135 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
elene@9 136
elene@9 137 glMatrixMode(GL_MODELVIEW);
elene@9 138 glLoadIdentity();
elene@9 139 glTranslatef(0, 0, -cam_dist);
elene@9 140 glRotatef(cam_phi, 1, 0, 0);
elene@9 141 glRotatef(cam_theta, 0, 1, 0);
elene@9 142
elene@9 143 /*
elene@6 144 glBindTexture(GL_TEXTURE_3D, vol->get_texture());
elene@6 145 glEnable(GL_TEXTURE_3D);
eleni@1 146 glBegin(GL_QUADS);
elene@6 147 glTexCoord3f(0, 0, cur_z); glVertex3f(-1, -1, 0);
elene@6 148 glTexCoord3f(0, 1, cur_z); glVertex3f(-1, 1, 0);
elene@6 149 glTexCoord3f(1, 1, cur_z); glVertex3f(1, 1, 0);
elene@6 150 glTexCoord3f(1, 0, cur_z); glVertex3f(1, -1, 0);
eleni@1 151 glEnd();
elene@6 152 glDisable(GL_TEXTURE_3D);
elene@9 153 */
eleni@0 154
elene@9 155 if(mesh->is_empty()) {
elene@9 156 printf("recalculating isosurface ... ");
elene@9 157 fflush(stdout);
elene@9 158 vol->create_mesh(mesh, thres);
elene@9 159 printf("done.\n");
elene@9 160 }
elene@9 161 mesh->draw();
elene@9 162
elene@9 163 //TODO: draw threshold
eleni@0 164 glutSwapBuffers();
eleni@0 165 assert(glGetError() == GL_NO_ERROR);
eleni@0 166 }
eleni@0 167
elene@10 168 static void reshape(int x, int y)
eleni@0 169 {
eleni@0 170 glViewport(0, 0, x, y);
elene@9 171 glMatrixMode(GL_PROJECTION);
elene@9 172 glLoadIdentity();
elene@9 173 gluPerspective(45, (float)x / (float)y, 0.5, 500);
elene@9 174
eleni@0 175 if(x != win_xsz || y != win_ysz) {
eleni@0 176 win_xsz = x;
eleni@0 177 win_ysz = y;
eleni@0 178 }
eleni@0 179 }
eleni@0 180
elene@10 181 static void keyboard(unsigned char key, int x, int y)
eleni@0 182 {
elene@9 183 key_state[(int)key] = true;
elene@9 184
eleni@0 185 switch(key) {
eleni@0 186 case 27:
eleni@0 187 exit(0);
elene@9 188 case 'w':
elene@9 189 {
elene@9 190 static bool wire;
elene@9 191
elene@9 192 wire = !wire;
elene@9 193 glPolygonMode(GL_FRONT_AND_BACK, wire ? GL_LINE : GL_FILL);
elene@9 194 }
elene@9 195 break;
eleni@0 196 default:
eleni@0 197 break;
eleni@0 198 }
eleni@0 199 }
eleni@0 200
elene@10 201 static void keyboard_up(unsigned char key, int x, int y)
elene@9 202 {
elene@9 203 key_state[(int) key] = false;
elene@9 204 }
elene@9 205
eleni@0 206 static int prev_x, prev_y;
elene@9 207 static bool bn_state[32];
elene@9 208 static float prev_thres;
elene@9 209
elene@10 210 static void mouse(int bn, int state, int x, int y)
eleni@0 211 {
eleni@0 212 prev_x = x;
eleni@0 213 prev_y = y;
elene@9 214
elene@9 215 bn_state[bn - GLUT_LEFT_BUTTON] = (state == GLUT_DOWN);
elene@9 216
elene@9 217 if(state == GLUT_DOWN) {
elene@9 218 prev_thres = thres;
elene@9 219 }
elene@9 220 else {
elene@9 221 if(thres != prev_thres) {
elene@9 222 mesh->clear();
elene@9 223 }
elene@9 224 }
eleni@0 225 }
eleni@0 226
elene@10 227 static void motion(int x, int y)
eleni@0 228 {
eleni@0 229 int dx = x - prev_x;
eleni@0 230 int dy = y - prev_y;
elene@9 231
elene@9 232 if(!dx && !dy)
elene@9 233 return;
elene@9 234
eleni@0 235 prev_x = x;
eleni@0 236 prev_y = y;
elene@6 237
elene@9 238 if(key_state[(int)'t']) {
elene@9 239 thres = (float)x / (float)win_xsz;
elene@9 240 printf("threshold: %f\n", thres);
elene@9 241
elene@9 242 glutPostRedisplay();
elene@9 243 return;
elene@9 244 }
elene@9 245
elene@9 246 // camera
elene@9 247 if(bn_state[0]) {
elene@9 248 if(key_state[(int)'z']) {
elene@9 249 //zoom the camera
elene@9 250
elene@9 251 cam_dist += dy * 0.1;
elene@9 252
elene@9 253 if(cam_dist < 0)
elene@9 254 cam_dist = 0;
elene@9 255 }
elene@9 256 else {
elene@9 257 //rotate the camera
elene@9 258
elene@9 259 cam_phi += dy * 0.5;
elene@9 260 cam_theta += dx * 0.5;
elene@9 261
elene@9 262 if(cam_phi > 90)
elene@9 263 cam_phi = 90;
elene@9 264 if(cam_phi < -90)
elene@9 265 cam_phi = -90;
elene@9 266 }
elene@9 267
elene@6 268 glutPostRedisplay();
elene@6 269 }
eleni@0 270 }