Hi,
I am encountering an issue where am sure I am the culprit but am not able to quite figure out what is it that I am doing wrong. Here is the vertex shader :
#version 400
layout(location=0) in vec3 in_Position;
layout(location=1) in vec3 in_Normal;
layout(location=2) in vec2 in_Tex;
out vec2 vs_outTex;
out vec3 vs_outNormal;
out vec3 vs_outLightDir;
out vec4 vs_outObjColor;
// uniforms...
uniform mat4 matWorld;
uniform mat4 matView;
uniform mat4 matProj;
vec3 LightDirection = vec3(1,1,1);
vec4 objColor = vec4(1,0,0,1);
void main()
{
mat4 WVP = matProj * matView * matWorld;
gl_Position = WVP * vec4(in_Position, 1.0);
vs_outNormal = in_Normal;
vs_outLightDir = normalize(LightDirection);
vs_outTex = in_Tex;
vs_outObjColor = objColor;
}
and here is the fragment shader :
#version 400
in vec2 vs_outTex;
in vec3 vs_outNormal;
in vec3 vs_outLightDir;
in vec4 vs_outObjColor;
out vec4 outColor;
uniform sampler2D texture_diffuse1;
void main()
{
vec4 baseColor = vec4(texture(texture_diffuse1, vs_outTex));
float NdotL = max(dot(vs_outNormal, vs_outLightDir), 0);
vec4 Emissive = baseColor;
vec4 Ambient = vec4(0.2, 0.2, 0.2, 1);
vec4 DiffuseDirect = vec4(NdotL);
outColor = vs_outObjColor * DiffuseDirect; //Emissive; // * (Ambient + DiffuseDirect);
}
Here is the output produced by the shader during rendering :
Obviously, you guys must have noticed that I have not transformed object normals from object space to world space. Due to this, when the object rotates the lighting rotates with it giving a feeling that light is attached to the object and rotates with it. However, logically what I am trying to do is implement simple directional light shader where direction is suppose to be in world space, so no matter what orientation object is in, lighting direction remains constant.
For this to work, I took my object space normals to world space by doing following changes to the code in vertex shader :
vs_outNormal = (matWorld * vec4(in_Normal, 1.0)).xyz;
After doing this, here is the output I get :
This time around, deer at the center receives proper lighting & gives impression of light ( which is global directional light ) being static if object rotates etc. However, other deer object's lighting has gone for the toss! Interestingly the closer i get left & right deers to the center deer object, lighting seems to be working fine. Here is the outptut :
Can anyone suggest where am I screwing up things? Why lighting seems to be working fine only when Object is at (0,0,0) but the time we move to left or right, lighting seem to be changing? ( Obviously this will happen in case the light source is point light with attenutation, but its directional light for me! )
Any thoughts or way forward in debugging will be really helpful.
Thanks.