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
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