1
\$\begingroup\$

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:enter image description here

The edges of the squares look a little jagged (I use triangles).

I especially don't understand this one:

enter image description here

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;
}
\$\endgroup\$
3
\$\begingroup\$

What you are rendering is correct linear interpolation of your lighting values. The odd highlighting of the edges and the darkening on others is, like Lorenzo pointed out, an optical illusion. I recommend you zoom in on the second image and look closely at how those color values are being interpolated along those points. Although it looks a little wonky further out, up close it all clearly follows the rules you think it should.

There are various methods you could use to change how this looks out. The simplest of which is to increase the resolution of your geometry, which provides more polys over which your lighting can interpolate. Another option might be to implement a more complicated lighting calculation, enabling you to map a more appealing curve to the interpolation.

Apologies if this is at all confusing, I'm just a computer graphics hobbyist who enjoys playing with rendering technologies.

\$\endgroup\$
  • \$\begingroup\$ Another option is to calculate lighting in fragment shader with normal normalized per-fragment instead of per-vertice. \$\endgroup\$ – Kromster says support Monica Mar 30 '16 at 5:16
1
\$\begingroup\$

The lighter middle is an optical illusion. Vertex colors appear a bit random (the geometry is too coarse and/or the lighting is approximate), but fragments are interpolated linearly as they should.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.