This topic is now archived and is closed to further replies.



Recommended Posts

Anyone have some simple code that will give you a normal if you put in three vertices (clockwise order) for simplicitys sake lets say the verticies ar set up like this: struct Vert { float x,y,z; }; struct Face { Vert Points[3]; }; thanks ----------------------------- Blue*Omega (Insert Witty Quote Here)

Share this post

Link to post
Share on other sites
For each face or triangle you do something like this:

// Make a silly struct
Struct Point3D {
float x,y,z ;
} Point3D

// Declare some stuff....
Point3D Pointa, Pointb, Pointc, out;


glNormal( out.x , out.y, out.z );

glVertex( Pointa.x ,Pointa.y, Pointa.z );
glVertex( Pointb.x,Pointb.y, Pointb.z );
glVertex( Pointc.x,Pointc.y, Pointc.z );

You''re problem is what''s out.x, out.y, out.z. Easy,

Use these simple functions, calling CalcNormal passing in a, b and c and getting back out filled with the normal coordinates of the plane a,b,c

void GLCalcNormal(Point3D a, Point3D b, Point3D c, Point3D *out)
Point3D v1,v2;

// Calculate two vectors from the three points
v1.x = a.x - b.x;
v1.y = a.y - b.y;
v1.z = a.z - b.z;

v2.x = b.x - c.x;
v2.y = b.y - c.y;
v2.z = b.z - c.z;

// Take the cross product of the two vectors to get
// the normal vector which will be stored in out
out->x = v1.y * v2.z - v1.z * v2.y;
out->y = v1.z * v2.x - v1.x * v2.z;
out->z = v1.x * v2.y - v1.y * v2.x;

// Normalize the vector (shorten length to one)

void GLElement::GLReduceToUnit(Point3D *vector)
float length;

// Calculate the length of the vector
length = (float)sqrt( (vector->x * vector->x ) +
(vector->y * vector->y ) +
(vector->z * vector->z ) );

// Keep the program from blowing up by providing an exceptable
// value for vectors that may calculated too close to zero.
if (length == 0.0f) length = 1.0f;

// Dividing each element by the length will result in a
// unit normal vector.
vector->x /= length;
vector->y /= length;
vector->z /= length;
//-- Modelling and animation in every dimension --//

Share this post

Link to post
Share on other sites