rev |
line source |
nuclear@0
|
1 #include <stdio.h>
|
nuclear@0
|
2 #include <stdlib.h>
|
nuclear@0
|
3 #include <assert.h>
|
nuclear@0
|
4
|
nuclear@0
|
5 #include <GL/glew.h>
|
nuclear@0
|
6 #ifndef __APPLE__
|
nuclear@0
|
7 #include <GL/glut.h>
|
nuclear@0
|
8 #else
|
nuclear@0
|
9 #include <GLUT/glut.h>
|
nuclear@0
|
10 #endif
|
nuclear@0
|
11
|
nuclear@0
|
12 #include <vmath/vmath.h>
|
nuclear@0
|
13 #include <imago2.h>
|
nuclear@0
|
14 #include "sdr.h"
|
nuclear@9
|
15 #include "volume.h"
|
nuclear@13
|
16 #include "ui.h"
|
nuclear@18
|
17 #include "demo.h"
|
nuclear@0
|
18
|
nuclear@3
|
19 #define XFER_MAP_SZ 512
|
nuclear@3
|
20
|
nuclear@11
|
21 static void render_volume();
|
nuclear@11
|
22 static void draw_slice();
|
nuclear@11
|
23 static void draw_xfer_func();
|
nuclear@5
|
24
|
nuclear@11
|
25 /*
|
nuclear@0
|
26 void keyb(unsigned char key, int x, int y);
|
nuclear@4
|
27 void keyb_up(unsigned char key, int x, int y);
|
nuclear@0
|
28 void mouse(int bn, int state, int x, int y);
|
nuclear@0
|
29 void motion(int x, int y);
|
nuclear@0
|
30 int parse_args(int argc, char **argv);
|
nuclear@11
|
31 */
|
nuclear@0
|
32
|
nuclear@11
|
33 static void create_ray_texture(int xsz, int ysz, float vfov, Vector2 *tex_scale);
|
nuclear@11
|
34 static Vector3 get_primary_ray_dir(int x, int y, int w, int h, float vfov_deg);
|
nuclear@0
|
35 static int round_pow2(int x);
|
nuclear@3
|
36 static void create_transfer_map(float mean, float sdev);
|
nuclear@0
|
37
|
nuclear@9
|
38 static float cam_theta = 0, cam_phi = 0, cam_dist = 4.0;
|
nuclear@9
|
39 static float cam_x, cam_y, cam_z;
|
nuclear@0
|
40
|
nuclear@13
|
41 static Vector2 tex_scale;
|
nuclear@9
|
42 static unsigned int vol_sdr, slice_sdr, ray_tex;
|
nuclear@9
|
43 static int win_xsz, win_ysz;
|
nuclear@11
|
44 static bool raytex_needs_recalc = true;
|
nuclear@0
|
45
|
nuclear@9
|
46 static unsigned int xfer_tex;
|
nuclear@9
|
47 static float xfer_mean = 0.7, xfer_sdev = 0.1;
|
nuclear@11
|
48 static bool xfertex_needs_recalc = true;
|
nuclear@3
|
49
|
nuclear@19
|
50 static float cur_z = 0.5;
|
nuclear@23
|
51 static bool clip_z;
|
nuclear@7
|
52 static float ray_step = 0.01;
|
nuclear@4
|
53
|
nuclear@18
|
54 static Volume *volume;
|
nuclear@9
|
55
|
nuclear@9
|
56
|
nuclear@11
|
57 bool volray_init()
|
nuclear@0
|
58 {
|
nuclear@0
|
59 glewInit();
|
nuclear@0
|
60
|
nuclear@9
|
61 if(!(vol_sdr = create_program_load("sdr/volray.v.glsl", "sdr/volray.p.glsl"))) {
|
nuclear@11
|
62 return false;
|
nuclear@0
|
63 }
|
nuclear@5
|
64 set_uniform_int(vol_sdr, "volume", 0);
|
nuclear@5
|
65 set_uniform_int(vol_sdr, "ray_tex", 1);
|
nuclear@5
|
66 set_uniform_int(vol_sdr, "xfer_tex", 2);
|
nuclear@7
|
67 set_uniform_float(vol_sdr, "ray_step", ray_step);
|
nuclear@23
|
68 set_uniform_float(vol_sdr, "zclip", 0.0);
|
nuclear@5
|
69
|
nuclear@9
|
70 if(!(slice_sdr = create_program_load(0, "sdr/slice.p.glsl"))) {
|
nuclear@11
|
71 return false;
|
nuclear@5
|
72 }
|
nuclear@5
|
73 set_uniform_int(slice_sdr, "volume", 0);
|
nuclear@5
|
74 set_uniform_int(slice_sdr, "xfer_tex", 1);
|
nuclear@0
|
75
|
nuclear@18
|
76 init_demo();
|
nuclear@18
|
77
|
nuclear@11
|
78 return true;
|
nuclear@0
|
79 }
|
nuclear@0
|
80
|
nuclear@18
|
81 void volray_setvolume(Volume *vol)
|
nuclear@13
|
82 {
|
nuclear@13
|
83 volume = vol;
|
nuclear@13
|
84 }
|
nuclear@13
|
85
|
nuclear@18
|
86 Volume *volray_getvolume()
|
nuclear@18
|
87 {
|
nuclear@18
|
88 return volume;
|
nuclear@18
|
89 }
|
nuclear@18
|
90
|
nuclear@21
|
91 void volray_setvalue(int which, float val)
|
nuclear@21
|
92 {
|
nuclear@21
|
93 switch(which) {
|
nuclear@21
|
94 case VOLRAY_ZCURSOR:
|
nuclear@21
|
95 cur_z = val;
|
nuclear@23
|
96 if(clip_z) {
|
nuclear@23
|
97 set_uniform_float(vol_sdr, "zclip", cur_z);
|
nuclear@23
|
98 }
|
nuclear@21
|
99 post_redisplay();
|
nuclear@21
|
100 break;
|
nuclear@21
|
101
|
nuclear@22
|
102 case VOLRAY_ZCLIP:
|
nuclear@23
|
103 clip_z = val > 0.5;
|
nuclear@23
|
104 set_uniform_float(vol_sdr, "zclip", clip_z ? cur_z : 0.0);
|
nuclear@23
|
105 post_redisplay();
|
nuclear@23
|
106 break;
|
nuclear@22
|
107
|
nuclear@21
|
108 default:
|
nuclear@21
|
109 break;
|
nuclear@21
|
110 }
|
nuclear@21
|
111 }
|
nuclear@21
|
112
|
nuclear@21
|
113 float volray_getvalue(int which)
|
nuclear@21
|
114 {
|
nuclear@21
|
115 switch(which) {
|
nuclear@21
|
116 case VOLRAY_ZCURSOR:
|
nuclear@21
|
117 return cur_z;
|
nuclear@21
|
118
|
nuclear@23
|
119 case VOLRAY_ZCLIP:
|
nuclear@23
|
120 return clip_z > 0.5 ? 1.0 : 0.0;
|
nuclear@23
|
121 break;
|
nuclear@23
|
122
|
nuclear@21
|
123 default:
|
nuclear@21
|
124 break;
|
nuclear@21
|
125 }
|
nuclear@21
|
126 return 0.0;
|
nuclear@21
|
127 }
|
nuclear@21
|
128
|
nuclear@11
|
129 void volray_draw(void)
|
nuclear@0
|
130 {
|
nuclear@4
|
131 /* recalculate primary ray texture if needed */
|
nuclear@1
|
132 if(raytex_needs_recalc) {
|
nuclear@1
|
133 create_ray_texture(win_xsz, win_ysz, 50.0, &tex_scale);
|
nuclear@1
|
134 }
|
nuclear@4
|
135 /* recalculate transfer function texture if needed */
|
nuclear@3
|
136 if(xfertex_needs_recalc) {
|
nuclear@3
|
137 create_transfer_map(xfer_mean, xfer_sdev);
|
nuclear@3
|
138 }
|
nuclear@1
|
139
|
nuclear@18
|
140 draw_demo();
|
nuclear@18
|
141
|
nuclear@13
|
142 glClear(GL_COLOR_BUFFER_BIT);
|
nuclear@13
|
143
|
nuclear@13
|
144 if(volume) {
|
nuclear@13
|
145 render_volume();
|
nuclear@28
|
146 //draw_slice();
|
nuclear@13
|
147 draw_xfer_func();
|
nuclear@13
|
148 }
|
nuclear@4
|
149
|
nuclear@4
|
150 assert(glGetError() == GL_NO_ERROR);
|
nuclear@4
|
151 }
|
nuclear@4
|
152
|
nuclear@11
|
153 static void render_volume(void)
|
nuclear@4
|
154 {
|
nuclear@4
|
155 /* set the camera transformation */
|
nuclear@0
|
156 glMatrixMode(GL_MODELVIEW);
|
nuclear@4
|
157 glPushMatrix();
|
nuclear@0
|
158 glLoadIdentity();
|
nuclear@1
|
159 glRotatef(-90, 1, 0, 0);
|
nuclear@0
|
160 glTranslatef(cam_x, cam_y, -cam_z);
|
nuclear@0
|
161 glRotatef(cam_theta, 0, 1, 0);
|
nuclear@0
|
162 glRotatef(cam_phi, 1, 0, 0);
|
nuclear@0
|
163 glTranslatef(0, 0, -cam_dist);
|
nuclear@0
|
164
|
nuclear@4
|
165 /* setup the texture matrix to map the useful part of the ray texture to [0,1] */
|
nuclear@0
|
166 glMatrixMode(GL_TEXTURE);
|
nuclear@4
|
167 glPushMatrix();
|
nuclear@1
|
168 glLoadIdentity();
|
nuclear@0
|
169 glScalef(tex_scale.x, tex_scale.y, 1.0);
|
nuclear@0
|
170
|
nuclear@4
|
171 /* tex unit0: volume data 3D texture */
|
nuclear@0
|
172 glActiveTexture(GL_TEXTURE0);
|
nuclear@13
|
173 glBindTexture(GL_TEXTURE_3D, volume->get_texture());
|
nuclear@0
|
174 glEnable(GL_TEXTURE_3D);
|
nuclear@0
|
175
|
nuclear@4
|
176 /* tex unit1: primary rays in view space */
|
nuclear@0
|
177 glActiveTexture(GL_TEXTURE1);
|
nuclear@1
|
178 glBindTexture(GL_TEXTURE_2D, ray_tex);
|
nuclear@0
|
179 glEnable(GL_TEXTURE_2D);
|
nuclear@0
|
180
|
nuclear@4
|
181 /* tex unit2: transfer function (1d) */
|
nuclear@3
|
182 glActiveTexture(GL_TEXTURE2);
|
nuclear@3
|
183 glBindTexture(GL_TEXTURE_1D, xfer_tex);
|
nuclear@3
|
184 glEnable(GL_TEXTURE_1D);
|
nuclear@3
|
185
|
nuclear@5
|
186 bind_program(vol_sdr);
|
nuclear@0
|
187 glBegin(GL_QUADS);
|
nuclear@0
|
188 glColor3f(1, 1, 1);
|
nuclear@1
|
189 glTexCoord2f(0, 1); glVertex2f(-1, -1);
|
nuclear@1
|
190 glTexCoord2f(1, 1); glVertex2f(1, -1);
|
nuclear@1
|
191 glTexCoord2f(1, 0); glVertex2f(1, 1);
|
nuclear@1
|
192 glTexCoord2f(0, 0); glVertex2f(-1, 1);
|
nuclear@0
|
193 glEnd();
|
nuclear@0
|
194 bind_program(0);
|
nuclear@0
|
195
|
nuclear@3
|
196 glActiveTexture(GL_TEXTURE2);
|
nuclear@3
|
197 glDisable(GL_TEXTURE_1D);
|
nuclear@1
|
198 glActiveTexture(GL_TEXTURE1);
|
nuclear@0
|
199 glDisable(GL_TEXTURE_2D);
|
nuclear@0
|
200 glActiveTexture(GL_TEXTURE0);
|
nuclear@0
|
201 glDisable(GL_TEXTURE_3D);
|
nuclear@0
|
202
|
nuclear@0
|
203 glMatrixMode(GL_TEXTURE);
|
nuclear@4
|
204 glPopMatrix();
|
nuclear@4
|
205 glMatrixMode(GL_MODELVIEW);
|
nuclear@4
|
206 glPopMatrix();
|
nuclear@4
|
207 }
|
nuclear@0
|
208
|
nuclear@28
|
209 void volray_draw_slice(void)
|
nuclear@5
|
210 {
|
nuclear@5
|
211 glActiveTexture(GL_TEXTURE0);
|
nuclear@13
|
212 glBindTexture(GL_TEXTURE_3D, volume->get_texture());
|
nuclear@5
|
213 glEnable(GL_TEXTURE_3D);
|
nuclear@5
|
214
|
nuclear@5
|
215 glActiveTexture(GL_TEXTURE1);
|
nuclear@5
|
216 glBindTexture(GL_TEXTURE_1D, xfer_tex);
|
nuclear@5
|
217 glEnable(GL_TEXTURE_1D);
|
nuclear@5
|
218
|
nuclear@5
|
219 bind_program(slice_sdr);
|
nuclear@5
|
220
|
nuclear@5
|
221 glBegin(GL_QUADS);
|
nuclear@5
|
222 glColor3f(1, 1, 1);
|
nuclear@5
|
223 glTexCoord3f(0, 1, cur_z); glVertex2f(-1, -1);
|
nuclear@5
|
224 glTexCoord3f(1, 1, cur_z); glVertex2f(1, -1);
|
nuclear@5
|
225 glTexCoord3f(1, 0, cur_z); glVertex2f(1, 1);
|
nuclear@5
|
226 glTexCoord3f(0, 0, cur_z); glVertex2f(-1, 1);
|
nuclear@5
|
227 glEnd();
|
nuclear@5
|
228
|
nuclear@5
|
229 bind_program(0);
|
nuclear@5
|
230
|
nuclear@5
|
231 glActiveTexture(GL_TEXTURE1);
|
nuclear@5
|
232 glDisable(GL_TEXTURE_1D);
|
nuclear@5
|
233 glActiveTexture(GL_TEXTURE0);
|
nuclear@5
|
234 glDisable(GL_TEXTURE_3D);
|
nuclear@5
|
235 }
|
nuclear@5
|
236
|
nuclear@11
|
237 static void draw_xfer_func(void)
|
nuclear@4
|
238 {
|
nuclear@4
|
239 glMatrixMode(GL_MODELVIEW);
|
nuclear@4
|
240 glPushMatrix();
|
nuclear@4
|
241 glTranslatef(-0.9, -0.9, 0);
|
nuclear@4
|
242 glScalef(0.5, 0.1, 1);
|
nuclear@4
|
243
|
nuclear@4
|
244 glBindTexture(GL_TEXTURE_1D, xfer_tex);
|
nuclear@4
|
245 glEnable(GL_TEXTURE_1D);
|
nuclear@4
|
246
|
nuclear@4
|
247 glBegin(GL_QUADS);
|
nuclear@4
|
248 glColor3f(1, 1, 1);
|
nuclear@4
|
249 glTexCoord1f(1);
|
nuclear@4
|
250 glVertex2f(1, 0);
|
nuclear@4
|
251 glVertex2f(1, 1);
|
nuclear@4
|
252 glTexCoord1f(0);
|
nuclear@4
|
253 glVertex2f(0, 1);
|
nuclear@4
|
254 glVertex2f(0, 0);
|
nuclear@4
|
255 glEnd();
|
nuclear@4
|
256
|
nuclear@4
|
257 glDisable(GL_TEXTURE_1D);
|
nuclear@4
|
258
|
nuclear@4
|
259 glLineWidth(2.0);
|
nuclear@4
|
260 glBegin(GL_LINE_LOOP);
|
nuclear@13
|
261 /*if(uimode == UIMODE_XFER) {
|
nuclear@5
|
262 glColor3f(1, 0, 0);
|
nuclear@13
|
263 } else {*/
|
nuclear@5
|
264 glColor3f(0, 0, 1);
|
nuclear@13
|
265 //}
|
nuclear@4
|
266 glVertex2f(0, 0);
|
nuclear@4
|
267 glVertex2f(1, 0);
|
nuclear@4
|
268 glVertex2f(1, 1);
|
nuclear@4
|
269 glVertex2f(0, 1);
|
nuclear@4
|
270 glEnd();
|
nuclear@4
|
271
|
nuclear@4
|
272 glPopMatrix();
|
nuclear@0
|
273 }
|
nuclear@0
|
274
|
nuclear@11
|
275 void volray_resize(int x, int y)
|
nuclear@0
|
276 {
|
nuclear@0
|
277 glViewport(0, 0, x, y);
|
nuclear@0
|
278
|
nuclear@0
|
279 if(x != win_xsz || y != win_ysz) {
|
nuclear@11
|
280 raytex_needs_recalc = true;
|
nuclear@0
|
281 win_xsz = x;
|
nuclear@0
|
282 win_ysz = y;
|
nuclear@0
|
283 }
|
nuclear@0
|
284 }
|
nuclear@0
|
285
|
nuclear@11
|
286 #if 0
|
nuclear@0
|
287 void keyb(unsigned char key, int x, int y)
|
nuclear@0
|
288 {
|
nuclear@0
|
289 switch(key) {
|
nuclear@0
|
290 case 27:
|
nuclear@0
|
291 exit(0);
|
nuclear@4
|
292
|
nuclear@4
|
293 case 'x':
|
nuclear@5
|
294 uimode = UIMODE_XFER;
|
nuclear@13
|
295 post_redisplay();
|
nuclear@4
|
296 break;
|
nuclear@5
|
297
|
nuclear@5
|
298 case 'c':
|
nuclear@5
|
299 uimode = UIMODE_CURSOR;
|
nuclear@13
|
300 post_redisplay();
|
nuclear@5
|
301 break;
|
nuclear@5
|
302
|
nuclear@5
|
303 default:
|
nuclear@5
|
304 break;
|
nuclear@4
|
305 }
|
nuclear@4
|
306 }
|
nuclear@4
|
307
|
nuclear@4
|
308 void keyb_up(unsigned char key, int x, int y)
|
nuclear@4
|
309 {
|
nuclear@4
|
310 switch(key) {
|
nuclear@4
|
311 case 'x':
|
nuclear@5
|
312 if(uimode == UIMODE_XFER) {
|
nuclear@5
|
313 uimode = UIMODE_DEFAULT;
|
nuclear@13
|
314 post_redisplay();
|
nuclear@5
|
315 }
|
nuclear@5
|
316 break;
|
nuclear@5
|
317
|
nuclear@5
|
318 case 'c':
|
nuclear@5
|
319 if(uimode == UIMODE_CURSOR) {
|
nuclear@5
|
320 uimode = UIMODE_DEFAULT;
|
nuclear@13
|
321 post_redisplay();
|
nuclear@5
|
322 }
|
nuclear@5
|
323 break;
|
nuclear@5
|
324
|
nuclear@5
|
325 default:
|
nuclear@4
|
326 break;
|
nuclear@0
|
327 }
|
nuclear@0
|
328 }
|
nuclear@13
|
329 #endif
|
nuclear@0
|
330
|
nuclear@0
|
331 static int bnstate[32];
|
nuclear@0
|
332 static int prev_x, prev_y;
|
nuclear@0
|
333
|
nuclear@13
|
334 void volray_mouse(int bn, int state, int x, int y)
|
nuclear@0
|
335 {
|
nuclear@13
|
336 bnstate[bn] = state;
|
nuclear@0
|
337 prev_x = x;
|
nuclear@0
|
338 prev_y = y;
|
nuclear@0
|
339 }
|
nuclear@0
|
340
|
nuclear@13
|
341 void volray_motion(int x, int y)
|
nuclear@0
|
342 {
|
nuclear@0
|
343 int dx = x - prev_x;
|
nuclear@0
|
344 int dy = y - prev_y;
|
nuclear@0
|
345 prev_x = x;
|
nuclear@0
|
346 prev_y = y;
|
nuclear@0
|
347
|
nuclear@13
|
348 /*switch(uimode) {
|
nuclear@5
|
349 case UIMODE_XFER:
|
nuclear@4
|
350 if(dx || dy) {
|
nuclear@4
|
351 xfer_mean += dx / (float)win_xsz;
|
nuclear@4
|
352 xfer_sdev += 0.5 * dy / (float)win_ysz;
|
nuclear@0
|
353
|
nuclear@4
|
354 xfer_mean = xfer_mean < 0.0 ? 0.0 : (xfer_mean > 1.0 ? 1.0 : xfer_mean);
|
nuclear@4
|
355 xfer_sdev = xfer_sdev < 0.0 ? 0.0 : (xfer_sdev > 1.0 ? 1.0 : xfer_sdev);
|
nuclear@0
|
356
|
nuclear@11
|
357 xfertex_needs_recalc = true;
|
nuclear@13
|
358 post_redisplay();
|
nuclear@4
|
359 }
|
nuclear@5
|
360 break;
|
nuclear@0
|
361
|
nuclear@5
|
362 case UIMODE_CURSOR:
|
nuclear@5
|
363 cur_z += 0.5 * dy / (float)win_ysz;
|
nuclear@5
|
364
|
nuclear@5
|
365 if(cur_z < 0.0)
|
nuclear@5
|
366 cur_z = 0.0;
|
nuclear@5
|
367 if(cur_z > 1.0)
|
nuclear@5
|
368 cur_z = 1.0;
|
nuclear@7
|
369
|
nuclear@7
|
370 set_uniform_float(vol_sdr, "zclip", cur_z);
|
nuclear@13
|
371 post_redisplay();
|
nuclear@5
|
372 break;
|
nuclear@5
|
373
|
nuclear@13
|
374 default:*/
|
nuclear@5
|
375 /* view control */
|
nuclear@4
|
376 if(bnstate[0]) {
|
nuclear@4
|
377 cam_theta += dx * 0.5;
|
nuclear@4
|
378 cam_phi += dy * 0.5;
|
nuclear@0
|
379
|
nuclear@4
|
380 if(cam_phi <= -90) cam_phi = -89;
|
nuclear@4
|
381 if(cam_phi >= 90) cam_phi = 89;
|
nuclear@13
|
382 post_redisplay();
|
nuclear@4
|
383 }
|
nuclear@4
|
384
|
nuclear@4
|
385 if(bnstate[1]) {
|
nuclear@4
|
386 cam_x += dx * 0.025;
|
nuclear@4
|
387 cam_y += dy * 0.025;
|
nuclear@13
|
388 post_redisplay();
|
nuclear@4
|
389 }
|
nuclear@4
|
390
|
nuclear@4
|
391 if(bnstate[2]) {
|
nuclear@4
|
392 cam_dist += dy * 0.025;
|
nuclear@4
|
393 if(cam_dist < 0.0) cam_dist = 0.0;
|
nuclear@13
|
394 post_redisplay();
|
nuclear@4
|
395 }
|
nuclear@13
|
396 //}
|
nuclear@0
|
397 }
|
nuclear@0
|
398
|
nuclear@13
|
399 #if 0
|
nuclear@0
|
400 int parse_args(int argc, char **argv)
|
nuclear@0
|
401 {
|
nuclear@0
|
402 int i;
|
nuclear@3
|
403 char *endp;
|
nuclear@0
|
404
|
nuclear@0
|
405 for(i=1; i<argc; i++) {
|
nuclear@2
|
406 if(argv[i][0] == '-' && argv[i][2] == 0) {
|
nuclear@2
|
407 switch(argv[i][1]) {
|
nuclear@3
|
408 case 'm':
|
nuclear@3
|
409 xfer_mean = strtod(argv[++i], &endp);
|
nuclear@3
|
410 if(endp == argv[i]) {
|
nuclear@3
|
411 fprintf(stderr, "-m must be followed by the transfer function mean\n");
|
nuclear@3
|
412 return -1;
|
nuclear@3
|
413 }
|
nuclear@2
|
414 break;
|
nuclear@3
|
415
|
nuclear@4
|
416 case 'd':
|
nuclear@3
|
417 xfer_sdev = strtod(argv[++i], &endp);
|
nuclear@3
|
418 if(endp == argv[i]) {
|
nuclear@4
|
419 fprintf(stderr, "-d must be followed by the transfer function std.deviation\n");
|
nuclear@3
|
420 return -1;
|
nuclear@3
|
421 }
|
nuclear@3
|
422 break;
|
nuclear@3
|
423
|
nuclear@3
|
424 default:
|
nuclear@3
|
425 fprintf(stderr, "unrecognized option: %s\n", argv[i]);
|
nuclear@3
|
426 return -1;
|
nuclear@2
|
427 }
|
nuclear@2
|
428 } else {
|
nuclear@9
|
429 if(fname) {
|
nuclear@9
|
430 fprintf(stderr, "unexpected argument: %s\n", argv[i]);
|
nuclear@2
|
431 return -1;
|
nuclear@2
|
432 }
|
nuclear@9
|
433 fname = argv[i];
|
nuclear@0
|
434 }
|
nuclear@0
|
435 }
|
nuclear@0
|
436
|
nuclear@9
|
437 if(!fname) {
|
nuclear@9
|
438 fprintf(stderr, "pass the volume descriptor filename\n");
|
nuclear@0
|
439 return -1;
|
nuclear@0
|
440 }
|
nuclear@0
|
441 return 0;
|
nuclear@0
|
442 }
|
nuclear@11
|
443 #endif
|
nuclear@0
|
444
|
nuclear@0
|
445
|
nuclear@11
|
446 static void create_ray_texture(int xsz, int ysz, float vfov, Vector2 *tex_scale)
|
nuclear@0
|
447 {
|
nuclear@1
|
448 int cur_tex_xsz, cur_tex_ysz;
|
nuclear@0
|
449 int tex_xsz = round_pow2(xsz);
|
nuclear@0
|
450 int tex_ysz = round_pow2(ysz);
|
nuclear@0
|
451 float *teximg, *dir;
|
nuclear@0
|
452
|
nuclear@11
|
453 teximg = new float[3 * xsz * ysz];
|
nuclear@0
|
454 dir = teximg;
|
nuclear@0
|
455
|
nuclear@11
|
456 for(int i=0; i<ysz; i++) {
|
nuclear@11
|
457 for(int j=0; j<xsz; j++) {
|
nuclear@11
|
458 Vector3 rdir = get_primary_ray_dir(j, i, xsz, ysz, vfov);
|
nuclear@1
|
459 *dir++ = rdir.x;
|
nuclear@1
|
460 *dir++ = rdir.y;
|
nuclear@1
|
461 *dir++ = rdir.z;
|
nuclear@0
|
462 }
|
nuclear@0
|
463 }
|
nuclear@0
|
464
|
nuclear@1
|
465 if(!ray_tex) {
|
nuclear@1
|
466 glGenTextures(1, &ray_tex);
|
nuclear@1
|
467 }
|
nuclear@1
|
468
|
nuclear@1
|
469 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &cur_tex_xsz);
|
nuclear@1
|
470 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &cur_tex_ysz);
|
nuclear@1
|
471
|
nuclear@1
|
472 if(tex_xsz > cur_tex_xsz || tex_ysz > cur_tex_ysz) {
|
nuclear@1
|
473 glBindTexture(GL_TEXTURE_2D, ray_tex);
|
nuclear@1
|
474 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
nuclear@1
|
475 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
nuclear@1
|
476 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
|
nuclear@1
|
477 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
|
nuclear@1
|
478 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F_ARB, tex_xsz, tex_ysz, 0, GL_RGB, GL_FLOAT, 0);
|
nuclear@1
|
479 }
|
nuclear@1
|
480
|
nuclear@1
|
481 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, xsz, ysz, GL_RGB, GL_FLOAT, teximg);
|
nuclear@11
|
482 delete [] teximg;
|
nuclear@0
|
483
|
nuclear@0
|
484 if(tex_scale) {
|
nuclear@0
|
485 tex_scale->x = (float)xsz / (float)tex_xsz;
|
nuclear@0
|
486 tex_scale->y = (float)ysz / (float)tex_ysz;
|
nuclear@0
|
487 }
|
nuclear@11
|
488 raytex_needs_recalc = false;
|
nuclear@0
|
489 }
|
nuclear@0
|
490
|
nuclear@11
|
491 static Vector3 get_primary_ray_dir(int x, int y, int w, int h, float vfov_deg)
|
nuclear@0
|
492 {
|
nuclear@0
|
493 float vfov = M_PI * vfov_deg / 180.0;
|
nuclear@0
|
494 float aspect = (float)w / (float)h;
|
nuclear@0
|
495
|
nuclear@0
|
496 float ysz = 2.0;
|
nuclear@0
|
497 float xsz = aspect * ysz;
|
nuclear@0
|
498
|
nuclear@0
|
499 float px = ((float)x / (float)w) * xsz - xsz / 2.0;
|
nuclear@0
|
500 float py = 1.0 - ((float)y / (float)h) * ysz;
|
nuclear@0
|
501 float pz = 1.0 / tan(0.5 * vfov);
|
nuclear@0
|
502
|
nuclear@0
|
503 float mag = sqrt(px * px + py * py + pz * pz);
|
nuclear@11
|
504 return Vector3(px / mag, py / mag, pz / mag);
|
nuclear@0
|
505 }
|
nuclear@0
|
506
|
nuclear@0
|
507 static int round_pow2(int x)
|
nuclear@0
|
508 {
|
nuclear@0
|
509 x--;
|
nuclear@0
|
510 x = (x >> 1) | x;
|
nuclear@0
|
511 x = (x >> 2) | x;
|
nuclear@0
|
512 x = (x >> 4) | x;
|
nuclear@0
|
513 x = (x >> 8) | x;
|
nuclear@0
|
514 x = (x >> 16) | x;
|
nuclear@0
|
515 return x + 1;
|
nuclear@0
|
516 }
|
nuclear@0
|
517
|
nuclear@3
|
518 static void create_transfer_map(float mean, float sdev)
|
nuclear@3
|
519 {
|
nuclear@3
|
520 static float map[XFER_MAP_SZ];
|
nuclear@3
|
521
|
nuclear@3
|
522 if(!xfer_tex) {
|
nuclear@3
|
523 glGenTextures(1, &xfer_tex);
|
nuclear@3
|
524 glBindTexture(GL_TEXTURE_1D, xfer_tex);
|
nuclear@3
|
525 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
nuclear@3
|
526 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
nuclear@3
|
527 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
nuclear@3
|
528 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
nuclear@3
|
529 glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE32F_ARB, XFER_MAP_SZ, 0, GL_LUMINANCE, GL_FLOAT, 0);
|
nuclear@3
|
530 }
|
nuclear@3
|
531
|
nuclear@11
|
532 for(int i=0; i<XFER_MAP_SZ; i++) {
|
nuclear@4
|
533 float x = (float)i / (float)(XFER_MAP_SZ - 1);
|
nuclear@4
|
534 map[i] = gaussian(x, mean, sdev) - 1.0;
|
nuclear@3
|
535 }
|
nuclear@3
|
536
|
nuclear@4
|
537 glTexSubImage1D(GL_TEXTURE_1D, 0, 0, XFER_MAP_SZ, GL_LUMINANCE, GL_FLOAT, map);
|
nuclear@11
|
538 xfertex_needs_recalc = false;
|
nuclear@3
|
539 }
|