# What is the normal matrix and how can i use it?

This topic is 2176 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi guys,

I'm currently trying to implement some diffuse lighting in a shader and i've been looking at some examples around the web. I understand the view, model and projection matrix and how they work to move from model to view to projection but i've been seeing this Normal Matrix and i have no idea what its for? The top of the shader has a basic setup like this

uniform mat4 ModelViewMatrix;
uniform mat3 NormalMatrix;
uniform mat4 ProjectionMatrix;
uniform mat4 MVP;

Apparently the NormalMatrix is a 3x3 matrix of the top left of the model view matrix

x x x x
x x x x
x x x x
x x x x

 MVP = Projection * view * model ModelView = view * model NormalMatrix = matrix3 (vector3(ModelView[0]), vector3(ModelView[1]), vector3(ModelView[2])); 

and later on it does

 vec3 norm = normalize (NormalMatrix * VertexNormal) 

I don't know why its multiplying the normal matrix by the current normal in the vertex shader. I suspect it has something to do with getting the interpolated normals over each face though.

Thanks for any help

##### Share on other sites
The normal matrix is most likely the inverse transpose of the modelview matrix.

There is a mathematical property that says that if the modelview matrix contains nonuniform scaling, then it cannot be used to transform normal vectors, but instead the inverse transpose of the modelview matrix must be used for the normals. This is because normal vectors are so-called covariant vectors (point vectors and tangent/bitangent vectors are called contravariant vectors). See e.g. Eric Lengyel's math book on the subject for a closer treatise on this.

If the modelview matrix does not scale, or only has uniform scale, a separate inverse-transposed version of the modelview matrix is not needed, and one can get away with by transforming the normal with the modelview matrix, and possibly renormalizing the result afterwards if there was a scale factor.

##### Share on other sites

If the modelview matrix does not scale, or only has uniform scale, a separate inverse-transposed version of the modelview matrix is not needed, and one can get away with by transforming the normal with the modelview matrix, and possibly renormalizing the result afterwards if there was a scale factor.

So this line

 vec3 norm = normalize (NormalMatrix * VertexNormal) 

is basically putting the normal in model view space and re-normalizing it?

##### Share on other sites
That line is transforming the vertex normal vector from model local space to camera view space, and re-normalizes the result.

• 10
• 16
• 9
• 13
• 41