Im trying to procedurally generate planets for a project I'm working on. By adding noise to each vertex, I'm able to generate elevation, but without having updated vertice normals for my shader. It originally looked something like the picture below.
When I tried to recalculate the vertex normals, I got some ugly sharp lines, most likely as a result from the way I was parsing the model.
this is the code Im using to recalculate the normals:
for (auto &x : normals)x = vec3(0); //zero normals
vec3 facenormal; //buffer
//indices is the faces/triangles indexed, in pairs of 3.
for (size_t i = 0; i<indices.size();)
{
facenormal = cross(
(shape[indices[i + 1]] - shape[indices[i]]),
(shape[indices[i + 2]] - shape[indices[i]])
);
normals[indices[i++]] += facenormal;
normals[indices[i++]] += facenormal;
normals[indices[i++]] += facenormal;
}
for (auto &x : normals)x = normalize(x); //normalize adjacent faces
Basically I add up the adjacent faces to the vertices they belong to, then normalize the value they have.
Then, based on suggestions, I took a look at the parsing and made sure it doesnt share vertices in the indexing. This seems to remove the lines, but im still stuck flat shading?
But according to this, it should be accurate. So I'm still not fixing the problem. What is the correct formula to do the vertex calculation, if its not what I'm already doing.
Like user Kolenda suggests, I was not modifying values at the base-mesh level. but rather, doing them after loading the indexed copies. So, what I end up with is a combination of flat shading faces "shading" ghost vertice normals. Iterating over the faces on a mesh level, with the same formula, solves the problem.