PDA

View Full Version : Illumination changes when rotating camera



diluo
11-09-2009, 09:21 AM
Hello, I have a problem with lighting in OpenGL, when I rotate camera changes the illumination of the scene.
The light is directional: incident at the same angle for all objects, like the sun.


const GLfloat lightAmbient[] = {0.2, 0.2, 0.2, 1.0};
const GLfloat lightDiffuse[] = {1.0, 0.6, 0.0, 1.0};
const GLfloat matAmbient[] = {0.6, 0.6, 0.6, 1.0};
const GLfloat matDiffuse[] = {1.0, 1.0, 1.0, 1.0};
const GLfloat matSpecular[] = {1.0, 1.0, 1.0, 1.0};
const GLfloat lightPosition[] = {0.0, 0.0, 1.0, 0.0};
const GLfloat lightShininess = 100.0;

glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, matAmbient);
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, matDiffuse);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, matSpecular);
glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, lightShininess);
glLightfv(GL_LIGHT0, GL_AMBIENT, lightAmbient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, lightDiffuse);
glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);




I tried calling glLightfv on each frame after tell opengl which is the matrix of the camera, but still happening:





glMatrixMode(GL_MODELVIEW);




float mat[4*4]={ CamMatrix->iM[0], CamMatrix->iM[4], -CamMatrix->iM[ 8], 0,
CamMatrix->iM[1], CamMatrix->iM[5], -CamMatrix->iM[ 9], 0,
CamMatrix->iM[2], CamMatrix->iM[6], -CamMatrix->iM[10], 0,
CamMatrix->iM[3], CamMatrix->iM[7], -CamMatrix->iM[11], 1 };


glLoadMatrixf( mat );
glPushMatrix();

float lightPosition[] = {0.0f, 2.0f, 1.0f, 0.0f};
glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);



Thanks in advance.

dorbie
11-09-2009, 12:24 PM
This should work, you need to ensure that the matrix has the viewing transform and only the viewing transform on the matrix stack.

If you load another viewing matrix differently then you'll get a discrepancy.

You're dicking around with that matrix, it needs to be the SAME matrix as the one loaded for viewing and loaded in the same way as viewing.

diluo
11-09-2009, 12:52 PM
This should work, you need to ensure that the matrix has the viewing transform and only the viewing transform on the matrix stack.

If you load another viewing matrix differently then you'll get a discrepancy.

You're dicking around with that matrix, it needs to be the SAME matrix as the one loaded for viewing and loaded in the same way as viewing.

But in my engine, I set modelview matrix for each 3D object:



//---Generate rotate matrix for OpenGl.
//---Final matrix is CamMatrix *ObjectMatrix
float mat[4*4]={ FinalMatrix->iM[0], FinalMatrix->iM[4], -FinalMatrix->iM[ 8], 0,
FinalMatrix->iM[1], FinalMatrix->iM[5], -FinalMatrix->iM[ 9], 0,
FinalMatrix->iM[2], FinalMatrix->iM[6], -FinalMatrix->iM[10], 0,
FinalMatrix->iM[3], FinalMatrix->iM[7], -FinalMatrix->iM[11], 1 };

glLoadMatrixf( mat );

.
.
.

glDrawElements( GL_TRIANGLES, iGlTriCount, GL_UNSIGNED_SHORT, iGlTris );



Where should I put glLightfv(GL_LIGHT0, GL_POSITION, lightPosition); ?

I do not tell opengl which is the camera matrix, I only use camera matrix to generate FinalMatrix. I do not use glLookAt, it is not available in iphone.

Thank you very much for your help

AdrianPi
11-09-2009, 12:56 PM
Where should I put glLightfv(GL_LIGHT0, GL_POSITION, lightPosition); ?
Right after the camera transform, before drawing any object

diluo
11-09-2009, 04:19 PM
Where should I put glLightfv(GL_LIGHT0, GL_POSITION, lightPosition); ?
Right after the camera transform, before drawing any object

Yes, I am doing it there.

starmerak
11-23-2009, 04:20 AM
and you pop the matrix after the camera transform?