badass

05-22-2010, 05:50 AM

im trying to set up lighting for my scene and apparently its not working..below is my calculate normal function to get normal for a square.. is it correct?

void normal (point p1,point p2,point p3,point p4, point &normal)

{

point a, b,c;

// calculate the vectors A and B

// note that v[3] is defined with counterclockwise winding in mind

// a

a.x = p3.x - p2.x;

a.y = p3.y - p2.y;

a.z = p3.z - p2.z;

// b

b.x = p2.x - p1.x;

b.y = p2.y - p1.y;

b.z = p2.z - p1.z;

// c

c.x = p1.x - p4.x;

c.y = p1.y - p4.y;

c.z = p1.z - p4.z;

// calculate the cross product and place the resulting vector

// into the address specified by vertex_t *normal

normal.x = c.x*((a.y * b.z) - (a.z * b.y));

normal.y = c.y*((a.z * b.x) - (a.x * b.z));

normal.z = c.z*((a.x * b.y) - (a.y * b.x));

// normalize

float len = (float)(sqrt((double)((normal.x * normal.x) + (normal.y * normal.y) + (normal.z * normal.z))));

// avoid division by 0

if (len == 0.0f)

len = 1.0f;

// reduce to unit size

normal.x /= len;

normal.y /= len;

normal.z /= len;

}

void normal (point p1,point p2,point p3,point p4, point &normal)

{

point a, b,c;

// calculate the vectors A and B

// note that v[3] is defined with counterclockwise winding in mind

// a

a.x = p3.x - p2.x;

a.y = p3.y - p2.y;

a.z = p3.z - p2.z;

// b

b.x = p2.x - p1.x;

b.y = p2.y - p1.y;

b.z = p2.z - p1.z;

// c

c.x = p1.x - p4.x;

c.y = p1.y - p4.y;

c.z = p1.z - p4.z;

// calculate the cross product and place the resulting vector

// into the address specified by vertex_t *normal

normal.x = c.x*((a.y * b.z) - (a.z * b.y));

normal.y = c.y*((a.z * b.x) - (a.x * b.z));

normal.z = c.z*((a.x * b.y) - (a.y * b.x));

// normalize

float len = (float)(sqrt((double)((normal.x * normal.x) + (normal.y * normal.y) + (normal.z * normal.z))));

// avoid division by 0

if (len == 0.0f)

len = 1.0f;

// reduce to unit size

normal.x /= len;

normal.y /= len;

normal.z /= len;

}