TBGL
Lesson #4
Matrix functions

This lesson will show you the simplicity of matrix handling


  Although there are several mathematical methods of performing matrix modifications, you will not need to struggle with this !

TBGL has several commands to handle this topic:



  tbgl_Scale x, y, z

  tbgl_Translate x, y, z

  tbgl_Rotate Angle, x, y, z
 


  TBGL_Scale can scale the current matrix by defined factor
The default value for X, Y and Z is 1.

If you want deform matrix in X axis by factor of two, you should use this:



  tbgl_Scale 2, 1, 1
 


  Just keep in mind that this affects not only the size of rendered objects, but even the operations with translation.


TBGL_Translate serves to translate or move the matrix. For example, if you want to draw two objects, one at the beginning of the coordinate system, and other moved by 5 in X, you should do:



  DrawMyObject()

  tbgl_Translate 5, 0, 0

  DrawMyObject()
 


  The last marix modifier is TBGL_Rotate. It allows you to rotate matrix by angle in degrees around axis defined by flag.
To rotate object by 10 degrees in X, 20 degrees in Y and 30 degrees in Z axis you will use this code:



  tbgl_Rotate 10, 1, 0, 0

  tbgl_Rotate 20, 0, 1, 0

  tbgl_Rotate 30, 0, 0, 1

  DrawMyObject()
 


  It's also very important to know, that the order of performed operations matters. It is not the same to do this:



  tbgl_Translate 5, 0, 0
  tbgl_Rotate 90, 0, 1, 0

  DrawMyObject()
 


... and this:





  tbgl_Rotate 90, 0, 1, 0
  tbgl_Translate 5, 0, 0

  DrawMyObject()
 


  The first one will move the object to position 5, 0, 0 and here the object will be rotated by 90 degrees around Y axis.

  The second one will first rotate the current matrix by 90 degrees and then translate the object.
Attention ! This means the object will be finally located at position 0, 0, -5. Why ? Because we rotated the matrix by 90 degrees counterclock wise and then translated by the vector 5, 0, 0.

  The last problem to solve is, how to isolate particular transformations for wished parts of code. The answer are TBGL_PushMatrix / TBGL_PopMatrix statements.

Look at this piece of code:



  tbgl_PushMatrix

    tbgl_Rotate 45, 0, 1, 0
    DrawMyObject()

  tbgl_PopMatrix

  tbgl_PushMatrix

    tbgl_Translate 5, 0, 0
    DrawMyObject()

  tbgl_PopMatrix
 


  Will we have the same "axis confusion" problem like in the last code ?
No ! Because we isolated two matrixes from each other.

First object will be just rotated by 45 around Y axis, and the second one will be just translated.

TBGL_Push / PopMatrix is very powerful tool for robot-like animations.
You can nest the TBGL_PushMatrix up to the 32 levels.

It helps you to design simple animation skeleton.
For example like this:



  tbgl_PushMatrix ' Main character matrix


    tbgl_PushMatrix ' First arm of character
      ... here you can perform translations relative to parent matrix and draw the first arm ...
    tbgl_PopMatrix


    tbgl_PushMatrix ' Second arm of character
      ... here you can perform translations relative to parent matrix and draw the second arm ...
    tbgl_PopMatrix

    ...

  tbgl_PopMatrix
 


  So that's all I can tell you about matrix handling in TBGL for this time. Hope you like it, don't forget to explore the bundled source codes below !


To test it just download this source code: Lesson 4 source code


PreviousHomeNext lesson


© Petr Schreiber, 2006