Skip to main content
Tweeted twitter.com/#!/StackGameDev/status/363038269103407104
Clarified the psuedo code for accuracy.
Source Link
Soapy
  • 915
  • 14
  • 31

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[0]Vertex[j] is just an index.Normal
    Vertex[Triangle[i].Vertex[0]].normal += result;
    Triangle[i]Vertex[Triangle[i].Vertex[1]Vertex[1]].Normalnormal += result;
    Triangle[i]Vertex[Triangle[i].Vertex[2]Vertex[2]].Normalnormal += 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[0];normal.x;
    float ny = Vertex[i].normal[1];normal.y;
    float nz = Vertex[i].normal[2];normal.z; 
    
    float len = sqrt(nx * nx + ny * ny + nz * nz);

    Vertex[i].normal[0]normal.x /= len;
    Vertex[i].normal[1]normal.y /= len;
    Vertex[i].normal[2]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?

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):

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
    Triangle[i].Vertex[0].Normal += result;
    Triangle[i].Vertex[1].Normal += result;
    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[0];
    float ny = Vertex[i].normal[1];
    float nz = Vertex[i].normal[2]; 
    
    float len = sqrt(nx * nx + ny * ny + nz * nz);

    Vertex[i].normal[0] /= len;
    Vertex[i].normal[1] /= len;
    Vertex[i].normal[2] /= 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?

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?

Shown how I find the average vertex normal.
Source Link
Soapy
  • 915
  • 14
  • 31

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):

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
    Triangle[i].Vertex[0].Normal += result;
    Triangle[i].Vertex[1].Normal += result;
    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[0];
    float ny = Vertex[i].normal[1];
    float nz = Vertex[i].normal[2]; 
    
    float len = sqrt(nx * nx + ny * ny + nz * nz);

    Vertex[i].normal[0] /= len;
    Vertex[i].normal[1] /= len;
    Vertex[i].normal[2] /= 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?

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):

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
    Triangle[i].Vertex[0].Normal += result;
    Triangle[i].Vertex[1].Normal += result;
    Triangle[i].Vertex[2].Normal += result;
}

After the loop I normalise each vertex normal to find the average normal.

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?

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):

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
    Triangle[i].Vertex[0].Normal += result;
    Triangle[i].Vertex[1].Normal += result;
    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[0];
    float ny = Vertex[i].normal[1];
    float nz = Vertex[i].normal[2]; 
    
    float len = sqrt(nx * nx + ny * ny + nz * nz);

    Vertex[i].normal[0] /= len;
    Vertex[i].normal[1] /= len;
    Vertex[i].normal[2] /= 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?

Source Link
Soapy
  • 915
  • 14
  • 31

.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):

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
    Triangle[i].Vertex[0].Normal += result;
    Triangle[i].Vertex[1].Normal += result;
    Triangle[i].Vertex[2].Normal += result;
}

After the loop I normalise each vertex normal to find the average normal.

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?