MightyLampshade

11-16-2010, 01:59 PM

Hey, I'm new to these forums

I have a problem with my code and I decided to ask the pro's with this (my uni lecturer simply doesn't understand what I want lol).

Anyway, I got a fragment of code to work from GameDev.net as it was the most understandable and "usable" piece of code I could find on how to create surface normal vectors. Every site I've come across has ridiculous algebra which I simply don't understand. If someone could adapt the following piece of code to at least look like it would work, or explain how to create normals and normalise them.

Firstly, a little explanation. The object I am trying to create is made through a vertex array. I am unsure as to how to (or even if I can) put normal vectors into the array. I got this far and now I am stumped as to where to go or how to adapt this code. The normal part is a section of code I got off the Internet from GameDev.net, and I don't know if this could be adapted to suit my project.

Any help would be great :D

Thanks!

// Code adapted from "http://www.gamedev.net/reference/articles/article1682.asp"

// Credit goes to Ben Woodhouse

void getFaceNormal(GLfloat *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

}

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

}

}

//////////////////////////////////////////////////////

// ******** SCENE ITEMS ******************************

//////////////////////////////////////////////////////

void girderBoxNormalTest()

{

glEnableClientState(GL_VERTEX_ARRAY);

glEnableClientState(GL_COLOR_ARRAY);

glEnableClientState(GL_NORMAL_ARRAY);

typedef GLfloat point3[3];

point3 vertices[8] = {{-2.4,-0.05,-0.05},{-2.4,-0.05,0.05},

{-2.4,0.05,0.05},{-2.4,0.05,-0.05},

{2.4,-0.05,-0.05},{2.4,-0.05,0.05},

{2.4,0.05,0.05},{2.4,0.05,-0.05}};

point3 normals[18];

point3 color[8] = {{0.0,0.0,0.0},{1.0,0.0,0.0},

{1.0,1.0,0.0},{0.0,1.0,0.0},

{0.0,0.0,1.0},{1.0,0.0,1.0},

{1.0,1.0,1.0},{0.0,1.0,1.0}};

GLubyte cubeIndices[24]={0,1,2,3, 1,5,6,2, 6,7,4,5, 7,3,0,4, 3,2,6,7, 5,4,0,1};

glVertexPointer(3,GL_FLOAT,0,vertices);

glColorPointer(3,GL_FLOAT,0,color);

glNormalPointer(GL_FLOAT,0,normals);

glDrawElements(GL_QUADS, 24, GL_UNSIGNED_BYTE, cubeIndices);

glDisableClientState(GL_NORMAL_ARRAY);

glDisableClientState(GL_VERTEX_ARRAY);

glDisableClientState(GL_COLOR_ARRAY);

}

I have a problem with my code and I decided to ask the pro's with this (my uni lecturer simply doesn't understand what I want lol).

Anyway, I got a fragment of code to work from GameDev.net as it was the most understandable and "usable" piece of code I could find on how to create surface normal vectors. Every site I've come across has ridiculous algebra which I simply don't understand. If someone could adapt the following piece of code to at least look like it would work, or explain how to create normals and normalise them.

Firstly, a little explanation. The object I am trying to create is made through a vertex array. I am unsure as to how to (or even if I can) put normal vectors into the array. I got this far and now I am stumped as to where to go or how to adapt this code. The normal part is a section of code I got off the Internet from GameDev.net, and I don't know if this could be adapted to suit my project.

Any help would be great :D

Thanks!

// Code adapted from "http://www.gamedev.net/reference/articles/article1682.asp"

// Credit goes to Ben Woodhouse

void getFaceNormal(GLfloat *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

}

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

}

}

//////////////////////////////////////////////////////

// ******** SCENE ITEMS ******************************

//////////////////////////////////////////////////////

void girderBoxNormalTest()

{

glEnableClientState(GL_VERTEX_ARRAY);

glEnableClientState(GL_COLOR_ARRAY);

glEnableClientState(GL_NORMAL_ARRAY);

typedef GLfloat point3[3];

point3 vertices[8] = {{-2.4,-0.05,-0.05},{-2.4,-0.05,0.05},

{-2.4,0.05,0.05},{-2.4,0.05,-0.05},

{2.4,-0.05,-0.05},{2.4,-0.05,0.05},

{2.4,0.05,0.05},{2.4,0.05,-0.05}};

point3 normals[18];

point3 color[8] = {{0.0,0.0,0.0},{1.0,0.0,0.0},

{1.0,1.0,0.0},{0.0,1.0,0.0},

{0.0,0.0,1.0},{1.0,0.0,1.0},

{1.0,1.0,1.0},{0.0,1.0,1.0}};

GLubyte cubeIndices[24]={0,1,2,3, 1,5,6,2, 6,7,4,5, 7,3,0,4, 3,2,6,7, 5,4,0,1};

glVertexPointer(3,GL_FLOAT,0,vertices);

glColorPointer(3,GL_FLOAT,0,color);

glNormalPointer(GL_FLOAT,0,normals);

glDrawElements(GL_QUADS, 24, GL_UNSIGNED_BYTE, cubeIndices);

glDisableClientState(GL_NORMAL_ARRAY);

glDisableClientState(GL_VERTEX_ARRAY);

glDisableClientState(GL_COLOR_ARRAY);

}