rayfract

view src/rayfract.cc @ 8:dfe7c98cf373

added stereo rendering
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 03 Nov 2011 02:18:46 +0200
parents 8a9aa21b32cf
children 628e7084a482
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <math.h>
5 #include <assert.h>
6 #include <GL/glew.h>
7 #include <GL/glut.h>
8 #include "sdr.h"
9 #include "gui.h"
10 #include "vmath.h"
12 #define DEG_TO_RAD(x) (M_PI * (x) / 180.0)
14 void disp();
15 void render();
16 void reshape(int x, int y);
17 void keyb(unsigned char key, int x, int y);
18 void keyb_up(unsigned char key, int x, int y);
19 void mouse(int bn, int state, int x, int y);
20 void motion(int x, int y);
21 void passive_motion(int x, int y);
22 void sball_motion(int x, int y, int z);
23 void sball_rot(int x, int y, int z);
24 void sball_button(int bn, int state);
27 int load_shader();
28 unsigned int create_ray_texture(int xsz, int ysz, float vfov, Vector2 *tex_scale = 0);
29 static Vector3 get_primary_ray_dir(int x, int y, int w, int h, float vfov_deg);
30 static int round_pow2(int x);
32 int parse_opt(int argc, char **argv);
35 float cam_theta = 0, cam_phi = 0, cam_dist = 4.0;
36 float cam_x, cam_y, cam_z;
38 unsigned int sdr;
39 unsigned int ray_tex;
40 Vector2 tex_scale;
41 Vector4 seed;
42 float err_thres = 0.0075;
43 int iter = 10;
44 float reflectivity = 0.2;
45 Vector3 color(0.75, 0.75, 0.75);
47 int use_stereo;
48 float eye_sep = 0.5;
50 int main(int argc, char **argv)
51 {
52 int xsz, ysz;
54 seed = Vector4(0.4, 0.0, 0.0, -0.8);
56 glutInitWindowSize(640, 480);
57 glutInit(&argc, argv);
59 if(parse_opt(argc, argv) == -1) {
60 return 1;
61 }
63 glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | (use_stereo ? GLUT_STEREO : 0));
64 glutCreateWindow("Raytraced Fractals");
65 xsz = glutGet(GLUT_WINDOW_WIDTH);
66 ysz = glutGet(GLUT_WINDOW_HEIGHT);
68 glutDisplayFunc(disp);
69 glutReshapeFunc(reshape);
70 glutKeyboardFunc(keyb);
71 glutKeyboardUpFunc(keyb_up);
72 glutMouseFunc(mouse);
73 glutMotionFunc(motion);
74 glutPassiveMotionFunc(passive_motion);
75 glutSpaceballMotionFunc(sball_motion);
76 glutSpaceballRotateFunc(sball_rot);
77 glutSpaceballButtonFunc(sball_button);
79 if(getenv("STEREO_METHOD") && strcmp(getenv("STEREO_METHOD"), "sequential") == 0) {
80 glutIdleFunc(glutPostRedisplay);
81 }
83 glewInit();
85 glEnable(GL_CULL_FACE);
87 if(load_shader() == -1) {
88 return 1;
89 }
91 if(gui_init(xsz, ysz) == -1) {
92 return 1;
93 }
95 glutMainLoop();
96 return 0;
97 }
99 int load_shader()
100 {
101 if(sdr) {
102 free_program(sdr);
103 }
105 if(!(sdr = create_program_load("sdr/sdr.v.glsl", "sdr/julia.p.glsl"))) {
106 return -1;
107 }
108 set_uniform_float4(sdr, "seed", seed.x, seed.y, seed.z, seed.w);
109 set_uniform_float(sdr, "err_thres", err_thres);
110 set_uniform_int(sdr, "iter", iter);
111 set_uniform_float(sdr, "reflectivity", reflectivity);
112 set_uniform_float3(sdr, "diffuse_color", color.x, color.y, color.z);
114 return 0;
115 }
117 void disp()
118 {
119 glMatrixMode(GL_MODELVIEW);
120 glLoadIdentity();
121 glTranslatef(cam_x, cam_y, -cam_z);
122 glRotatef(cam_theta, 0, 1, 0);
123 glRotatef(cam_phi, 1, 0, 0);
124 glTranslatef(0, 0, -cam_dist);
126 float lpos[] = {-1, 1, 3, 0};
127 glLightfv(GL_LIGHT0, GL_POSITION, lpos);
130 set_uniform_float4(sdr, "seed", seed.x, seed.y, seed.z, seed.w);
131 set_uniform_float(sdr, "reflectivity", reflectivity);
132 set_uniform_int(sdr, "iter", iter);
133 set_uniform_float(sdr, "err_thres", err_thres);
134 set_uniform_float3(sdr, "diffuse_color", color.x, color.y, color.z);
136 glBindTexture(GL_TEXTURE_2D, ray_tex);
139 if(use_stereo) {
140 glDrawBuffer(GL_BACK_LEFT);
141 set_uniform_float(sdr, "eye_offs", -eye_sep / 2.0);
142 } else {
143 set_uniform_float(sdr, "eye_offs", 0);
144 }
146 render();
148 if(use_stereo) {
149 glDrawBuffer(GL_BACK_RIGHT);
151 set_uniform_float(sdr, "eye_offs", eye_sep / 2.0);
152 render();
153 }
155 glutSwapBuffers();
156 assert(glGetError() == GL_NO_ERROR);
157 }
159 void render()
160 {
161 glMatrixMode(GL_TEXTURE);
162 glPushMatrix();
163 glScalef(tex_scale.x, tex_scale.y, 1.0);
165 glEnable(GL_TEXTURE_2D);
166 bind_program(sdr);
168 glBegin(GL_QUADS);
169 glColor3f(1, 1, 1);
170 glTexCoord2f(0, 1); glVertex2f(-1, -1);
171 glTexCoord2f(1, 1); glVertex2f(1, -1);
172 glTexCoord2f(1, 0); glVertex2f(1, 1);
173 glTexCoord2f(0, 0); glVertex2f(-1, 1);
174 glEnd();
176 bind_program(0);
177 glDisable(GL_TEXTURE_2D);
179 glMatrixMode(GL_TEXTURE);
180 glPopMatrix();
182 gui_draw();
183 }
185 void reshape(int x, int y)
186 {
187 glViewport(0, 0, x, y);
188 glMatrixMode(GL_PROJECTION);
189 glLoadIdentity();
190 gluPerspective(45.0, (float)x / (float)y, 1.0, 1000.0);
192 imtk_set_viewport(x, y);
194 if(ray_tex) {
195 glDeleteTextures(1, &ray_tex);
196 }
197 ray_tex = create_ray_texture(x, y, 50.0, &tex_scale);
198 }
201 void keyb(unsigned char key, int x, int y)
202 {
203 switch(key) {
204 case 27:
205 exit(0);
207 case '-':
208 if(iter > 1) {
209 iter--;
210 set_uniform_int(sdr, "iter", iter);
211 printf("iter: %d\n", iter);
212 glutPostRedisplay();
213 }
214 break;
216 case '=':
217 iter++;
218 set_uniform_int(sdr, "iter", iter);
219 printf("iter: %d\n", iter);
220 glutPostRedisplay();
221 break;
223 case ',':
224 err_thres -= 0.001;
225 set_uniform_float(sdr, "err_thres", err_thres);
226 printf("maximum error: %f\n", err_thres);
227 glutPostRedisplay();
228 break;
230 case '.':
231 err_thres += 0.001;
232 set_uniform_float(sdr, "err_thres", err_thres);
233 printf("maximum error: %f\n", err_thres);
234 glutPostRedisplay();
235 break;
237 case 's':
238 load_shader();
239 glutPostRedisplay();
240 break;
242 case 'S':
243 use_stereo = !use_stereo;
244 glutPostRedisplay();
245 break;
247 case 'r':
248 reflectivity = reflectivity > 0.0 ? 0.0 : 0.6;
249 set_uniform_float(sdr, "reflectivity", reflectivity);
250 glutPostRedisplay();
251 break;
253 case '`':
254 {
255 static bool vis = true;
256 vis = !vis;
257 gui_set_visible(vis);
258 glutPostRedisplay();
259 }
260 break;
262 case '[':
263 eye_sep -= 0.1;
264 printf("eye separation: %f\n", eye_sep);
265 glutPostRedisplay();
266 break;
268 case ']':
269 eye_sep += 0.1;
270 printf("eye separation: %f\n", eye_sep);
271 glutPostRedisplay();
272 break;
275 case '\n':
276 case '\r':
277 printf("(%.3f %+.3fi %+.3fj %+.3fk) i:%d err: %.4f cam(theta: %.2f phi: %.2f rad: %.2f)\n", seed.w,
278 seed.x, seed.y, seed.z, iter, err_thres, cam_theta, cam_phi, cam_dist);
279 break;
280 }
282 imtk_inp_key(key, 1);
283 glutPostRedisplay();
284 }
286 void keyb_up(unsigned char key, int x, int y)
287 {
288 imtk_inp_key(key, 0);
289 glutPostRedisplay();
290 }
292 int bnstate[16];
294 int prev_x = -1, prev_y;
295 void mouse(int bn, int state, int x, int y)
296 {
297 int mod;
299 mod = glutGetModifiers();
301 if(mod) {
302 bnstate[bn] = state == GLUT_DOWN ? 1 : 0;
303 if(state == GLUT_DOWN) {
304 if(bn == 3) {
305 cam_dist -= 0.1;
306 glutPostRedisplay();
307 if(cam_dist < 0) cam_dist = 0;
308 } else if(bn == 4) {
309 cam_dist += 0.1;
310 glutPostRedisplay();
311 } else {
312 prev_x = x;
313 prev_y = y;
314 }
315 } else {
316 prev_x = -1;
317 }
318 } else {
319 imtk_inp_mouse(bn, state == GLUT_DOWN ? 1 : 0);
320 }
321 glutPostRedisplay();
322 }
324 void motion(int x, int y)
325 {
326 if(bnstate[0]) {
327 cam_theta += (x - prev_x) * 0.5;
328 cam_phi += (y - prev_y) * 0.5;
330 if(cam_phi < -90) cam_phi = -90;
331 if(cam_phi > 90) cam_phi = 90;
333 glutPostRedisplay();
334 }
336 if(bnstate[1]) {
337 cam_y += (y - prev_y) * 0.1;
338 glutPostRedisplay();
339 }
341 if(bnstate[2]) {
342 cam_dist += (y - prev_y) * 0.1;
343 if(cam_dist < 0.0) cam_dist = 0.0;
344 glutPostRedisplay();
345 }
347 prev_x = x;
348 prev_y = y;
350 imtk_inp_motion(x, y);
351 glutPostRedisplay();
352 }
354 void passive_motion(int x, int y)
355 {
356 imtk_inp_motion(x, y);
357 glutPostRedisplay();
358 }
360 void sball_motion(int x, int y, int z)
361 {
362 float dx = (float)x * 0.0015f;
363 float dy = (float)y * 0.0015f;
364 float dz = -(float)z * 0.001f;
365 float angle = -DEG_TO_RAD(cam_theta);
367 cam_x += cos(angle) * dx + sin(angle) * dz;
368 cam_z += -sin(angle) * dx + cos(angle) * dz;
369 cam_y += dy;
371 glutPostRedisplay();
372 }
374 void sball_rot(int x, int y, int z)
375 {
376 cam_theta += -y / 15.0;
377 cam_phi += -x / 15.0;
378 glutPostRedisplay();
379 }
381 void sball_button(int bn, int state)
382 {
383 if(state == GLUT_DOWN) {
384 switch(bn) {
385 case 0:
386 /* TODO reset */
387 break;
389 default:
390 break;
391 }
392 }
393 }
396 unsigned int create_ray_texture(int xsz, int ysz, float vfov, Vector2 *tex_scale)
397 {
398 unsigned int tex;
399 int tex_xsz = round_pow2(xsz);
400 int tex_ysz = round_pow2(ysz);
401 float *teximg, *dir;
403 teximg = new float[3 * tex_xsz * tex_ysz];
404 dir = teximg;
406 for(int i=0; i<tex_ysz; i++) {
407 for(int j=0; j<tex_xsz; j++) {
408 if(j < xsz && i < ysz) {
409 Vector3 rdir = get_primary_ray_dir(j, i, xsz, ysz, vfov);
410 dir[0] = rdir.x;
411 dir[1] = rdir.y;
412 dir[2] = rdir.z;
413 } else {
414 dir[0] = dir[1] = 0.0f;
415 dir[2] = 1.0f;
416 }
418 dir += 3;
419 }
420 }
422 glGenTextures(1, &tex);
423 glBindTexture(GL_TEXTURE_2D, tex);
424 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
425 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
426 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
427 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
428 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F_ARB, tex_xsz, tex_ysz, 0, GL_RGB, GL_FLOAT, teximg);
429 delete [] teximg;
431 if(tex_scale) {
432 tex_scale->x = (float)xsz / (float)tex_xsz;
433 tex_scale->y = (float)ysz / (float)tex_ysz;
434 }
435 return tex;
436 }
438 static Vector3 get_primary_ray_dir(int x, int y, int w, int h, float vfov_deg)
439 {
440 float vfov = M_PI * vfov_deg / 180.0;
441 float aspect = (float)w / (float)h;
443 float ysz = 2.0;
444 float xsz = aspect * ysz;
446 float px = ((float)x / (float)w) * xsz - xsz / 2.0;
447 float py = 1.0 - ((float)y / (float)h) * ysz;
448 float pz = 1.0 / tan(0.5 * vfov);
450 float mag = sqrt(px * px + py * py + pz * pz);
452 return Vector3(px / mag, py / mag, pz / mag);
453 }
455 static int round_pow2(int x)
456 {
457 x--;
458 x = (x >> 1) | x;
459 x = (x >> 2) | x;
460 x = (x >> 4) | x;
461 x = (x >> 8) | x;
462 x = (x >> 16) | x;
463 return x + 1;
464 }
466 int parse_opt(int argc, char **argv)
467 {
468 int i;
470 for(i=1; i<argc; i++) {
471 if(argv[i][0] == '-' && argv[i][2] == 0) {
472 switch(argv[i][1]) {
473 case 's':
474 use_stereo = !use_stereo;
475 break;
477 default:
478 goto invalid;
479 }
480 } else {
481 goto invalid;
482 }
483 }
485 return 0;
487 invalid:
488 fprintf(stderr, "invalid option: %s\n", argv[i]);
489 return -1;
490 }