Chin
Chin

Reputation: 20665

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?

Upvotes: 4

Views: 5476

Answers (1)

mock_blatt
mock_blatt

Reputation: 965

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/

Upvotes: 5

Related Questions