# Thread: What is a matrix's purpose in OpenGL

1. ## What is a matrix's purpose in OpenGL

In the OpenGL book that I bought it didn't go into detail what a Matrix was. I looked it up on the internet and discovered that it is a two-dimensional array (that's pretty much all it told me). What I don't understand is what is its purpose in OpenGL. I know there are functions in OpenGL to do with it (glPushMatrix and glPopMatrix i think), but I just don't what it does.
TIA All transformations (scaling, rotating, translation and etc) you can represent by one matrix.

P.S.: sorry for bad English  3. To clear up what x0ras said: the model view matrix keeps track of where you are in space. When you call all of those glRotate, glTranslate, and glScale calls, you're changing the matrix. In laymans terms, the matrix stack can be used to easily undo transoformations. If you try to make a 4x4 matrix in your code to check it out for yourself, keep in mind that OpenGL wants column major matricies.

If you were to read up on a transform matrix in a book, you would probably get something similar to matrix A below. The same matrix should actually look like matrix B in your code.
Code:
```A:                                   B:
| x  y  z  w |                    | x  x  x  0 |
| x  y  z  w |                    | y  y  y  0 |
| x  y  z  w |                    | z  z  z  0 |
| 0  0  0  1 |                    | w  w  w  1 |``` 4. Wow apparently my Matrices lesson hasnt made it to the tut page yet. I'll quote it here and if it doesnt format right ill include a link to the rtf file.

Lesson 8: Matrices

Written by: RoD

Sorry for the long wait for the newer lessons, life caught up with me and called in its debts. So, as you all have waited for, now we move into Matrices. This lesson is meant as a brief touch on matrices, not a great in-depth tutorial on them. A matrix is a two-dimensional array of numbers, with a set number of rows and columns. A matrix can be three dimensional also, but despite what you may be thinking, you don’t need to use three dimensional matrices in 3D programming.

The dimensions of matrices are defined as m x n, which means that the matrix has m rows and n columns. For example, if u had a matrix with 4 rows and 3 columns you would say that you had a 4 x 3 matrix. Let’s show an example matrix, we’ll call it “example”, and it’s a 3 x 3 matrix.

| 1 2 3 |
Example = | 4 5 6 |
| 7 8 9 |

To receive a value from a specific position in the matrix you would use the row and column value. Using matrix “example”, the value of 5 is at the (1,1) location. Why isn’t its location (2,2)? When you actually implement the matrix it is defined as two-dimensional, and in C/C++ the array start location is always (0,0). So the value 1 is at position (0,0), 2 is at (0,1), 3 is at (0,2), etc. Allow me to give you a diagram to demonstrate this concept:

| M0,0 M0,1 M0,2 |
| M1,0 M1,1 M1,2 |
| M2,0 M2,1 M2,2 |

This representation is important because this is not the last time you will see it. We’ll see this representation again in code, and during discussion. Now on to my least favorite part, matrices and math.

One useful matrix in matrix mathematics is the identity matrix. In the identity matrix ones are placed in the main diagonal of the matrix while zeroes are placed elsewhere. While identity matrices may be of any size, they must be square, or m x m. Now I am going to show you an example 3 x 3 identity matrix.

| 1 0 0 |
M = | 0 1 0 |
| 0 0 1 |

Ready for some cool facts on this matrix? Of course you are, now sit down! Whenever you multiply an identity matrix by another matrix, the result is always the original matrix. More to come on that soon, your brain is really spinning now isn’t it?

Matrix addition and subtraction is pretty straightforward. Each element of the two matrices are added to or subtracted from each other with the result being placed in the same (i,j) location as the elements that have been operated on. As you can see, M(0,0) is added to N(0,0) to get the result M+N(0,0). Each element of the result is computed in this way for both addition and subtraction.

| 1 2 3 | | 1 2 3 |
M = | 4 5 6 | N = | 4 5 6 |
| 7 8 9 | | 7 8 9 |

| 2 2 2 | | 1 2 3 | | (2+1) (2+2) (2+3) | | 3 4 5 |
M+N = | 0 1 4 | - | 4 5 6 | = | (0+4) (1+5) (4+6) | = | 4 6 10 |
| 7 3 1 | | 7 8 9 | | (7+7) (3+8) (1+9) | | 14 11 10 |

Note: Subtraction is exactly the opposite. Due to the surprising effort of making that diagram in MS Word, I am not making one for subtraction too.

When it comes to matrix multiplication there are two ways to do it. Firstly we will discuss scalar matrix multiplication, which involves multiplying a matrix by a scalar value. This operation is surprisingly simple and just involves multiplying the scalar value by each element of the matrix. As an example we will use a 2 x 2 matrix by the scalar value of 2:

M = | 1 3 | k = 2
| 2 0 |

C = k*m = 2*| 1 3 | = | (2*1) (2*3) | = | 2 6 |
| 2 0 | | (2*2) (2*0) | | 4 0 |

It’s important to know that not all matrices are created equal when multiplication is involved. In order to multiply two matrices together, the inner dimension of each matrix must be the same. The inner dimension applies to the column value of the first matrix and the row value of the second matrix.

Before multiplying two matrices, you can determine what dimensions the resulting matrix should have. The dimensions of the resulting matrix are equal to the outer dimensions of the two matrices. So if A is a 3 x 1 matrix and B is a 1 x 3 matrix, then the resulting matrix C is a 3 x 3 matrix. In other words, (3x1) x (1x3) will result in a 3 x 3 matrix.

There are many websites and guides on the internet about matrices to give u a more in-depth exploration of matrices and matrix mathematics. I hope you enjoyed reading from this lesson as much as I enjoyed writing it! Feel free to contact me at rod@cprogramming.com, or on the forums of this very site!

And as always, happy coding! 5. heres the actual file, much better formatting!

http://s87387589.onlinehome.us/Lesson8.rtf 6. Cool... Thanks everyone Popular pages Recent additions 