PDA

View Full Version : Normals and Transformations



dvm
12-04-2003, 05:56 PM
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.

flare
12-04-2003, 08:56 PM
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!

dvm
12-04-2003, 09:45 PM
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!

Relic
12-05-2003, 01:28 AM
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.

croce
12-08-2003, 06:46 AM
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?