volmetrics

view src/main.cc @ 9:40febfed6cff

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