PDA

View Full Version : Scenes and Entities



Intio
08-12-2008, 22:04
Here is a listing of the "tilesforplatformer.tbasic" file from the 1.6 bonus pack:



'
' Simple grid rotation, v2
'

Uses "TBGL"

' -- Create and show window
Dim hWnd As Dword = TBGL_CreateWindowEx("2D tiles, randomly enabling and disabling - press ESC to quit", 640, 480, 32, %TBGL_WS_WINDOWED or %TBGL_WS_MAXIMIZEBOX or %TBGL_WS_CLOSEBOX)
TBGL_ShowWindow

' -- First we need to load textures
%texOne = 1
%texTwo = 2
tbgl_LoadTexture APP_SourcePath+"Textures\Praha_Kameni1.bmp", %texOne, %TBGL_TEX_MIPMAP
tbgl_LoadTexture APP_SourcePath+"Textures\Praha_Kameni2.bmp", %texTwo, %TBGL_TEX_MIPMAP

%lTile = 1
CreateTileList(%lTile)

' -- Create scene
%SCENE1 = 1
TBGL_SceneCreate(%SCENE1)

' -- Create basic entities
%eCamera = 1
%eGridStart = 40

' -- Create camera to look from 15, 15, 15 to 0, 0, 0
TBGL_EntityCreateCamera(%SCENE1, %eCamera)
TBGL_EntitySetPos(%SCENE1, %eCamera, 6, 6, 15)
TBGL_EntitySetTargetPos(%SCENE1, %eCamera, 6, 6, 0)


' -- Here we create grid
dim x, y as long
dim Tile as long = %eGridStart

for x = 1 to 10
for y = 1 to 10
TBGL_EntityCreatedlslot(%SCENE1, Tile, 0, %lTile) ' -- Tile is child of %eGridPivot, and rendered as defined in the display list
tbgl_EntitySetTexture(%SCENE1, Tile, rnd(%texOne, %texTwo)) ' -- Random texture
tbgl_EntitySetPos(%SCENE1, Tile, x, y, 0) ' -- Set position
Tile += 1 ' -- Increment tile ID

next
next
%eGridEnd = Tile - 1

dim FrameRate as double

' -- Maxx FPS possible
tbgl_UseVSync %TRUE

' -- Resets status of all keys
TBGL_GetAsyncKeyState(-1)

' -- Main loop
While TBGL_IsWindow(hWnd)
FrameRate = TBGL_GetFrameRate
TBGL_ClearFrame

' -- Random enabling, disabling tiles
if rnd > 0.5 then tbgl_EntitySetuse(%SCENE1, rnd(%eGridStart, %eGridEnd), rnd(%FALSE, %TRUE))

' -- Render scene
TBGL_SceneRender(%SCENE1)

TBGL_DrawFrame

' -- ESCAPE key to exit application
If TBGL_GetWindowKeyState(hWnd, %VK_ESCAPE) Then Exit While

Wend

TBGL_DestroyWindow

sub CreateTileList( listSlot as long )

tbgl_NewList listSlot
TBGL_BeginPoly %GL_QUADS ' Start polygon Building my quad based on 4 vertexes
TBGL_TexCoord2d 0,0 ' Set texture coordinate
TBGL_Vertex 0,0,0 ' Add Vertex
TBGL_TexCoord2d 1,0 ' Set texture coordinate
TBGL_Vertex 1,0,0 ' Add Vertex
TBGL_TexCoord2d 1,1 ' Set texture coordinate
TBGL_Vertex 1, 1,0 ' Add Vertex
TBGL_TexCoord2d 0,1 ' Set texture coordinate
TBGL_Vertex 0, 1,0 ' Add Vertex
TBGL_EndPoly ' Ends polygon definition tbgl_EndList
tbgl_EndList

end sub


Can someone tell me what the advantages of using the TBGL_SceneCreate command, and the various 'Entity' commands at the beginning of the program, are?

I know that there are various Entity examples within the SampleScripts folder (which I will look at if it turns out that they are efficient for what I might do), but I was wondering if they are there simply for ease of use - or do they give a genuine performance increase over what could be programmed manually? How significant is the advantage of using them compared to any potential overhead for calling them?

Also... eGridPivot?

Thanks for any enlightenment.

Petr Schreiber
08-12-2008, 23:02
Hi Intio,

thank you for interesting question - I will put following explanations to TBGL manual so other will know what is entity system good for, thanks! :)

Here are few advantages of entity system:
1) it allows you to specify multiple cameras ( and choose which one to use), check:


SampleScripts\TBGL\TBGL_0220_Samples\RedBlueGlasses\TBGL022_RedBlueGlasses.tbasic

to see how you can setup differently two cameras to be used for stereoscopic viewing

2) it allows attaching entity to entity - even camera or light to entity. No need to do matrix math on your own to get position of object after rotations and then set it to other entity. Thanks to magic of Charles Pegge, entity system uses assembly optimized matrix multiplication, very fast one.

3) pivots can serve as abstract parent for entities. Again, in that stereoscopic example imagine it as head to which you attach two eyes - cameras. By moving pivot you move also both cameras.

4) various utility functions allow to turn entites to face other entities, track points on entities and get them in global coordiantes, calculate angles and distances between them...

5) speed advantage: ThinBasic is very fast interpreted language, but as all transformations occur inside compiled TBGL module, TBGL_SceneRender will usually perform better for big amounts of objects rather than using script to render object by object using FOR/NEXT.

If you are good at assembly ( oxygen module ), you can do many of mentioned things "your way" without performance hits.
Still, if you prefer pure OpenGL coding, or lower level TBGL, nothing prevents you from doing so.

I hope I answered your questions, if not, let me know!


Petr

Intio
09-12-2008, 20:44
Thanks for the swift and comprehensive reply Petr!

;D