Vexator
12-09-2011, 02:36 AM
I'm rather embarrassed to ask for help on this but I can't figure out what the problem is. I want to simulate basic phong shading in GLSL. However, the resulting shading is influenced by the camera's orientation. Can you spot my mistake? I've tried switching spaces, to no avail.
Thank you!
Vertex shader:
#version 330 core
layout(location = 0) in vec4 in_vertexPos;
layout(location = 1) in vec2 in_textureCoords;
layout(location = 2) in vec4 in_weights;
layout(location = 3) in ivec4 in_boneIds;
layout(location = 4) in vec3 in_normal;
out Vertex
{
vec3 normal;
vec3 eyeVector;
vec3 lightVector;
vec2 textureCoords;
} vertex;
layout(std140) uniform;
uniform Transform
{
mat4 view;
mat4 model;
mat3 normal;
mat4 modelView;
vec4 animate;
mat4 bones[64];
} transform;
uniform Projection
{
mat4 perspective;
mat4 orthographic;
} projection;
vec3 vLightPosition = vec3( 5.0, 5.0, 5.0 );
void main()
{
vertex.normal = transform.normal * in_normal;
vertex.lightVector = mat3(transform.modelView) * (vLightPosition - in_vertexPos.xyz);
gl_Position = projection.perspective * transform.modelView * in_vertexPos;
}
Fragment shader:
#version 330 core
in Vertex
{
vec3 normal;
vec3 eyeVector;
vec3 lightVector;
vec2 textureCoords;
} vertex;
out vec4 finalColor;
uniform Material
{
vec4 ambient;
vec4 diffuse;
vec4 specular;
float shininess;
} material;
uniform bool renderWireframes;
uniform sampler2D u_diffuseTexture;
void main()
{
float diff = max(0.0, dot(normalize(vertex.normal), normalize(vertex.lightVector)));
finalColor = vec4( vec3(diff), 1.0);
}
Here's how I setup the uniform matrices:
m_graphicsEngine->updateUniformBuffer( m_transformBuffer, &m_viewMatrix, "view" );
const mat4 &modelMatrix = transform->getModelMatrix();
m_graphicsEngine->updateUniformBuffer( m_transformBuffer, &modelMatrix, "model" );
const mat4 modelViewMatrix = m_viewMatrix*modelMatrix;
m_graphicsEngine->updateUniformBuffer( m_transformBuffer, &modelViewMatrix, "modelView" );
const mat3 normalMatrix = glm::inverseTranspose( mat3(modelViewMatrix) );
m_graphicsEngine->updateUniformBuffer( m_transformBuffer, &normalMatrix, "normal" );
Thank you!
Vertex shader:
#version 330 core
layout(location = 0) in vec4 in_vertexPos;
layout(location = 1) in vec2 in_textureCoords;
layout(location = 2) in vec4 in_weights;
layout(location = 3) in ivec4 in_boneIds;
layout(location = 4) in vec3 in_normal;
out Vertex
{
vec3 normal;
vec3 eyeVector;
vec3 lightVector;
vec2 textureCoords;
} vertex;
layout(std140) uniform;
uniform Transform
{
mat4 view;
mat4 model;
mat3 normal;
mat4 modelView;
vec4 animate;
mat4 bones[64];
} transform;
uniform Projection
{
mat4 perspective;
mat4 orthographic;
} projection;
vec3 vLightPosition = vec3( 5.0, 5.0, 5.0 );
void main()
{
vertex.normal = transform.normal * in_normal;
vertex.lightVector = mat3(transform.modelView) * (vLightPosition - in_vertexPos.xyz);
gl_Position = projection.perspective * transform.modelView * in_vertexPos;
}
Fragment shader:
#version 330 core
in Vertex
{
vec3 normal;
vec3 eyeVector;
vec3 lightVector;
vec2 textureCoords;
} vertex;
out vec4 finalColor;
uniform Material
{
vec4 ambient;
vec4 diffuse;
vec4 specular;
float shininess;
} material;
uniform bool renderWireframes;
uniform sampler2D u_diffuseTexture;
void main()
{
float diff = max(0.0, dot(normalize(vertex.normal), normalize(vertex.lightVector)));
finalColor = vec4( vec3(diff), 1.0);
}
Here's how I setup the uniform matrices:
m_graphicsEngine->updateUniformBuffer( m_transformBuffer, &m_viewMatrix, "view" );
const mat4 &modelMatrix = transform->getModelMatrix();
m_graphicsEngine->updateUniformBuffer( m_transformBuffer, &modelMatrix, "model" );
const mat4 modelViewMatrix = m_viewMatrix*modelMatrix;
m_graphicsEngine->updateUniformBuffer( m_transformBuffer, &modelViewMatrix, "modelView" );
const mat3 normalMatrix = glm::inverseTranspose( mat3(modelViewMatrix) );
m_graphicsEngine->updateUniformBuffer( m_transformBuffer, &normalMatrix, "normal" );