fixed: auto_ptr -> unique_ptr
[opensg.git] / Examples / Advanced / DeferredShading / DSPointLight.fp.glsl
blobec2d874c79ce71759d4f701fddde40c8e4ac2469
1 #version 120
3 #extension GL_ARB_texture_rectangle : require
4 #extension GL_ARB_texture_rectangle : enable
6 // compute point light INDEX for fragment at POS with normal NORM
7 // and diffuse material color MDIFF
8 vec4 computePointLight(int index, vec3 pos, vec3 norm, vec4 mDiff)
10     vec4  color      = vec4(0., 0., 0., 0.);
12     vec3  lightDirUN = gl_LightSource[index].position.xyz - pos;
13     vec3  lightDir   = normalize(lightDirUN);
14     float NdotL      = max(dot(norm, lightDir), 0.);
16     if(NdotL > 0.)
17     {
18         float lightDist = length   (lightDirUN);
19         float distAtt   = dot(vec3(gl_LightSource[index].constantAttenuation,
20                                    gl_LightSource[index].linearAttenuation,
21                                    gl_LightSource[index].quadraticAttenuation),
22                               vec3(1., lightDist, lightDist * lightDist));
23         distAtt = 1. / distAtt;
25         color = distAtt * NdotL * mDiff * gl_LightSource[index].diffuse;
26     }
28     return color;
31 // DS input buffers
32 uniform sampler2DRect texBufPos;
33 uniform sampler2DRect texBufNorm;
34 uniform sampler2DRect texBufDiff;
35 uniform vec2          vpOffset;
37 // DS pass
38 void main(void)
40     vec2 lookup = gl_FragCoord.xy - vpOffset;
41     vec3 norm   = texture2DRect(texBufNorm, lookup).xyz;
43     if(dot(norm, norm) < 0.95)
44     {
45         discard;
46     }
47     else
48     {
49         vec4  posAmb = texture2DRect(texBufPos,  lookup);
50         vec3  pos    = posAmb.xyz;
51         float amb    = posAmb.w;
52         vec4  mDiff  = texture2DRect(texBufDiff, lookup);
54         gl_FragColor = computePointLight(0, pos, norm, mDiff);
55     }