# HG changeset patch # User John Tsiombikas # Date 1379721647 -10800 # Node ID 464e1d135d6815cfd5c30cf18583131da569e1a3 # Parent d797639e0234368668f9db6dc8febecbf4b2756c hurraaaay! diff -r d797639e0234 -r 464e1d135d68 sdr.glsl --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sdr.glsl Sat Sep 21 03:00:47 2013 +0300 @@ -0,0 +1,44 @@ +uniform sampler2D tex; +uniform float aspect, scale; +uniform float lens_center_offset; +uniform vec4 dist_factors; + +vec2 distort_texcoords(in vec2 tc, out float dbgrad); +float barrel_scale(float x, in vec4 k); + +void main() +{ + float dbgrad; + vec2 tc = distort_texcoords(gl_TexCoord[0].xy, dbgrad); + + gl_FragColor.rgb = texture2D(tex, tc).rgb; + gl_FragColor.rgb += mix(vec3(1.0, 1.0, 0.0), vec3(0.0), smoothstep(0.01, 0.05, dbgrad)); + gl_FragColor.a = 1.0; +} + +vec2 distort_texcoords(in vec2 tc, out float dbgrad) +{ + // map tc [0, 1] -> [-1, 1] + vec2 pt = tc * 2.0 - 1.0; + + pt.x += lens_center_offset * 2.0; + pt.y /= aspect; // correct for aspect ratio + + float rad = barrel_scale(dot(pt, pt), dist_factors); + pt *= rad; // scale the point by the computer distortion radius + + dbgrad = length(pt) * rad; + + pt.y *= aspect; + pt.x -= lens_center_offset * 2.0; + + // map back to range [0, 1] + return pt * 0.5 + 0.5; +} + +float barrel_scale(float rad, in vec4 k) +{ + float radsq = rad * rad; + float radquad = radsq * radsq; + return k.x + k.y * radsq + k.z * radquad + k.w * radquad * radsq; +} diff -r d797639e0234 -r 464e1d135d68 src/main.cc --- a/src/main.cc Fri Sep 20 10:14:29 2013 +0300 +++ b/src/main.cc Sat Sep 21 03:00:47 2013 +0300 @@ -11,6 +11,7 @@ static void disp(); static void draw_scene(); static void draw_teapot(); +static void draw_squares(); static void draw_grid(float size, float spacing); static void idle(); static void reshape(int x, int y); @@ -32,7 +33,6 @@ static int rtarg_width, rtarg_height; static unsigned int fbo, tex[2], zbuf; - int main(int argc, char **argv) { glutInitWindowSize(1280, 800); @@ -98,6 +98,8 @@ rtarg_height = height; } + printf("render target: %dx%d\n", rtarg_width, rtarg_height); + // create render targets for each eye GLenum wrap_mode = GL_CLAMP_TO_EDGE; if(!GLEW_SGIS_texture_edge_clamp) { @@ -142,9 +144,13 @@ cam.track_vr(); - glMatrixMode(GL_PROJECTION); + /*glMatrixMode(GL_PROJECTION); glLoadIdentity(); - gluPerspective(RAD_TO_DEG(vr_get_fov()), (float)rtarg_width / (float)rtarg_height, 0.5, 500.0); + float fov = RAD_TO_DEG(vr_get_fov()); + gluPerspective(fov, (float)rtarg_width / (float)rtarg_height, 0.25, 500.0);*/ + float proj_matrix[16]; + + float eye_dist = vr_get_eyedist(); glViewport(0, 0, rtarg_width, rtarg_height); @@ -156,10 +162,14 @@ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + glMatrixMode(GL_PROJECTION); + vr_get_proj_matrix(proj_matrix, VR_EYE_LEFT); + glLoadTransposeMatrixf(proj_matrix); + glMatrixMode(GL_MODELVIEW); glLoadIdentity(); + glTranslatef(eye_dist / 2.0, 0, 0); cam.use_inverse(); - glTranslatef(0.32, 0, 0); draw_scene(); @@ -169,10 +179,14 @@ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + glMatrixMode(GL_PROJECTION); + vr_get_proj_matrix(proj_matrix, VR_EYE_RIGHT); + glLoadTransposeMatrixf(proj_matrix); + glMatrixMode(GL_MODELVIEW); glLoadIdentity(); + glTranslatef(-eye_dist / 2.0, 0, 0); cam.use_inverse(); - glTranslatef(-0.32, 0, 0); draw_scene(); // return to the regular window framebuffer @@ -187,6 +201,8 @@ glutSwapBuffers(); assert(glGetError() == GL_NO_ERROR); + + glFinish(); } @@ -195,6 +211,8 @@ float lpos[] = {0, 60, 0, 1}; glLightfv(GL_LIGHT0, GL_POSITION, lpos); + draw_grid(100.0, 2.5); + static Vector2 teapos[] = { Vector2(-8, 8), Vector2(8, 8), Vector2(8, -8), Vector2(-8, -8) }; @@ -206,7 +224,7 @@ glPopMatrix(); } - draw_grid(100.0, 2.5); + draw_squares(); } static void draw_teapot() @@ -225,6 +243,37 @@ glFrontFace(GL_CCW); } +static void draw_squares() +{ + static const int num_sq = 4; + + glPushAttrib(GL_ENABLE_BIT | GL_LINE_BIT); + glDisable(GL_LIGHTING); + + glMatrixMode(GL_MODELVIEW); + glPushMatrix(); + glTranslatef(0, 1, 0); + + + glLineWidth(3.0); + glColor3f(1.0, 0.7, 0.2); + + float zdist = 2.0; + for(int i=0; i