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

Threaded View

Previous Post Previous Post   Next Post Next Post
  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

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
  •