2

I'm trying to replace Assimp in my project and make a custom object loader.

Assimp generates normals from vertices when aiProcess_GenNormals flag is supplied. It also generate smooth normals when aiProcess_GenSmoothNormals is given.

Now, I'm generating normals from vertices but I couldn't achieve to make them smooth.

I found these two posts which say to divide normal sums to repetition count in indices. I tried to do so but I couldn't make it work.

int i, c;
for (i = 0; i < triangles.size(); i++) {

    Traingle& tri = triangles[i];

    glm::vec3 v1 = vertices[tri.indices[0]].position;
    glm::vec3 v2 = vertices[tri.indices[1]].position;
    glm::vec3 v3 = vertices[tri.indices[2]].position;

    glm::vec3 delta1 = v2 - v1;
    glm::vec3 delta2 = v3 - v1;
    glm::vec3 normal = glm::cross(delta1, delta2);

    for (c = 0; c < 3; c++) {
        vertices[tri.indices[c]].normal += normal;
        vertices[tri.indices[c]].count += 1.0f;
    }
}

for (i = 0; i < vertices.size(); i++) {
    Vertex& ver = vertices[i];
    glm::vec3 normal = glm::normalize(ver.normal / ((float)vertices[i].count));
    smoothNormals.push_back(normal);
}

I also looked over Assimp's source in order to understand how they did it and saw that, they used a special spatial sort tree.

Do I have to do how they do it or is there another way of smoothing normals in cpu?

Edit: I'm painting normals. First picture is generated normals with above code and second picture is Assimp's smoothed normals. Third one is Assimp's generated normals.

generated normals

smoothed normals with Assimp

Assimp's generated normals

JeJoRic
  • 95
  • 6
  • What results do you get and what results do you want? The method you have should give you pretty smooth normals. Btw, it makes no sense to divide by vertex count since you are normalizing anyway. – Nico Schertler Aug 15 '18 at 04:41
  • Possible duplicate of [How to achieve smooth tangent space normals?](https://stackoverflow.com/questions/21766605/how-to-achieve-smooth-tangent-space-normals) – Spektre Aug 15 '18 at 07:34
  • @NicoSchertler I've added painted normal pictures. As you said normals seem smooth. – JeJoRic Aug 15 '18 at 12:34
  • @Spektre I've seen that post, already added as reference, and tried to use that algorithm. But smoothing with that algorithm and Assimp differs some way as seen in pictures. – JeJoRic Aug 15 '18 at 12:42
  • @Molomon If I see it right You forgot to normalize `normal` in the first loop before adding it to the average ... that can mess things out as the cross product will give you normal sized to the triangle area * 2 which does not go well with not uniform triangle distribution in the mesh... – Spektre Aug 15 '18 at 13:30

0 Answers0