• Create Account

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

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

4 replies to this topic

### #1rocklobster  Members

Posted 10 June 2012 - 06:03 AM

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

### #2clb  Members

Posted 10 June 2012 - 06:14 AM

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.
Me+PC=clb.demon.fi | C++ Math and Geometry library: MathGeoLib, test it live! | C++ Game Networking: kNet | 2D Bin Packing: RectangleBinPack | Use gcc/clang/emcc from VS: vs-tool | Resume+Portfolio | gfxapi, test it live!

### #3rocklobster  Members

Posted 10 June 2012 - 11:56 AM

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?

### #4clb  Members

Posted 10 June 2012 - 12:21 PM

That line is transforming the vertex normal vector from model local space to camera view space, and re-normalizes the result.
Me+PC=clb.demon.fi | C++ Math and Geometry library: MathGeoLib, test it live! | C++ Game Networking: kNet | 2D Bin Packing: RectangleBinPack | Use gcc/clang/emcc from VS: vs-tool | Resume+Portfolio | gfxapi, test it live!

### #5rocklobster  Members

Posted 10 June 2012 - 12:38 PM

Found a good visual description of the problem of not using the normal matrix to convert the normals into eye space

http://www.lighthouse3d.com/tutorials/glsl-tutorial/the-normal-matrix/

Edited by rocklobster, 11 June 2012 - 03:07 AM.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.