Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 3 of 3

Thread: Flickering when diffuse shader used

  1. #1
    Newbie Newbie
    Join Date
    Nov 2018
    Posts
    2

    Unhappy Flickering when diffuse shader used

    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?

    Click image for larger version. 

Name:	normal-1.jpg 
Views:	9 
Size:	9.7 KB 
ID:	2873Click image for larger version. 

Name:	normal-2.jpg 
Views:	9 
Size:	6.9 KB 
ID:	2874


    Vertex shader:
    Code :
    #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:
    Code :
    #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);
    		}
    	//}
    }

  2. #2
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    2,975
    Quote Originally Posted by mccloud35 View Post
    Vertex shader:
    Code :
    	frag_normal = mat3(transpose(inverse(model))) * normalize(frag_normal);
    You're using frag_normal (which is undefined at that point) instead of normal:
    Code :
    	frag_normal = mat3(transpose(inverse(model))) * normalize(normal);

  3. #3
    Newbie Newbie
    Join Date
    Nov 2018
    Posts
    2
    Thanks you!!! That fixed it. I wasted so many hours debugging this.

    Are there any tools that will help to debug values in shaders? I'm using VS 2017 on Win10.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •