Skip to content Skip to sidebar Skip to footer

Opengl Es 2.0 An Android: What Dimensions Should The Normalmatrix Have?

I am learning OpenGL ES 2.0, without ever having learned OpenGL or OpenGL ES 1.x. I'm applying non-uniform scaling to my modelViewMatrix, so the tutorials tell me that I need to ta

Solution 1:

A 3x3 matrix is enough to transform the normals.

The primary purpose of using 4x4 matrices that operate on homogenous coordinates for positions is that they can express translations. A 3x3 matrix applied to a 3-member vector can not express translations. You can easily confirm that because it will always map the origin back to the origin.

Since normals are vectors, and not positions, we specifically do not want to apply the translation part of the modelview matrix to them. They describe directions, and directions do not change when a translation is applied.

So the cleanest approach is to use a 3x3 matrix for normals, and set it to the inverse-transpose of the top-left 3x3 elements of the modelview matrix.

In the case where you only have rotations and uniform scaling in the modelview matrix (which does not apply to your specific situation), people sometimes use the same modelview matrix that they also use for the positions. Which is correct, as long as the translation part is not applied. This can be done by setting the w component of the vector to zero for the multiplication:

vec3transformedNormal= (modelViewMatrix * vec4(originalNormal, 0.0)).xyz

With the w component being zero, the matrix elements that encode the translation have no effect on the result, and this corresponds to using only the top-left 3x3 part of the matrix. As long as this is the same as the inverse-transpose of the matrix, which is the case for rotations and uniform scaling, this is a valid shortcut.

Post a Comment for "Opengl Es 2.0 An Android: What Dimensions Should The Normalmatrix Have?"