Jump to content
  • Advertisement

Archived

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

Calculate surface normal?

This topic is 6023 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

Is there a function in DirectX8 to calculate the surface normal for a polygon? The only one I can find works for models only.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!