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)
18 float NdotL = max(dot(lightDir, norm), 0.);
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;
38 uniform sampler2DRect texBufPos;
39 uniform sampler2DRect texBufNorm;
40 uniform sampler2DRect texBufDiff;
41 uniform vec2 vpOffset;
46 vec2 lookup = gl_FragCoord.xy - vpOffset;
47 vec3 norm = texture2DRect(texBufNorm, lookup).xyz;
49 if(dot(norm, norm) < 0.95)
55 vec4 posAmb = texture2DRect(texBufPos, lookup);
56 vec3 pos = posAmb.xyz;
58 vec4 mDiff = texture2DRect(texBufDiff, lookup);
60 gl_FragColor = computeSpotLight(0, pos, norm, mDiff);