PDA

View Full Version : Calculating normals

12-01-2003, 11:46 AM
I'm doing a normals calculation for my object loaded from 3d studio(milkshape actually).My problem is that my normals aren't correct(checked)
Code:
void CalculateNormals(unsigned short *PolysArray,unsigned int PolysCount,float* VertexArray,unsigned int VertexCount,float *NormalsArray)
{
unsigned int i,j;
float lenght;
float temp_normal[3]={0,0,0};
float *temp_polyg_normal; ov
float temp_vector1[3]={0,0,0},temp_vector2[3]={0,0,0};
temp_polyg_normal=new float[PolysCount*3];
for(i=0;i<PolysCount*3;i++)
*(temp_polyg_normal+i)=0;
for(i=0;i<PolysCount;i++)
{
temp_vector1[0]=(*(VertexArray+(*(PolysArray+3*i+1))))-(*(VertexArray+(*(PolysArray+3*i))));
temp_vector1[1]=(*(VertexArray+(*(PolysArray+3*i+1))+1))-(*(VertexArray+(*(PolysArray+3*i))+1)); temp_vector1[2]=(*(VertexArray+(*(PolysArray+3*i+1))+2))-(*(VertexArray+(*(PolysArray+3*i))+2));
temp_vector2[0]=(*(VertexArray+(*(PolysArray+3*i+2))))-(*(VertexArray+(*(PolysArray+3*i+1)))); temp_vector2[1]=(*(VertexArray+(*(PolysArray+3*i+2))+1))-(*(VertexArray+(*(PolysArray+3*i+1))+1));
temp_vector2[2]=(*(VertexArray+(*(PolysArray+3*i+2))+2))-(*(VertexArray+(*(PolysArray+3*i+1))+2)); //vypocet 2 vektora polygonu i(os z)
*(temp_polyg_normal+3*i)=temp_vector1[1]*temp_vector2[2]-temp_vector2[1]*temp_vector1[2];
*(temp_polyg_normal+3*i+1)=temp_vector1[2]*temp_vector2[0]-temp_vector2[2]*temp_vector1[0]; y)
*(temp_polyg_normal+3*i+2)=temp_vector1[0]*temp_vector2[1]-temp_vector2[0]*temp_vector1[1]; z)
}
for(i=0;i<VertexCount;i++)
{ for(j=0;j<PolysCount;j++)
{
if((i==*(PolysArray+3*j)) &#0124; &#0124; (i==*(PolysArray+3*j+1)) &#0124; &#0124; (i==*(PolysArray+3*j+2)))
{
temp_normal[0]+=*(temp_polyg_normal+3*j); temp_normal[1]+=*(temp_polyg_normal+3*j+1);
temp_normal[2]+=*(temp_polyg_normal+3*j+2);
}
}
lenght=(float)sqrt((float)pow(temp_normal[0],2)+(float)pow(temp_normal[1],2)+(float)pow(temp_normal[2],2));
if(lenght==0) lenght=1;
*(NormalsArray+3*i)=temp_normal[0]/lenght;
*(NormalsArray+3*i+1)=temp_normal[1]/lenght;
*(NormalsArray+3*i+2)=temp_normal[2]/lenght;
}
}

What is wrong ?