# Calculating vertex_normals (vn) causes ugly lines C++/openGL

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.

• the vertices around a triangle shouldn't have the same normals. What you're doing now is flat shading Mar 16 '17 at 10:30
• They dont. Notice +=, and not =, this sum is normalized after, to give the value for each vertice (point). Basically, each point will be the sum of the normals adjacent to it, giving it a smooth curve to pass to the GPU. This is not the problem @Bálint Mar 16 '17 at 10:37
• What method was used to generate the mesh? Are there duplicated vertices along the seams perhaps(which didnt show themselves when the surface was smooth)? Mar 16 '17 at 11:17
• Your guess is good enough to have me looking for a few hours, but I cant find it yet @wondra Mar 17 '17 at 7:38