fixed: auto_ptr -> unique_ptr
[opensg.git] / Examples / Advanced / DeferredShading / DSPointLightShadow.fp.glsl
blob525fb6f725dc5dab815e9324b17c9c8623c7984f
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 point light INDEX for fragment at POS with normal NORM
10 // and diffuse material color MDIFF
11 vec4 computePointLight(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 NdotL      = max(dot(norm, lightDir), 0.);
19     if(NdotL > 0.)
20     {
21         vec4  shadow    = OSG_SSME_FP_calcShadow(vec4(pos, 1.));
23         float lightDist = length   (lightDirUN);
24         float distAtt   = dot(vec3(gl_LightSource[index].constantAttenuation,
25                                    gl_LightSource[index].linearAttenuation,
26                                    gl_LightSource[index].quadraticAttenuation),
27                               vec3(1., lightDist, lightDist * lightDist));
28         distAtt = 1. / distAtt;
30         color = shadow * distAtt * NdotL * mDiff * gl_LightSource[index].diffuse;
31     }
33     return color;
36 // DS input buffers
37 uniform sampler2DRect texBufPos;
38 uniform sampler2DRect texBufNorm;
39 uniform sampler2DRect texBufDiff;
40 uniform vec2          vpOffset;
42 // DS pass
43 void main(void)
45     vec2 lookup = gl_FragCoord.xy - vpOffset;
46     vec3 norm   = texture2DRect(texBufNorm, lookup).xyz;
48     if(dot(norm, norm) < 0.95)
49     {
50         discard;
51     }
52     else
53     {
54         vec4  posAmb = texture2DRect(texBufPos,  lookup);
55         vec3  pos    = posAmb.xyz;
56         float amb    = posAmb.w;
57         vec4  mDiff  = texture2DRect(texBufDiff, lookup);
59         gl_FragColor = computePointLight(0, pos, norm, mDiff);
60     }