# .md5mesh normals are not smooth

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;
p2 = Triangle[i].Vertex;
p3 = Triangle[i].Vertex;

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].normal += result;
Vertex[Triangle[i].Vertex].normal += result;
Vertex[Triangle[i].Vertex].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?

• At a guess, your OBJ model shares vertices and the MD5 one duplicates them?
– Will
Aug 1 '13 at 15:44
• I wrote an answer without seeing "After the loop I normalise each vertex normal to find the average normal." so I deleted it, but my guess would be that this is where it goes wrong. Care to post that code? Aug 1 '13 at 15:57
• I've edited the post to show that code. Aug 1 '13 at 16:11

The way you calculate your normals, you end up with this because you take the cross product of the triangle sides. Where red circles are normals pointing directly at the screen. Sideways it looks like this (both black lines are triangle faces seen from the side) Notice how for every face, the vertices that make it up are parallel to each other. Because of this, the interpolated value of the normals along the entire face is the same as the value at every vertex! In which case the interpolated value will vary along the face and give you non-flat shading. I guess the easy way to calculate such normals would be to make another pass and set every vertex's normal to the normalized sum of the normal assigned to it for every face that shares it.

Your problem is that you're not calculating the average normals properly. Basically you seem to only be normalizing the ones you calculated.

Since your faces are all triangles, the pseudocode would look something like this (suboptimal naive algorithm warning)

foreach vertex in mesh.vertices
//Find the 3 vertices belonging to different faces that are actually the same vertex but with different normals
vertex shared_vertices = mesh.find_vertices_at_same_position(vertex)
normal = sum(shared_vertices)
normal = normalize(normal)

foreach v in shared_vertices
v.normal = normal


The calculated normal will then be the blue arrow in this image: Another cool thing is that once that work is done you could actually share the vertices between faces instead of having 3 copies since all their attributes are the same (thereby saving 2/3 of the memory required to hold your current vertex data)

• Sorry for the confusion. This is what I'm already doing, see the edits in my post for clarification. Aug 1 '13 at 16:49
• @Soapy That code should be correct so I can only assume your vertices aren't shared properly. Are you sure you only store 1 vertex, shared among 3 faces? Aug 1 '13 at 17:25
• Yea I'm sure. Because for each triangle there is, I get each vertex that contributes to the triangle and work out the surface normal, and add this to each vertex normal. If there is a vertex that contributes to more than one triangle it will sum each triangles surface normal to the existing normal. Aug 1 '13 at 17:35
• @Soapy Where are your files coming from? For example, in Blender, you have to explicitly set your normals to smoothed before exporting. Aug 1 '13 at 19:29
• This is what happens when I use the smooth normals in blender. link Aug 2 '13 at 10:06

It turns out that it was an error in my rendering code.

The code in my question does work out the normals correctly, so if anyone is stuck, my code should work.

The problem was related to how I was interpreting the index buffer.