PDA

View Full Version : Ligth mooving with camera

ikergarcia1996
12-18-2016, 04:01 PM
Hi! Im trying to add lighting to my program, the ligths seams to work as I expect when I load a object, but, when I rotate the camera around it the ligth apear to move with the camera.
Here is a example, the ligth source is located at the point 0,100,0, so the top face of the cube is iluminated (the cube is at point 0,0,0), as I expect.
2308

The problem comes when i move the camera to the bottom of the cube, It lights up as if the ligth source has move with the camera.

2309

I suspect that the problem comes from the Nomal vectors. I calculate the normal vector for the objet (for every surface and vertex) when I load it. Must I update this vectors when I modify the camera?

This is the function that draws the objets

glMatrixMode(GL_MODELVIEW);
glLoadMatrixd(camera->cameraglulookat); //Result from glulookat stored at "camera->cameraglulookat"
glPushMatrix();

glMultMatrixd(aux_obj->trans_matrix); //matrix with the transformations applied to the object

for (f = 0; f < aux_obj->num_faces; f++) {
glBegin(GL_POLYGON);

for (v = 0; v < aux_obj->face_table[f].num_vertices; v++) {
v_index = aux_obj->face_table[f].vertex_table[v];

if(tilum==FLAT)
glNormal3f(aux_obj->face_table[f].normal.x,
aux_obj->face_table[f].normal.y,
aux_obj->face_table[f].normal.z);
else //tilum==SMOOTH
glNormal3f(aux_obj->vertex_table[v_index].normal.x,
aux_obj->vertex_table[v_index].normal.y,
aux_obj->vertex_table[v_index].normal.z);

glVertex3d(aux_obj->vertex_table[v_index].coord.x,
aux_obj->vertex_table[v_index].coord.y,
aux_obj->vertex_table[v_index].coord.z);

}
glEnd();
}
glPopMatrix();

Thanks!

UPDATE:

I found the solution, the matrix at GL_MODELVIEW also affect to the ligth source, so, adding a line to set the ligth position again at 0,100,0 before drawing has solved the problem
Here is the updated code:

glMatrixMode(GL_MODELVIEW);
glLoadMatrixd(camera->cameraglulookat); //Result from glulookat stored at "camera->cameraglulookat"
glPushMatrix();

glMultMatrixd(aux_obj->trans_matrix); //matrix with the transformations applied to the object

//--------------------------------------------------------------------------------------------------------------------------------
glLightfv( GL_LIGHT1, GL_POSITION, listaluces[1].position); // listaluces[1].position = {0,100,0,0};
//--------------------------------------------------------------------------------------------------------------------------------
for (f = 0; f < aux_obj->num_faces; f++) {
glBegin(GL_POLYGON);

for (v = 0; v < aux_obj->face_table[f].num_vertices; v++) {
v_index = aux_obj->face_table[f].vertex_table[v];

if(tilum==FLAT)
glNormal3f(aux_obj->face_table[f].normal.x,
aux_obj->face_table[f].normal.y,
aux_obj->face_table[f].normal.z);
else //tilum==SMOOTH
glNormal3f(aux_obj->vertex_table[v_index].normal.x,
aux_obj->vertex_table[v_index].normal.y,
aux_obj->vertex_table[v_index].normal.z);

glVertex3d(aux_obj->vertex_table[v_index].coord.x,
aux_obj->vertex_table[v_index].coord.y,
aux_obj->vertex_table[v_index].coord.z);

}
glEnd();
}
glPopMatrix();

Silence
12-18-2016, 10:41 PM
Where is your light set up ? The subject is about lighting, but there is no code about lighting.