Sign in to follow this  
rocklobster

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

Recommended Posts

rocklobster    415
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

[b]x x x [/b]x
[b]x x x [/b]x
[b]x x x[/b] x
x x x x


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

and later on it does

[CODE]
vec3 norm = normalize (NormalMatrix * VertexNormal)
[/CODE]

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 this post


Link to post
Share on other sites
clb    2147
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 this post


Link to post
Share on other sites
rocklobster    415
[quote name='clb' timestamp='1339330441' post='4947897']
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.
[/quote]
So this line

[quote name='rocklobster' timestamp='1339329806' post='4947894']
[CODE]
vec3 norm = normalize (NormalMatrix * VertexNormal)
[/CODE]
[/quote]
is basically putting the normal in model view space and re-normalizing it?

Share this post


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

Share this post


Link to post
Share on other sites
rocklobster    415
Found a good visual description of the problem of not using the normal matrix to convert the normals into eye space

[url="http://www.lighthouse3d.com/tutorials/glsl-tutorial/the-normal-matrix/"]http://www.lighthouse3d.com/tutorials/glsl-tutorial/the-normal-matrix/[/url] Edited by rocklobster

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this