Here's my code. It has 2 methods to calculate normals and whichever I use I get the same seam discontinuities:
void calculate_normals( const float3* Vertex, float3* Normal, const unsigned int VertexCount,
const uint16* Index, const unsigned int IndexCount)
{
#if 1
std::vector<float3>* NormalBuffer = new std::vector<float3>[VertexCount];
std::fill(Normal, Normal+VertexCount, float3(0.0f));
for( int i = 0; i < IndexCount; i += 3 )
{
// get the three vertices that make the faces
float3 p1 = Vertex[Index[i+0]];
float3 p2 = Vertex[Index[i+1]];
float3 p3 = Vertex[Index[i+2]];
float3 v1 = p2 - p1;
float3 v2 = p3 - p1;
float3 normal = cross(v2, v1);
normal.normalize();
// Store the face's normal for each of the vertices that make up the face.
NormalBuffer[Index[i+0]].push_back( normal );
NormalBuffer[Index[i+1]].push_back( normal );
NormalBuffer[Index[i+2]].push_back( normal );
}
// Now loop through each vertex vector, and avarage out all the normals stored.
for( int i = 0; i < VertexCount; ++i )
{
for( int j = 0; j < NormalBuffer.size(); ++j )
Normal += NormalBuffer[j];
//Normal /= float(NormalBuffer.size());
Normal = float3(Normal).normalize();
}
delete [] NormalBuffer;
#endif
#if 0
std::fill(Normal, Normal+VertexCount, float3(0,0,0));
for( int i = 0; i < IndexCount; i += 3 )
{
// get the three vertices that make the faces
float3 p1 = Vertex[Index[i+0]];
float3 p2 = Vertex[Index[i+1]];
float3 p3 = Vertex[Index[i+2]];
float3 v1 = p2 - p1;
float3 v2 = p3 - p1;
float3 normal = cross(v2, v1);
//assert(normal != float3(0));
normal.normalize();
// Store the face's normal for each of the vertices that make up the face.
Normal[Index[i+0]] += normal;
Normal[Index[i+1]] += normal;
Normal[Index[i+2]] += normal;
}
for( int i = 0; i < VertexCount; ++i )
{
Normal = float3(Normal).normalize();
}
#endif
}
Any ideas? This normal calculation is messing up tangents and bitangents too so the lighting is messed up along the seams..