GLSL - 衰减不起作用

时间:2012-04-20 06:00:48

标签: opengl

问题:随着距离的增加,衰减不会降低光强度。

我的漫反射,镜面反射和环境反射看起来都有效。我添加了衰减变量的公式,然后将衰减乘以每个漫反射,镜面和环境。

这是地板照明的图片,灯光位于立方体后面:

img http://oi42.tinypic.com/28luzci.jpg

.vert

varying vec3 N;
varying vec3 v;
varying vec3 c;
varying float dist;

void main(void)  
{
vec4 ecPos;
vec3 aux;

ecPos = gl_ModelViewMatrix * gl_Vertex;
aux = vec3(gl_LightSource[0].position-ecPos);
dist = length(aux);

c = vec3(gl_Color);
v = vec3(gl_ModelViewMatrix * gl_Vertex);       
N = normalize(gl_NormalMatrix * gl_Normal);
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;  
}

.frag

varying vec3 N;
varying vec3 v;
varying vec3 c;    
varying float dist;

void main (void)  
{  
float att;

att = 1.0 / (gl_LightSource[0].constantAttenuation +
            gl_LightSource[0].linearAttenuation * dist +
            gl_LightSource[0].quadraticAttenuation * dist * dist);

vec3 L = normalize(gl_LightSource[0].position.xyz - v);   
vec3 E = normalize(-v); // we are in Eye Coordinates, so EyePos is (0,0,0)  
vec3 R = normalize(-reflect(L,N));      

float nDotL = max(dot(N,L), 0.0);
float rDotE = max(dot(R,E),0.0);
float power = pow(rDotE, gl_FrontMaterial.shininess);

//calculate Ambient Term:  
vec4 Iamb = gl_FrontLightProduct[0].ambient * att;        

//calculate Diffuse Term:  
vec4 Idiff = gl_FrontLightProduct[0].diffuse * nDotL * att;
Idiff = clamp(Idiff, 0.0, 1.0);     

// calculate Specular Term:
vec4 Ispec = gl_FrontLightProduct[0].specular * power * att;
Ispec = clamp(Ispec, 0.0, 1.0); 

// write Total Color:  
gl_FragColor = Iamb + Idiff + Ispec + c;   
}