how to make sure my normals are always pointing "towards" me...

Started by
3 comments, last by Spencer 20 years, 11 months ago
Hi all! I don''t know if this is a "pure" math question...but i think it is more math than graphics so here i go: When i load an .md2 file in my program i want to calculate the normals for each triangle in it. The triangles are connected like a triangle strip and the vertecies are numbered CCW..(if i am not mistaken?). Now, to calculate the normal to a triangle i use the cross product of the vectors of this polygon (excuse the somewhat strange explantion, but you know what i mean) but here also comes my problem. How can i know if the resulting normal will be "towards" me or "away" from me?? I guess this problems reduces to how i pick the vertices that make up my vectors for the cross product...i searched the forums but i didnt find an answer to this...hope someone can help me... thank you --Spencer "Relax, this dragon is sleeping..."
--Spencer"All in accordance with the prophecy..."
Advertisement
Hi Spencer, you do seem to be on the right lines with the cross product.

You just need to take it one step further. Once you have calculated the normal for each of the polygons, you then use this normal against the current view vector.

The cross product between these will give a positive or negative result. if result > 0 the angle is less than 90 degrees so facing you, and if result < 0 then greater than 90 degrees.

I think that''s right, but can someone confirm that for me :o)
Any triangle can be CCW or CW depending on which side you look at it from. The md2 convention says that the triangles are defined in such a way that they will be visible when viewed by the camera from the CCW side. This also means that the normal vector for the triangle will point towards you when you look at it from the CCW side. Therefore, you can find a normal vector that points towards the outside of the model using the following approach. The triangle is defined by vertices v1, v2, v3, in that order.

r1 = v2 - v1
r2 = v3 - v1
outward_facing_normal = Normalize(r1.cross(r2));

Given that normal, as Count_Zero says, you have to deal with the view matrix to determine if the normal is pointing towards you (the camera) or away from you. But, Count_Zero has his dot products and cross products confused. He actually gives the right information, but should have said dot product, not cross product. Here''s a correction:

The normal points towards the camera if outward_facing_normal.DotProduct(camera_look_direction_vector) is negative, and points away from the camera if outward_facing_normal.DotProduct(camera_look_direction_vector) is positive.

Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.
Graham Rhodes Moderator, Math & Physics forum @ gamedev.net
thank you so much for your replies....gonna run home a try it out



--Spencer

"Relax, this dragon is sleeping..."
--Spencer"All in accordance with the prophecy..."
to summarise,

triangle (V0, V1, V2), with verts listed counter-clockwise (right-handed coordinate system, the 'true' one ).


N = (V1 - V0) x (V2 - V1)

camera is at position P

normal is facing the camera if

(P . N) - (V0 . N) > 0


a triangle is lit by a directional light of Direction L if

L . N < 0



[edited by - oliii on May 29, 2003 8:34:13 PM]

Everything is better with Metal.

This topic is closed to new replies.

Advertisement