PDA

View Full Version : Calculating normals with set vertexes?

ImpactDNI
10-03-2002, 09:32 AM
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?

10-03-2002, 11:02 AM
I am looking for the same information, if i find i will post it here, if you do could you do the same?

thanks.

nexusone
10-03-2002, 11:34 AM
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]=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?

pleopard
10-03-2002, 06:46 PM
Yeah, what Nex said ... but break the quad into two triangles and compute a normal for each triangle ... problem solved ... I promise you that you really don't want to go off doing this for quads, even the slightest aberation in the surface flatness will hose a lit model.

ImpactDNI
10-07-2002, 09:15 AM
When i try to send getFaceNormal(n1, v1, v2, v3); it has a problem, it says it cant convert the v1 to v1[3]

heres my code (look for ********)

float normal(int num, float v1[3], float v2[3], float v3[3]){
float newvects[2][3];
float points[3][3];
for (int a = 0; a<3; a++){
points[0][a] = v1[a];
points[1][a] = v2[a];
points[2][a] = v3[a];
}
}

int v1[3] = {-5,1,0};
int v2[3] = {-5,1,35};
int v3[3] = {5,1,35};
int v4[3] = {5,1,0};
int v5[3] = {-5,-1,0};
int v6[3] = {-5,-1,35};
int v7[3] = {5,-1,35};
int v8[3] = {5,-1,0};
int v9[3] = {0,0,45};
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glColor3f(.8,.8,.8);
float n1[3];
********** normal(1, v1, v2, v3);
glVertex3iv(v1);
glVertex3iv(v2);
glVertex3iv(v3);
glVertex3iv(v4);

glVertex3iv(v5);
glVertex3iv(v6);
glVertex3iv(v7);
glVertex3iv(v8);

glVertex3iv(v1);
glVertex3iv(v5);
glVertex3iv(v6);
glVertex3iv(v2);

glVertex3iv(v4);
glVertex3iv(v8);
glVertex3iv(v7);
glVertex3iv(v3);

glVertex3iv(v1);
glVertex3iv(v5);
glVertex3iv(v8);
glVertex3iv(v4);
glEnd();
glBegin(GL_TRIANGLES);
glVertex3iv(v2);
glVertex3iv(v3);
glVertex3iv(v9);

glVertex3iv(v6);
glVertex3iv(v7);
glVertex3iv(v9);

glVertex3iv(v2);
glVertex3iv(v6);
glVertex3iv(v9);

glVertex3iv(v3);
glVertex3iv(v7);
glVertex3iv(v9);
glEnd();
glutSwapBuffers();
}

the first entry "1" is just for which n# the normal is...