I'm currently working on a project that requires me to load .md5mesh format and draw it.
Following this link I've managed to load the mesh into the engine successfully, but a problem arises when calculating normals; they just don't seem to smooth.
To clarify that it was not my rendering or shader code that was the problem, I loaded a model of .OBJ format, and that lights smoothly.
The mesh is calculated correctly too, as I am able to load in complex models with multiple joints and mesh parts.
Here's a screenshot of the lighting.
And here is how I currently calculate the normals (All normals are set to zero before computing):
EDIT Amended the psuedo-code to be more accurate to what I have. The original may have been confusing.
for (unsigned int i = 0; i < NumberOfTriangles; i++)
{
Math3::vec3 r, s, result;
Math3::vec3 p1, p2, p3;
p1 = Triangle[i].Vertex[0];
p2 = Triangle[i].Vertex[1];
p3 = Triangle[i].Vertex[2];
r = p2 - p1;
s = p3 - p1;
result = Math3::Cross(s, r);
// Add the triangles face normal to each vertex
// The Vertex's are not local to the triangles.
// Triangle[i].Vertex[j] is just an index.
Vertex[Triangle[i].Vertex[0]].normal += result;
Vertex[Triangle[i].Vertex[1]].normal += result;
Vertex[Triangle[i].Vertex[2]].normal += result;
}
After the loop I normalise each vertex normal to find the average normal.
EDIT Here is how I find the average normal:
for (unsigned int i = 0; i < NumberOfVerts; i++)
{
float nx = Vertex[i].normal.x;
float ny = Vertex[i].normal.y;
float nz = Vertex[i].normal.z;
float len = sqrt(nx * nx + ny * ny + nz * nz);
Vertex[i].normal.x /= len;
Vertex[i].normal.y /= len;
Vertex[i].normal.z /= len;
}
As you can see in the image the .md5mesh model is shading flat instead of shading smoothly like the .OBJ model.
So what am I missing?