🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Some doubt...help me

Started by
5 comments, last by Bart Simpson 24 years ago
Hi, I''ve this problems... If I have a triangle face crooked (not perpendicular or parallel on axes) how can I declare the normal? For example how is the normal for the face glVertex3f(-1.0, -1.0, 1.0); glVertex3f(1.0, -1.0, 1.0); glVertex3f(0.0, 1.0, 0.0); How can I use better resolutions (like 1024x768 or 800x600)? I have just replaced 640,480 with the new values but don''t work fine... Thanks
Advertisement

Well this is not the best way but it will get you started.

/* WARNING NOT COMPLETE OR VERY GOOD CODE */
void norm( float *v1, float *v2, float *v3, float *normalv)
{
double ax, ay ,az;
double bx, by, bz;
double len;

ax = v1[0] - v2[0];
ay = v1[1] - v2[1];
az = v1[2] - v2[2];
bx = v3[0] - v2[0];
by = v3[1] - v2[1];
bz = v3[2] - v2[2];

normalv[0] = +(ax*bz - az*by);
normalv[1] = -(ax*bz - az*bx);
normalv[2] = +(ax*by - ay*bx);
/* this will give you a normal for a triangle but you should normalize it */

len = sqrtf( pow(normalv[0], 2) + pow(normalv[1], 2) + pow(normal, 2));
normalv[0] /= len;
normalv[1] /= len;
normalv[2] /= len;
}

This is from memory so if it dose not work sorry but you should be able to get the idea of what is going of from that.
there is probably a much better way of doing this but this is what I use hope this helped
Validus:Is there a better way for computing normals than the one you posted?(computing crossproduct of two vectors of the triangles)IF so do you know of any documents describing this method or could you explain it?Thanx

-=Jimmy=-
-=Jimmy=-
Nate (nate.scuzzy.net)''s tutorial on vectors and normals explains pretty much what Validus wrote, that''s what I use. What you should do is calculate normals when your program loads, not every frame. If you are using objects of some type (I created my own w/file format) then it is easier, but if you are just writing polygons in by hand then you might want to use something such as glEnable( GL_AUTO_NORMAL ) to have OpenGL calculate the normals for you (I think that''s the right one).

Morgan
I learned the cross prod way from the Red book, Ii describes a better way if you know that mathematical description of the surface that you are trying to find. how ever I am not that good at calcus so I have not been able to figure out how it works. If any one has some code to find the normal for an analytic surface I would be interested to see it. Of course most poly data I use I do not know the equations f there surfers.
Here is all what the others had said before in a compact function:


#define SubtVer(AB, a, b)\
((AB).x = (b).x-(a).x,\
(AB).y = (b).y-(a).y,\
(AB).z = (b).z-(a).z)

#define CrossProductVer(AB, a, b)\
((AB).x = (a).y*(b).z-(a).z*(b).y,\
(AB).y = (a).z*(b).x-(a).x*(b).z,\
(AB).z = (a).x*(b).y-(a).y*(b).x)

typedef struct
{
float x,y,z; // The vertex coordinates
} VERTEX;

// Calculates the normal of a plane
void NormalizePlane(VERTEX *ResultV, VERTEX V1, VERTEX V2, VERTEX V3)
{ // begin NormalizePlane()
VERTEX V1_V2, V1_V3;

SubtVer(V1_V2, V1, V2);
SubtVer(V1_V3, V1, V3);
CrossProductVer(*ResultV, V1_V2, V1_V3);
} // end NormalizePlane()


This funktion calculates the normal of a plane and store the normalized vertex in the pointer variable. It''s easy to understand.
Essentially, computing a plane normal is about a cross product.
The cross product equation operates on two vectors (both going into or out of a point) and is like this:

a x b = mod(a)mod(b)sin(angle_between)normalalised normal

a crappy description, but thats one identity.
What you are alooking for is the normal, you could re-arrange the equation and mess around a bit to get it.
But the easiest way is to use a matrix determinent to get you the components.

I could try and write an example matrix out, but it would look horrible in the text enviroment.
Instead, I''ll give you a simple function that computes the cross product vector (the normal)

This assumes that vectors are arrays of floats

void CrossProduct (vector3d a, vector3d b, vector3d cross)
{
cross[0] = a[1]*b[2] - a[2]*b[1];
cross[1] = a[2]*b[0] - a[0]*b[2];
cross[2] = a[0]*b[1] - a[1]*b[0];
}

The above will probably come out crappy in the board, but you should get the idea.

-Mezz

This topic is closed to new replies.

Advertisement