PDA

View Full Version : simple question about normals



honny
01-10-2003, 01:51 AM
i have some trouble with normals.

i have an application drawing lot of quads, for each quads i needs its normal.

so for each quad i have a loop that sounds like this :

glBegin(GL_QUADS);

glNormal3f(n0,n1,n2);
glVertex3f(i,random,j);
glVertex3f(i+1,random,j);
glVertex3f(i+1,random,j+1);
glVertex3f(i,random,j+1);

glEnd();

it is not exactly like this but it is the meaning .

the problem is that the values of normal is always 0.

what is wrong ?

talking of math i know that a normal for a surface is :

(v0-v1) x (v1-v2)

where v0,v1,v2 are 3 vertices of the surfaces.
in my case they are 3 vertices of my quad.

T
01-10-2003, 01:55 AM
I recommend calculating the normal as:

N = (v1 - v0) x (v2 - v0)

where 'x' means the cross product.

jebus
01-10-2003, 06:51 AM
yes, your normal calculation is incorrect. use the right hand rule to remember which vectors to cross for a normal. point fingers of right hand along first vector, curl fingers toward second vector and thumb is your normal. vertices must be in counter-clockwise order for this to work.

EDIT: this was meant for the thread creator, not you T http://www.opengl.org/discussion_boards/ubb/smile.gif

jebus

[This message has been edited by jebus (edited 01-10-2003).]

01-10-2003, 08:24 AM
You can use this function:

void calcNormal(float v[3][3],float out[3])
{ int x,y;
x=0;y=1;z=2;

v1[x]=v[0][x]-v[1][x];
v1[y]=v[0][y]-v[1][y];
v1[z]=v[0][z]-v[1][z];

v2[x]=v[1][x]-v[2][x];
v2[y]=v[1][y]-v[2][y];
v2[z]=v[1][z]-v[2][z];

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];
}