View Full Version : Calculating Surface Normals

G4Soon

06-10-2001, 04:49 PM

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?

ngill

06-10-2001, 07:38 PM

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=ry1*rz2-rz1*ry2

ny=rz1*rx2-rx1*rz2

nz=rx1*ry2-ry1*rx2

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

len=sqrt(nx*nx+ny*ny+nz*nz)

if (len>0) {

nx*=(1/len)

ny*=(1/len)

nz*=(1/len)

} else {

/* oops, you've got a problem http://www.opengl.org/discussion_boards/ubb/smile.gif */

}

That should be it. Enjoy http://www.opengl.org/discussion_boards/ubb/smile.gif

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.

G4Soon

06-11-2001, 11:41 AM

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!

Powered by vBulletin® Version 4.2.2 Copyright © 2016 vBulletin Solutions, Inc. All rights reserved.