oculus1

diff 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 diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/sdr.glsl	Sat Sep 21 03:00:47 2013 +0300
     1.3 @@ -0,0 +1,44 @@
     1.4 +uniform sampler2D tex;
     1.5 +uniform float aspect, scale;
     1.6 +uniform float lens_center_offset;
     1.7 +uniform vec4 dist_factors;
     1.8 +
     1.9 +vec2 distort_texcoords(in vec2 tc, out float dbgrad);
    1.10 +float barrel_scale(float x, in vec4 k);
    1.11 +
    1.12 +void main()
    1.13 +{
    1.14 +	float dbgrad;
    1.15 +	vec2 tc = distort_texcoords(gl_TexCoord[0].xy, dbgrad);
    1.16 +
    1.17 +	gl_FragColor.rgb = texture2D(tex, tc).rgb;
    1.18 +	gl_FragColor.rgb += mix(vec3(1.0, 1.0, 0.0), vec3(0.0), smoothstep(0.01, 0.05, dbgrad));
    1.19 +	gl_FragColor.a = 1.0;
    1.20 +}
    1.21 +
    1.22 +vec2 distort_texcoords(in vec2 tc, out float dbgrad)
    1.23 +{
    1.24 +	// map tc [0, 1] -> [-1, 1]
    1.25 +	vec2 pt = tc * 2.0 - 1.0;
    1.26 +
    1.27 +	pt.x += lens_center_offset * 2.0;
    1.28 +	pt.y /= aspect;	// correct for aspect ratio
    1.29 +
    1.30 +	float rad = barrel_scale(dot(pt, pt), dist_factors);
    1.31 +	pt *= rad;	// scale the point by the computer distortion radius
    1.32 +
    1.33 +	dbgrad = length(pt) * rad;
    1.34 +
    1.35 +	pt.y *= aspect;
    1.36 +	pt.x -= lens_center_offset * 2.0;
    1.37 +
    1.38 +	// map back to range [0, 1]
    1.39 +	return pt * 0.5 + 0.5;
    1.40 +}
    1.41 +
    1.42 +float barrel_scale(float rad, in vec4 k)
    1.43 +{
    1.44 +	float radsq = rad * rad;
    1.45 +	float radquad = radsq * radsq;
    1.46 +	return k.x + k.y * radsq + k.z * radquad + k.w * radquad * radsq;
    1.47 +}