1
问题:随着距离的增加,衰减不起作用。GLSL - 衰减不起作用
我的漫反射,镜面反射和环境反射看起来正在工作。我添加了衰减变量的公式,然后将每次漫反射,镜面反射和环境的衰减倍数。
这里是地面照明的图像,该光被定位仅次于立方体:
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;
}
这个问题不能作为被应答,因为我们不知道任何值都是。如果没有填充“gl_LightSource [0] .linearAttenuation”和“gl_LightSource [0] .quadraticAttenuation”,那么这些方程就没有意义。 – 2012-04-20 06:18:15
我通过“glLightf(GL_LIGHT0,GL_QUADRATIC_ATTENUATION,0.0004f);”设置第一个灯的四倍衰减;“ – user1337604 2012-04-20 06:26:44
你的场景的规模是什么?物体有多远? – 2012-04-20 06:32:56