I have a very simple diffuse shader in glsl. When I use my normals to for diffuse shading a weird flickering artifact occurs. This is screenshots taken a second apart. Commenting out the diffuse part simply stops the flickering. If the normals were simply incorrect I can understand this. But, they are just varying with time, and I’m not sure what’s going wrong. Any clue what’s happening?
[ATTACH=CONFIG]1883[/ATTACH][ATTACH=CONFIG]1884[/ATTACH]
Vertex shader:
#version 410
layout(location = 0) in vec3 pos;
layout(location = 1) in vec3 normal;
layout(location = 2) in vec2 tex_coord;
uniform mat4 model;
uniform mat4 camera;
uniform mat4 proj;
out vec3 frag_pos;
out vec3 frag_normal;
out vec2 frag_tex_coord;
void main() {
gl_Position = proj * camera * model * vec4(pos, 1.f);
frag_pos = vec3(model * vec4(pos, 1.f));
frag_normal = mat3(transpose(inverse(model))) * normalize(frag_normal);
frag_tex_coord = tex_coord;
}
Fragment shader:
#version 410
#define NUM_LIGHTS 1
struct Light {
vec3 pos;
vec3 color;
};
uniform sampler2D diffuse_tex;
uniform sampler2D specular_tex;
in vec3 frag_pos;
in vec3 frag_normal;
in vec2 frag_tex_coord;
out vec4 final_color;
uniform Light lights[NUM_LIGHTS];
uniform bool single_color;
uniform bool enable_specular;
//const vec4 amb_color = vec4(0.2f, 0.2f, 0.2f, 1.f);
const vec4 amb_color = vec4(0.0f, 0.0f, 0.0f, 1.f);
void main() {
final_color = amb_color;
//for (int i = 0; i < NUM_LIGHTS; ++i) {
Light light = lights[0];
vec3 normalized_frag_normal = normalize(frag_normal);
vec3 world_pos_to_light_dir = normalize(light.pos - frag_pos);
float lambertian = max(dot(normalized_frag_normal, world_pos_to_light_dir), 0.f); // if i replace normalized_frag_normal here with some const the flickering stops, obviously that's not a workaround cause I want some shading
vec3 diffuse_color = lambertian * texture(diffuse_tex, frag_tex_coord).xyz;
if (single_color) {
final_color = vec4(1.f, 0.f, 0.f, 1.f);
} else {
final_color += vec4(diffuse_color, 0.f);
}
//}
}