Skip to main content
gamedev.net
🔒 Locked

normal calculation

Started by VViCKiD 6 replies 1k views
Original Post
VViCKiD
VViCKiD
Hey guys... I have a real noob question... Can someone body please tell me how to calculate the normal for one vertice of a triangle ? I need to calculate the normals for each vertice defining a triangle for Garaud shading or Phong Shading ... Thanks in advance.. Cheers...
PlutoWarrior
PlutoWarrior
I don't have a lot of time to explain it in detail, but it has to do with taking the cross product of the two vectors that extend from the vertex you want to calculate the normal for. That will give you the normal. Do a search for it on google or even on this site and you should come up with some good stuff.
---------------------------------------------Warning: This post may contain traces of Q-Basic
smitty1276
smitty1276
Triangle points, counter clockwise, are P0, P1, and P2. (P1-P0).cross(P2-P0) gives you the normal to the polygon. The normals will all be the same if you want a polygonal surface. If you want a smooth contoured surface, you will want to average the normals of all of the polys around each vertex.
VViCKiD
VViCKiD
Ok thanks for replying guys...
So if I want smooth contoured surfaces of one triangle, would I need to calculate 3 normals (one for each vertex) ? That's what I am confused about..
mother
mother
First you need to calculate each triangle's normal:

Vector CalcTriangleNormal(Vector p1, Vector p2, Vector p3){    Vector edge1 = v2 - v1;    Vector edge2 = v3 - v1;    Vector normal = CrossProduct(edge1, edge2);    return normal;}


Then, for each vertex, you need to figure out which triangles it is a part of, and average those triangles' normals. This gives you the vertex normal.

This assumes that the mesh is an approximation of a smooth surface.

To average the normals, simply add them all up and scale the result by 1/numNormals.
VViCKiD
VViCKiD
Excellent !!

I get it now !!

Thanks guys !!!
JohnBolton
JohnBolton
There is more than one way of creating normals in order to simulate a smooth surface. The easiest way is this:
  1. Compute a normal for each triangle.
  2. For each vertex, "average" the normals of all the triangles that use this vertex. This is known as an "unweighted" normal.
You can also compute a "weighted" normal by scaling each triangle's normal by the area of the triangle before adding. You can also compute a vertex normal (weighted or unweighted) directly from the edges that fan out from it.

Quote:
Original post by mother
To average the normals, simply add them all up and scale the result by 1/numNormals.

No. To "average" the normals, you add them and then normalize the result. Dividing by the number of vectors will not give you a unit vector.

John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
mother
mother
Quote:
Original post by mother
To average the normals, simply add them all up and scale the result by 1/numNormals.

No. To "average" the normals, you add them and then normalize the result. Dividing by the number of vectors will not give you a unit vector.[/quote]

Right you are. My mistake. Thanks for the correction. :)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.