1
votes

I'm trying to implement some basic lighting and shading following the tutorial over here and here.

Everything is more or less working but I get some kind of strange flickering on object surfaces due to the shading.

I have two images attached to show you guys how this problem looks.

Flickering on surface

Another example

I think the problem is related to the fact that I'm passing vertex coordinates from vertex shader to fragment shader to compute some lighting variables as stated in the above linked tutorials.

Here is some source code (stripped out unrelated code).

Vertex Shader:

#version 150 core

in vec4 pos;
in vec4 in_col;
in vec2 in_uv;
in vec4 in_norm;

uniform mat4 model_view_projection;


out vec4 out_col;
out vec2 passed_uv;
out vec4 out_vert;
out vec4 out_norm;


void main(void) {
    gl_Position = model_view_projection * pos;

    out_col = in_col;
    out_vert = pos;
    out_norm = in_norm;
    passed_uv = in_uv;
}

and Fragment Shader:

#version 150 core

uniform sampler2D tex;

uniform mat4 model_mat;

in vec4 in_col;
in vec2 passed_uv;
in vec4 vert_pos;
in vec4 in_norm;

out vec4 col;

void main(void) {

    mat3 norm_mat = mat3(transpose(inverse(model_mat)));
    vec3 norm = normalize(norm_mat * vec3(in_norm));

    vec3 light_pos = vec3(0.0, 6.0, 0.0);
    vec4 light_col = vec4(1.0, 0.8, 0.8, 1.0);

    vec3 col_pos = vec3(model_mat * vert_pos);

    vec3 s_to_f = light_pos - col_pos;

    float brightness = dot(norm, normalize(s_to_f));
    brightness = clamp(brightness, 0, 1);

    gl_FragColor = out_col;

    gl_FragColor = vec4(brightness * light_col.rgb * gl_FragColor.rgb, 1.0);
}

As I said earlier I guess the problem has to do with the way the vertex position is passed to the fragment shader. If I change the position values to something static no more flickering occurs. I changed all other values to statics, too. It's the same result - no flickering if I am not using the vertex position data passed from vertex shader.

So, if there is someone out there with some GL-wisdom .. ;) Any help would be appreciated. Side note: running all this stuff on an Intel HD 4000 if that may provide further information.

Thanks in advance! Ivan

1
What are the near and far plane values? - legends2k
near is set to 0.1 and far to 1000 - insaneivan
Can you try increasing near, say, to 10 or 50? - legends2k
I did. Tried both of the values but still the same flickering - insaneivan
I always thought that gl_FragCoord is in window space and not in world space. Am I wrong here? - insaneivan

1 Answers

3
votes

The names of the out variables in the vertex shader and the in variables in the fragment shader need to match. You have this in the vertex shader:

out vec4 out_col;
out vec2 passed_uv;
out vec4 out_vert;
out vec4 out_norm;

and this in the fragment shader:

in vec4 in_col;
in vec2 passed_uv;
in vec4 vert_pos;
in vec4 in_norm;

These variables are associated by name, not by order. Except for passed_uv, the names do not match here. For example, you could use these declarations in the vertex shader:

out vec4 passed_col;
out vec2 passed_uv;
out vec4 passed_vert;
out vec4 passed_norm;

and these in the fragment shader:

in vec4 passed_col;
in vec2 passed_uv;
in vec4 passed_vert;
in vec4 passed_norm;

Based on the way I read the spec, your shader program should actually fail to link. At least in the GLSL 4.50 spec, in the table on page 43, it lists "Link-Time Error" for this situation. The rules seem somewhat ambiguous in earlier specs, though.