' -- Camera on orbit
%visualizeCameraMovement = TRUE ' -- Set to false to see through eyes of camera
'---Load needed modules
Uses "TBGL"
Begin Const
' -- Scenes
%sScene = 1
' -- Entities
%eCameraPivotLR = 1
%eCameraPivotUD
%eCamera
%eLight
%eGridList
%eBox
%eVirtualCameraBody
End Const
Function TBMain()
Dim hWnd As Dword
'---Creates OpenGL window and returns handle
hWnd = TBGL_CreateWindowEx("Camera for editor, use arrows and pageup/pagedown - press ESC to quit", 640, 480, 32, %TBGL_WS_WINDOWED )
'---Shows TBGL window
TBGL_ShowWindow
' -- First we will create world as place to contain entities
tbgl_SceneCreate(%sScene)
' -- Camera pivot
TBGL_EntityCreatePivot( %sScene, %eCameraPivotLR)
TBGL_EntityCreatePivot( %sScene, %eCameraPivotUD, %eCameraPivotLR)
' -- Camera
TBGL_EntityCreateCamera( %sScene, %eCamera) ' -- Camera becomes parent of pivot
If %visualizeCameraMovement Then
' -- We set our eyes somewhere to look better on subject
TBGL_EntitySetPos ( %sScene, %eCamera, 0, 10,10 )
Else
' -- We shift camera slightly to not appear inside virtual camera
TBGL_EntitySetPos ( %sScene, %eCamera, 0, 0, -0.55 )
End If
' -- Light
TBGL_EntityCreateLight(%sScene, %eLight)
TBGL_EntitySetPos(%sScene, %eLight, 15, 10, 5)
TBGL_EntitySetColor(%sScene, %eLight, 255, 255, 255)
' -- Grid
TBGL_EntityCreateDLSlot( %sScene, %eGridList, 0, CreateGridAsDList() )
' -- Box
TBGL_EntityCreateBox( %sScene, %eBox, 0, 1, 1, 1 )
TBGL_EntityCreateBox( %sScene, %eVirtualCameraBody, %eCameraPivotUD, 0.1, 0.3, 1 )
TBGL_EntitySetColor( %sScene, %eVirtualCameraBody, 255, 128, 0 )
TBGL_EntitySetPos(%sScene, %eVirtualCameraBody, 0, 0, 5)
If %visualizeCameraMovement = FALSE Then
' -- Stick camera to virtual camera body
TBGL_EntitySetParent(%sScene, %eCamera, %eVirtualCameraBody)
End If
dim FrameRate as double
'---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
' -- Make camera look at box
TBGL_EntitySetTarget(%sScene, %eCamera, %eBox)
tbgl_SceneRender(%sScene)
TBGL_DrawFrame ' Swaps the buffers - displays rendered image
If TBGL_GetWindowKeyState( hWnd, %VK_UP) Then TBGL_EntityPush(%sScene, %eVirtualCameraBody, 0, 0, -5/FrameRate) ' -- Move camera closer
If TBGL_GetWindowKeyState( hWnd, %VK_DOWN) Then TBGL_EntityPush(%sScene, %eVirtualCameraBody, 0, 0, 5/FrameRate)
If TBGL_GetWindowKeyState( hWnd, %VK_LEFT) Then TBGL_EntityTurn(%sScene, %eCameraPivotLR, 0, -90/FrameRate, 0) ' -- Rotate Left-Right pivot
If TBGL_GetWindowKeyState( hWnd, %VK_RIGHT) Then TBGL_EntityTurn(%sScene, %eCameraPivotLR, 0, 90/FrameRate, 0)
If TBGL_GetWindowKeyState( hWnd, %VK_PGUP) Then TBGL_EntityTurn(%sScene, %eCameraPivotUD, -90/FrameRate, 0, 0) ' -- Rotate Up-Down pivot
If TBGL_GetWindowKeyState( hWnd, %VK_PGDN) Then TBGL_EntityTurn(%sScene, %eCameraPivotUD, 90/FrameRate, 0, 0)
if TBGL_GetwindowKeyState( hWnd, %VK_ESCAPE) then EXIT WHILE
WEND
'---Closes OpenGL window
TBGL_DestroyWindow
End Function
Function CreateGridAsDList() As Long ' Returns to which display list we save
local i, j as long
TBGL_NewList 1
TBGL_UseLighting FALSE
'---Let's build a grid
TBGL_BeginPoly %GL_LINES ' Starts polygon definition based on 2 vertex lines
TBGL_Color 0,255,0 ' Defines color
For i = -10 To 10
For j = -10 To 10
TBGL_Vertex -10, 0, j ' Adds vertex
TBGL_Vertex 10, 0, j ' Adds vertex
TBGL_Vertex i, 0, -10 ' Adds vertex
TBGL_Vertex i, 0, 10 ' Adds vertex
Next
Next
TBGL_EndPoly
TBGL_UseLighting TRUE
tbgl_EndList
function = 1
End Function
I hope it helps! Must run now, bye bye!
Bookmarks