View Full Version : Lighting/Shading artifacts

02-17-2016, 01:07 PM
Hello!I've been experimenting with OpenGL for a while now and I've started loading simple levels from OBJ Files and placing light sources. The light system didn't work too bad at first, but then I saw some weird looking artifacts, caused by the lighting. I've removed the texture from my shaders, but the artifacts are still there when coloring the whole room white. I also tried adjusting the precision to highp, but this hasn't changed anything either.

This is what the artifacts look like:


This is my vertex shader:

#version 330 core

// Vertex shader
layout(location = 0) in vec3 vpos;
layout(location = 1) in vec2 vuv;
layout(location = 2) in vec3 vnormal;

out vec2 uv; // UV coordinates
out vec3 normal; // Normal in camera space
out vec3 pos; // Position in camera space
out vec3 light[3]; // Vertex -> light vector in camera space

uniform mat4 mv; // View * model matrix
uniform mat4 mvp; // Proj * View * Model matrix
uniform mat3 nm; // Normal matrix for transforming normals into c-space

void main() {
// Pass uv coordinates
uv = vuv;

// Adjust normals
normal = normalize(nm * vnormal);

// Calculation of vertex in camera space
pos = (mv * vec4(vpos, 1.0)).xyz;

// Vector vertex -> light in camera space
light[0] = (mv * vec4(0.0,0.3,0.0,1.0)).xyz - pos;
light[1] = (mv * vec4(-6.0,0.3,0.0,1.0)).xyz - pos;
light[2] = (mv * vec4(0.0,0.3,4.8,1.0)).xyz - pos;

// Pass position after projection transformation
gl_Position = mvp * vec4(vpos, 1.0);

And this is my fragment shader:

#version 330 core

// Fragment shader
layout(location = 0) out vec3 color;

in vec2 uv; // UV coordinates
in vec3 normal; // Normal in camera space
in vec3 pos; // Position in camera space
in vec3 light[3]; // Vertex -> light vector in camera space

void main() {
vec3 n = normalize(normal);

// Ambient
color = 0.05 * vec3(1.0);

// Diffuse lights
for (int i = 0; i < 3; i++) {
float cos = clamp(dot(n,normalize(light[i])), 0.0, 1.0);
float distance = dot(light[i],light[i]);
color += 0.6 * vec3(1.0) * cos / distance;

Has anyone had any similiar problems or does anyone know where the error might be? My best guesses would have been the precision or texture problems, changing both didn't make the artifacts disappear.

02-18-2016, 03:22 AM
This is just normal banding; see e.g http://stackoverflow.com/questions/16005952/opengl-gradient-banding-artifacts

You'll need to write to a higher-repcision framebuffer.