I really don't understand lighting in OpenGL... I set up the following:
Code:
//////////////////////////////////////////////////////////////////
////// *LIGHTING* //////////////////////
///////////////////////////////////////////////////////////////////
GLfloat lightAmbient[]= { 1.0f, 1.0f, 1.0f, 1.0f };
GLfloat specularLight[]= { 1.0f, 1.0f, 1.0f, 1.0f };
GLfloat diffuseLight[]= { 1.0f, 1.0f, 1.0f, 1.0f };
GLfloat LightPosition[]= { 0.0f, 3.0f, -2.0f, 1.0f };
///////////////////////////////////////////////////////////////////
And then in initialization:
Code:
glEnable(GL_TEXTURE_2D); // Enable Texture Mapping
glShadeModel(GL_SMOOTH); // Enable Smooth Shading
glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background
glClearDepth(1.0f); // Depth Buffer Setup
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do
glEnable(GL_CULL_FACE); // Enable culling.
glFrontFace(GL_CCW); // Enable counter clockwise culling.
glEnable(GL_LIGHTING); // Enable lighting.
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
///////////Lights//////////////////////////////
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight);
glLightfv(GL_LIGHT0, GL_SPECULAR, specularLight);
glLightfv(GL_LIGHT0, GL_AMBIENT, lightAmbient);
glLightfv(GL_LIGHT0, GL_POSITION, LightPosition);
glEnable(GL_LIGHT0);
///////////////////////////////////////////////
///////////COLOR MATERIAL//////////////////////
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
glMaterialfv(GL_FRONT, GL_SPECULAR, specularLight);
glMateriali(GL_FRONT, GL_SHININESS, 128);
glColorMaterial(GL_BACK, GL_AMBIENT_AND_DIFFUSE);
glMaterialfv(GL_BACK, GL_SPECULAR, specularLight);
glMateriali(GL_BACK, GL_SHININESS, 128);
///////////////////////////////////////////////
The problem is that when I draw a box
Code:
void Box::draw()
{
glRotatef(getRotX(), 1.0f, 0.0f, 0.0f);
glRotatef(getRotY(), 0.0f, 1.0f, 0.0f);
glRotatef(getRotZ(), 0.0f, 0.0f, 1.0f);
glTranslatef(getX(), getY(), getZ());
glColor3f(getRed(), getGreen(), getBlue());
glBegin(GL_QUADS);
// Front Face
glNormal3f( 0.0f, 0.0f, getLength());
//glTexCoord2f(0.0f, 0.0f);
glVertex3f(-getHeight(), -getWidth(), getLength());
//glTexCoord2f(1.0f, 0.0f);
glVertex3f( getHeight(), -getWidth(), getLength());
//glTexCoord2f(1.0f, 1.0f);
glVertex3f( getHeight(), getWidth(), getLength());
//glTexCoord2f(0.0f, 1.0f);
glVertex3f(-getHeight(), getWidth(), getLength());
// Back Face
glNormal3f( 0.0f, 0.0f,-getLength());
//glTexCoord2f(1.0f, 0.0f);
glVertex3f(-getHeight(), -getWidth(), -getLength());
//glTexCoord2f(1.0f, 1.0f);
glVertex3f(-getHeight(), getWidth(), -getLength());
//glTexCoord2f(0.0f, 1.0f);
glVertex3f( getHeight(), getWidth(), -getLength());
//glTexCoord2f(0.0f, 0.0f);
glVertex3f( getHeight(), -getWidth(), -getLength());
// Top Face
glNormal3f( 0.0f, getLength(), 0.0f);
//glTexCoord2f(0.0f, 1.0f);
glVertex3f(-getHeight(), getWidth(), -getLength());
//glTexCoord2f(0.0f, 0.0f);
glVertex3f(-getHeight(), getWidth(), getLength());
//glTexCoord2f(1.0f, 0.0f);
glVertex3f( getHeight(), getWidth(), getLength());
//glTexCoord2f(1.0f, 1.0f);
glVertex3f( getHeight(), getWidth(), -getLength());
// Bottom Face
glNormal3f( 0.0f,-getLength(), 0.0f);
//glTexCoord2f(1.0f, 1.0f);
glVertex3f(-getHeight(), -getWidth(), -getLength());
//glTexCoord2f(0.0f, 1.0f);
glVertex3f( getHeight(), -getWidth(), -getLength());
//glTexCoord2f(0.0f, 0.0f);
glVertex3f( getHeight(), -getWidth(), getLength());
//glTexCoord2f(1.0f, 0.0f);
glVertex3f(-getHeight(), -getWidth(), getLength());
// Right face
glNormal3f( getLength(), 0.0f, 0.0f);
//glTexCoord2f(1.0f, 0.0f);
glVertex3f( getHeight(), -getWidth(), -getLength());
//glTexCoord2f(1.0f, 1.0f);
glVertex3f( getHeight(), getWidth(), -getLength());
//glTexCoord2f(0.0f, 1.0f);
glVertex3f( getHeight(), getWidth(), getLength());
//glTexCoord2f(0.0f, 0.0f);
glVertex3f( getHeight(), -getWidth(), getLength());
// Left Face
glNormal3f(-getLength(), 0.0f, 0.0f);
//glTexCoord2f(0.0f, 0.0f);
glVertex3f(-getHeight(), -getWidth(), -getLength());
//glTexCoord2f(1.0f, 0.0f);
glVertex3f(-getHeight(), -getWidth(), getLength());
//glTexCoord2f(1.0f, 1.0f);
glVertex3f(-getHeight(), getWidth(), getLength());
//glTexCoord2f(0.0f, 1.0f);
glVertex3f(-getHeight(), getWidth(), -getLength());
glEnd();
It just appears brown, although shaded and despite the fact that the boxes are given different colors.
My scene drawing code goes here:
Code:
int DrawGLScene(GLvoid) // Here's Where We Do All The Drawing
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
glLoadIdentity(); // Reset The Current Modelview Matrix
glColor3f(1.0f,1.0f,1.0f); // Set The Color To White
(*currentCam).draw();
glBindTexture(GL_TEXTURE_2D, texture[2]); // Map Texture to the Box
// Draw the Room ie Crate
glBegin(GL_QUADS);
// Front Face
glNormal3f( 0.0f, 0.0f, 9.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-9.0f, -5.0f, 9.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 9.0f, -5.0f, 9.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 9.0f, 5.0f, 9.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-9.0f, 5.0f, 9.0f);
// Back Face
glNormal3f( 0.0f, 0.0f,-9.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-9.0f, -5.0f, -9.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-9.0f, 5.0f, -9.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 9.0f, 5.0f, -9.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f( 9.0f, -5.0f, -9.0f);
// Top Face
glNormal3f( 0.0f, 9.0f, 0.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-9.0f, 5.0f, -9.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-9.0f, 5.0f, 9.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 9.0f, 5.0f, 9.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 9.0f, 5.0f, -9.0f);
// Bottom Face
glNormal3f( 0.0f,-9.0f, 0.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-9.0f, -5.0f, -9.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 9.0f, -5.0f, -9.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f( 9.0f, -5.0f, 9.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-9.0f, -5.0f, 9.0f);
// Right face
glNormal3f( 9.0f, 0.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 9.0f, -5.0f, -9.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 9.0f, 5.0f, -9.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 9.0f, 5.0f, 9.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f( 9.0f, -5.0f, 9.0f);
// Left Face
glNormal3f(-9.0f, 0.0f, 0.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-9.0f, -5.0f, -9.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-9.0f, -5.0f, 9.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-9.0f, 5.0f, 9.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-9.0f, 5.0f, -9.0f);
glEnd();
glLoadIdentity();
lister.drawObjects(currentCam); //so far just calling the box code above for multiple objects
Finally, the drawObjects method:
Code:
void ObjectList::drawObjects(Camera *cam)
{
Object* current=head;
while(current !=NULL)
{
glLoadIdentity();
(*cam).draw();
(*current).draw();
current=(*current).getNext();
}
}
The problems are the following:
-All the boxes appear brown
-The inside of the room ie crate are pitch black, but the outside is textured and lit
Thanks, and sorry for all the code, but it's better to have it and not need it, right?