fixed: auto_ptr -> unique_ptr
[opensg.git] / Examples / Advanced / DeferredShading / DSSpotLight.fp.glsl
blob4988c1e8cedd029e83a271e71832b4003fed9a85
1 #version 120
3 #extension GL_ARB_texture_rectangle : require
4 #extension GL_ARB_texture_rectangle : enable
6 // compute spot light INDEX for fragment at POS with normal NORM
7 // and diffuse material color MDIFF
8 vec4 computeSpotLight(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 spotEffect = dot(-lightDir, gl_LightSource[index].spotDirection);
16     if(spotEffect > gl_LightSource[index].spotCosCutoff)
17     {
18         float NdotL = max(dot(lightDir, norm), 0.);
19         
20         if(NdotL > 0.)
21         {
22             float lightDist = length(lightDirUN);
23             float att       = dot(vec3(gl_LightSource[index].constantAttenuation,
24                                        gl_LightSource[index].linearAttenuation,
25                                        gl_LightSource[index].quadraticAttenuation),
26                                   vec3(1., lightDist, lightDist * lightDist)       );
27             spotEffect = pow(spotEffect, gl_LightSource[index].spotExponent);
28             att        = spotEffect / att;
30             color = att * NdotL * mDiff * gl_LightSource[index].diffuse;
31         }
32     }
33     
34     return color;
37 // DS input buffers
38 uniform sampler2DRect     texBufPos;
39 uniform sampler2DRect     texBufNorm;
40 uniform sampler2DRect     texBufDiff;
41 uniform vec2              vpOffset;
43 // DS pass
44 void main(void)
46     vec2  lookup = gl_FragCoord.xy - vpOffset;
47     vec3  norm   = texture2DRect(texBufNorm, lookup).xyz;
49     if(dot(norm, norm) < 0.95)
50     {
51         discard;
52     }
53     else
54     {
55         vec4  posAmb = texture2DRect(texBufPos,  lookup);
56         vec3  pos    = posAmb.xyz;
57         float amb    = posAmb.w;
58         vec4  mDiff  = texture2DRect(texBufDiff, lookup);
60         gl_FragColor = computeSpotLight(0, pos, norm, mDiff);
61     }