I have a scene with several models with individual positions and rotations. Given normals, the shaders apply simple bidirectional lighting to each pixel.
That is my vertex shader.
#version 150
in vec3 position;
in vec3 normal;
in vec2 texcoord;
out vec3 f_normal;
out vec2 f_texcoord;
uniform mat4 model;
uniform mat4 view;
uniform mat4 proj;
void main()
{
mat4 mvp = proj * view * model;
f_normal = normal;
f_texcoord = texcoord;
gl_Position = mvp * vec4(position, 1.0);
}
And here is the fragment shader.
#version 150
in vec3 f_normal;
in vec2 f_texcoord;
uniform sampler2D tex;
vec3 sun = vec3(0.5, 1.0, 1.5);
void main()
{
vec3 light = max(0.0, dot(normalize(f_normal), normalize(sun)));
gl_FragColor = texture(tex, f_texcoord) * vec4(light, 1.0);
}
For objects without rotation this works fine. But for rotated models the lighting is rotated as well which, of course, shouldn't be the case.
The reason for that is that the normals are not rotated. I already tried f_normal = model * normal;
but this applies both rotation and transformation to the normals.
So how can I rotate the normals in the vertex shader before I send them to the fragment shader for lighting? What is a common approach?