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.