PDA

View Full Version : help using TBGL_CallList



efly2020
30-09-2008, 21:44
I modified the TBGL_Demo5_SpaceFlight example to use TBGL_CallList
but the stars do not move. Obviously I don't understand how to use
TBGL_CallList but is there a way to use it with this demo so I might
understand how TBGL_CallList does work?

Thanks for any assistance.

Petr Schreiber
30-09-2008, 22:31
Hi,

you were so close!

First brief info, maybe I will just repeat what you know.

What does TBGL_NewList index / TBGL_EndList do?
It creates new static snapshot of geometric object. Every OpenGL or TBGL command between mentioned pair will be "cached" in graphic card driver.

What does NOT TBGL_NewList index / TBGL_EndList do?
It does not cache in any way other commands, like ThinBasic MSGBOX, FOR/NEXT ... for example.

What following code does


TBGL_NewList 1
FOR i = 1 to 3
TBGL_Translate 0, 0, 1
TBGL_Sphere 1
NEXT
TBGL_EndList


... this code is cached in form:


TBGL_Translate 0, 0, 1
TBGL_Sphere 1
TBGL_Translate 0, 0, 1
TBGL_Sphere 1
TBGL_Translate 0, 0, 1
TBGL_Sphere 1


... so just the raw generated GPU commands.

What was wrong with the code?
You cached the rendering code of "star":


TBGL_BeginPoly %GL_Lines ' Starts polygon definition based on 2 vertex lines
TBGL_Color rnd(0, 255),rnd(0, 255),rnd(0, 255) ' Sets RGB color
TBGL_Vertex Star(i).x,Star(i).y,Star(i).z ' Adds vertex
TBGL_Color 0,0,0
TBGL_Vertex Star(i).x,Star(i).y,Star(i).z-5
TBGL_EndPoly ' Ends polygon definition


... but as display list takes static "snapshot", it hardcached the Star(i).x,Star(i).y,Star(i).z as numeric literal, not as variables. So if at the moment Star(i).x,Star(i).y,Star(i).z was 1, 2, 3; it will render the stars at that location forever when using TBGL_CallList.

So I cannot use display list in this case?
Sure you can. To animate stars, you use


Star(i).z += 20/FrameRate


Original code was dynamic, so it took the value from array and rendered line at that position.
To do the same with DL, you have to cache just particles ( stars ) and transform it yourself.
As they move just in Z, you can let that X and Y cached and shift just in Z:


TBGL_ClearFrame ' Prepares clear frame
TBGL_Camera 0,0,5,0,0,0 ' Setups camera to look from 0,0,5 to 0,0,0

For i = 1 to %MaxStars
TBGL_PushMatrix
TBGL_Translate 0, 0, Star(i).z
TBGL_CallList i
TBGL_PopMatrix

Star(i).z += 20/FrameRate ' Stars fly at 20 units per second
If Star(i).z > 5 Then GenerateNewStar(i) ' If it is behind viewer, then reborn

Next

TBGL_DrawFrame ' Swaps the buffers - displays rendered image


I attach code for you

Are there any problems in this sample
This example has one little problem, because of which it is not deadly advantageous to use display lists.

Issue #1:
The cached geometry is tiny - basically one line, 2 vertices. Display lists grow in efficiency with growing complexity of geometry cached. On most cards this is still not such a problem and rendering will be faster or same anyway.

Issue #2:
Birth and death. As we recycle 300 stars, we let them fly, and once they are too far, we want to set them new color and new position. But ouch! This means recompiling the list.
This ends in some performance hit, as we need to delete old list and do it from the scratch.


Petr

Petr Schreiber
30-09-2008, 23:06
Hmm,

when I think of it, all stars:
- have the same dimension
- fade to black

That allows creating star template display list ( one for all ):


' -- Create star template
%listStar = 1
TBGL_NewList %listStar ' This will save a lot of of performance
TBGL_BeginPoly %GL_Lines ' Starts polygon definition based on 2 vertex lines
TBGL_Vertex 0, 0, 0 ' Adds vertex - notice color for it is undefined

TBGL_Color 0,0,0 ' Second vertex has black color
TBGL_Vertex 0, 0,-5
TBGL_EndPoly ' Ends polygon definition
TBGL_EndList


I attach 2 new versions for you, now even with that color flicker.

Standard version is just about 10% faster, entity version brings 70% improvement.

This version also removes the need for deleting and recreating the list. So just issue #1 - too small geometry batch persists. But nothing fatal :)


Petr