Here is for normal of a triangle, as long as the quad is flat it may work ok using only three sides.
float n1[3];
getFaceNormal(n1, v1, v2, v3);
glNormal3fv( n1 );
void normalize(float * vect) //scales a vector a length of 1
{
float length;
int a;
length=sqrt( //A^2 + B^2 + C^2 = length^2
pow(vect[0],2)+
pow(vect[1],2)+
pow(vect[2],2)
);
for (a=0;a<3;++a) //divides vector by its length to normalise
{
vect[a]/=length;
}
}
void crossProduct(float *c,float a[3], float b[3]) //finds the cross product of two vectors
{
c[0]=a[1]*b[2] - b[1]*a[2];
c[1]=a[2]*b[0] - b[2]*a[0];
c[2]=a[0]*b[1] - b[0]*a[1];
}
void getFaceNormal(float *norm,float pointa[3],float pointb[3],float pointc[3])
{
float vect[2][3];
int a,b;
float point[3][3];
for (a=0;a<3;++a)
{
point[0][a]=pointa[a]; //copies points into point
point[1][a]=pointb[a];
point[2][a]=pointc[a];
}
for (a=0;a<2;++a) //calculates vectors from point[0] to point[1]
{ //and point[0] to point[2]
for (b=0;b<3;++b)
{
vect[a][b]=point[2-a][b]-point[0][b];
}
}
crossProduct(norm,vect[0],vect[1]); //calculates vector at 90° to to 2 vectors
normalize(norm); //makes the vector length 1
}
Originally posted by ImpactDNI:
[b]say i have these 3 verticies…
int v1[3] = {-5,1,0};
int v2[3] = {-5,1,35};
int v3[3] = {5,1,35};
int v4[3] = {5,1,0};
i make a GL_QUADS with these 4 verticies…
how do i find the normal? (i want to have a separate function that i could send 3 of the verticies to and have it return the normal…
someone help me?[/b]