TBGL
Lesson #3
Various types of polygons

This lesson will present you the most useful types of polygons


  In the previous tutorials we learnt how to define triangle to display.
But sometimes, we need polygon created with more then 3 vertices, sometimes
just to draw something like dots, lines ...

This tutorial will solve most of these questions. First I'd like to arrange
one thing. The number of tbgl_BeginPoly, tbglEndPoly tags should be minimal.

To create 2 triangles, most people could think of this :



  tbgl_BeginPoly %GL_TRIANGLES

    tbgl_Vertex -1, 0, 0
    tbgl_Vertex 1, 0, 0
    tbgl_Vertex 0, 1, 0

  tbgl_EndPoly

  tbgl_BeginPoly %GL_TRIANGLES

    tbgl_Vertex -2, 0,-1
    tbgl_Vertex 2, 0,-1
    tbgl_Vertex 0, 2,-1
  
  tbgl_EndPoly
 


  This is right code in terms of syntax and usability, but this style
is boring to write, and it is also slower on some cards.
If you are asking: "How else then ?", then my response is this code :



  tbgl_BeginPoly %GL_TRIANGLES
  
    tbgl_Vertex -1, 0, 0
    tbgl_Vertex 1, 0, 0
    tbgl_Vertex 0, 1, 0
  
    tbgl_Vertex -2, 0,-1
    tbgl_Vertex 2, 0,-1
    tbgl_Vertex 0, 2,-1
  
  tbgl_EndPoly

 


  Simple, isn't it ? We will save two function calls.
Now, when we are ready, let's discover other styles of polygon definition.

I'v choosen the way from simple to more complex "objects".

So lets start with points. Points are quite unique entity. They have garanted
size in pixels, which can be changed OUTSIDE tbgl_BeginPoly / tbgl_EndPoly using
the tbgl_PointSize function. What does it mean "garanted" ? It means that the
depth of the point in the space won't affect its size on display. This feature
is handy, when you are creating field of "stars". You will place points on
various places, but they will keep their size. I'v talked enough, here is sample :



  tbgl_PointSize 3
  
  tbgl_BeginPoly %GL_POINTS
  
    tbgl_Vertex 0, 0, -1
  
    tbgl_Vertex 1, 0, -10
  
    tbgl_Vertex -2,-2, -2
  
  tbgl_EndPoly
 


  This will display little squares (points ) with 3 pixel edges on different
places in space.


The next type of vertex-defined objects is line. There are various modes of
work with lines. The simpliest one is %GL_LINES. With each pair of vertexes
you define 1 line. The following code will produce 2 independent lines




  tbgl_BeginPoly %GL_LINES
  
    tbgl_Vertex 0, 0, -1
    tbgl_Vertex 0, 1, -1
  
    tbgl_Vertex 1, 0, -2
    tbgl_Vertex 1,-1, -2
  
  tbgl_EndPoly

 


  The lines, like the points, also maintains their width independently on its
distance from observer. You can specify the width of lines using
tbgl_LineWidth statement, with pixel size as parameter.

Other technique of drawing lines is %GL_LINE_STRIP. It will create only one
"strip" of lines, starting from first defined vertex. It can save coding
in some cases. To display something like "V" character, you would use 4 calls
of tbgl_Vertex using %GL_LINES style:





  tbgl_BeginPoly %GL_LINES
  
    tbgl_Vertex -1, 1, -5
    tbgl_Vertex 0, 0, -5
    
    tbgl_Vertex 0, 0, -5
    tbgl_Vertex 1, 1, -5
  
  tbgl_EndPoly

 


... but with %GL_LINE_STRIP you have to
define just 3 vertexes





  tbgl_BeginPoly %GL_LINE_STRIP
  
    tbgl_Vertex -1, 1, -5
    tbgl_Vertex 0, 0, -5
    tbgl_Vertex 1, 1, -5
  
  tbgl_EndPoly

 



  The last style of line is %GL_LINE_LOOP. It can be code lines saver too,
if you want to draw closed shapes. This is simple way to draw a square.




  tbgl_BeginPoly %GL_LINE_LOOP
  
    tbgl_Vertex -1, -1, -5
    tbgl_Vertex -1, 1, -5
    tbgl_Vertex 1, 1, -5
    tbgl_Vertex 1, -1, -5
  
  tbgl_EndPoly

 


  With %GL_LINE_STRIP you had to call tbgl_Vertex 5 times to do this, with
%GL_LINES even 8 times ! So thats the power of %GL_LINE_LOOP...


Both lines and points are nice a useful, but keep in mind that they are
( very surprisingly ! ) quite big performance eater. Of course, drawing
20 lines or points won't make your computer to render one frame 10 minutes,
but overusing of it can lead to some performance leaks on older computers.


The other styles are representing polygons. Like in case of lines, even
in defining there are some vertex use optimizations. There are predefined
equates for specifying 3 point polygons ( %GL_TRIANGLES ),
4 point ones ( %GL_QUADS ) and even "true" n-gons with no limits for the
number of vertexes ( %GL_POLYGON ).


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


It should produce this image


PreviousHomeNext lesson


© Petr Schreiber, 2006