Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate the normal matrix?

I have some trouble with my normal matrix.

vs.glsl

#version 440

in vec3 vPosition;
in vec3 vNormal;

out vec4 eyeCordFs;
out vec4 eyeNormalFs;

uniform mat4 model;
uniform mat4 view;
uniform mat4 proj;

void main()
{   
    mat4 modelView = view * model;
    mat4 normalMatrix = view * transpose(inverse(model));
    vec4 eyeNorm = normalize(normalMatrix * vec4(vNormal, 0.0));
    vec4 eyeCord= modelView * vec4(vPosition, 1.0);

    eyeCordFs = eyeCord;
    eyeNormalFs = eyeNorm;

    gl_Position = proj * modelView * vec4( vPosition,1.0);
}

fs.glsl

#version 440

in vec4 eyeCordFs;
in vec4 eyeNormalFs;

out vec3 outputColor;

uniform vec4 lightPos;

void main()
{
    vec4 s = normalize(lightPos - eyeCordFs) ;
    vec4 r = reflect(-s,eyeNormalFs);
    vec4 v = normalize(-eyeCordFs);
    float spec = max( dot(v,r),0.0 );
    float diff = max(dot(eyeNormalFs,s),0.0);

    vec3 diffColor = diff * vec3(1,0,0);
    vec3 specColor = pow(spec,3) * vec3(1,1,1);
    vec3 ambientColor = vec3(0.1,0.1,0.1);

    outputColor =  diffColor + 0.5 * specColor + ambientColor; 
}

The output of this looks like enter image description here

Which seemed a bit strange to me. But I knew that I am not scaling anything so I thought I could use the modelView matrix to transform my normals.

So I changed the line

vec4 eyeNorm = normalize(normalMatrix * vec4(vNormal, 0.0));

to

vec4 eyeNorm = normalize(modelView * vec4(vNormal, 0.0));

And the output now looks like this

enter image description here

which looks correct. Am I calculating my normalMatrix the wrong way?

like image 345
Maik Klein Avatar asked Jan 12 '14 20:01

Maik Klein


People also ask

How do you find the normal of a matrix?

A matrix is normal if and only if either pre-multiplying or post-multiplying it by its conjugate transpose gives the same result. It turns out that a matrix is normal if and only if it is unitarily similar to a diagonal matrix.

What is normal matrix GLSL?

The normal matrix is defined as 'the transpose of the inverse of the upper-left 3x3 part of the model matrix'.

What is a normal in linear algebra?

In geometry, a normal is an object such as a line, ray, or vector that is perpendicular to a given object. For example, the normal line to a plane curve at a given point is the (infinite) line perpendicular to the tangent line to the curve at the point.

Are all real matrices normal?

All normal matrices are diagonalizable, but not all diagonalizable matrices are normal.


1 Answers

The normal matrix is the transpose inverse of the modelview matrix. So in GLSL it would be

mat4 normalMatrix = transpose(inverse(modelView));

However you should NOT calculate the normal matrix in the shader. Doing that in the shader wastes a lot of precious GPU cycles. Matrix inversion is not a cheap operation to start with and doing it in the shader forces the GPU to perform the calculation for each and every vertex again and again. Precalculate it on the CPU and pass it as a uniform.

like image 167
datenwolf Avatar answered Oct 18 '22 14:10

datenwolf