Hello all,
I implemented basic diffuse and ambient lighting in a glsl shader which I'm using to draw some cubes. However at a distance there are some strange artifacts on the edge's of the object. It's not aliasing. At least I'm reasonably sure. It's almost like a whole chunk of the edge isn't being lit correctly or something.
Here's a picture:
It doesn't occur when the lighting is turned off. So it's definitely the lighting that's causing it.
Obviously I want this to go away. I'm not quite sure what code needs to be shown so here's the fragment shader for starters:
#version 330
in vec2 UV;
in vec3 Normal0;
out vec4 FragColor;
uniform sampler2D myTextureSampler;
void main() {
vec3 DirectionalLightColor = vec3(1.0f, 1.0f, 1.0f);
float AmbientLightIntensity = 0.4f;
float DirectionalLightIntensity = 1.2f;
vec3 DirectionalLightDirection = normalize(vec3(1.0f, 1.0f, 1.0f));
vec4 AmbientColor = vec4(DirectionalLightColor * AmbientLightIntensity, 1.0f);
float DiffuseFactor = dot(normalize(Normal0), -DirectionalLightDirection);
vec4 DiffuseColor;
if (DiffuseFactor > 0) {
DiffuseColor = vec4(DirectionalLightColor * DirectionalLightIntensity * DiffuseFactor, 1.0f);
}
else {
DiffuseColor = vec4(0, 0, 0, 0);
}
//FragColor = (texture(myTextureSampler, UV)) * (AmbientColor + DiffuseColor);
//FragColor = (texture(myTextureSampler, UV));
FragColor = vec4(0.5,0.5,0.5,1) * (AmbientColor + DiffuseColor);
}
And here's the vertex shader as well incase it might be the cause of the problem
#version 330
uniform mat4 gMVP;
uniform mat4 gWorld;
layout (location = 0) in vec3 Position;
layout (location = 1) in vec2 TexCoords;
layout (location = 2) in vec3 Normal;
out vec2 UV;
out vec3 Normal0;
void main() {
gl_Position = gMVP * vec4(Position, 2.0);
UV = TexCoords;
Normal0 = (gWorld * vec4(Normal, 0.0)).xyz;
}
I'm not that experienced with OpenGL so I'm not even sure how to go about debugging this. I observed the effect lessens when you get closer to the objects. Don't know if that's relevant.
Oh here's the code that submits the draw call as well
void DrawCube(State* state, Maths::Vec3f Position, Maths::Vec3f Rotation, Maths::Vec3f Scale)
{
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, cube.VertexPositionsBuffer);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, cube.TexCoordsBuffer);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(2);
glBindBuffer(GL_ARRAY_BUFFER, cube.VertexNormalsBuffer);
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, cube.ElementBuffer);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, cube.Texture);
glUniform1i(state->TextureUniformHandle, 0);
Maths::Mat4f scale = Maths::MakeScale(Scale);
Maths::Mat4f translate = Maths::MakeTranslate(Position);
Maths::Mat4f rotate = Maths::MakeRotate(Rotation);
Maths::Mat4f cameraTranslate = Maths::MakeTranslate(Maths::Vec3f(-state->Camera.Position.X, -state->Camera.Position.Y, -state->Camera.Position.Z));
Maths::Mat4f cameraRotate = Maths::MakeLookAt(state->Camera.Forward, state->Camera.Up);
Maths::Mat4f projection = Maths::MakePerspective(800, 450, 0.01, 100, 30);
Maths::Mat4f Model = translate * rotate * scale;
Maths::Mat4f MVP = projection * cameraRotate * cameraTranslate * Model;
glUniformMatrix4fv(state->gMVPHandle, 1, GL_TRUE, &MVP.m[0][0]);
glUniformMatrix4fv(state->gWorldHandle, 1, GL_TRUE, &Model.m[0][0]);
glDrawElements(GL_TRIANGLES, cube.NumIndices, GL_UNSIGNED_INT,0);
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
glDisableVertexAttribArray(2);
}