So, I got a model matrix (Via uniform variable) in the shader, is there any way to use the model matrix to transform the normal of a vertex that has been transformed with glm::transform/rotate?
If you want, here is the code:
VERTEX SHADER:
#version 330 core
layout(location = 0) in vec3 vertexPosition_modelspace;
layout(location = 1) in vec3 vertexColor;
layout(location = 2) in vec2 vertexUV;
layout(location = 3) in vec3 vertexNormal;
out vec3 Normal;
out vec3 fragmentColor;
out vec3 vertPos;
out vec2 UV;
uniform mat4 MVP;
void main(){
gl_Position = MVP * vec4(vertexPosition_modelspace,1.0f);
UV = vertexUV;
fragmentColor = vertexColor;
vertPos = vertexPosition_modelspace;
Normal = vertexNormal;
}
FRAGMENT SHADER:
#version 330 core
in vec3 Normal;
in vec3 fragmentColor;
in vec3 vertPos;
in vec2 UV;
out vec3 color;
uniform sampler2D textureSampler;
uniform mat4 Model;
void main(){
vec3 lightPos = vec3(1, 1, 1);
mat3 normalMatrix = transpose(inverse(mat3(Model))); // I think this line could do it, but it doesn't seem to work.
vec3 normal = normalize(normalMatrix * Normal);
vec3 fragPosition = vec3(Model * vec4(vertPos, 1));
vec3 surfaceToLight = lightPos - fragPosition;
float brightness = dot(normal, surfaceToLight) / (length(surfaceToLight) * length(normal));
brightness = clamp(brightness, 0.2, 1);
color = vec3(brightness * 1 * (texture(textureSampler, UV).rgb * fragmentColor));
}
* length(normal)
when calculating brightness is pretty redundant since the normal is always normalized meaning its length is 1. \$\endgroup\$