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