Jump to content
  • Advertisement


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


Face normals?

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''ve searched the forums for this already but got no results... What is the math for calculating the normal of a triangle that is being drawn counter-clockwise? I know it has to do with cross-product and normalizing or somthing... but, can someone give me an example?

Share this post

Link to post
Share on other sites
Guest Anonymous Poster
The answer is in the question. If your triangle has vertices A, B, C (in anti-clockwise order) form the vectors A->B, and B->C. Take the cross-product of those two vectors. Normalize it. Ta-da!

Share this post

Link to post
Share on other sites
VEC3D p1,p2,p3;
VEC3D v1,v2;

v1.x = p1.x - p2.x;
v1.y = p1.y - p2.y;
v1.z = p1.z - p2.z;

v2.x = p2.x - p3.x;
v2.y = p2.y - p3.y;
v2.z = p2.z - p3.z;

//Find the cross product of v1 and v2
//Then normalize the result

[edited by - starboarder on October 9, 2003 7:28:25 PM]

Share this post

Link to post
Share on other sites
Ok, i did some extra reasearch and I wrote this code for Calculating the normals:

void calcNorms(cMesh mesh)
fVector v1, v2;
float length;
for(int i = 0; i < mesh.nFaces; i++)
v1.x = mesh.pVertices[mesh.pFaces[i].nIndices[0]].x - mesh.pVertices[mesh.pFaces[i].nIndices[1]].x;
v1.y = mesh.pVertices[mesh.pFaces[i].nIndices[0]].y - mesh.pVertices[mesh.pFaces[i].nIndices[1]].y;
v1.z = mesh.pVertices[mesh.pFaces[i].nIndices[0]].z - mesh.pVertices[mesh.pFaces[i].nIndices[1]].z;

v2.x = mesh.pVertices[mesh.pFaces[i].nIndices[1]].x - mesh.pVertices[mesh.pFaces[i].nIndices[2]].x;
v2.y = mesh.pVertices[mesh.pFaces[i].nIndices[1]].y - mesh.pVertices[mesh.pFaces[i].nIndices[2]].y;
v2.z = mesh.pVertices[mesh.pFaces[i].nIndices[1]].z - mesh.pVertices[mesh.pFaces[i].nIndices[2]].z;

mesh.pFaces[i].pNormal.x = v2.y*v1.z - v2.z*v1.y;
mesh.pFaces[i].pNormal.y = v2.z*v1.x - v2.x*v1.z;
mesh.pFaces[i].pNormal.z = v2.x*v1.y - v2.y*v1.x;

length = sqrtf(mesh.pFaces[i].pNormal.x * mesh.pFaces[i].pNormal.y + mesh.pFaces[i].pNormal.y * mesh.pFaces[i].pNormal.y + mesh.pFaces[i].pNormal.y * mesh.pFaces[i].pNormal.y);

mesh.pFaces[i].pNormal.x /= length;
mesh.pFaces[i].pNormal.y /= length;
mesh.pFaces[i].pNormal.z /= length;

but they are Not calculated correctly because I get weird results!

What did i do wrong?!?

[edited by - colinisinhere on October 9, 2003 11:21:40 PM]

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!