Hey guys,
I'm implementing deferred shading and I've decided to calculate shading for a single global light in the first pass, then do all the other lights in the second pass. I'm not sure If I've got my normal/light and vertex positions all in the correct space and the lighting effect does not look correct.
Vertex shader:
#version 400
layout (location = 0) in vec3 in_Position;
layout (location = 1) in vec3 in_Normal;
layout (location = 2) in vec3 in_TexCoord;
layout (location = 3) in vec3 in_Tangent;
layout (location = 4) in vec3 in_Binormal;
out vec3 PositionEye;
out vec3 NormalEye;
out vec3 TexCoord;
out vec3 Tangent;
out vec3 Binormal;
out vec3 LightDirEye;
uniform vec3 GlobalLightPos;
uniform mat4 u_ModelMatrix;
uniform mat4 u_ViewMatrix;
uniform mat4 u_ModelViewMatrix;
uniform mat3 u_NormalMatrix;
uniform mat4 u_MVP;
void main()
{
PositionEye = vec3(u_ModelViewMatrix * vec4(in_Position, 1.0));
NormalEye = u_NormalMatrix * in_Normal;
TexCoord = in_TexCoord;
vec3 lightDirWorld = normalize(GlobalLightPos - in_Position);
LightDirEye = u_NormalMatrix * lightDirWorld;
gl_Position = u_MVP * vec4(in_Position, 1.0);
}
Fragment shader:
#version 400
in vec3 PositionEye;
in vec3 NormalEye;
in vec3 TexCoord;
in vec3 Tangent;
in vec3 Binormal;
in vec3 LightDirEye;
uniform sampler2D Texture;
uniform float TexRepeat;
layout (location = 0) out vec3 out_Position;
layout (location = 1) out vec3 out_Normal;
layout (location = 2) out vec3 out_Diffuse;
layout (location = 3) out vec3 out_Tangent;
layout (location = 4) out vec3 out_Binormal;
vec3 shadePixel(vec3 diff)
{
vec3 s = LightDirEye;
float sDotN = max(dot(s, NormalEye), 0.0);
vec3 diffuse = vec3(1.0, 1.0, 1.0) * diff * sDotN;
return diffuse;
}
void main()
{
vec3 diff = texture(Texture, TexCoord * TexRepeat);
out_Position = PositionEye;
out_Normal = NormalEye;
out_Diffuse = shadePixel(texture(Texture, TexCoord * TexRepeat));
out_Tangent = Tangent;
out_Binormal = Binormal;
}
Uniforms:
glm::vec3 lightPos = m_sceneGraph->GetGlobalLightPos();
glm::mat4 viewMatrix = m_renderSystem->GetCamera()->GetViewMatrix();
glm::mat3 normalMat = m_renderSystem->GetCamera()->GetNormalMatrix();
m_graphics->SetUniform(id, "GlobalLightPos", lightPos);
m_graphics->SetUniform(id, "u_ViewMatrix", 1, false, viewMatrix);
m_graphics->SetUniform(id, "u_NormalMatrix", 1, false, normalMat);
glm::mat4 modelMatrix = modelNode->GetTransform();
glm::mat4 modelView = viewMatrix * modelMatrix;
glm::mat4 mvp = m_renderSystem->GetCamera()->GetProjectionMatrix() * modelView;
m_graphics->SetUniform(id, "u_ModelMatrix", 1, false, modelMatrix);
m_graphics->SetUniform(id, "u_ModelViewMatrix", 1, false, modelView);
m_graphics->SetUniform(id, "u_MVP", 1, false, mvp);
The light pos is just glm::vec3(0.0f, 50.0f, 0.0f)
and the normal matrix is:
glm::mat3 Camera::GetNormalMatrix()
{
return glm::transpose(glm::inverse(glm::mat3(m_view)));
}
Can't seem to work out what is going wrong here.