Sign in to follow this  

Per pixel light problems

This topic is 3663 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, Im trying to implement ppl on my engine, this are the vertex and fragment shaders. Im getting some strange results, the main one is that altought the light is always on (0,0,0) it is moving when i move my character or the camara position.
varying vec3 normal;
varying vec4 ecPosition;

void main()
{
   //calcular la normal del vertice	
   normal = gl_NormalMatrix * gl_Normal;

   //posicion del vertices
   ecPosition = gl_ModelViewMatrix * gl_Vertex;

   //coordenadas de la textura
   gl_TexCoord[0] = gl_MultiTexCoord0; 

   gl_TexCoord[1] = gl_MultiTexCoord1; 

   gl_Position = ftransform();
}



varying vec3 normal;
varying vec4 ecPosition;

uniform sampler2D texture0;

uniform sampler2D texture1;

void main()
{
	vec3 N, L, R, V, H;
        N                = normalize(normal);
        //direccion
        vec3  direccion  = vec3(gl_LightSource[0].position - ecPosition);
        float distancia  = length(direccion);
        L                = normalize(direccion);
	V                = vec3(normalize(-ecPosition));

	//calculo de la luz ambiente
	vec4 vAmbient  = gl_LightSource[0].ambient * gl_FrontMaterial.ambient;	

	//calculo de la luz difusa Id = Dl * Dm * LambertTerm
        float LamberTerm = max( dot(N, L), 0.0);
	vec4 vDiffuse  = gl_LightSource[0].diffuse  * gl_FrontMaterial.diffuse * LamberTerm;	
        
        //Calculo de la especular
        H = (L + V)/2; //calculo del HalfVector
        float specular = max( dot(N,H) , 0.0);
        vec4 vSpecular = gl_LightSource[0].specular * gl_FrontMaterial.specular * specular;	

        vec4 finalight = vAmbient + vDiffuse + vSpecular;
        //vec3 suma      = texture2D(texture0,  vec2(gl_TexCoord[0])).xyz * texture2D(texture1,  vec2(gl_TexCoord[1])).xyz;   
	//gl_FragColor   = vec4(suma, 1) + finalight;
        gl_FragColor = finalight;
}


Any idea? Thanks in advance!!!

Share this post


Link to post
Share on other sites
Thanks for the help!.


Thats the way i set the light:

static GLfloat position[4] = {0.0, 0.0, 0.0, 1.0};
static GLfloat LightAmb[4] = {1.0, 1.0, 1.0, 1.0};
static GLfloat LightDif[4] = {1.0, 1.0, 1.0, 1.0};
static GLfloat LightSpc[4] = {1.0, 1.0, 1.0, 1.0};
glLightfv(GL_LIGHT0, GL_POSITION, position);
glLightfv(GL_LIGHT0, GL_AMBIENT, LightAmb);
glLightfv(GL_LIGHT0, GL_DIFFUSE, LightDif);
glLightfv(GL_LIGHT0, GL_SPECULAR, LightSpc);



The light was moving because i had comment one matrix multiply line. Now it is static, but it continues doing strange thing.

I want to apply the light on world and models. i render the world and the models in this way:

setCamera
Active light shader
Draw World
Translatef model to its position
Rotatef model by wanted direction
Draw model
Deactive shader

1st strange thing:

The light position seems to be correct when i comment all the model stuff, if i put all the model stuff again the light seems to change its position (it continues static but in a new position).


2nd strange thing:
With comment or uncomment model stuff the face object that arent facing the light seems to be as lightened as those that are facing.


Any idea?




Share this post


Link to post
Share on other sites
When you set the position or direction of a light, it get's transformed by the current modelview matrix.

Set your light positions after multiplying the modelview by the view matrix, but before applying the world matrix.

I personally find that very annoying.

Share this post


Link to post
Share on other sites
New strange thing!

This morning i was testing it on ubuntu + ati x300 now im on window xp + nvidia 7600GS.

I dont remenber to change any code but now i dont have the model rendering problem, i will test it again ati on friday, anyway...

See this pic:



Blue box is the light.

why it split the scene in light and shadow?
why the objects that arent facing the light look much darkener?
why if i change light position to (0,0,0,0) instead (0,0,0,1)it starts moving with the camera?
and the most important, why i cant fuck jade raymond?

Oh!! please help!

Share this post


Link to post
Share on other sites
Finally it was making a mistake passing the worlds'normals, so now it works well for the world and the shaders are ok.

But now im having a new problem, when i want to render my model i do this:

Translatef model to its position
Rotatef model by wanted direction
Draw model

And the models is not lighting correctly, now i set the light position manually doing this:

lightpos = gl_ModelViewMatrix * vec4(lpos, 1);

The vertex calculation is still the same:

ecPosition = gl_ModelViewMatrix * gl_Vertex;

I try to pass the orginal matrix (before doing translatef and rotatef for the model) and apply it to the light istead gl_ModelViewMatrix, but i think that i need to manipulate ecPosition too but i dont see it clearly.

Any help???

Share this post


Link to post
Share on other sites

This topic is 3663 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this