Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding OpenGL Matrices

Tags:

I'm starting to learn about 3D rendering and I've been making good progress. I've picked up a lot regarding matrices and the general operations that can be performed on them.

One thing I'm still not quite following is OpenGL's use of matrices. I see this (and things like it) quite a lot:

x y z n
-------
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1

So my best understanding, is that it is a normalized (no magnitude) 4 dimensional, column-major matrix. Also that this matrix in particular is called the "identity matrix".

Some questions:

  • What is the "nth" dimension?
  • How and when are these applied?

My biggest confusion arises from how OpenGL makes use of this kind of data.

like image 919
Alexander Trauzzi Avatar asked Mar 17 '10 19:03

Alexander Trauzzi


People also ask

How does OpenGL matrix work?

By default, in OpenGL, the viewer is positioned on the z axis, it is like using a camera to take a shot. Imagine that your camera points to the origin of the Cartesian system. The up direction is parallel to the Oy axis and in the positive sense of Oy. The view matrix in OpenGL controls the way we look at a scene.

What is identity matrix OpenGL?

Just to recap what others have said, the identity matrix is a matrix that is such that when you multiply a vector/matrix with it, the result is that same vector/matrix. It's the equivalent of the number 1 with multiplication or the number 0 with addition.

What is the common size of OpenGL matrices *?

OpenGL uses 4 x 4 matrix for transformations. Notice that 16 elements in the matrix are stored as 1D array in column-major order. You need to transpose this matrix if you want to convert it to the standard convention, row-major format.

How a matrix works in graphics programming?

A matrix can be multiplied by any other matrix as long as the first matrix has the same number of columns as the second matrix has rows. So a 1x3 matrix can be multiplied by a 3x3 matrix, which is fortunate because that's exactly what you need to do to multiply a matrix times a vector in 2D graphics programs.


2 Answers

In most 3D graphics a point is represented by a 4-component vector (x, y, z, w), where w = 1. Usual operations applied on a point include translation, scaling, rotation, reflection, skewing and combination of these.

These transformations can be represented by a mathematical object called "matrix". A matrix applies on a vector like this:

[ a b c tx ] [ x ]   [ a*x + b*y + c*z + tx*w ]
| d e f ty | | y | = | d*x + e*y + f*z + ty*w |
| g h i tz | | z |   | g*x + h*y + i*z + tz*w |
[ p q r s  ] [ w ]   [ p*x + q*y + r*z +  s*w ]

For example, scaling is represented as

[ 2 . . . ] [ x ]   [ 2x ]
| . 2 . . | | y | = | 2y |
| . . 2 . | | z |   | 2z |
[ . . . 1 ] [ 1 ]   [ 1  ]

and translation as

[ 1 . . dx ] [ x ]   [ x + dx ]
| . 1 . dy | | y | = | y + dy |
| . . 1 dz | | z |   | z + dz |
[ . . . 1  ] [ 1 ]   [   1    ]

One of the reason for the 4th component is to make a translation representable by a matrix.

The advantage of using a matrix is that multiple transformations can be combined into one via matrix multiplication.

Now, if the purpose is simply to bring translation on the table, then I'd say (x, y, z, 1) instead of (x, y, z, w) and make the last row of the matrix always [0 0 0 1], as done usually for 2D graphics. In fact, the 4-component vector will be mapped back to the normal 3-vector vector via this formula:

[ x(3D) ]   [ x / w ]
| y(3D) ] = | y / w |
[ z(3D) ]   [ z / w ]

This is called homogeneous coordinates. Allowing this makes the perspective projection expressible with a matrix too, which can again combine with all other transformations.

For example, since objects farther away should be smaller on screen, we transform the 3D coordinates into 2D using formula

x(2D) = x(3D) / (10 * z(3D))
y(2D) = y(3D) / (10 * z(3D))

Now if we apply the projection matrix

[ 1 . .  . ] [ x ]   [  x   ]
| . 1 .  . | | y | = |  y   |
| . . 1  . | | z |   |  z   |
[ . . 10 . ] [ 1 ]   [ 10*z ]

then the real 3D coordinates would become

x(3D) := x/w = x/10z
y(3D) := y/w = y/10z
z(3D) := z/w = 0.1

so we just need to chop the z-coordinate out to project to 2D.

like image 190
kennytm Avatar answered Oct 28 '22 11:10

kennytm


The short answer that might help you get started is that the 'nth' dimension, as you call it, does not represent any visualizable quantity. It is added as a practical tool to enable matrix multiplications that cause translation and perspective projection. An intuitive 3x3 matrix cannot do those things.

A 3d value representing a point in space always gets 1 appended as the fourth value to make this trick work. A 3d value representing a direction (i.e. a normal, if you are familiar with that term) gets 0 appended in the fourth spot.

like image 38
Alan Avatar answered Oct 28 '22 10:10

Alan