oculus1

view sdr/sdr.glsl @ 21:ef4c9d8eeca7

added shaderless distortion method
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 02 Oct 2013 04:09:37 +0300
parents f3672317e5c2
children
line source
1 uniform sampler2D tex;
2 uniform float aspect, scale;
3 uniform float lens_center_offset;
4 uniform vec4 dist_factors;
5 uniform vec2 tex_scale;
7 vec2 distort_texcoords(in vec2 tc);
8 float barrel_scale(float x, in vec4 k);
10 void main()
11 {
12 vec2 tc = distort_texcoords(gl_TexCoord[0].xy);
14 float vis = any(greaterThan(tc, vec2(1.0))) || any(lessThan(tc, vec2(0.0))) ? 0.0 : 1.0;
16 gl_FragColor.rgb = texture2D(tex, tc * tex_scale).rgb * vis;
17 gl_FragColor.a = 1.0;
18 }
20 vec2 distort_texcoords(in vec2 tc)
21 {
22 // map tc [0, 1] -> [-1, 1]
23 vec2 pt = tc * 2.0 - 1.0;
25 pt.x += lens_center_offset * 2.0;
26 pt.y /= aspect; // correct for aspect ratio
28 float rad = barrel_scale(dot(pt, pt), dist_factors);
29 pt *= rad; // scale the point by the computer distortion radius
31 pt /= scale;
32 pt.y *= aspect;
33 pt.x -= lens_center_offset * 2.0;
35 // map back to range [0, 1]
36 return pt * 0.5 + 0.5;
37 }
39 float barrel_scale(float rad, in vec4 k)
40 {
41 float radsq = rad * rad;
42 float radquad = radsq * radsq;
43 return k.x + k.y * radsq + k.z * radquad + k.w * radquad * radsq;
44 }