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