PDA

View Full Version : Help with GLSL/OpenGL Shader; Uneven Lighting



tmason
02-06-2015, 12:02 AM
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();

}

}


http://s13.postimg.org/o0nfnh83r/Uneven_Lighting.jpg

uwi2k2
02-06-2015, 12:27 AM
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

tmason
02-06-2015, 03:54 AM
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.

Alfonse Reinheart
02-06-2015, 06:18 AM
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.

tmason
02-06-2015, 11:06 AM
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.