rayfract

changeset 8:dfe7c98cf373

added stereo rendering
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 03 Nov 2011 02:18:46 +0200
parents c27b24c9fdd2
children 628e7084a482
files sdr/julia.p.glsl src/rayfract.cc
diffstat 2 files changed, 90 insertions(+), 12 deletions(-) [+]
line diff
     1.1 --- a/sdr/julia.p.glsl	Wed Oct 05 02:26:46 2011 +0300
     1.2 +++ b/sdr/julia.p.glsl	Thu Nov 03 02:18:46 2011 +0200
     1.3 @@ -5,6 +5,7 @@
     1.4  uniform int iter;
     1.5  uniform float reflectivity;
     1.6  uniform vec3 diffuse_color;
     1.7 +uniform float eye_offs;
     1.8  
     1.9  #define quat(s, x, y, z)	vec4(x, y, z, s)
    1.10  #define quat_identity()		vec4(0.0, 0.0, 0.0, 1.0)
    1.11 @@ -298,7 +299,7 @@
    1.12  	Ray ray;
    1.13  	vec2 tc = gl_TexCoord[0].xy;
    1.14  	ray.dir = gl_NormalMatrix * normalize(texture2D(ray_tex, tc).xyz);
    1.15 -	ray.origin = (gl_ModelViewMatrix * vec4(0.0, 0.0, 0.0, 1.0)).xyz;
    1.16 +	ray.origin = (gl_ModelViewMatrix * vec4(eye_offs, 0.0, 0.0, 1.0)).xyz;
    1.17  	return ray;
    1.18  }
    1.19  
     2.1 --- a/src/rayfract.cc	Wed Oct 05 02:26:46 2011 +0300
     2.2 +++ b/src/rayfract.cc	Thu Nov 03 02:18:46 2011 +0200
     2.3 @@ -1,5 +1,6 @@
     2.4  #include <stdio.h>
     2.5  #include <stdlib.h>
     2.6 +#include <string.h>
     2.7  #include <math.h>
     2.8  #include <assert.h>
     2.9  #include <GL/glew.h>
    2.10 @@ -11,6 +12,7 @@
    2.11  #define DEG_TO_RAD(x)	(M_PI * (x) / 180.0)
    2.12  
    2.13  void disp();
    2.14 +void render();
    2.15  void reshape(int x, int y);
    2.16  void keyb(unsigned char key, int x, int y);
    2.17  void keyb_up(unsigned char key, int x, int y);
    2.18 @@ -27,6 +29,9 @@
    2.19  static Vector3 get_primary_ray_dir(int x, int y, int w, int h, float vfov_deg);
    2.20  static int round_pow2(int x);
    2.21  
    2.22 +int parse_opt(int argc, char **argv);
    2.23 +
    2.24 +
    2.25  float cam_theta = 0, cam_phi = 0, cam_dist = 4.0;
    2.26  float cam_x, cam_y, cam_z;
    2.27  
    2.28 @@ -39,6 +44,9 @@
    2.29  float reflectivity = 0.2;
    2.30  Vector3 color(0.75, 0.75, 0.75);
    2.31  
    2.32 +int use_stereo;
    2.33 +float eye_sep = 0.5;
    2.34 +
    2.35  int main(int argc, char **argv)
    2.36  {
    2.37  	int xsz, ysz;
    2.38 @@ -47,7 +55,12 @@
    2.39  
    2.40  	glutInitWindowSize(640, 480);
    2.41  	glutInit(&argc, argv);
    2.42 -	glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
    2.43 +
    2.44 +	if(parse_opt(argc, argv) == -1) {
    2.45 +		return 1;
    2.46 +	}
    2.47 +
    2.48 +	glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | (use_stereo ? GLUT_STEREO : 0));
    2.49  	glutCreateWindow("Raytraced Fractals");
    2.50  	xsz = glutGet(GLUT_WINDOW_WIDTH);
    2.51  	ysz = glutGet(GLUT_WINDOW_HEIGHT);
    2.52 @@ -63,13 +76,14 @@
    2.53  	glutSpaceballRotateFunc(sball_rot);
    2.54  	glutSpaceballButtonFunc(sball_button);
    2.55  
    2.56 -	glEnable(GL_DEPTH_TEST);
    2.57 -	glEnable(GL_LIGHTING);
    2.58 -	glEnable(GL_LIGHT0);
    2.59 -	glEnable(GL_CULL_FACE);
    2.60 +	if(getenv("STEREO_METHOD") && strcmp(getenv("STEREO_METHOD"), "sequential") == 0) {
    2.61 +		glutIdleFunc(glutPostRedisplay);
    2.62 +	}
    2.63  
    2.64  	glewInit();
    2.65  
    2.66 +	glEnable(GL_CULL_FACE);
    2.67 +
    2.68  	if(load_shader() == -1) {
    2.69  		return 1;
    2.70  	}
    2.71 @@ -102,8 +116,6 @@
    2.72  
    2.73  void disp()
    2.74  {
    2.75 -	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    2.76 -
    2.77  	glMatrixMode(GL_MODELVIEW);
    2.78  	glLoadIdentity();
    2.79  	glTranslatef(cam_x, cam_y, -cam_z);
    2.80 @@ -121,11 +133,35 @@
    2.81  	set_uniform_float(sdr, "err_thres", err_thres);
    2.82  	set_uniform_float3(sdr, "diffuse_color", color.x, color.y, color.z);
    2.83  
    2.84 +	glBindTexture(GL_TEXTURE_2D, ray_tex);
    2.85 +
    2.86 +
    2.87 +	if(use_stereo) {
    2.88 +		glDrawBuffer(GL_BACK_LEFT);
    2.89 +		set_uniform_float(sdr, "eye_offs", -eye_sep / 2.0);
    2.90 +	} else {
    2.91 +		set_uniform_float(sdr, "eye_offs", 0);
    2.92 +	}
    2.93 +
    2.94 +	render();
    2.95 +
    2.96 +	if(use_stereo) {
    2.97 +		glDrawBuffer(GL_BACK_RIGHT);
    2.98 +
    2.99 +		set_uniform_float(sdr, "eye_offs", eye_sep / 2.0);
   2.100 +		render();
   2.101 +	}
   2.102 +
   2.103 +	glutSwapBuffers();
   2.104 +	assert(glGetError() == GL_NO_ERROR);
   2.105 +}
   2.106 +
   2.107 +void render()
   2.108 +{
   2.109  	glMatrixMode(GL_TEXTURE);
   2.110  	glPushMatrix();
   2.111  	glScalef(tex_scale.x, tex_scale.y, 1.0);
   2.112  
   2.113 -	glBindTexture(GL_TEXTURE_2D, ray_tex);
   2.114  	glEnable(GL_TEXTURE_2D);
   2.115  	bind_program(sdr);
   2.116  
   2.117 @@ -144,9 +180,6 @@
   2.118  	glPopMatrix();
   2.119  
   2.120  	gui_draw();
   2.121 -
   2.122 -	glutSwapBuffers();
   2.123 -	assert(glGetError() == GL_NO_ERROR);
   2.124  }
   2.125  
   2.126  void reshape(int x, int y)
   2.127 @@ -206,6 +239,11 @@
   2.128  		glutPostRedisplay();
   2.129  		break;
   2.130  
   2.131 +	case 'S':
   2.132 +		use_stereo = !use_stereo;
   2.133 +		glutPostRedisplay();
   2.134 +		break;
   2.135 +
   2.136  	case 'r':
   2.137  		reflectivity = reflectivity > 0.0 ? 0.0 : 0.6;
   2.138  		set_uniform_float(sdr, "reflectivity", reflectivity);
   2.139 @@ -221,6 +259,19 @@
   2.140  		}
   2.141  		break;
   2.142  
   2.143 +	case '[':
   2.144 +		eye_sep -= 0.1;
   2.145 +		printf("eye separation: %f\n", eye_sep);
   2.146 +		glutPostRedisplay();
   2.147 +		break;
   2.148 +
   2.149 +	case ']':
   2.150 +		eye_sep += 0.1;
   2.151 +		printf("eye separation: %f\n", eye_sep);
   2.152 +		glutPostRedisplay();
   2.153 +		break;
   2.154 +
   2.155 +
   2.156  	case '\n':
   2.157  	case '\r':
   2.158  		printf("(%.3f %+.3fi %+.3fj %+.3fk) i:%d err: %.4f cam(theta: %.2f phi: %.2f rad: %.2f)\n", seed.w,
   2.159 @@ -411,3 +462,29 @@
   2.160  	x = (x >> 16) | x;
   2.161  	return x + 1;
   2.162  }
   2.163 +
   2.164 +int parse_opt(int argc, char **argv)
   2.165 +{
   2.166 +	int i;
   2.167 +
   2.168 +	for(i=1; i<argc; i++) {
   2.169 +		if(argv[i][0] == '-' && argv[i][2] == 0) {
   2.170 +			switch(argv[i][1]) {
   2.171 +			case 's':
   2.172 +				use_stereo = !use_stereo;
   2.173 +				break;
   2.174 +
   2.175 +			default:
   2.176 +				goto invalid;
   2.177 +			}
   2.178 +		} else {
   2.179 +			goto invalid;
   2.180 +		}
   2.181 +	}
   2.182 +
   2.183 +	return 0;
   2.184 +
   2.185 +invalid:
   2.186 +	fprintf(stderr, "invalid option: %s\n", argv[i]);
   2.187 +	return -1;
   2.188 +}