PDA

View Full Version : Does Light follow the glMultMatrixd rules ?



ViewOn01
09-24-2009, 02:00 AM
Hi,

I have create a simple cube and a simple light... but when I rotate the cube (and not the light) the back face sounds "black"... but I have not rotate the light !

In fact, the face in "front" should always be lighted, right ?

Why this effect occur ? An idea ?

Here is my code :

private void Setup_CubeLighting()
{
float distance = (float)VIEWPORT_SPACE_SIZE * 20;

//----
Gl.glEnable(Gl.GL_LIGHTING);

//---- Lights
float[] LightAmbient = new float[] { 0f, 0f, 0f, 1.0f };
float[] LightDiffuseSpecular;
float[] LightPosition;

// Front
LightPosition = new float[] { .0f, .0f, distance, 1.0f };
LightDiffuseSpecular = new float[] { 0.25f, 0f, 0f, 1f };
Gl.glLightfv(Gl.GL_LIGHT0, Gl.GL_AMBIENT, LightAmbient);
Gl.glLightfv(Gl.GL_LIGHT0, Gl.GL_DIFFUSE, LightDiffuseSpecular);
Gl.glLightfv(Gl.GL_LIGHT0, Gl.GL_SPECULAR, LightDiffuseSpecular);
Gl.glLightfv(Gl.GL_LIGHT0, Gl.GL_POSITION, LightPosition);
Gl.glEnable(Gl.GL_LIGHT0);

// Right
//LightPosition = new float[] { distance, .0f, .0f, 1.0f };
//LightDiffuseSpecular = new float[] { 0f, 0.25f, 0f, 1f };
//Gl.glLightfv(Gl.GL_LIGHT1, Gl.GL_AMBIENT, LightAmbient);
//Gl.glLightfv(Gl.GL_LIGHT1, Gl.GL_DIFFUSE, LightDiffuseSpecular);
//Gl.glLightfv(Gl.GL_LIGHT1, Gl.GL_SPECULAR, LightDiffuseSpecular);
//Gl.glLightfv(Gl.GL_LIGHT1, Gl.GL_POSITION, LightPosition);
//Gl.glEnable(Gl.GL_LIGHT1);

//// Left
//LightPosition = new float[] { -distance, .0f, .0f, 1.0f };
//LightDiffuseSpecular = new float[] { 0f, 0f, 0.25f, 1f };
//Gl.glLightfv(Gl.GL_LIGHT3, Gl.GL_AMBIENT, LightAmbient);
//Gl.glLightfv(Gl.GL_LIGHT3, Gl.GL_DIFFUSE, LightDiffuseSpecular);
//Gl.glLightfv(Gl.GL_LIGHT3, Gl.GL_SPECULAR, LightDiffuseSpecular);
//Gl.glLightfv(Gl.GL_LIGHT3, Gl.GL_POSITION, LightPosition);
//Gl.glEnable(Gl.GL_LIGHT3);

// Top
//LightPosition = new float[] { .0f, distance, .0f, 1.0f };
//Gl.glLightfv(Gl.GL_LIGHT4, Gl.GL_AMBIENT, LightAmbient);
//Gl.glLightfv(Gl.GL_LIGHT4, Gl.GL_DIFFUSE, LightDiffuseSpecular);
//Gl.glLightfv(Gl.GL_LIGHT4, Gl.GL_SPECULAR, LightDiffuseSpecular);
//Gl.glLightfv(Gl.GL_LIGHT4, Gl.GL_POSITION, LightPosition);
//Gl.glEnable(Gl.GL_LIGHT4);

//// Bottom
//LightPosition = new float[] { .0f, -distance, .0f, 1.0f };
//Gl.glLightfv(Gl.GL_LIGHT5, Gl.GL_AMBIENT, LightAmbient);
//Gl.glLightfv(Gl.GL_LIGHT5, Gl.GL_DIFFUSE, LightDiffuseSpecular);
//Gl.glLightfv(Gl.GL_LIGHT5, Gl.GL_SPECULAR, LightDiffuseSpecular);
//Gl.glLightfv(Gl.GL_LIGHT5, Gl.GL_POSITION, LightPosition);
//Gl.glEnable(Gl.GL_LIGHT5);
}

private void Render_GuideCube()
{
//---- Viewport size
int width = (int)VIEWPORT_SPACE_SIZE;
int height = (int)VIEWPORT_SPACE_SIZE;
Gl.glViewport((int)(oglImage.ActualWidth - width), (int)(oglImage.ActualHeight - height), width, height);

#region Look at

Gl.glMatrixMode(Gl.GL_PROJECTION);
Gl.glLoadIdentity();

Vector3 eye = new Vector3();
Vector3 eyeAt = new Vector3();
Vector3 eyeNormal = new Vector3();
GetViewVectors(ref eye, ref eyeAt, ref eyeNormal, VIEWPORT_SPACE_SIZE);

// Ortho is a Square because the viewport is now a square
Gl.glOrtho(-VIEWPORT_SPACE_SIZE, VIEWPORT_SPACE_SIZE, -VIEWPORT_SPACE_SIZE, VIEWPORT_SPACE_SIZE, -500, 500);

Glu.gluLookAt(
eye.x, eye.y, eye.z,
eyeAt.x, eyeAt.y, eyeAt.z,
eyeNormal.x, eyeNormal.y, eyeNormal.z);

Gl.glMatrixMode(Gl.GL_MODELVIEW);
Gl.glLoadIdentity();

#endregion

#region Lighting

Setup_CubeLighting();

#endregion

Gl.glPushMatrix();
Gl.glMultMatrixd(_arcBall.Matrix);

#region Draw the cube

Gl.glColor3f(0.7f, 0.7f, 0.7f);
Gl.glEnable(Gl.GL_COLOR_MATERIAL);

//---- Materials
Gl.glMaterialfv(Gl.GL_FRONT_AND_BACK, Gl.GL_AMBIENT, glfMatAmbient);
Gl.glMaterialfv(Gl.GL_FRONT_AND_BACK, Gl.GL_DIFFUSE, glfMatDiffuse);
Gl.glMaterialfv(Gl.GL_FRONT_AND_BACK, Gl.GL_SPECULAR, glfMatSpecular);
Gl.glMaterialfv(Gl.GL_FRONT_AND_BACK, Gl.GL_EMISSION, glfMatEmission);
Gl.glMaterialf(Gl.GL_FRONT_AND_BACK, Gl.GL_SHININESS, fShininess);

//---- Textures
InitializeTextures();

Gl.glEnable(Gl.GL_TEXTURE_2D);

// Front
Gl.glBindTexture(Gl.GL_TEXTURE_2D, _frontTextureId);
Gl.glBegin(Gl.GL_QUADS);
if (_guideCubeSelection == GuideCubeNames.Front)
Gl.glColor3f(1f, 0f, 0f);
Gl.glTexCoord2f(0, 0); Gl.glVertex3d(-_cubeSize, -_cubeSize, _cubeSize);
Gl.glTexCoord2f(1, 0); Gl.glVertex3d(_cubeSize, -_cubeSize, _cubeSize);
Gl.glTexCoord2f(1, 1); Gl.glVertex3d(_cubeSize, _cubeSize, _cubeSize);
Gl.glTexCoord2f(0, 1); Gl.glVertex3d(-_cubeSize, _cubeSize, _cubeSize);
Gl.glColor3f(0.7f, 0.7f, 0.7f);
Gl.glEnd();

// Back
Gl.glBindTexture(Gl.GL_TEXTURE_2D, _backTextureId);
Gl.glBegin(Gl.GL_QUADS);
if (_guideCubeSelection == GuideCubeNames.Back)
Gl.glColor3f(1f, 0f, 0f);
Gl.glTexCoord2f(0, 0); Gl.glVertex3d(_cubeSize, -_cubeSize, -_cubeSize);
Gl.glTexCoord2f(1, 0); Gl.glVertex3d(-_cubeSize, -_cubeSize, -_cubeSize);
Gl.glTexCoord2f(1, 1); Gl.glVertex3d(-_cubeSize, _cubeSize, -_cubeSize);
Gl.glTexCoord2f(0, 1); Gl.glVertex3d(_cubeSize, _cubeSize, -_cubeSize);
Gl.glColor3f(0.7f, 0.7f, 0.7f);
Gl.glEnd();

// Left
Gl.glBindTexture(Gl.GL_TEXTURE_2D, _leftTextureId);
Gl.glBegin(Gl.GL_QUADS);
if (_guideCubeSelection == GuideCubeNames.Left)
Gl.glColor3f(1f, 0f, 0f);
Gl.glTexCoord2f(0, 0); Gl.glVertex3d(-_cubeSize, -_cubeSize, -_cubeSize);
Gl.glTexCoord2f(1, 0); Gl.glVertex3d(-_cubeSize, -_cubeSize, _cubeSize);
Gl.glTexCoord2f(1, 1); Gl.glVertex3d(-_cubeSize, _cubeSize, _cubeSize);
Gl.glTexCoord2f(0, 1); Gl.glVertex3d(-_cubeSize, _cubeSize, -_cubeSize);
Gl.glColor3f(0.7f, 0.7f, 0.7f);
Gl.glEnd();

// Right
Gl.glBindTexture(Gl.GL_TEXTURE_2D, _rightTextureId);
Gl.glBegin(Gl.GL_QUADS);
if (_guideCubeSelection == GuideCubeNames.Right)
Gl.glColor3f(1f, 0f, 0f);
Gl.glTexCoord2f(0, 0); Gl.glVertex3d(_cubeSize, -_cubeSize, _cubeSize);
Gl.glTexCoord2f(1, 0); Gl.glVertex3d(_cubeSize, -_cubeSize, -_cubeSize);
Gl.glTexCoord2f(1, 1); Gl.glVertex3d(_cubeSize, _cubeSize, -_cubeSize);
Gl.glTexCoord2f(0, 1); Gl.glVertex3d(_cubeSize, _cubeSize, _cubeSize);
Gl.glColor3f(0.7f, 0.7f, 0.7f);
Gl.glEnd();

// Top
Gl.glBindTexture(Gl.GL_TEXTURE_2D, _topTextureId);
Gl.glBegin(Gl.GL_QUADS);
if (_guideCubeSelection == GuideCubeNames.Top)
Gl.glColor3f(1f, 0f, 0f);
Gl.glTexCoord2f(0, 0); Gl.glVertex3d(-_cubeSize, _cubeSize, _cubeSize);
Gl.glTexCoord2f(1, 0); Gl.glVertex3d(_cubeSize, _cubeSize, _cubeSize);
Gl.glTexCoord2f(1, 1); Gl.glVertex3d(_cubeSize, _cubeSize, -_cubeSize);
Gl.glTexCoord2f(0, 1); Gl.glVertex3d(-_cubeSize, _cubeSize, -_cubeSize);
Gl.glColor3f(0.7f, 0.7f, 0.7f);
Gl.glEnd();

// Bottom
Gl.glBindTexture(Gl.GL_TEXTURE_2D, _bottomTextureId);
Gl.glBegin(Gl.GL_QUADS);
if (_guideCubeSelection == GuideCubeNames.Bottom)
Gl.glColor3f(1f, 0f, 0f);
Gl.glTexCoord2f(0, 0); Gl.glVertex3d(-_cubeSize, -_cubeSize, -_cubeSize);
Gl.glTexCoord2f(1, 0); Gl.glVertex3d(_cubeSize, -_cubeSize, -_cubeSize);
Gl.glTexCoord2f(1, 1); Gl.glVertex3d(_cubeSize, -_cubeSize, _cubeSize);
Gl.glTexCoord2f(0, 1); Gl.glVertex3d(-_cubeSize, -_cubeSize, _cubeSize);
Gl.glColor3f(0.7f, 0.7f, 0.7f);
Gl.glEnd();

Gl.glDisable(Gl.GL_TEXTURE_2D);

#endregion

#region Draw the lines

Gl.glLineWidth(1);
Gl.glColor3f(1f, 1f, 1f);

Gl.glBegin(Gl.GL_LINE_LOOP);
Gl.glVertex3d(-_cubeSize, _cubeSize, -_cubeSize);
Gl.glVertex3d(-_cubeSize, -_cubeSize, -_cubeSize);
Gl.glVertex3d(_cubeSize, -_cubeSize, -_cubeSize);
Gl.glVertex3d(_cubeSize, _cubeSize, -_cubeSize);
Gl.glEnd();

Gl.glBegin(Gl.GL_LINE_LOOP);
Gl.glVertex3d(-_cubeSize, _cubeSize, _cubeSize);
Gl.glVertex3d(-_cubeSize, -_cubeSize, _cubeSize);
Gl.glVertex3d(_cubeSize, -_cubeSize, _cubeSize);
Gl.glVertex3d(_cubeSize, _cubeSize, _cubeSize);
Gl.glEnd();

Gl.glBegin(Gl.GL_LINE_LOOP);
Gl.glVertex3d(-_cubeSize, _cubeSize, -_cubeSize);
Gl.glVertex3d(-_cubeSize, -_cubeSize, -_cubeSize);
Gl.glVertex3d(-_cubeSize, -_cubeSize, _cubeSize);
Gl.glVertex3d(-_cubeSize, _cubeSize, _cubeSize);
Gl.glEnd();

Gl.glBegin(Gl.GL_LINE_LOOP);
Gl.glVertex3d(_cubeSize, _cubeSize, -_cubeSize);
Gl.glVertex3d(_cubeSize, -_cubeSize, -_cubeSize);
Gl.glVertex3d(_cubeSize, -_cubeSize, _cubeSize);
Gl.glVertex3d(_cubeSize, _cubeSize, _cubeSize);
Gl.glEnd();

#endregion

Gl.glPopMatrix();

#region Reset

ResetLight(Gl.GL_LIGHT0);

Gl.glDisable(Gl.GL_LIGHTING);
Gl.glDisable(Gl.GL_COLOR_MATERIAL);

#endregion

}

Dark Photon
09-24-2009, 06:33 AM
I have create a simple cube and a simple light... but when I rotate the cube (and not the light) the back face sounds "black"... but I have not rotate the light !
In fact, the face in "front" should always be lighted, right ?
Why this effect occur ? An idea ?
You're not passing any vertex normals down. Normals are necessary for proper lighting. See glNormal3f.