oculus1
view sdr.glsl @ 14:cceffea995a4
implemented wasd controls and clamping of the texcoords to [0, 1]
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sat, 21 Sep 2013 03:35:53 +0300 |
parents | 464e1d135d68 |
children |
line source
1 uniform sampler2D tex;
2 uniform float aspect, scale;
3 uniform float lens_center_offset;
4 uniform vec4 dist_factors;
6 vec2 distort_texcoords(in vec2 tc);
7 float barrel_scale(float x, in vec4 k);
9 void main()
10 {
11 vec2 tc = distort_texcoords(gl_TexCoord[0].xy);
13 float vis = any(greaterThan(tc, vec2(1.0)) || lessThan(tc, vec2(0.0))) ? 0.0 : 1.0;
15 gl_FragColor.rgb = texture2D(tex, tc).rgb * vis;
16 gl_FragColor.a = 1.0;
17 }
19 vec2 distort_texcoords(in vec2 tc)
20 {
21 // map tc [0, 1] -> [-1, 1]
22 vec2 pt = tc * 2.0 - 1.0;
24 pt.x += lens_center_offset * 2.0;
25 pt.y /= aspect; // correct for aspect ratio
27 float rad = barrel_scale(dot(pt, pt), dist_factors);
28 pt *= rad; // scale the point by the computer distortion radius
30 pt /= scale;
31 pt.y *= aspect;
32 pt.x -= lens_center_offset * 2.0;
34 // map back to range [0, 1]
35 return pt * 0.5 + 0.5;
36 }
38 float barrel_scale(float rad, in vec4 k)
39 {
40 float radsq = rad * rad;
41 float radquad = radsq * radsq;
42 return k.x + k.y * radsq + k.z * radquad + k.w * radquad * radsq;
43 }