I'm trying to optimize my terrain by reducing the triangle count while keeping as much detail as possible. The reduction worked fine, I cut the number of vertices by a 5th without much visual loss. There's a problem with the calculations of the normals on this new asymmetrical mesh.
I have normals per vertex and here's the snippet for calculating normals:
private void calcNormal(Vector<Triangle_dt> triangles, Point_dt point) {
Vec3 normal = new Vec3(0, 0, 0);
for (Triangle_dt triangle : triangles) {
Vec3 a = getPos(triangle.p1());
Vec3 b = getPos(triangle.p2());
Vec3 c = getPos(triangle.p3());
Vec3 AB = b.subtract(a);
Vec3 AC = c.subtract(a);
normal = normal.add(AB.cross(AC));
}
setNormal(point, normal.getUnitVector());
}
Where triangles are the triangles connected to the vertex (point). I Add all the triangle normals together (without normalizing to make the final vector weighted by triangle area) and then finally normalizing the end result.
I believe the calculations are correct but there are annoying artifacts in the result (it's lit with directional light):
As you can see there's unwanted lines where the vertices are sparse. It's caused due to small clusters of points close together but far from the next set of points (see next picture below). Any idea of how to prevent this? Here's the same view with point rendering: