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 5 of 5

Thread: Help with GLSL/OpenGL Shader; Uneven Lighting

  1. #1
    Junior Member Regular Contributor
    Join Date
    Apr 2014
    Posts
    109

    Question Help with GLSL/OpenGL Shader; Uneven Lighting

    Hello,

    So I have been banging my head against the wall on a GLSL shader issue; essentially my lighting is uneven and weird.

    See my picture below; the problem is that I get the bright highlight in the OpenGL scene but the rest of the scene is unbelievably dark and makes lighting unusable.

    What am I doing wrong with the shader?

    Also, please let me know what else you need to see (code, etc.) to help troubleshoot the issue:

    Vertex Shader:
    Code :
    #version 330 core
     
    #extension GL_ARB_explicit_attrib_location : require
     
    layout(location = 0) in vec3 vPosition;
    layout(location = 1) in vec3 vNormal;
    layout(location = 2) in vec2 vUV;
     
    layout (std140) uniform Sunlight
    {
     
      vec4 SunlightPosition;
      vec4 SunlightDiffuse;
      vec4 SunlightSpecular;
      vec4 SunlightDirection;
      float constantAttenuation, linearAttenuation, quadraticAttenuation;
      float spotCutoff, spotExponent;
      float	EnableLighting;
      float	EnableSun;
      float ExtraValue;
     
    };
     
    out vec2 TextureCoordinates;
    out vec3 Vertex_Normal;
    out vec4 Vertex_LightDir;
    out vec4 Vertex_EyeVec;
     
    uniform mat4 MVP;
    uniform mat4 ViewMatrix;
    uniform mat4 ViewModelMatrix;
    uniform mat3 NormalMatrix;
     
    void main()
    {
     
      gl_Position = MVP * vec4(vPosition, 1.0);
     
      TextureCoordinates = vUV;
     
      Vertex_Normal = vec3(ViewModelMatrix * vec4(vNormal, 1.0));
     
      vec4 view_vertex = ViewModelMatrix * vec4(vPosition, 1.0);
      vec4 LP = ViewMatrix * SunlightPosition;
      Vertex_LightDir = LP - view_vertex;
      Vertex_EyeVec = -view_vertex;
     
    }
    Fragment Shader:
    Code :
    #version 330
    #extension GL_ARB_explicit_attrib_location : require
     
    precision highp float;
     
    //
    // These values vary per Mesh
    //
     
    uniform vec4 	AmbientMeshColor;
    uniform vec4 	EmissiveMeshColor;
    uniform vec4 	DiffuseMeshColor;
    uniform vec4 	SpecularMeshColor;
    uniform vec4	SceneBrightnessColor;
    uniform float 	MeshShininess;
    uniform float	ObjectHasTextureFile;
     
    //
    // Sunlight Settings.
    //
     
    layout (std140) uniform Sunlight
    {
     
      vec4 SunlightPosition;
      vec4 SunlightDiffuse;
      vec4 SunlightSpecular;
      vec4 SunlightDirection;
      float constantAttenuation, linearAttenuation, quadraticAttenuation;
      float spotCutoff, spotExponent;
      float	EnableLighting;
      float	EnableSun;
      float ExtraValue;
     
    };
     
    uniform vec4		SceneAmbient;
     
    //
    // Whether Materials are enabled at all.
    //
     
    uniform float		IfEnableTextures;
     
    //
    // If we are just simply drawing the skybox.
    //
     
    uniform float		DrawingSkyBox;
     
    uniform float		EnableWireframe;
     
    uniform vec4		WireframeColor;
     
    uniform float		TextureCoordinateDebug;
     
    uniform sampler2D MainTextureSampler; 
     
    in vec2 TextureCoordinates;
    in vec3 Vertex_Normal;
    in vec4 Vertex_LightDir;
    in vec4 Vertex_EyeVec;
     
    vec4 finalDiffuseColor;
     
    out vec4 finalColor;
     
    void DrawSkyBox() {
     
    	finalColor = texture(MainTextureSampler, TextureCoordinates);
     
    }
     
    void DrawWireFrame() {
     
    	finalColor = WireframeColor;
     
    }
     
    void main()
     
    {
     
    	if (DrawingSkyBox != 1.0) {
     
    		if (ObjectHasTextureFile == 1.0) {
     
    			finalDiffuseColor = texture(MainTextureSampler, TextureCoordinates);
     
    		} else {
     
    			finalDiffuseColor = DiffuseMeshColor;
     
    		}
     
    		vec4 CurrentObjectColor = AmbientMeshColor; 
     
    		vec4 N = vec4(normalize(Vertex_Normal), 0);
     
    		vec4 L = normalize(Vertex_LightDir);
     
    		float lambertTerm = dot(N,L);
     
    		if (lambertTerm > 0.0 && EnableSun == 1.0)
     
    		{
     
    			if (ObjectHasTextureFile == 1.0) {
     
    				CurrentObjectColor += SunlightDiffuse * DiffuseMeshColor * lambertTerm * finalDiffuseColor;
     
    			} else {
     
    				CurrentObjectColor += SunlightDiffuse * finalDiffuseColor * lambertTerm;
     
    			}
     
    			vec4 E = normalize(Vertex_EyeVec);
     
    			vec4 R = reflect(-L, N);
     
    			float specular = pow( max(dot(R, E), 0.0), MeshShininess);
     
    			CurrentObjectColor += SunlightSpecular * SpecularMeshColor * specular;	
    		}
     
    		finalColor.rgb = CurrentObjectColor.rgb;
     
    		//finalColor.rgb += SceneBrightnessColor.rgb;
     
    		finalColor.a = DiffuseMeshColor.a;
     
    		//finalColor.a += SceneBrightnessColor.a;
     
     
    	} else {
     
    		DrawSkyBox();
     
    	}
     
    }

    Last edited by tmason; 02-06-2015 at 12:05 AM. Reason: Fixed Image

  2. #2
    Junior Member Regular Contributor uwi2k2's Avatar
    Join Date
    Mar 2011
    Posts
    138
    hi,

    without looking deeper it may be that the normals of your 3d model are wrong.
    open it in a 3d program and check it.

    regards
    uwi2k2
    uwi2k2 - OpenGL Trainer: www.opengl-trainer.com
    ---------------------------------------------------------
    The OpenGL and VULKAN Podcast ( OpenGL2GO ) : OpenGL & Vulkan Podcast
    ---------------------------------------------------------
    timbling.com - Indie TeamUp

  3. #3
    Junior Member Regular Contributor
    Join Date
    Apr 2014
    Posts
    109
    Hello,

    Thank you, I will look into this.

    One item I am trying to find which seems simple to do using the GLM library:

    How do I calculate the Inverse View matrix? I could write my own functions but I wonder if I can use the GLM::inverse() function against either the Model, View, or Projection matrix.

    Thank you.

  4. #4
    Senior Member OpenGL Lord
    Join Date
    May 2009
    Posts
    5,932
    How do I calculate the Inverse View matrix? I could write my own functions but I wonder if I can use the GLM::inverse() function against either the Model, View, or Projection matrix.
    It wouldn't be much of an inverse matrix if you couldn't.

  5. #5
    Junior Member Regular Contributor
    Join Date
    Apr 2014
    Posts
    109
    Quote Originally Posted by Alfonse Reinheart View Post
    It wouldn't be much of an inverse matrix if you couldn't.
    OK, thank you.

    Maybe I am an idiot that is missing the obvious but to get the Inverse View matrix I just do glm::inverse(ViewMatrix), right?

    I just want to be sure I am doing it against the right Matrix.

Tags for this Thread

Posting Permissions

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