Question

I'm trying to implement a simple viewer and I was trying to implement light attenuation for point light.

The problem I have is the following:

I have that unnatural line going over the sphere.

The relevant code in shader is:

....
vec3 Ldist = uLightPosition-vPosition.xyz;
vec3 L = normalize(Ldist);
....
float NdotL = max(dot(N,L),0.0);


float attenuation = 1.0/ (Ldist*Ldist);

vec3 light = uAmbientColor;

if(NdotL>0.0){
    specularWeighting = rho_s * computeBRDF(roughness, Didx, Gidx, Fidx, L,  N,  V);
    light = light + NdotL*uLightColor*attenuation*(specularWeighting*specularColor*envColor.rgb + diffuseColor);
}   

Being new to slightly more advanced lighting, I really can't see what could be wrong.

(I know that maybe should be a different question, but being so small I was wondering if I could ask this here as well: is there are any rule of thumb to select the light and intensity position to have a nice result on a single object like the sphere up there?)

Was it helpful?

Solution

The following doesn't really make sense:

vec3 Ldist = uLightPosition-vPosition.xyz;
[...]
float attenuation = 1.0/ (Ldist*Ldist);

First of all, this shouldn't even compile, as Ldist is a vec3 and the * operator will do a component wise multiplication, leaving you whit a scalar divided by a vector. But apart from the syntax issues, and assuming that just len(LDist) was meant (which I will call d in the following), the attenuation term still does not make sense. Typically, the attenuation term used is

1.0/(a + b*d + c * d*d)

with a, b and c being the constant, linear and quadratric light attenuation coefficients, respectively. What is important to note here is that if the denominator of that equation becomes < 1, the "attenuation" will be aobve 1 - so the opposite effect is achieved. Since in a general scene, the distance can be as low as 0, the only way to make sure that this will never happen is by setting a >= 1, which is typically done. So I recommend that you use at least 1.0/(1.0+d) as attenuation term, or add some constant attenuation coefficient in general.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top