asido
asido

Reputation: 98

Matrices in OpenGL

So I am learning OpenGL with as the main resource having the "Red book". I am reading about matrix algebra, rotation/scaling/transform matrices and everything is great, but I just don't get one simple thing. Let's say the function glLoadIdentity(). It sets the default matrix of 4x4. So it sets 3 vertices and 1 point: (1,0,0) (0,1,0) (0,0,1) vertices, (0,0,0) point. But my question is, what do those correspond? Generally speaking, what does a matrix correspond in OpenGL? I got an idea that these are the directions of axies. But the axies of what? The camera?

Upvotes: 0

Views: 985

Answers (2)

Marcelo Cantos
Marcelo Cantos

Reputation: 185852

The default matrix is simply the identity matrix:

/1 0 0 0\
|0 1 0 0|
|0 0 1 0|
\0 0 0 1/

In the more general case (ignoring perspective and possibly other exotic transforms)...

/a d g j\
|b e h k|
|c f i l|
\0 0 0 1/

...the components of the transformed coordinate system are as follows:

         /a\
X-axis = |b|
         \c/

         /d\
Y-axis = |e|
         \f/


         /g\
Z-axis = |h|
         \i/

         /j\
Origin = |k|
         \l/

If you correlate these to the identity matrix, you can see where your "3 vertices and 1 point" come from.

Beyond the identity matrix, this applies to any transform — rotation, translation, etc. — that keeps the bottom row at (0 0 0 1), and provides a simple way to visualise such transforms. Simply think of the four components above as representing where the axes (1 0 0), (0 1 0), (0 0 1) and the origin (0 0 0) end up after being transformed by the matrix (keeping in mind that the axes are not absolute, but relative to the origin).

Upvotes: 0

Jackson Pope
Jackson Pope

Reputation: 14640

OpenGL matrices only correspond to a transformation, moving objects, vectors and points defined in one coordinate space to another. If you have a matrix M (m11 - m44 as shown below) and a vector V (v1 - v4) in one coordinate space then multiplying by M will convert your V vector (which could describe a movement vector, object location or an object vertex) to W (w1-w4) in a different coordinate space:

| m11 m12 m13 m14 |   | v1 |     | w1 |
| m21 m22 m23 m24 |   | v2 |     | w2 |
| m31 m32 m33 m34 | X | v3 |  =  | w3 |
| m41 m42 m43 m44 |   | v4 |     | w4 |

Where:

w1 = m11 * v1 + m12 * v2 + m13 * v3 + m14 * v4
w2 = m21 * v1 + m22 * v2 + m23 * v3 + m24 * v4
w3 = m31 * v1 + m32 * v2 + m33 * v3 + m34 * v4
w4 = m41 * v1 + m42 * v2 + m43 * v3 + m44 * v4

So if we think of v1 - v3 as the old x, y and z coordinates and set v4 to 1, then we can think of w1 - w3 as the new x, y and z coordinates there are a few things we can see:
m11 is a multiplier from the old x coordinate to the new one so it's used in scale transformations (and similarly for m22 and m33)
m14 multiplied by 1 and added to the new x coordinate so it is used for translations (and similarly for m24 and m34)

Rotations are a little harder to conceptualise but they are done by setting the other matrix values to appropriate values. You can read more here: http://gpwiki.org/index.php/Matrix_math

Upvotes: 5

Related Questions