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