TBGL
Lesson #2
Colors

In this lesson you will learn how to bind color to vertexes and how to change background color


  It's good to have read lesson 1, because this one is based on its code, and introduces just few modifications.
The following code samples are just parts of all program, which can be downloaded from the end of the page

  If you are disappointed by the just black and white output of the lesson 1, here you will see splash of colors.

  The black background of the scene could be sometimes inconvenient. What about making it ... dark blue ? It's quite straightforward.
Just use this:



  tbgl_BackColor 0, 0, 128
 


  Using this function with color coded in RGB as parameter will cause change of the default color of the background.

Well, TBGL module allows you to assign unique color to the each vertex too. For this you can use tbgl_Color function. It takes 3 parameters in range 0 - 255. In other words, just RGB again.



  tbgl_BeginPoly %GL_TRIANGLES

    tbgl_Color 255, 0, 0
    tbgl_Vertex -1, 0, 0

    tbgl_Color 0, 255, 0
    tbgl_Vertex 1, 0, 0

    tbgl_Color 0, 0, 255
    tbgl_Vertex 0, 1, 0

  tbgl_EndPoly
 


  This simple code will produce triangle with red, green and blue vertices. The colors will melt each other, so it will look really nice.
But to add some juice to the scene, let's make it more dynamic ! What about pulsing colors?



  tbgl_BeginPoly %GL_TRIANGLES

    tbgl_Color 128+SIN(TIMER)*127, 0, 0
    tbgl_Vertex -1, 0, 0

    tbgl_Color 0, 128+SIN(TIMER+1)*127, 0
    tbgl_Vertex 1, 0, 0

    tbgl_Color 0, 0, 128+SIN(TIMER+2)*127
    tbgl_Vertex 0, 1, 0

  tbgl_EndPoly
 


  SIN function is well known periodical function, which can return value from -1 to 1.
TIMER function returns numbers of seconds, which elapsed since midnight, so it changes.
Now the triangle vertexes will almost independently "glow".


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


It should produce this image


PreviousHomeNext lesson


© Petr Schreiber, 2006