Normals and Transformations

Hi, my problem is: I’m creating a cube through 6 polygons (based on an example in OpenGL Game Programming) and use one normal for each side. The code is the following:

void DrawCube(float xPos, float yPos, float zPos)
{
glPushMatrix();
glTranslatef(xPos, yPos, zPos);
glBegin(GL_POLYGON);
glNormal3f(0.0f, 1.0f, 0.0f); // top face
glVertex3f(0.5f, 0.5f, 0.5f);
glVertex3f(0.5f, 0.5f, -0.5f);
glVertex3f(-0.5f, 0.5f, -0.5f);
glVertex3f(-0.5f, 0.5f, 0.5f);
glEnd();
glBegin(GL_POLYGON);
glNormal3f(0.0f, 0.0f, 1.0f); // front face
glVertex3f(0.5f, 0.5f, 0.5f);
glVertex3f(-0.5f, 0.5f, 0.5f);
glVertex3f(-0.5f, -0.5f, 0.5f);
glVertex3f(0.5f, -0.5f, 0.5f);
glEnd();
glBegin(GL_POLYGON);
glNormal3f(1.0f, 0.0f, 0.0f); // right face
glVertex3f(0.5f, 0.5f, 0.5f);
glVertex3f(0.5f, -0.5f, 0.5f);
glVertex3f(0.5f, -0.5f, -0.5f);
glVertex3f(0.5f, 0.5f, -0.5f);
glEnd();
glBegin(GL_POLYGON);
glNormal3f(-1.0f, 0.0f, 0.0f); // left face
glVertex3f(-0.5f, 0.5f, 0.5f);
glVertex3f(-0.5f, 0.5f, -0.5f);
glVertex3f(-0.5f, -0.5f, -0.5f);
glVertex3f(-0.5f, -0.5f, 0.5f);
glEnd();
glBegin(GL_POLYGON);
glNormal3f(0.0f, -1.0f, 0.0f); // bottom face
glVertex3f(-0.5f, -0.5f, 0.5f);
glVertex3f(-0.5f, -0.5f, -0.5f);
glVertex3f(0.5f, -0.5f, -0.5f);
glVertex3f(0.5f, -0.5f, 0.5f);
glEnd();
glBegin(GL_POLYGON);
glNormal3f(0.0f, 0.0f, -1.0f); // back face
glVertex3f(0.5f, -0.5f, -0.5f);
glVertex3f(-0.5f, -0.5f, -0.5f);
glVertex3f(-0.5f, 0.5f, -0.5f);
glVertex3f(0.5f, 0.5f, -0.5f);
glEnd();
glPopMatrix();
}

In my display (or render) function I rotate the cube around the 3 axes (3 seperate calls to glRotatef). Does opengl take into account the rotations for the calculation of normals or do they have to be recalculated for each frame? What I’m asking is, is this example correct (since the cube is rotating?) from the normal calculation point of view? Or do I have to use something like:

float normalArray[3][6];

CalculateNormal(normalArray);

for each frame? Thanks.

No you dont need to update the Normal everytime. When you call the rotate and translate calls, openGL takes care of finding out the new coordinates for your vertices and also your new normals.

You basically create the object in local coordinates and give the normal accordingly. When you rotate/translate the object to some new place it is the responibility of OpenGL to update the coordinates and normal according to the new position.

So, you go ahead and try it out. It should work as it is. Lemme know if there is any problem!

Yap, everything worked fine. you can check out the finished result in
http://users.epp.teiher.gr/grafiki/files/exercises/lights.rar
Thanks for the answer. It was a rather silly question but it really bugged me!

If you want to be smart, never put constant attributes after the glBegin, in your case move the glNormals before the glBegin.
Or even better, let them in but use one GL_QUADS primitive for the whole cube.

Originally posted by Relic:
If you want to be smart, never put constant attributes after the glBegin, in your case move the glNormals before the glBegin.
why is that? what do you mean?