Archived

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

arkansas

Calculating Normals

Recommended Posts

OK, although this is covered *many* times across the internet (try googling):

The normal to a triangle or some other polygon can be calculated by taking the cross product of two adjacent edges.

Say the triangle is represented by three (3D) points Pi.

Edge 1 can be E1 = P2 - P1
Edge 2 can be E2 = P3 - P1

Now normal N is

N = E1 X E2

Where X is the cross product, defined like this in a function:


    
inline Vector3 Cross(const Vector3& v1,const Vector3& v2)
{
double c0 = ( v1.c[1] * v2.c[2] ) - ( v1.c[2] * v2.c[1] );
double c1 = ( v1.c[2] * v2.c[0] ) - ( v1.c[0] * v2.c[2] );
double c2 = ( v1.c[0] * v2.c[1] ) - ( v1.c[1] * v2.c[0] );

return Vector3(c0,c1,c2);
}


Now if you want normals for a smooth mesh (ie normal per vertex), you need to accumulate the normals for each triangle for each vertex and then average them together (and renormalize if necessary).

[edited by - JuNC on December 26, 2002 3:14:34 PM]

Share this post


Link to post
Share on other sites
I would suggest gametutorials.com they have a nice tutorial on normals and all that. This is where i learned. also, i do beleive that NeHe has a tut on them too.

IT Administrator / Software Engineere
http://drdsoftware.cjb.net

Share this post


Link to post
Share on other sites