Lighting a darned cube...

Okay, here goes: I’m trying to draw a unit cube. This works OK. I try to add lighting, and it doesn’t work very well. My professor said that cubes are easy to do with lighting because the vector normals are just the vectors themselves. However, the cube looks quite strange when I add lighting… it looks like the back part of the cube (when I rotate the scene around) is kind of inverted… What am I doing wrong here?

Perhaps an easier quesion is: what are the normals of a unit vector, and when do I call glNormal? Before or after glVertex3*?

here’s a calc normals function its from 3-d graphics programming with open gl by clayton walnum a good book that i recomend to people just starting 3d programming

void CLightingView::CalcNormal(double *p1, double *p2, double *p3, double *n) {
double a[3], b[3];
a[0] = p2[0] - p1[0];
a[1] = p2[1] - p1[1];
a[2] = p2[2] - p1[2];
b[0] = p3[0] - p1[0];
b[1] = p3[1] - p1[1];
b[2] = p3[2] - p1[2];

n[0] = a[1] * b[2] - a[2] * b[1];
n[1] = a[2] * b[0] - a[0] * b[2];
n[2] = a[0] * b[1] - a[1] * b[0];

double length = sqrt(n[0]*n[0]+n[1]*n[1]+n[2]*n[2]);
if (length != 0)
{
	n[0] = n[0] / length;
	n[1] = n[1] / length;
	n[2] = n[2] / length;
} 

}

and here’s a call to it the important part is up top the double p# and n then it calls the function the rest is just there so you know where the numbers come from

void CLightingView::DrawCube()
{
glTranslatef(0.0f, 0.0f, -3.0f);
glRotatef(20.0f, 1.0f, 0.0f, 0.0f);
glRotatef(20.0f, 0.0f, 1.0f, 0.0f);
//glRotatef(20.f, 1.0f, 0.0f, 0.0f);

double p1[] = { -0.5, 0.5, 0.5};
double p2[] = { -0.5, -0.5, 0.5};
double p3[] = { 0.5, -0.5, 0.5};
double n[3];
CalcNormal(p1, p2, p3, n);

glBegin(GL_POLYGON);
	glNormal3f((GLfloat)n[0], (GLfloat)n[1], (GLfloat)n[2]);
	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();

p1[0] = 0.5; p1[1] = 0.5; p1[2] = 0.5;
p2[0] = 0.5; p2[1] = -0.5; p2[2] = 0.5;
p3[0] = 0.5; p3[1] = -0.5; p3[2] = -0.5;
CalcNormal(p1, p2, p3, n);

glBegin(GL_POLYGON);
	glNormal3f((GLfloat)n[0], (GLfloat)n[1], (GLfloat)n[2]);
	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();

p1[0] = 0.5; p1[1] = 0.5; p1[2] = -0.5;
p2[0] = 0.5; p2[1] = -0.5; p2[2] = 0.5;
p3[0] = -0.5; p3[1] = -0.5; p3[2] = -0.5;
CalcNormal(p1, p2, p3, n);

glBegin(GL_POLYGON);
	glNormal3f((GLfloat)n[0], (GLfloat)n[1], (GLfloat)n[2]);
	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();

p1[0] = -0.5; p1[1] = 0.5; p1[2] = -0.5;
p2[0] = 0.5; p2[1] = -0.5; p2[2] = -0.5;
p3[0] = -0.5; p3[1] = -0.5; p3[2] = 0.5;
CalcNormal(p1, p2, p3, n);

glBegin(GL_POLYGON);
	glNormal3f((GLfloat)n[0], (GLfloat)n[1], (GLfloat)n[2]);
	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();

p1[0] = -0.5; p1[1] = -0.5; p1[2] = 0.5;
p2[0] = -0.5; p2[1] = -0.5; p2[2] = -0.5;
p3[0] = 0.5; p3[1] = -0.5; p3[2] = -0.5;
CalcNormal(p1, p2, p3, n);

glBegin(GL_POLYGON);
	glNormal3f((GLfloat)n[0], (GLfloat)n[1], (GLfloat)n[2]);
	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();

p1[0] = -0.5; p1[1] = 0.5; p1[2] = -0.5;
p2[0] = -0.5; p2[1] = 0.5; p2[2] = 0.5;
p3[0] = 0.5; p3[1] = 0.5; p3[2] = 0.5;
CalcNormal(p1, p2, p3, n);

glBegin(GL_POLYGON);
	glNormal3f((GLfloat)n[0], (GLfloat)n[1], (GLfloat)n[2]);
	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();

}