Hi all!
I''ve finished the 2.0 version of my mesh file, and by now it stores vertex data, index data, normals, binormals and tangents. I''ve programmed a .3DS Loader that reads .3DS files, computes all data and saves zipped into a file of my own, but since I have shared and non-shared vertex, I had to write a special algorithm to compute normals.
This is the first (obvius) algorithm I thougt:
for (int i=0;i<facesnumber;++i)
{
//get 3 vector in a face
Vector3D v0=vertexdata[faces[i].v1];
Vector3D v1=vertexdata[faces[i].v2];
Vector3D v2=vertexdata[faces[i].v3];
//calculate side vectors
Vector3D side0=v0-v1;
Vector3D side1=v0-v2;
//crossproduct them to get face''s normal
Vector3D normal=side0*side1;
normal.Normalize();
normaldata[faces[i].v1]=normaldata[faces[i].v1]+normal;
normaldata[faces[i].v2]=normaldata[faces[i].v2]+normal;
normaldata[faces[i].v3]=normaldata[faces[i].v3]+normal;
}
for (int i=0;i<vertexnumber;++i)
normaldata[i].Normalize();
It''s lighting fast, but when a vertex is duplicated, there''s no more smooth normals, you can clearly see a straigth edge. So I thougth this other algorithm:
for (int i=0;i<facesnumber;++i)
{
//get 3 vector in a face
v0=vertexdata[faces[i].v1];
v1=vertexdata[faces[i].v2];
v2=vertexdata[faces[i].v3];
//calculate side vectors
side0=v0-v1;
side1=v0-v2;
//crossproduct them to get face''s normal
normal=side0*side1;
normal.Normalize();
normalface[i]=normal;
}
int count=0;
for (int v=0;v<vertexnumber;++v)
{
normal.LoadZero;
//Get current vertex
v4=vertexdata[v];
count=0;
for( int i=0; i<facesnumber; ++i )
{
//Get 3 vertex in a face
v1=vertexdata[faces[i].v1];
v2=vertexdata[faces[i].v2];
v3=vertexdata[faces[i].v3];
// If current vertex VALUE (not index!!) is into face,
// compute face normal into vertex normal
if ((v1 == v4) ||
(v2 == v4) ||
(v3 == v4) )
{
count++;
normal = normal+normalface[i];
}
}
//ponderate normal over faces
if (count>1)
{
normal=normal/count;
normal.Normalize();
normaldata[v]=normal;
}
}
}
But is SOOO DAMN SLOW!!!!
The QUESTION:
Anyone sees any way to get it faster?? I have no more ideas... It''s 1:30 AM in the morning and I''m veery tired...
Thanks!!
---------------------------------------
"It''s only a bit, it can''t hurt you..."
--------------------------------------- "It's only a bit, it can't hurt you..."