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.