fixed: auto_ptr -> unique_ptr
[opensg.git] / Examples / Advanced / DeferredShading / DSSpotLightShadow.fp.glsl
blob0e6711b63fc53ce1bc96feb187e2c31415a5903b
1 #version 120
3 #extension GL_ARB_texture_rectangle : require
4 #extension GL_ARB_texture_rectangle : enable
6 // forward decls
7 vec4 OSG_SSME_FP_calcShadow(in vec4 ecFragPos);
9 // compute spot light INDEX for fragment at POS with normal NORM
10 // and diffuse material color MDIFF
11 vec4 computeSpotLight(int index, vec3 pos, vec3 norm, vec4 mDiff)
13     vec4 color = vec4(0., 0., 0., 0.);
15     vec3  lightDirUN = gl_LightSource[index].position.xyz - pos;
16     vec3  lightDir   = normalize(lightDirUN);
17     float spotEffect = dot(-lightDir, gl_LightSource[index].spotDirection);
19     if(spotEffect > gl_LightSource[index].spotCosCutoff)
20     {
21         float NdotL = max(dot(lightDir, norm), 0.);
22         
23         if(NdotL > 0.)
24         {
25             vec4  shadow    = OSG_SSME_FP_calcShadow(vec4(pos, 1.));
27             float lightDist = length(lightDirUN);
28             float att       = dot(vec3(gl_LightSource[index].constantAttenuation,
29                                        gl_LightSource[index].linearAttenuation,
30                                        gl_LightSource[index].quadraticAttenuation),
31                                   vec3(1., lightDist, lightDist * lightDist)       );
32             spotEffect = pow(spotEffect, gl_LightSource[index].spotExponent);
33             att        = spotEffect / att;
35             color = shadow * att * NdotL * mDiff * gl_LightSource[index].diffuse;
36         }
37     }
38     
39     return color;
42 // DS input buffers
43 uniform sampler2DRect     texBufPos;
44 uniform sampler2DRect     texBufNorm;
45 uniform sampler2DRect     texBufDiff;
47 uniform vec2              vpOffset;
49 // DS pass
50 void main(void)
52     vec2 lookup = gl_FragCoord.xy - vpOffset;
53     vec3 norm   = texture2DRect(texBufNorm, lookup).xyz;
55     if(dot(norm, norm) < 0.95)
56     {
57         discard;
58     }
59     else
60     {
61         vec4  posAmb = texture2DRect(texBufPos,  lookup);
62         vec3  pos    = posAmb.xyz;
63         float amb    = posAmb.w;
64         vec4  mDiff  = texture2DRect(texBufDiff, lookup);
66         gl_FragColor = computeSpotLight(0, pos, norm, mDiff);
67     }