PDA

View Full Version : tbgl cityfog project



christianssen
09-03-2010, 10:57
(following my works from here: http://community.thinbasic.com/index.php?topic=3222.15)

hi all.

after making some little progress with tbgl my next steps. how I can make multiple tbgl_box with tbgl entity? I desactivated my code lines to see what's not perfect.
perhaps petr, frank, michael or anybody else can check and help to solve my problem.


'-denis tbgl learning edition ;)

'---Load needed modules
Uses "TBGL"

Begin Const
' -- Our world will have two entities
%ENT_CAMERA = 1
%ENT_GRIDLIST
%ENT_CreateCity
%ENT_CreateBlockCity
%ENT_BLOX
End Const

' -- Display lists
Begin Const
%LST_GRID = 1
%LST_BOX
%LST_CITY
%LST_BLOCKCITY
%LST_BLOX
End Const


Dim hWnd As Dword

'---Creates OpenGL window and returns handle
hWnd = TBGL_CreateWindowEx("CityFog: Moving in 3D, City entity boxed 2 - press ESC to quit", 720, 620, 32, %TBGL_WS_WINDOWED Or %TBGL_WS_CLOSEBOX)
'---Shows TBGL window
TBGL_ShowWindow

TBGL_LoadTexture APP_SourcePath+"stevenson2.bmp", 1, %TBGL_TEX_MIPMAP
'=============================================================================
'= The most imortant 3 lines of code in whole program :) :
'=============================================================================
TBGL_SetupFog 0,0,128,128,5 ' Sets attributes of fog: R, G, B, Alpha, and density in percents
TBGL_BackColor 0,0,128 ' It's good idea to use same color for fog and background
TBGL_UseFog %TRUE ' "Turn on" the fog

' -- First we will create world as place to contain entities
%MY_WORLD = 1
tbgl_SceneCreate(%MY_WORLD)

' .. Camera
tbgl_EntityCreateCamera( %MY_WORLD, %ENT_CAMERA)
TBGL_EntitySetPos ( %MY_WORLD, %ENT_CAMERA, 4.0, 24.0, -30.0 )

' .. Grid
tbgl_EntityCreateDLSlot( %MY_WORLD, %ENT_GRIDLIST, 0, CreateGridAsDList() )

' CreateCityAsDList()
' .. City
' TBGL_EntityCreateDLSlot( %MY_WORLD, %ENT_CreateCity, 0, %LST_CITY)
' TBGL_EntitySetPos ( %MY_WORLD, %ENT_CreateCity, 0.0, -0.25, 0.0 )

' 1)--------------------------------------new list for BlockCity, ok :)
CreateBlockCityAsDList()
' .. BlockCity
TBGL_EntityCreateDLSlot( %MY_WORLD, %ENT_CreateBlockCity, 0, %LST_BLOCKCITY)
TBGL_EntitySetPos ( %MY_WORLD, %ENT_CreateBlockCity, 0.0, -0.25, 0.0 )
'---------------------------------------new list for BlockCity

Dim i As Long
'~~~~~~~~~~~~~~~~~~~~~~~~~~~ + my question + ~~~~~~~~~~~~~~~~~~~~~~~~~~~
' 2)---------------------------------------how build new List for BLOX with i = 20 ?
' For i = -20 To 20 Step 4
' TBGL_EntityCreateBox( %MY_WORLD, %ENT_BLOX+i, 0, %LST_BLOX+i)
' TBGL_EntitySetPos ( %MY_WORLD, %ENT_BLOX+i, 2.0, 0.25, -25.0 )
' Next
'----------------------------------------------new List for BLOX with i = 20 ?
'~~~~~~~~~~~~~~~~~~~~~~~~~~~ + my question + ~~~~~~~~~~~~~~~~~~~~~~~~~~~


dim FrameRate as double
dim Sheeps as long

'---Resets key status before checking
TBGL_ResetKeyState()

'---Main script loop
WHILE TBGL_IsWindow(hWnd)

'---Script will run on different PCs so we must assure
'---constant speed of movement by scaling movements relative to frame rate
FrameRate = TBGL_getFrameRate

'---Prepares clear frame
TBGL_ClearFrame

tbgl_SceneRender(%MY_WORLD)


TBGL_DrawFrame ' Swaps the buffers - displays rendered image

if TBGL_GetwindowKeyState( hWnd, %VK_UP) then tbgl_EntityPush(%MY_WORLD, %ENT_CAMERA, 0, 0, 5/FrameRate) ' 5 meters/second
if TBGL_GetwindowKeyState( hWnd, %VK_DOWN) then tbgl_EntityPush(%MY_WORLD, %ENT_CAMERA, 0, 0, -5/FrameRate)

If TBGL_GetWindowKeyState( hWnd, %VK_UP) Then TBGL_EntityPush(%MY_WORLD, %ENT_CREATECITY, 0, 0, 5/FrameRate) ' 5 meters/second
If TBGL_GetWindowKeyState( hWnd, %VK_DOWN) Then TBGL_EntityPush(%MY_WORLD, %ENT_CREATECITY, 0, 0, -5/FrameRate)

If TBGL_GetWindowKeyState( hWnd, %VK_UP) Then TBGL_EntityPush(%MY_WORLD, %ENT_BLOX, 0, 0, 5/FrameRate) ' 5 meters/second
If TBGL_GetWindowKeyState( hWnd, %VK_DOWN) Then TBGL_EntityPush(%MY_WORLD, %ENT_BLOX, 0, 0, -5/FrameRate)

if TBGL_GetwindowKeyState( hWnd, %VK_PGUP) then tbgl_EntityPush(%MY_WORLD, %ENT_CAMERA, 0, 5/FrameRate, 0 )
if TBGL_GetwindowKeyState( hWnd, %VK_PGDN) then tbgl_EntityPush(%MY_WORLD, %ENT_CAMERA, 0, -5/FrameRate, 0 )

if TBGL_GetwindowKeyState( hWnd, %VK_LEFT) then tbgl_EntityTurn(%MY_WORLD, %ENT_CAMERA, 0, 60/FrameRate, 0)
if TBGL_GetwindowKeyState( hWnd, %VK_RIGHT) then tbgl_EntityTurn(%MY_WORLD, %ENT_CAMERA, 0, -60/FrameRate, 0) ' 60°/second

if TBGL_GetwindowKeyState( hWnd, %VK_ESCAPE) then EXIT WHILE

WEND

'---Closes OpenGL window
TBGL_DestroyWindow

Function CreateGridAsDList() As Long ' Returns to which display list we save
local i, j as long

TBGL_NewList %LST_GRID
'---Let's build a grid
TBGL_BeginPoly %GL_LINES ' Starts polygon definition based on 2 vertex lines
TBGL_Color 0,255,255 ' Defines color

For i = -20 To 20
For j = -20 To 20
TBGL_Vertex -20, 0, j ' Adds vertex
TBGL_Vertex 20, 0, j ' Adds vertex

TBGL_Vertex i, 0, -20 ' Adds vertex
TBGL_Vertex i, 0, 20 ' Adds vertex

Next
Next

TBGL_EndPoly
tbgl_EndList

function = 1
End Function

' create city: --------------------------------------------------------------------------------
Function CreateCityAsDList() As Long ' Returns to which display list we save

Local i, j, height As Long

TBGL_NewList %LST_CITY
TBGL_UseTexturing %TRUE
TBGL_BindTexture 1

For i = -40 To 40 Step 8
For j = -30 To 30 Step 6
height = Rnd(6, 24)

TBGL_BeginPoly %GL_QUADS
TBGL_Color Rnd(128,255),Rnd(128,255),Rnd(128,255)

TBGL_TexCoord2D 0,0
TBGL_Vertex -2.5+i,0,-2.5+j

TBGL_TexCoord2D 0,1
TBGL_Vertex 2.5+i,0,-2.5+j

TBGL_TexCoord2D 1,1
TBGL_Vertex 2.5+i,height,-2.5+j

TBGL_TexCoord2D 1,0
TBGL_Vertex -2.5+i,height,-2.5+j

TBGL_TexCoord2D 0,0
TBGL_Vertex -2.5+i,0,2.5+j

TBGL_TexCoord2D 0,1
TBGL_Vertex 2.5+i,0,2.5+j

TBGL_TexCoord2D 1,1
TBGL_Vertex 2.5+i,height,2.5+j

TBGL_TexCoord2D 1,0
TBGL_Vertex -2.5+i,height,2.5+j

TBGL_TexCoord2D 0,0
TBGL_Vertex 2.5+i,0,-2.5+j

TBGL_TexCoord2D 0,1
TBGL_Vertex 2.5+i,0,2.5+j

TBGL_TexCoord2D 1,1
TBGL_Vertex 2.5+i,height,2.5+j

TBGL_TexCoord2D 1,0
TBGL_Vertex 2.5+i,height,-2.5+j

TBGL_TexCoord2D 0,0
TBGL_Vertex -2.5+i,0,-2.5+j

TBGL_TexCoord2D 0,1
TBGL_Vertex -2.5+i,0,2.5+j

TBGL_TexCoord2D 1,1
TBGL_Vertex -2.5+i,height,2.5+j

TBGL_TexCoord2D 1,0
TBGL_Vertex -2.5+i,height,-2.5+j

TBGL_TexCoord2D 0,0
TBGL_Vertex -2.5+i,height,-2.5+j

TBGL_TexCoord2D 0,1
TBGL_Vertex -2.5+i,height,2.5+j

TBGL_TexCoord2D 1,1
TBGL_Vertex 2.5+i,height,2.5+j

TBGL_TexCoord2D 1,0
TBGL_Vertex 2.5+i,height,-2.5+j

TBGL_EndPoly

Next
Next

TBGL_EndList

TBGL_UseTexturing %FALSE
Function = 1

End Function

' Create new city with tbgl_box ---------------------------------------------------
Function CreateBlockCityAsDList() As Long ' Returns to which display list we save

Local i, j, height As Long

TBGL_NewList %LST_BLOCKCITY
TBGL_UseTexturing %TRUE
TBGL_BindTexture 1
TBGL_Color Rnd(128,255),Rnd(128,255),Rnd(128,255)

For i = -40 To 40 Step 8
TBGL_BOX 1+i,2+i,1+i
For j = -30 To 30 Step 6
height = Rnd(6, 24)
TBGL_Box 1+j,2+j,1+j


Next
Next

TBGL_EndList

TBGL_UseTexturing %FALSE
Function = 1

End Function


many thanks for help in advance, bye, denis

'------------------------------------------------

last post from link above from petr:

Frank,

thanks for providing the help.

Denis - there is a way to simplify the code by using TBGL_Box primitive, instead of complex TBGL_BeginPoly/TBGL_EndPoly.


Petr

Michael Clease
09-03-2010, 16:11
Dim i As Long
'~~~~~~~~~~~~~~~~~~~~~~~~~~~ + my question + ~~~~~~~~~~~~~~~~~~~~~~~~~~~
' 2)---------------------------------------how build new List for BLOX with i = 20 ?
For i = -20 To 20 Step 4
TBGL_EntityCreateBox( %MY_WORLD, %ENT_BLOX+20+i, 0, %LST_BLOX+20+i)
TBGL_EntitySetPos ( %MY_WORLD, %ENT_BLOX+20+i, 2.0, 0.25, -25.0 )
Next
'----------------------------------------------new List for BLOX with i = 20 ?
'~~~~~~~~~~~~~~~~~~~~~~~~~~~ + my question + ~~~~~~~~~~~~~~~~~~~~~~~~~~~


Is this the question(plus my fix) because I dont understand what you are enquiring about the only thing I would say is displays can not have a number less than 1, so ive offset the values.

Petr Schreiber
09-03-2010, 17:52
Hi Denis,

entities cannot have negative or zero entitiyID, which Michael nicely corrected already.

I would also recommend checking out help file for TBGL_EntityCreateBox and the meaning of the parameters. You just enter x size and others you leave zero, which will result in 1D box :)

You also place all boxes on the same place, which is little bit redundnant.

The rest seems ok!


Petr

christianssen
10-03-2010, 11:03
well, thats always important to look at the tbgl help files! Have forgotten.
thank you michael, petr for good advice.

How I can change camera in y-position for my scene with a slow shaking ? I think this
may be possible ? but doesn't work. better to use tbgl_EntityPush or Turn ? don't think so.


' .. Camera
tbgl_EntityCreateCamera( %MY_WORLD, %ENT_CAMERA)
TBGL_EntitySetPos ( %MY_WORLD, %ENT_CAMERA, 4.0, 24.0+Sin(GetTickCount/1000/SlowMotion)/10,-30.0 )
'moving very slow camera for Camera-Y pos: with ? +sin(gettickcount/1000/SlowMotion)/10


the scene shouldn't be calm when you start this app.
new in my scene a cross, new light (distance) and a labyrinth only with boxes.

bye, denis

Petr Schreiber
10-03-2010, 18:24
Hi Denis,

you had it almost right, the problem is you specify the SIN... position only after creation, but not in the main program loop.
Put the line:



TBGL_EntitySetPos ( %MY_WORLD, %ENT_CAMERA, 4.0, 24.0+Sin(GetTickCount/1000/SlowMotion)/10,-30.0 )



before TBGL_SceneRender, you will see it will work ok.


Petr

christianssen
11-03-2010, 10:23
thanks petr for help, the slow shaking effect for camera works well now :)

sorry for one more question. It's just how to learn a new mother language and I am in the first class :lol:

How to build fonts (lines 47-50) with TBGL_PRINTFONT at right place ? See my example at lines 105-112. I wanted place below my cross becaue I thought it must be lightened, but doesn't work. same thing before tbgl_sceneRender. cannot seen anything. perhaps somebody can help where to place this TBGL_PRINTFONT.

thanks, denis

Petr Schreiber
11-03-2010, 11:00
Hi Denis,

I corrected few problems in your code:

Camera could not "walk"
FPS statistics were not working ok
Cross size and text position is now resolution independent



Petr

christianssen
11-03-2010, 11:35
petr, many thanks for fast reply, wow! :)

a) the Tbgl_protected area I like and seems a good idea!
b) TBGL_RenderMatrix2D you have used for certain area for displaying text and framerates?

c) problem: the camera shakes, ok. but PAGEUP, PAGEDOWN for moving camera up/down for bigger steps doesn't work anymore ? camera has difficults to understand if I use keys for up/down pushs because it's perhaps better to make "if camera > 1 then"... statements?

I am very glad to see progress and tbgl knowledge for my example, denis

Petr Schreiber
11-03-2010, 11:50
Hi Denis,

b)-> Yes. As the crosshair and text is 2D stuff, I set 2D rendering for that part
c)-> PgUp, PgDown cannot work, as the shake simulation is done using SetPos, which resets the Y coordinate.

You can replace:


TBGL_EntitySetPos(%MY_WORLD, %ENT_CAMERA, x, 24.0+Sin(GetTickCount/1000/SlowMotion)/10, z )


with something like:


TBGL_EntityPush(%MY_WORLD, %ENT_CAMERA, 0, Sin(GetTickCount/1000/SlowMotion)/1000, 0 )


Then PgUp/Down will work nicely.


Petr

christianssen
12-03-2010, 09:57
thanks for hints, help to petr and gentle words for motivation from frank too (other board). good to know this forum is alive! :) there are a lot of big things to learn with tbgl I see. few lines of code, big and imposants result, that's making fun! 8)

it's worth to learn tbgl language. I like also the math. board with python openGL examples!
thanks to all, denis