Hi,
On the terrain I generate using a heightmap, I encounter artifacts where the mesh edges can be visible as dark marks on the terrain.
Strangely, the grid quads appear darker, but not the triangle edge in the quads, which even tends to be lighter.
The problem appears when the terrain is steep enough, and with a certain lighting angle.
Here is a picture of the problem (here is the diffuse component only):
Here is the code I use to generate the normals:
glm::vec3 Terrain::vertexNormal(int x, int y)
{
glm::vec3 normalAdjacentSurface1 = surfaceNormal(
retrievePosition(x, y),
retrievePosition(x+1, y-1),
retrievePosition(x, y-1));
glm::vec3 normalAdjacentSurface2 = surfaceNormal(
retrievePosition(x, y),
retrievePosition(x+1, y),
retrievePosition(x+1, y-1));
glm::vec3 normalAdjacentSurface3 = surfaceNormal(
retrievePosition(x, y),
retrievePosition(x, y+1),
retrievePosition(x+1, y));
glm::vec3 normalAdjacentSurface4 = surfaceNormal(
retrievePosition(x, y),
retrievePosition(x-1, y+1),
retrievePosition(x, y+1));
glm::vec3 normalAdjacentSurface5 = surfaceNormal(
retrievePosition(x, y),
retrievePosition(x-1, y),
retrievePosition(x-1, y+1));
glm::vec3 normalAdjacentSurface6 = surfaceNormal(
retrievePosition(x, y),
retrievePosition(x-1, y),
retrievePosition(x, y-1));
glm::vec3 normal = glm::vec3(0.0f, 0.0f, 0.0f);
normal += normalize(normalAdjacentSurface1);
normal += normalize(normalAdjacentSurface2);
normal += normalize(normalAdjacentSurface3);
normal += normalize(normalAdjacentSurface4);
normal += normalize(normalAdjacentSurface5);
normal += normalize(normalAdjacentSurface6);
normal = normalize(normal);
return normal;
}
glm::vec3 Terrain::surfaceNormal(glm::vec3 P0, glm::vec3 P1, glm::vec3 P2)
{
return (normalize(glm::cross(P1-P0, P2-P0)));
}
glm::vec3 Terrain::retrievePosition(int x, int y)
{
float height = getVertexFromWorldCoordinates(x, y)->height;
return(glm::vec3(x,height,y));
}
In the fragment shader, I use a simple Blinn-Phong model. Here is the diffuse part (which is where the problem comes from):
float diffuseStrength = 0.9;
vec3 norm = normalize(Normal);
vec3 lightDir = normalize(lightPos - FragPos);
float diff = max(dot(norm, lightDir), 0.0);
vec3 diffuse = diff * lightColor * diffuseStrength;
What could I do wrong?