I am basically following this tutorial about diffuse lighting in OpenGL. I did some modifications but the idea is the same, at least that's what I think. However, my fragment shader is not showing any colors. I checked and rechecked my uniform variables and light properties and data is ok on CPU side (don't know how to check on GPU side, though). Here are my shaders where I cannot spot any errors, maybe someone can help me out
VERTEX SHADER
#version 410
in vec4 vposition;
in vec3 vnormal;
uniform mat4 M;
uniform mat4 VP;
out vec4 fcolor;
out vec3 fnormal;
void main()
{
gl_Position = vposition * M * VP;
fcolor = vposition + vec4(0.5);
fnormal = (M * vec4(vnormal, 0.0)).xyz;
}
FRAGMENT SHADER
#version 410
in vec4 fcolor;
in vec3 fnormal;
out vec4 color;
uniform vec3 lightColor;
uniform vec3 lightDirection;
uniform float lightIntensity;
void main()
{
vec3 N = normalize(fnormal);
vec3 L = normalize(lightDirection);
float visibility = clamp(dot(N, L), 0.0, 1.0);
color = fcolor * vec4(lightColor, 1.0) * lightIntensity * visibility;
//color = vec4(1.0); Not even this shows model in complete white
}
UPDATE
The buffer for my mesh are created like follows, maybe it helps out detecting the problem
void Mesh::initBuffers()
{
glGenVertexArrays(1, &m_verticesVao);
glBindVertexArray(m_verticesVao);
glGenBuffers(1, &m_positionsVbo);
glBindBuffer(GL_ARRAY_BUFFER, m_positionsVbo);
glBufferData(GL_ARRAY_BUFFER, m_numVertices * 3 * sizeof(float), &m_positions[0], GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), 0);
glGenBuffers(1, &m_normalsVbo);
glBindBuffer(GL_ARRAY_BUFFER, m_normalsVbo);
glBufferData(GL_ARRAY_BUFFER, m_numVertices * 3 * sizeof(float), &m_normals[0], GL_STATIC_DRAW);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), 0);
glGenBuffers(1, &m_positionsEbo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_positionsEbo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, m_numIndices * 3 * sizeof(unsigned int), &m_indices[0], GL_STATIC_DRAW);
glBindVertexArray(0);
m_initBuffers = true;
}
Commenting all the code related to the normal in shaders results in a correct rendered mesh with colors (or white)
If it helps, you can checkout my code