annotate sdr/sdr.glsl @ 16:f3672317e5c2
made teapots many sizes and more colorful, added phong shader
author |
John Tsiombikas <nuclear@member.fsf.org> |
date |
Sat, 21 Sep 2013 05:22:48 +0300 |
parents |
sdr.glsl@cceffea995a4 |
children |
1b107de821c1 |
rev |
line source |
nuclear@13
|
1 uniform sampler2D tex;
|
nuclear@13
|
2 uniform float aspect, scale;
|
nuclear@13
|
3 uniform float lens_center_offset;
|
nuclear@13
|
4 uniform vec4 dist_factors;
|
nuclear@13
|
5
|
nuclear@14
|
6 vec2 distort_texcoords(in vec2 tc);
|
nuclear@13
|
7 float barrel_scale(float x, in vec4 k);
|
nuclear@13
|
8
|
nuclear@13
|
9 void main()
|
nuclear@13
|
10 {
|
nuclear@14
|
11 vec2 tc = distort_texcoords(gl_TexCoord[0].xy);
|
nuclear@13
|
12
|
nuclear@14
|
13 float vis = any(greaterThan(tc, vec2(1.0)) || lessThan(tc, vec2(0.0))) ? 0.0 : 1.0;
|
nuclear@14
|
14
|
nuclear@14
|
15 gl_FragColor.rgb = texture2D(tex, tc).rgb * vis;
|
nuclear@13
|
16 gl_FragColor.a = 1.0;
|
nuclear@13
|
17 }
|
nuclear@13
|
18
|
nuclear@14
|
19 vec2 distort_texcoords(in vec2 tc)
|
nuclear@13
|
20 {
|
nuclear@13
|
21 // map tc [0, 1] -> [-1, 1]
|
nuclear@13
|
22 vec2 pt = tc * 2.0 - 1.0;
|
nuclear@13
|
23
|
nuclear@13
|
24 pt.x += lens_center_offset * 2.0;
|
nuclear@13
|
25 pt.y /= aspect; // correct for aspect ratio
|
nuclear@13
|
26
|
nuclear@13
|
27 float rad = barrel_scale(dot(pt, pt), dist_factors);
|
nuclear@13
|
28 pt *= rad; // scale the point by the computer distortion radius
|
nuclear@13
|
29
|
nuclear@14
|
30 pt /= scale;
|
nuclear@13
|
31 pt.y *= aspect;
|
nuclear@13
|
32 pt.x -= lens_center_offset * 2.0;
|
nuclear@13
|
33
|
nuclear@13
|
34 // map back to range [0, 1]
|
nuclear@13
|
35 return pt * 0.5 + 0.5;
|
nuclear@13
|
36 }
|
nuclear@13
|
37
|
nuclear@13
|
38 float barrel_scale(float rad, in vec4 k)
|
nuclear@13
|
39 {
|
nuclear@13
|
40 float radsq = rad * rad;
|
nuclear@13
|
41 float radquad = radsq * radsq;
|
nuclear@13
|
42 return k.x + k.y * radsq + k.z * radquad + k.w * radquad * radsq;
|
nuclear@13
|
43 }
|