volmetrics
view 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 |
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 static bool init(void);
14 static GLUI *create_ui(void);
15 static void display(void);
16 static void reshape(int x, int y);
17 static void keyboard(unsigned char key, int x, int y);
18 static void keyboard_up(unsigned char key, int x, int y);
19 static void mouse(int button, int state, int x, int y);
20 static 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 static 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];
90 static void toggle_use_orig(int id)
91 {
92 for(int i=0; i<3; i++) {
93 if(use_orig_vol_res) {
94 res_spin[i]->disable();
95 } else {
96 res_spin[i]->enable();
97 }
98 }
99 }
100 static void thres_change(int id)
101 {
102 static float prev_thres = thres;
104 if(prev_thres != thres) {
105 prev_thres = thres;
106 mesh->clear();
107 }
108 }
109 static GLUI *create_ui()
110 {
111 GLUI *ui = GLUI_Master.create_glui("ui");
112 assert(ui);
114 ui->set_main_gfx_window(glutGetWindow());
116 GLUI_Spinner *thres_spin = ui->add_spinner("iso threshold", GLUI_SPINNER_FLOAT, &thres, 0, thres_change);
117 thres_spin->set_float_limits(0, 1);
119 GLUI_Panel *res_panel = ui->add_panel("volume resolution");
121 ui->add_checkbox_to_panel(res_panel, "original resolution", &use_orig_vol_res, 0, toggle_use_orig);
123 static const char *res_spin_name[] = {"x", "y", "z"};
124 for(int i=0; i<3; i++) {
125 res_spin[i] = ui->add_spinner_to_panel(res_panel, res_spin_name[i], GLUI_SPINNER_INT, vol_res + i);
126 res_spin[i]->set_int_limits(8, 256); // TODO limits as arguments or config
127 res_spin[i]->disable();
128 }
130 return ui;
131 }
133 static void display(void)
134 {
135 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
137 glMatrixMode(GL_MODELVIEW);
138 glLoadIdentity();
139 glTranslatef(0, 0, -cam_dist);
140 glRotatef(cam_phi, 1, 0, 0);
141 glRotatef(cam_theta, 0, 1, 0);
143 /*
144 glBindTexture(GL_TEXTURE_3D, vol->get_texture());
145 glEnable(GL_TEXTURE_3D);
146 glBegin(GL_QUADS);
147 glTexCoord3f(0, 0, cur_z); glVertex3f(-1, -1, 0);
148 glTexCoord3f(0, 1, cur_z); glVertex3f(-1, 1, 0);
149 glTexCoord3f(1, 1, cur_z); glVertex3f(1, 1, 0);
150 glTexCoord3f(1, 0, cur_z); glVertex3f(1, -1, 0);
151 glEnd();
152 glDisable(GL_TEXTURE_3D);
153 */
155 if(mesh->is_empty()) {
156 printf("recalculating isosurface ... ");
157 fflush(stdout);
158 vol->create_mesh(mesh, thres);
159 printf("done.\n");
160 }
161 mesh->draw();
163 //TODO: draw threshold
164 glutSwapBuffers();
165 assert(glGetError() == GL_NO_ERROR);
166 }
168 static void reshape(int x, int y)
169 {
170 glViewport(0, 0, x, y);
171 glMatrixMode(GL_PROJECTION);
172 glLoadIdentity();
173 gluPerspective(45, (float)x / (float)y, 0.5, 500);
175 if(x != win_xsz || y != win_ysz) {
176 win_xsz = x;
177 win_ysz = y;
178 }
179 }
181 static void keyboard(unsigned char key, int x, int y)
182 {
183 key_state[(int)key] = true;
185 switch(key) {
186 case 27:
187 exit(0);
188 case 'w':
189 {
190 static bool wire;
192 wire = !wire;
193 glPolygonMode(GL_FRONT_AND_BACK, wire ? GL_LINE : GL_FILL);
194 }
195 break;
196 default:
197 break;
198 }
199 }
201 static void keyboard_up(unsigned char key, int x, int y)
202 {
203 key_state[(int) key] = false;
204 }
206 static int prev_x, prev_y;
207 static bool bn_state[32];
208 static float prev_thres;
210 static void mouse(int bn, int state, int x, int y)
211 {
212 prev_x = x;
213 prev_y = y;
215 bn_state[bn - GLUT_LEFT_BUTTON] = (state == GLUT_DOWN);
217 if(state == GLUT_DOWN) {
218 prev_thres = thres;
219 }
220 else {
221 if(thres != prev_thres) {
222 mesh->clear();
223 }
224 }
225 }
227 static void motion(int x, int y)
228 {
229 int dx = x - prev_x;
230 int dy = y - prev_y;
232 if(!dx && !dy)
233 return;
235 prev_x = x;
236 prev_y = y;
238 if(key_state[(int)'t']) {
239 thres = (float)x / (float)win_xsz;
240 printf("threshold: %f\n", thres);
242 glutPostRedisplay();
243 return;
244 }
246 // camera
247 if(bn_state[0]) {
248 if(key_state[(int)'z']) {
249 //zoom the camera
251 cam_dist += dy * 0.1;
253 if(cam_dist < 0)
254 cam_dist = 0;
255 }
256 else {
257 //rotate the camera
259 cam_phi += dy * 0.5;
260 cam_theta += dx * 0.5;
262 if(cam_phi > 90)
263 cam_phi = 90;
264 if(cam_phi < -90)
265 cam_phi = -90;
266 }
268 glutPostRedisplay();
269 }
270 }