I am currently working on an OpenGL framework and recently created a .ply (Polygon File Format) mesh importer. With the importer working, I wanted to try out a simple lighting shader on an imported mesh but am running into issues.
The mesh is a low-res version of the famous Stanford Bunny taken from the Stanford 3D Scanning Repository and was initially imported into Blender to verify correctness and then exported once again in order to get nX, nY, nZ values as opposed to confidence and intensity. This file can be found at http://pastebin.com/raw.php?i=xRmLYN0s
What is happening is that the lighting shader is acted like a flat shader that changes color based on camera orientation. Images are included at the end of this post.
The shader is from the OpenGL Super Bible and is described as Simple diffuse, directional, and vertex based light:
Vertex
#version 140
uniform mat4 mvMatrix;
uniform mat4 pMatrix;
uniform vec4 vColor;
in vec4 vVertex;
in vec3 vNormal;
out vec4 vFragColor;
void main( void )
{
mat3 mNormalMatrix;
mNormalMatrix[0] = mvMatrix[0].xyz;
mNormalMatrix[1] = mvMatrix[1].xyz;
mNormalMatrix[2] = mvMatrix[2].xyz;
vec3 vNorm = normalize(mNormalMatrix * vNormal);
vec3 vLightDir = vec3(0.0, 0.0, 1.0);
float fDot = max(0.0, dot(vNorm, vLightDir));
vFragColor.rgb = vColor.rgb * fDot;
vFragColor.a = vColor.a;
gl_Position = ( pMatrix * mvMatrix ) * vVertex;
}
Fragment
#version 140
in vec4 vFragColor;
void main( void )
{
gl_FragColor = vFragColor;
}
Could this be an issue with the normals? I have verified that they are being extracted from the model file properly and they are then fed into a VAO:
glBindVertexArray( m_VAO );
glEnableVertexAttribArray( ToolBox::Math::GLSL_ATTR::NORMAL );
glBindBuffer( GL_ARRAY_BUFFER, m_NormalBuffer );
glBufferData( GL_ARRAY_BUFFER, sizeof( GLfloat ) * num_elements, normals, GL_STATIC_DRAW );
glVertexAttribPointer( ToolBox::Math::GLSL_ATTR::NORMAL, 3, GL_FLOAT, GL_FALSE, 0, ( const GLvoid* )0 );
glBindVertexArray( 0 );
glBindBuffer( GL_ARRAY_BUFFER, 0 );
This is the same VAO used for the spatial (x,y,z) information and has been used in the past for color and texture values. Also the shaders are parsed by a custom ShaderManager class, and the vNormal attribute has been confirmed to be bound to the same index (ToolBox::Math::GLSL_ATTR::NORMAL aka 2) via glBindAttribLocation as the normal buffer in the above snippet.
From what I can tell, the normals should be being passed correctly from the file to the shader. But if I set vFragColor.rgb = vNormal.xyz in the vertex shader, the bunny comes out a solid red. Thats not good, is it?
So any ideas why the shader is not working? Is it bad? Are the normal not being passed? Something else?
Thank you for any assistance you may be able to provide. If you need more information, just let me know.
Images
Rotating around the bunny:



Setting vFragColor.rgb = vNormal.xyz;

Model in Blender (confirming its correctness)


ToolBox_Batch.cpp,ToolBox_Batch_Module_StanfordPLY.cpp,ToolBox_Shaders.cpp; Though the code is long and has little internal documentation as of now. - ssell