• ### What is your GameDev Story?

#### Archived

This topic is now archived and is closed to further replies.

# How do you calculate normals for a cube?

This topic is 6395 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Each corner of the cube has 3 vertices for the 3 touching faces. How do I get the normals for each vertice of each corner? Thank you for your help, -Corillian

##### Share on other sites
Check out Nehe''s lesson 7/8 tut. That should help you out a lot (http://nehe.gamedev.net in case u didn''t know)

------------------------------
Trent (ShiningKnight)
E-mail me
OpenGL Game Programming Tutorials

##### Share on other sites
Those tutorials have nothing to do with per vertex normal calculations. Lesson 8 has nothing to do with normals and lesson 7 only discusses face normals briefly.

##### Share on other sites
a unit normal = vector / vector''s size . magnitude/size =
sqrt(x2 + y2 + z2). read up on cross product''s too. hope that helped.

##### Share on other sites
Corillian: uhh, he asked how to calculate hte normals for a cube, and what does lesson 7 do? Calculate the normals for a cube, so it can be LIGHTED.

------------------------------
Trent (ShiningKnight)
E-mail me
OpenGL Game Programming Tutorials

##### Share on other sites
Take the average of the face normals of every face that uses the vertex.

There are better ways of calculating like weighting by face angle. You can read in the 3D Studio Max SDK how this method works.

Edited by - bobo on July 20, 2001 3:31:14 AM

##### Share on other sites
Each face has 4 vertices and 4 lines (vectors) ajoining them.
To calculate the normal get the cross product of
any 2 of the 4 vectors.
I have included code for a cube with the surface
normals stated explicitly.
The normal to the top face is the unit vector pointing up
(0,1,0)
The normal to the bottom face is the unit vector pointing down
(0,-1,0) etc.
Hope it works

void DrawBox()
{
glPushMatrix();
glTranslatef(position.x,position.y,position.z);
glBegin(GL_POLYGON);
glNormal3f(0.0f, 1.0f, 0.0f); //top face Normal

glVertex3f(0.5f, 0.5f, 0.5f); // top face
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); // N front face

glVertex3f(0.5f, 0.5f, 0.5f); // front face
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); // N right face

glVertex3f(0.5f, 0.5f, 0.5f); // right face
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); // N left face

glVertex3f(-0.5f, 0.5f, 0.5f); // left face
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); // N bottom face

glVertex3f(0.5f, 0.5f, 0.5f); // bottom face
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); // N back face

glVertex3f(0.5f, 0.5f, -0.5f); // back face
glVertex3f(-0.5f, 0.5f, -0.5f);
glVertex3f(-0.5f, -0.5f, -0.5f); glVertex3f(0.5f, -0.5f, -0.5f);
glEnd();

glPopMatrix();

}

##### Share on other sites
Ok, thanks for all of your help =)

##### Share on other sites
Guys... I don''t think Corillian was talking about face normals.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 15
• 9
• 11
• 9
• 9
• ### Forum Statistics

• Total Topics
634134
• Total Posts
3015749
×