oculus1

view sdr.glsl @ 13:464e1d135d68

hurraaaay!
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 21 Sep 2013 03:00:47 +0300
parents
children cceffea995a4
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, out float dbgrad);
7 float barrel_scale(float x, in vec4 k);
9 void main()
10 {
11 float dbgrad;
12 vec2 tc = distort_texcoords(gl_TexCoord[0].xy, dbgrad);
14 gl_FragColor.rgb = texture2D(tex, tc).rgb;
15 gl_FragColor.rgb += mix(vec3(1.0, 1.0, 0.0), vec3(0.0), smoothstep(0.01, 0.05, dbgrad));
16 gl_FragColor.a = 1.0;
17 }
19 vec2 distort_texcoords(in vec2 tc, out float dbgrad)
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 dbgrad = length(pt) * rad;
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 }