fixed: auto_ptr -> unique_ptr
[opensg.git] / Examples / Advanced / DeferredShading / DSLights.fp.glsl
blob2219ef2e990060986d5263b774e9f3b7091bc588
1 #version 120
2 #extension GL_ARB_texture_rectangle : require
3 #extension GL_ARB_texture_rectangle : enable
5 #extension GL_EXT_gpu_shader4 : require
6 #extension GL_EXT_gpu_shader4 : enable
8 // forward decls
9 vec4 OSG_SSME_FP_calcShadow(in vec4 ecFragPos);
11 uniform sampler2DRect     texBufPos;
12 uniform sampler2DRect     texBufNorm;
13 uniform sampler2DRect     texBufDiff;
15 // compute point light INDEX for fragment at POS with normal NORM
16 // and diffuse material color MDIFF
17 vec4 computePointLight(int index, vec3 pos, vec3 norm, vec4 mDiff)
19     vec4  color      = vec4(0., 0., 0., 0.);
21     vec3  lightDirUN = gl_LightSource[index].position.xyz - pos;
22     vec3  lightDir   = normalize(lightDirUN);
23     float NdotL      = max(dot(norm, lightDir), 0.);
25     if(NdotL > 0.)
26     {
27         vec4  shadow    = OSG_SSME_FP_calcShadow(vec4(pos, 1.));
29         float lightDist = length   (lightDirUN);
30         float distAtt   = dot(vec3(gl_LightSource[index].constantAttenuation,
31                                    gl_LightSource[index].linearAttenuation,
32                                    gl_LightSource[index].quadraticAttenuation),
33                               vec3(1., lightDist, lightDist * lightDist));
34         distAtt = 1. / distAtt;
36         color = shadow * distAtt * NdotL * mDiff * gl_LightSource[index].diffuse;
37 //        color = shadow * 0.5;
38     }
40     return color;
43 // compute directional light INDEX for fragment at POS with normal NORM
44 // and diffuse material color MDIFF
45 vec4 computeDirLight(int index, vec3 pos, vec3 norm, vec4 mDiff)
47     vec4 color = vec4(0., 0., 0., 0.);
49     vec3  lightDir = gl_LightSource[index].position.xyz;
50     float NdotL    = max(dot(norm, lightDir), 0.);
52     if(NdotL > 0.)
53     {
54         vec4  shadow = OSG_SSME_FP_calcShadow(vec4(pos, 1.));
56         color = shadow * NdotL * mDiff * gl_LightSource[index].diffuse;
57     }
59     return color;
62 // compute spot light INDEX for fragment at POS with normal NORM
63 // and diffuse material color MDIFF
64 vec4 computeSpotLight(int index, vec3 pos, vec3 norm, vec4 mDiff)
66     vec4 color = vec4(0., 0., 0., 0.);
68     vec3  lightDirUN = gl_LightSource[index].position.xyz - pos;
69     vec3  lightDir   = normalize(lightDirUN);
70     float spotEffect = dot(-lightDir, gl_LightSource[index].spotDirection);
72     if(spotEffect > gl_LightSource[index].spotCosCutoff)
73     {
74         float NdotL = max(dot(lightDir, norm), 0.);
75         
76         if(NdotL > 0.)
77         {
78             vec4  shadow    = OSG_SSME_FP_calcShadow(vec4(pos, 1.));
80             float lightDist = length(lightDirUN);
81             float distAtt   = dot(vec3(gl_LightSource[index].constantAttenuation,
82                                        gl_LightSource[index].linearAttenuation,
83                                        gl_LightSource[index].quadraticAttenuation),
84                                   vec3(1., lightDist, lightDist * lightDist)       );
85             spotEffect = pow(spotEffect, gl_LightSource[index].spotExponent);
86             distAtt    = spotEffect / distAtt;
88             color = shadow * distAtt * NdotL * mDiff * gl_LightSource[index].diffuse;
89         }
90     }
91     
92     return color;
96 void main(void)
98     vec3  norm  = texture2DRect(texBufNorm, gl_FragCoord.xy).xyz;
100     if(dot(norm, norm) < 0.95)
101     {
102         discard;
103     }
104     else
105     {
106         vec4  posAmb = texture2DRect(texBufPos,  gl_FragCoord.xy);
107         vec3  pos    = posAmb.xyz;
108         float amb    = posAmb.w;
109         vec4  mDiff  = texture2DRect(texBufDiff, gl_FragCoord.xy);
111         gl_FragColor = computePointLight(0, pos, norm, mDiff);
112     }