Returns a matrix to transform the normal.

Source: Internet
Author: User

Source: http://www.lighthouse3d.com/opengl/glsl/index.php? Normalmatrix

Glsl tutorial

 

The gl_normalmatrix

The gl_normalmatrix is present in vertex shaders. in here some light is shed on what is this matrix and what is it. this section was got red by the excellent book by Eric Lengyel "Mathematics for 3D game programming and computer graphics ".

Specified computations are done in eye space. this has to do with the fact that lighting needs to be saved med in this space, otherwise eye position dependent effects, such as specular lights wocould be harder to implement.

Hence we need a way to transform the normal into eye space. To transform a vertex to eye space we can write:

 

vertexEyeSpace = gl_ModelViewMatrix * gl_Vertex;

So why can't we just do the same with a normal vector? First a normal is a vector of 3 floats and the modelview matrix is 4x4. This cocould be easily overcome with the following code:

 

normalEyeSpace = vec3(gl_ModelViewMatrix * vec4(gl_Normal,0.0));

So,Gl_normalmatrixIs just a shortcut to simplify code writing? No, not really. The above line of code will work in some circunstances but not all.

Lets have a look at a potential problem:

In the above figure we see a triangle, with a normal and a tangent vectors. The following figure shows what happens when the modelview matrix contains a non-uniform scale.

Note: If the scale was uniform, then the direction of the normal wowould have been preserved, the length wowould have been affected but this can be easily fixed with a normalization.

In the above figure the modelview matrix was applied to all the vertices as well as to the normal and the result is clearly wrong: the normal is no longer perpendicular to the surface.

So now we know that we can't apply the modelview in all cases to transform the normal vector. The question is then, what matrix shocould we apply?

We know that, prior to the matrix transformation T. n = 0, since the vectors are by definition perpendicular. we also know that after the transformation n '. t 'must remain equal to zero, since they must remain perpendicular to each other. let's assume that the matrix G is the correct matrix to transform the normal vector. t can be multiplied safely by the upper left 3x3 submatrix of the modelview (T is a vector, hence the W component is zero ). this is because t can be computed as the difference between two vertices, therefore the same matrix that is used to transform the vertices can be used to transform T. hence the following equation:

The dot product can be transformed into a product of vectors, therefore:

Note that the transpose of the first vector must be considered since this is required to multiply the vectors. We also know that the transpose of a multiplication is the multiplication of the transposes, hence:

We started by stating that the dot product between N and T was zero, so if the following equation is true then we are on the right track.

Applying a little algebra yieds

Therefore the correct matrix to transform the normal is the transpose of the inverse of the M matrix. OpenGL computes this for us inGl_normalmatrix.

In the beginning of this section it was stated that using the modelview matrix wocould work in some cases. Whenever the 3x3 upper left submatrix of the modelview is orthogonal we have:

This is because with an orthogonal matrix, the transpose is the same as the inverse. So what is an orthogonal matrix? An orthogonal matrix is a matrix where all columns/rows are unit length, and are mutually perpendicular. this implies that when two vectors are multiplied by such a matrix, the angle between them after transformation by an orthogonal matrix is the same as prior to that transformation. simply put the transformation preserves the angle relation between vectors, hence normals remain perpendicular Tangents! Furthermore it preserves the length of the vectors as well.

So when can we be sure that M is orthogonal? When we limit our geometric operations to rotations and translations, I. e. When in the OpenGL application we only useGlrotateAnd gl_translate and notGlscale. These operations guarantee that M is orthogonal. Note:GlulookatAlso creates an orthogonal matrix!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.