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