| Lesson #8
Optimizing the scene
The knowledge collected from first seven tutorials would be useless, if you couldn't run your graphics at reasonable speed
Luckily you have few aces in your cardpack :). The first one is very high thinBASIC execution speed. And the others will be revealed by this tutorial !
I will begin with the simpliest things. As you know, less statements result in faster execution. And so it is in TBGL. I will start with easy example of drawing red triangle:
Is this code correct ? Syntactically yes, it will run OK. But the definition of the same color for each vertex is simply evitable. This code will do same work for you, and it will save execution of two statements.
TBGL / OpenGL works like a "state automat", until properties are not defined by other way, the last set value is valid. For this reason color defined before first vertex will be applied to second and third too.
Second very often overlooked issue is use of textures. When you design your 3D model you are experimenting with various texture sets and it can result in working, but not fast optimized code. Similary to the colors, it is smart to minimize the changing of actual texture, e.g. calls to tbgl_BindTexture. It's fine to hand defragment your code, to group parts of model definitions which use the same texture on isolated places to gain speed.
Other technique which can make the rendering faster is using tbgl_SetPrimitiveQuality in case of scenes composed from built-in TBGL primitives. According to the distance from viewer you can decrease objects quality.
This were simple, but usable hints to gain some percents of performance on drawing the model. But do you want real big jump towards higher framerate ?
You can also get some performance improvements by dividing your scene in some code parts, regions, which would be displayed according to observers postion. But this is sometimes hard to implement.
As you probably know, expression in fewer lines of code very often leads to speeder execution. So, what about performing the rendering of whole scene / model using one line of code ? Sounds strange to you ? Don't worry. The technique I'll explain here is called "display list". It is based on simple idea of kind of precaching. Using it, you will have to process the code of the model just once and then just call it.
You can precache up to 255 objects. The syntax to define display list object is very simple. You just have to determine the area of code which you want to make display list by statements tbgl_NewList / tbgl_EndList. Like on this example:
This definition will tell to your graphic card to "save" red colored triangle, isolated in it's own matrix under number 1 ( value of user defined constant %MY_TRIANGLE ). The number of object, parameter of tbgl_NewList, must be in range from 1 to 255.
If you try to assign some code to already defined display list, it will not erase the original one, but it will combine old code with new one. This can list in main program loop is not good idea, as it is memory consuming.
For this reason it is wise to define all necessary display list before starting to render them in the main program loop. But how to render them ? Easily ! Just call them using
That's the mentioned one line of code to draw whole model. As thinBASIC has not to render the model parsing the lines because it is saved in graphic card memory in special format, it is very, very fast !
The difference in speed when using display list can be really dramatic. You can get speed boost of factor of 3, even 4 ! During my last tests, I was able to render model created by one million and half textured and colored display list optimized polygons ! And not on NASA box, it was box with "just" 850 MHz processor and Radeon 9600 card. As the all work is translated from CPU to graphic card ( GPU ), you can use gained processor reserves for other computations. Looks like ideal solution. And it is, with one restriction - it doesn't apply, when OpenGL is not hardware accelerated on your box. How to determine TBGL / OpenGL is supported on PC, where script is running ? Explore the bundled scripts with examples of this technique !