3

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?

Chin
  • 19,717
  • 37
  • 107
  • 164
  • Possible duplicate of [What is the logic behind transforming normals with the transpose of the inverse of the modelview matrix?](http://stackoverflow.com/questions/13654401/what-is-the-logic-behind-transforming-normals-with-the-transpose-of-the-inverse) – Peter O. Mar 30 '16 at 09:07
  • For future reference: _Never_ calculate the transpose inverse _in_ the vs – RecursiveExceptionException Sep 23 '16 at 19:27

1 Answers1

4

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/

mock_blatt
  • 955
  • 5
  • 11