PDA

View Full Version : Normal Calculation

seamuslawless
02-27-2003, 07:48 AM
Hi,
I am creating a program to draw 3D terrain. I draw individual contours at different heights and use a list of GL_TRIANGLES to join the contours and make the 3D terrain. I am having difficulty calculating the normals for the triangle list however and am getting some very weird effects.

Currently the was I calculate the normals is to take two lines of the triangle

^ /\
| / \
| /____\
|__________>

and calc the normal as follows

Vector3 crossProduct(Vector3 v1, Vector3 v2, Vector3 v3)
{
Vector3 e1, e2;

e1 = Vector3(v2.x - v1.x,
v2.y - v1.y,
v2.z - v1.z);

e2 = Vector3(v3.x - v1.x,
v3.y - v1.y,
v3.z - v1.z);

return Vector3(e1.y * e2.z
- e1.z * e2.y,
-e1.x * e2.z
+ e1.z * e2.x,
e1.x * e2.y
- e1.y * e2.x);
}

I'm calculating one normal per triangle. Does this calculation look correct ?

Any help that you can give will be much appreciated !

PM
02-27-2003, 08:14 AM
Vector3 crossProduct(Vector3 v1, Vector3 v2, Vector3 v3)
{
Vector3 e1, e2;
e1 = Vector3(v1.x-v2.x, v1.y-v2.y, v1.z-v2.z);
e2 = Vector3(v2.x-v3.x, v2.y-v3.y, v2.z-v3.z);
return Vector3(e1.y*e2.z-e1.z*e2.z, e1.z*e2.x-e1.x*e2.z, e1.x*e2.y-e1.y*e2.x)
}

That should do it.