# HG changeset patch # User John Tsiombikas # Date 1320279526 -7200 # Node ID dfe7c98cf373a1b65aad11bbb1f1eef18ad70b4f # Parent c27b24c9fdd2974efe1977beb8d0a55fabcdf588 added stereo rendering diff -r c27b24c9fdd2 -r dfe7c98cf373 sdr/julia.p.glsl --- a/sdr/julia.p.glsl Wed Oct 05 02:26:46 2011 +0300 +++ b/sdr/julia.p.glsl Thu Nov 03 02:18:46 2011 +0200 @@ -5,6 +5,7 @@ uniform int iter; uniform float reflectivity; uniform vec3 diffuse_color; +uniform float eye_offs; #define quat(s, x, y, z) vec4(x, y, z, s) #define quat_identity() vec4(0.0, 0.0, 0.0, 1.0) @@ -298,7 +299,7 @@ Ray ray; vec2 tc = gl_TexCoord[0].xy; ray.dir = gl_NormalMatrix * normalize(texture2D(ray_tex, tc).xyz); - ray.origin = (gl_ModelViewMatrix * vec4(0.0, 0.0, 0.0, 1.0)).xyz; + ray.origin = (gl_ModelViewMatrix * vec4(eye_offs, 0.0, 0.0, 1.0)).xyz; return ray; } diff -r c27b24c9fdd2 -r dfe7c98cf373 src/rayfract.cc --- a/src/rayfract.cc Wed Oct 05 02:26:46 2011 +0300 +++ b/src/rayfract.cc Thu Nov 03 02:18:46 2011 +0200 @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -11,6 +12,7 @@ #define DEG_TO_RAD(x) (M_PI * (x) / 180.0) void disp(); +void render(); void reshape(int x, int y); void keyb(unsigned char key, int x, int y); void keyb_up(unsigned char key, int x, int y); @@ -27,6 +29,9 @@ static Vector3 get_primary_ray_dir(int x, int y, int w, int h, float vfov_deg); static int round_pow2(int x); +int parse_opt(int argc, char **argv); + + float cam_theta = 0, cam_phi = 0, cam_dist = 4.0; float cam_x, cam_y, cam_z; @@ -39,6 +44,9 @@ float reflectivity = 0.2; Vector3 color(0.75, 0.75, 0.75); +int use_stereo; +float eye_sep = 0.5; + int main(int argc, char **argv) { int xsz, ysz; @@ -47,7 +55,12 @@ glutInitWindowSize(640, 480); glutInit(&argc, argv); - glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE); + + if(parse_opt(argc, argv) == -1) { + return 1; + } + + glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | (use_stereo ? GLUT_STEREO : 0)); glutCreateWindow("Raytraced Fractals"); xsz = glutGet(GLUT_WINDOW_WIDTH); ysz = glutGet(GLUT_WINDOW_HEIGHT); @@ -63,13 +76,14 @@ glutSpaceballRotateFunc(sball_rot); glutSpaceballButtonFunc(sball_button); - glEnable(GL_DEPTH_TEST); - glEnable(GL_LIGHTING); - glEnable(GL_LIGHT0); - glEnable(GL_CULL_FACE); + if(getenv("STEREO_METHOD") && strcmp(getenv("STEREO_METHOD"), "sequential") == 0) { + glutIdleFunc(glutPostRedisplay); + } glewInit(); + glEnable(GL_CULL_FACE); + if(load_shader() == -1) { return 1; } @@ -102,8 +116,6 @@ void disp() { - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(cam_x, cam_y, -cam_z); @@ -121,11 +133,35 @@ set_uniform_float(sdr, "err_thres", err_thres); set_uniform_float3(sdr, "diffuse_color", color.x, color.y, color.z); + glBindTexture(GL_TEXTURE_2D, ray_tex); + + + if(use_stereo) { + glDrawBuffer(GL_BACK_LEFT); + set_uniform_float(sdr, "eye_offs", -eye_sep / 2.0); + } else { + set_uniform_float(sdr, "eye_offs", 0); + } + + render(); + + if(use_stereo) { + glDrawBuffer(GL_BACK_RIGHT); + + set_uniform_float(sdr, "eye_offs", eye_sep / 2.0); + render(); + } + + glutSwapBuffers(); + assert(glGetError() == GL_NO_ERROR); +} + +void render() +{ glMatrixMode(GL_TEXTURE); glPushMatrix(); glScalef(tex_scale.x, tex_scale.y, 1.0); - glBindTexture(GL_TEXTURE_2D, ray_tex); glEnable(GL_TEXTURE_2D); bind_program(sdr); @@ -144,9 +180,6 @@ glPopMatrix(); gui_draw(); - - glutSwapBuffers(); - assert(glGetError() == GL_NO_ERROR); } void reshape(int x, int y) @@ -206,6 +239,11 @@ glutPostRedisplay(); break; + case 'S': + use_stereo = !use_stereo; + glutPostRedisplay(); + break; + case 'r': reflectivity = reflectivity > 0.0 ? 0.0 : 0.6; set_uniform_float(sdr, "reflectivity", reflectivity); @@ -221,6 +259,19 @@ } break; + case '[': + eye_sep -= 0.1; + printf("eye separation: %f\n", eye_sep); + glutPostRedisplay(); + break; + + case ']': + eye_sep += 0.1; + printf("eye separation: %f\n", eye_sep); + glutPostRedisplay(); + break; + + case '\n': case '\r': printf("(%.3f %+.3fi %+.3fj %+.3fk) i:%d err: %.4f cam(theta: %.2f phi: %.2f rad: %.2f)\n", seed.w, @@ -411,3 +462,29 @@ x = (x >> 16) | x; return x + 1; } + +int parse_opt(int argc, char **argv) +{ + int i; + + for(i=1; i