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:
#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:
#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();
}
}