I have a problem with my per vertex diffuse lighting implementation. I cannot find the problem. tried everything. Maybe someone can help.
Here is the code.
Here is how I calculate normals for every point and store it in vbo
//NormalsglGenBuffers(1, &m_vbo_normals);glBindBuffer(GL_ARRAY_BUFFER, m_vbo_normals);for (int i = 0; i < 24; i += 3){glm::vec3 normal = glm::normalize(glm::cross(glm::vec3(corners[indices[i + 1]]) -glm::vec3(corners[indices]), glm::vec3(corners[indices[i + 2]]) -glm::vec3(corners[indices])));normals = normal.x;normals[i + 1] = normal.y;normals[i + 2] = normal.z;}glBufferData(GL_ARRAY_BUFFER, 24 * sizeof(float), normals, GL_STATIC_DRAW);glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, 0);glEnableVertexAttribArray(2);
Vertex.vert
#version 150 coreuniform mat4 modelview, projection;
uniform mat3 normalMatrix;uniform vec3 lightPos;uniform vec3 lambient;uniform vec3 ldiffuse;in vec4 position;in vec3 normal;in vec3 inColour;out vec3 outColour;void main(){gl_Position = projection*modelview*position;vec3 ambient = inColour * lambient;//current position of the vertexvec3 pos = vec3(modelview * position);vec3 n = normalize(normalMatrix * normal);//vector to pointing to the light posvec3 I = normalize(lightPos - pos);float dcont = max(0.0, dot(n, I));vec3 diffuse = dcont * inColour * ldiffuse;outColour = diffuse;}
fragmet.frag
#version 150 corein vec3 outColour;
out vec3 outFrag;void main(){outFrag = outColour;}
and here is how I calculate normalMatrix and send uniform variables
glTranslatef( 0.0, 0.0, -2.0 );
glRotatef( m_euler[0], 1.0, 0.0, 0.0 );glRotatef( m_euler[1], 0.0, 1.0, 0.0 );glRotatef( m_euler[2], 0.0, 0.0, 1.0 );glGetFloatv( GL_MODELVIEW_MATRIX, modelview );glGetFloatv( GL_PROJECTION_MATRIX, projection);glUniformMatrix4fv( m_uniform_modelview, 1, GL_FALSE, modelview );glUniformMatrix4fv( m_uniform_projection, 1, GL_FALSE, projection );glm::mat3* MV = new glm::mat3 (modelview[16]);setUniform(m_shader, "normalMatrix", -glm::transpose(glm::inverse(*MV)));glUniform3f(glGetUniformLocation(m_shader->GetProgramHandle(), "lambient"), 1.0, 1.0, 1.0);glUniform3f(glGetUniformLocation(m_shader->GetProgramHandle(), "ldiffuse"), 1.0, 1.0, 1.0);glUniform3f(glGetUniformLocation(m_shader->GetProgramHandle(), "lightPos"), 1.0, 1.0, 1.0);glUniform3f(glGetUniformLocation(m_shader->GetProgramHandle(), "direction"), 1.0, 1.0, 0.0);
Thank you for help.