I recently made terrain generation for my game, and I created a lighting system (simply diffuse lignting), but there's a strange problem around the edges.
I know, OpenGL interpolates the different values between the vertex shader and fragment shader, but in this case, it comes out strangely. Here is the issue:
The edges of the squares look a little jagged (I use triangles).
I especially don't understand this one:
Every vertex of that area is dark, but the middle is lighter.
Here are the codes for the vertex and fragment shader:
Vertex shader:
#version 330 core
in vec3 vertex;
in vec3 normal;
in vec4 color;
uniform mat4 projection;
uniform mat4 view;
uniform mat4 model;
out vec3 vertexNormal;
out vec3 cameraPos;
out vec3 vert;
out vec4 vertColor;
void main() {
gl_Position = projection * view * model * vec4(vertex, 1.0);
vertexNormal = normal;
cameraPos = vec3(view[3][0], view[3][1], view[3][2]);
vert = vertex;
vertColor = color;
}
Fragment shader:
#version 330 core
in vec3 vertexNormal;
in vec3 cameraPos;
in vec3 vert;
in vec4 vertColor;
uniform vec3 sunColor;
uniform vec3 sunDirection;
void main() {
vec3 toLightVector = -sunDirection;
vec3 uLightVector = normalize(toLightVector);
float diffuse = max(dot(uLightVector, vertexNormal), 0.1);
vec3 lightColor = diffuse * sunColor;
gl_FragColor = vec4(lightColor, 1.0) * vertColor;
}