Calculate surface normal?

Started by
1 comment, last by flaXen 22 years, 4 months ago
Is there a function in DirectX8 to calculate the surface normal for a polygon? The only one I can find works for models only.
Advertisement
There''s no function to do it, but the formula to calculate the normal of a polygon is pretty simple: Normal = Crossproduct of V and W, where V is a vector representing one side of the polygon and W is a vector representing another side.

Calculating surface normals at each vertex is a little more complex and involves averaging normalised polygon normals for each polygon the vertex is a part of.


--------------------

Never eat anything bigger than your own head.
--------------------Never eat anything bigger than your own head.
Just to clarify, if you have a triangle composed of points represented by vectors a,b,c, then you need to get 2 edges of the triangle (p,q) by:

p=b-a;
q=c-a;

Then take the Cross Product:

n = p cross q;

Then normalize the vector (if you need a unit vector):

normalize ( n );

This gives you the unit vector normal to the surface of the triangle.

If you want the vertex normal of a given vertex just sum up the triangle face normals of every triangle surrounding the given vertex, average them, and normalize the result.

for each vertex{  vertex_normal = 0,0,0;  number_of_surrounding_faces = 0;  for each triangle  {       if vertex is part of triangle       {          vertex_normal += triangle_normal;           number_of_surrounding_faces++;       }  }   vertex_normal /= number_of_surrounding_faces;   normalize ( vertex_normal );} 


Forgive the pseudo code, since I am at home for Thanksgiving without my computer. Of course if you are using the IDirect3dMesh class, it can do the vertex normal calculations for you.

This topic is closed to new replies.

Advertisement