4
\$\begingroup\$

In openGl ES and the World of 3D - why use the invers matrix?

The thing is that I dont have any intuition to, why it is used, therefore please correct me:

As fare as I understand, it is used in shaders - and can help you to figure out the opposite direction of the normals?

Invers in ordinary numbers is like; The product of a number and its multiplicative inverse is 1. Observe that 3/5 * 5/3 = 1. In a matrix this will give you the Identity Matrix, which is the base coordinate system or the orion of the World space - right. But the invers is - some other coordinate system?

You can use the transpose(Row-major order to Column-major order) of a square matrix to find the inverted matrix, as calculating the invers is process heavy - and the transpose is giving you the inverted matrix as a bi product?

Again, I am looking for getting some intuition of this - and therefore be able to use it as intended. Thank you for any reply that will guide me in the right direction.

Regards

\$\endgroup\$

2 Answers 2

9
\$\begingroup\$

A matrix is used to convert a vector from one coordinate system to another (say, from coordinate system 'A' to coordinate system 'B').

The inverse of a matrix is a matrix which converts the other direction (say, from 'B' back into 'A')

In games, we commonly have a matrix which will transform positions from world-space into homogenous eye-space, since rendering all happens in homogenous eye-space. But homogenous coordinates can be difficult to work with, so some types of shaders want to convert those homogenous camera-relative coordinates back into world coordinates, so inverting the matrix allows us to easily convert those coordinates back into world-space inside the shader.

In models we often have a hierarchy, where each piece of the model has a matrix which describes how to convert from its local coordinate system (used by its own vertices), to the coordinate system used by its parent, so that you can multiply a vertex's position by that matrix to find where the vertex is relative to the parent. (And if you then multiply by the parent's matrix, and the parent's parent's matrix, and so on, you'll eventually find the vertex's absolute world position) If you invert the child-to-parent matrix, though, it allows you to go the other way; take one of the parent's vertices, multiply its position by the matrix, and the result will tell you where that position would be, expressed inside the child's coordinate system (and thus, relative to its vertices).

In a game, we normally specify matrices going just one direction; from deeply nested renderable objects, through less-deeply-nested renderable objects, through a view matrix, and finally into homogenous eye-space for rendering. Which is normally all we need, since all we normally want to do is to find world-space or render-space. But for those unusual cases where we really do want to know where a world-space point is relative to a nested object (or which direction a light is shining relative to a movable object, or etc), then inverting matrices allows us to transform vectors between coordinate systems in the opposite direction.

\$\endgroup\$
3
  • \$\begingroup\$ @TrevorPowell Just missed the transpose part. I know that statement is fake (you can't invert a matrix by just transposing it) but, what's the purpose of transposing a matrix? (other than going from column major systems to row major systems and vice-versa) \$\endgroup\$ Nov 18, 2012 at 17:36
  • \$\begingroup\$ Much belatedly: A matrix's "transpose" is swapping its rows for its columns. For example, if a 3x3 matrix's rows are {a1,a2,a3},{b1,b2,b3},{c1,c2,c3}, then the rows of its transpose are {a1,b1,c1},{a2,b2,c2}, {a3,b3,c3}. As it turns out, if you have a 3x3 matrix which represents a rotation in 3D space, the transpose of that matrix is also the inverse of that rotation matrix. And it's a LOT easier and faster to calculate. We often use rotation matrices in computer graphics, so it can be a handy thing to know. But using the transpose to invert only works for 3x3 pure rotation matrices. \$\endgroup\$ Sep 8, 2013 at 4:35
  • \$\begingroup\$ (More explicitly: The "transpose == inverse" trick does not work for the commonly-used sorts of 4x4 matrices, or for 3x3 matrices which combine both rotation and scaling) \$\endgroup\$ Sep 8, 2013 at 4:38
2
\$\begingroup\$

Trevor gave a good explanation about matrices, but since your question included "can help you to figure out the opposite direction of the normals?", I think normal matrices should be mentioned as well. Normal matrix is needed for transforming normals when the modelview matrix has more than just translation and rotation. It can be calculated from the modelview matrix with transpose of the inverse.

\$\endgroup\$
2
  • \$\begingroup\$ Note that the gl_NormalMatrix discussed in the link (like most 'standard' shader uniforms) was removed in OpenGL 3.x. So if you're targeting recent versions of the OpenGL spec and you want to use a normal matrix, you'll have to calculate the matrix explicitly. \$\endgroup\$ Nov 18, 2012 at 11:42
  • \$\begingroup\$ Thank you all for your replys. Just to be clear, you find the normal by invers from projection matrix to ModelView matrix and then the transpose of this(a tangent vectors) will give you the direction of the normal. Sorry, but I am just learning to wrap my head around this. \$\endgroup\$
    – user699215
    Nov 19, 2012 at 11:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .