Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I have to calculate the transpose of the inverse of the model matrix in order to calculate the normal for the reflection texture?

I am following this tutorial to create a skybox/cubemap with environmental mapping. I am having some trouble understanding a calculation in the vertex shader:

#version 330 core
layout (location = 0) in vec3 position;
layout (location = 1) in vec3 normal;

out vec3 Normal;
out vec3 Position;

uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;

void main()
{
    gl_Position = projection * view * model * vec4(position, 1.0f);
    Normal = mat3(transpose(inverse(model))) * normal;
    Position = vec3(model * vec4(position, 1.0f));
}

Here, the author is calculating the Normal before passing it to the fragment shader to calculate the reflection texture:

Normal = mat3(transpose(inverse(model))) * normal;

My question is, what exactly does this calculation do? Why do you have to calculate the transpose of the inverse of the model matrix before multiplying it with the normal?

like image 268
Chin Avatar asked Sep 20 '25 15:09

Chin


1 Answers

If you don't do this, uneven scaling will distort the normal. I think this sums it up, with pictures, better than I possibly could: http://www.lighthouse3d.com/tutorials/glsl-12-tutorial/the-normal-matrix/

like image 153
mock_blatt Avatar answered Sep 23 '25 06:09

mock_blatt