airseb

01-23-2005, 01:08 PM

Hi !

I try to calculate normals per vertex, but i have wrong values. Can you help me ?

this is an idea of the result :

par vertex -0.57735 -0.57735 -0.57735

par vertex -0.0901489 0.243461 -0.965712

par vertex 0.488888 -0.637189 -0.595801

par vertex -0.57735 -0.57735 -0.57735

par vertex -0.57735 -0.57735 -0.57735

par vertex -0.57735 -0.57735 -0.57735

par vertex -0.57735 -0.57735 -0.57735

par vertex -0.451648 0.88473 -0.115186

par vertex -0.57735 -0.57735 -0.57735

this my code :

for (polyCounter = 0 ; polyCounter< myObjects[counterObj].nbVertices ; polyCounter++)

{

myObjects[counterObj].perVertexNormals[polyCounter].x=0 ;

myObjects[counterObj].perVertexNormals[polyCounter].y=0 ;

myObjects[counterObj].perVertexNormals[polyCounter].z=0 ;

for (polyCounter2 = 0 ; polyCounter2< 100 ; polyCounter2++)

{

tableIndices[polyCounter][polyCounter2]= -1 ;//-1 because 0 is used as an indice

}

}

int nIndice = 0 ;

int counter, j=0 ;

do

{

for(counter=0 ; counter < myObjects[counterObj].nbPoly ; counter++)

{

for(i=0 ; i<3 ; i++)

{

if(myObjects[counterObj].myIndices[counter][i]==nIndice)

{

normalIndice = (counter*3)+i ;/*thanks to this operation i can go from a two dimension array to a one dimension array*/

myObjects[counterObj].perVertexNormals[normalIndice].x += myObjects[counterObj].perFaceNormals[counter].x ;

myObjects[counterObj].perVertexNormals[normalIndice].y += myObjects[counterObj].perFaceNormals[counter].y ;

myObjects[counterObj].perVertexNormals[normalIndice].z += myObjects[counterObj].perFaceNormals[counter].z ;

/*if(myObjects[counterObj].myIndices[counter][i]==1)

cout<<"indice "<<myObjects[counterObj].perVertexNormals[normalIndice].x<<endl ;*/

tableIndices[nIndice][j]=normalIndice ;//array storing for a same vertex the number of indices

j++ ;

}

}

}

nIndice++ ;

j=0 ;

}

while(nIndice != (myObjects[counterObj].nbVertices-1)) ;

i=0 ;//counters

c=0 ;

c2=0 ;

do

{

do

{

do

{

if(i != c2)

{

myObjects[counterObj].perVertexNormals[tableIndices[c][i]].x += myObjects[counterObj].perVertexNormals[tableIndices[c][c2]].x ;

myObjects[counterObj].perVertexNormals[tableIndices[c][i]].y += myObjects[counterObj].perVertexNormals[tableIndices[c][c2]].y ;

myObjects[counterObj].perVertexNormals[tableIndices[c][i]].z += myObjects[counterObj].perVertexNormals[tableIndices[c][c2]].z ;

}

c2++ ;

}

while(tableIndices[c][c2] != -1) ;

i++ ;

c2=0 ;

}

while(tableIndices[c][i] != -1) ;

c++ ;

i=0;

}

while(c < myObjects[counterObj].nbVertices) ;

for(counter=0 ; counter < myObjects[counterObj].nbVertices ; counter++)

{

magnitude=sqrt(myObjects[counterObj].perVertexNormals[counter].x * myObjects[counterObj].perVertexNormals[counter].x +

myObjects[counterObj].perVertexNormals[counter].y * myObjects[counterObj].perVertexNormals[counter].y +

myObjects[counterObj].perVertexNormals[counter].z * myObjects[counterObj].perVertexNormals[counter].z) ;

myObjects[counterObj].perVertexNormals[counter].x /= magnitude ;

myObjects[counterObj].perVertexNormals[counter].y /= magnitude ;

myObjects[counterObj].perVertexNormals[counter].z /= magnitude ;

}

I try to calculate normals per vertex, but i have wrong values. Can you help me ?

this is an idea of the result :

par vertex -0.57735 -0.57735 -0.57735

par vertex -0.0901489 0.243461 -0.965712

par vertex 0.488888 -0.637189 -0.595801

par vertex -0.57735 -0.57735 -0.57735

par vertex -0.57735 -0.57735 -0.57735

par vertex -0.57735 -0.57735 -0.57735

par vertex -0.57735 -0.57735 -0.57735

par vertex -0.451648 0.88473 -0.115186

par vertex -0.57735 -0.57735 -0.57735

this my code :

for (polyCounter = 0 ; polyCounter< myObjects[counterObj].nbVertices ; polyCounter++)

{

myObjects[counterObj].perVertexNormals[polyCounter].x=0 ;

myObjects[counterObj].perVertexNormals[polyCounter].y=0 ;

myObjects[counterObj].perVertexNormals[polyCounter].z=0 ;

for (polyCounter2 = 0 ; polyCounter2< 100 ; polyCounter2++)

{

tableIndices[polyCounter][polyCounter2]= -1 ;//-1 because 0 is used as an indice

}

}

int nIndice = 0 ;

int counter, j=0 ;

do

{

for(counter=0 ; counter < myObjects[counterObj].nbPoly ; counter++)

{

for(i=0 ; i<3 ; i++)

{

if(myObjects[counterObj].myIndices[counter][i]==nIndice)

{

normalIndice = (counter*3)+i ;/*thanks to this operation i can go from a two dimension array to a one dimension array*/

myObjects[counterObj].perVertexNormals[normalIndice].x += myObjects[counterObj].perFaceNormals[counter].x ;

myObjects[counterObj].perVertexNormals[normalIndice].y += myObjects[counterObj].perFaceNormals[counter].y ;

myObjects[counterObj].perVertexNormals[normalIndice].z += myObjects[counterObj].perFaceNormals[counter].z ;

/*if(myObjects[counterObj].myIndices[counter][i]==1)

cout<<"indice "<<myObjects[counterObj].perVertexNormals[normalIndice].x<<endl ;*/

tableIndices[nIndice][j]=normalIndice ;//array storing for a same vertex the number of indices

j++ ;

}

}

}

nIndice++ ;

j=0 ;

}

while(nIndice != (myObjects[counterObj].nbVertices-1)) ;

i=0 ;//counters

c=0 ;

c2=0 ;

do

{

do

{

do

{

if(i != c2)

{

myObjects[counterObj].perVertexNormals[tableIndices[c][i]].x += myObjects[counterObj].perVertexNormals[tableIndices[c][c2]].x ;

myObjects[counterObj].perVertexNormals[tableIndices[c][i]].y += myObjects[counterObj].perVertexNormals[tableIndices[c][c2]].y ;

myObjects[counterObj].perVertexNormals[tableIndices[c][i]].z += myObjects[counterObj].perVertexNormals[tableIndices[c][c2]].z ;

}

c2++ ;

}

while(tableIndices[c][c2] != -1) ;

i++ ;

c2=0 ;

}

while(tableIndices[c][i] != -1) ;

c++ ;

i=0;

}

while(c < myObjects[counterObj].nbVertices) ;

for(counter=0 ; counter < myObjects[counterObj].nbVertices ; counter++)

{

magnitude=sqrt(myObjects[counterObj].perVertexNormals[counter].x * myObjects[counterObj].perVertexNormals[counter].x +

myObjects[counterObj].perVertexNormals[counter].y * myObjects[counterObj].perVertexNormals[counter].y +

myObjects[counterObj].perVertexNormals[counter].z * myObjects[counterObj].perVertexNormals[counter].z) ;

myObjects[counterObj].perVertexNormals[counter].x /= magnitude ;

myObjects[counterObj].perVertexNormals[counter].y /= magnitude ;

myObjects[counterObj].perVertexNormals[counter].z /= magnitude ;

}