Calculating Surface Normals

Here’s my situation:

Let’s say I have 3 verticies: v1,v2 and v3.

how would I calculate a surface normal?

assume that when the points are laballed counter-clockwise relative to you, the normal vector points towards you.

example:

      v1

v2

       v3

the normal would be coming straight at you.

how would I calculate the exact surface normal vector using the 3 sets of x,y, and z values?

take the cross product… and by right hand rule the normal would face towards you.

cross product can also be seen as the determinant of these three vectors:

<i,j,k>
<v2-v1>
<v3-v1>

correct me if I am wrong… it’s been a little while

You simply do (in pseudocode) :

rx1=v1x-v2x
ry1=v1y-v2y
rz1=v1z-v2z
rx2=v3x-v2x
ry2=v3y-v2y
rz2=v3z-v2z

nx=ry1rz2-rz1ry2
ny=rz1rx2-rx1rz2
nz=rx1ry2-ry1rx2

Ok, now we have to normalize it (make length 1) :

len=sqrt(nxnx+nyny+nznz)
if (len>0) {
nx
=(1/len)
ny*=(1/len)
nz*=(1/len)
} else {
/* oops, you’ve got a problem */
}

That should be it. Enjoy

Note : this might be for clockwise, I simply ripped it from some working code of mine. Simply invert the normal for counterclockwise if that’s the case.

Thanks, guys!

I actually just picked up an Algebra & Geometry textbook from my math teacher today, and got the necessary math from there. but thanks anyway!