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