introduction to opengl - matrices

Upload: korisnikkk

Post on 05-Apr-2018

221 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/31/2019 Introduction to OpenGL - Matrices

    1/5

    Faculty of Information Technology, Mostar

    09

    Introduction to OpenGLMatrices

    [email protected]

  • 7/31/2019 Introduction to OpenGL - Matrices

    2/5

    PrefaceMatrices play a very important role in computer graphics. Almost all graphics libraries apply matrices in

    their operations.

    It is crucial that you understand how matrices work in OpenGL. And almost all concepts here are

    applicable for most other graphics libraries and robotics field as well =).

    So let us get started =D.

  • 7/31/2019 Introduction to OpenGL - Matrices

    3/5

    Introduction to MatricesIn mathematics, a matrix (plural matrices, or less commonly matrixes) is a rectangular array of numbers,

    such as:

    Figure 1 Regular Matrix

    An item in a matrix is called an entry or an element. In the example, e.g. 1 and 5 are entries. Entries are

    often denoted by a variable with two subscripts like a1, 2 for example.

    So looking to a matrix, it is a collection of numbers divided in rows and columns. But in graphics

    application development you can look at it as a collection of vectors! Which allows us to do

    transformations which basically stored in 4x4 matrix (in the case of OpenGL and other robotics

    applications) and also allows to store the coordinate axis, which describes how we view the world or

    how the world is oriented. So instead of using 3 vectors or 4 vectors to describe your whole scene, we

    use 1 matrix to describe the whole thing =).

    NOTE: In OpenGL we use homogeneous coordinate system, and as a consequence, we will use a 4x4

    matrix that describes 4 dimensions where you can define translation, rotation and scaling and store it in

    the same matrix.

    Figure 2 shows how every row and column is corresponding to an axis; the W axis is taken for good

    measures. For further research about homogeneous coordinate system please refer to math references

    since discussing it here will be beyond the scope of this document =):

    Figure 2 a matrix in OpenGL (using Homogenous coordinate system)

    I will not cover adding or subtracting matrices because it is quite straight forward. Just add or subtract

    each element from the corresponding element, and thats it =). But number of elements in Matrix A

    should equal to the number of elements in Matrix B, or you cannot add or subtract A from B! so you

    should have 3x3 + 3x3 or 4x4 4x4 but you cannot do 4x3 + 3x4.

    Multiplying a matrix by a scalar is also done by multiplying the every element of the matrix with the

    scalar number =).

    What we are concerned with this document is multiplying matrices together (in other words Matrix A x

    Matrix B).

  • 7/31/2019 Introduction to OpenGL - Matrices

    4/5

    Multiplying MatricesIn order to multiply 2 matrices, you have to make sure thatthe number of columns of Matrix A is equal

    to the number of rows in Matrix B. And the new matrix which you are going to get will have a number

    of columns equal to number of Rows in Matrix A, and the number of rows will equal to number of

    columns in Matrix B, as shown in figure 3:

    2 x 3

    3 x 4

    Figure 3 Matrix multiplication rule

    As a result of this multiplication you will end up with a 2x4 matrix.

    Let us look to a small example here so we clarify how the operation is done by multiplying two 2x2

    matrices:

    7 25 3

    2 56 4

    If you recall how we made a dot product between 2 vectors, here you are applying the same operation

    with the rows and columns of these matrices, so you will get the following:

    7 2+ 2 6 = 26So the first element of the matrix at location 0,0 will equal to 26. Completing the calculations you will

    end up with the following result:

    26 43

    28 37Let us have a look at one more example of multiplying 2x3 by 3x2 matrices. So let me show you adifferent way of visualizing this operation, so probably it will make the light bulb turn in your head just in

    case if it didnt =):

    So the task is to do1 36 52 1

    2 5 76 10 3.And we are going to end up with 2x2 matrix as described before!

    1 36 52 1

    2 5 76 10 3 46 38

    72 71

    The rule applies, we can multiply!!

    This is the dimension of the result matrix

  • 7/31/2019 Introduction to OpenGL - Matrices

    5/5

    The lines represent which dot product you have to do with whom, so you will do the following:

    2,5,71,6,2 = 462,5,73,5,1 = 38and

    6,10,31,6,2 = 72

    6,10,33,5,1 = 71Hope it is clear by now =).

    Now imagine if I want to multiply 4x4 matrix by a 4x1 matrix (which is as a matter of fact a vector), I will

    end up with 4x1 vector!! That means if the 4x4 matrix was a transformation matrix, and multiplied it by

    the 4x1 vector, I will get the new location for that vector =). So that is why matrix multiplication is very

    important in defining how our objects will be positioned, behave, etc.

    In the next chapter we will have an introduction to OpenGL, and will discuss in detail how OpenGL

    makes the transformations using matrices multiplication =).