# Difference between revisions of "Calculating a Surface Normal"

m (revert page) |
m (→Calculating a surface normal) |
||

Line 1: | Line 1: | ||

+ | == Algorithm == | ||

A surface normal for a triangle can be calculated by taking the vector cross product of two edges of that triangle. The order of the vertices used in the calculation will affect the direction of the normal (in or out of the face w.r.t. winding). | A surface normal for a triangle can be calculated by taking the vector cross product of two edges of that triangle. The order of the vertices used in the calculation will affect the direction of the normal (in or out of the face w.r.t. winding). | ||

− | So for a triangle p1, p2, p3, if the vector U = p2 - p1 and the vector V = p3 - p1 then the normal N = U X V and can be calculated by: | + | So for a triangle p1, p2, p3, if the vector ''U'' = p2 - p1 and the vector ''V'' = p3 - p1 then the normal ''N'' = ''U X V'' and can be calculated by: |

− | + | ''N''x = ''U''y''V''z - ''U''z''V''y | |

− | + | ''N''y = ''U''z''V''x - ''U''x''V''z | |

− | + | ''N''z = ''U''x''V''y - ''U''y''V''x | |

+ | |||

+ | == Pseudo-code == | ||

+ | Given that a vector is a structure composed of three floating point numbers and a Triangle is a structure composed of three Vectors, based on the above definitions: | ||

+ | <pre> | ||

+ | Begin Function CalculateSurfaceNormal (Input Triangle) Returns Vector | ||

+ | |||

+ | Set Vector U to (Triangle.p2 minus Triangle.p1) | ||

+ | Set Vector V to (Triangle.p3 minus Triangle.p1) | ||

+ | |||

+ | Set Normal.x to (multiply U.y by V.z) minus (multiply U.z by V.y) | ||

+ | Set Normal.y to (multiply U.z by V.x) minus (multiply U.x by V.z) | ||

+ | Set Normal.z to (multiply U.x by V.y) minus (multiply U.y by V.x) | ||

+ | |||

+ | Returning Normal | ||

+ | |||

+ | End Function | ||

+ | </pre> | ||

[[Category:Algorithm]] | [[Category:Algorithm]] |

## Revision as of 15:58, 23 August 2008

## Algorithm

A surface normal for a triangle can be calculated by taking the vector cross product of two edges of that triangle. The order of the vertices used in the calculation will affect the direction of the normal (in or out of the face w.r.t. winding).

So for a triangle p1, p2, p3, if the vector *U* = p2 - p1 and the vector *V* = p3 - p1 then the normal *N* = *U X V* and can be calculated by:

*N*x = *U*y*V*z - *U*z*V*y

*N*y = *U*z*V*x - *U*x*V*z

*N*z = *U*x*V*y - *U*y*V*x

## Pseudo-code

Given that a vector is a structure composed of three floating point numbers and a Triangle is a structure composed of three Vectors, based on the above definitions:

Begin Function CalculateSurfaceNormal (Input Triangle) Returns Vector Set Vector U to (Triangle.p2 minus Triangle.p1) Set Vector V to (Triangle.p3 minus Triangle.p1) Set Normal.x to (multiply U.y by V.z) minus (multiply U.z by V.y) Set Normal.y to (multiply U.z by V.x) minus (multiply U.x by V.z) Set Normal.z to (multiply U.x by V.y) minus (multiply U.y by V.x) Returning Normal End Function