0
votes

I tried to incorporate attentuation, but it failed does nothing.

I have diffuse, ambient, and specular lighting working. I just need to dim the light as the fragments get further away from the light.

Also, i have the attenuation parameter for my light:

glLightf(GL_LIGHT0, GL_QUADRATIC_ATTENUATION, 0.0004f);

This is the floor lighting, the light is positioned just behind the cube:

http://oi43.tinypic.com/i39fuo.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;   
}
1
possible duplicate of GLSL - Attenuation not workingNicol Bolas

1 Answers

0
votes

From this image i cant' really see anything. How about setting a small object as lightsource.

Which object's use this shader?

Some things that come to my mind:

  1. You normalized your normal in your vertex shader, which is an unnecessary step.
    Passed vectors from vertex to fragment shader must be normalized inside fragment shader since they will be interpolated.
    Aslong you don't do any length based calculations in your vertex shader, which you aren't no normalization is necessary in vertex shader.

    You should normalize the normal in fragment shader, then you don't need to normalize your reflect vector.

  2. Attenuation is not based on anything "complex" calculated in shader. So output it and then check the rest. How does your diffuse term looks like?

  3. Further hints:
    You could place the light vector and attenuation calculation inside vertex shader and pass it as to fragment shader (pack it in a 4 vec component) to save interpolators.

  4. the final specular clamp is unecessary, the values should be within [0, 1] range automatically. If not you have a problem.