' -- Simple occlusion culling
Uses "TBGL", "Console"
Begin Const
%sScene = 1
%eCamera = 1, %eLight, %eBox
End Const
Function TBMain()
Double FrameRate
DWord hWnd = TBGL_CreateWindowEx("Move object via W,A,S,D - press ESC to quit", 640, 480, 32, %TBGL_WS_WINDOWED Or %TBGL_WS_CLOSEBOX)
TBGL_ShowWindow
TBGL_SceneCreate(%sScene)
TBGL_EntityCreateCamera(%sScene, %eCamera)
TBGL_EntitySetPos(%sScene, %eCamera, 0, 15, 15)
TBGL_EntitySetTargetPos(%sScene, %eCamera, 0, 0, 0)
TBGL_EntityCreateLight(%sScene, %eLight)
TBGL_EntitySetPos(%sScene, %eLight, 15, 10, 5)
TBGL_EntityCreateBox(%sScene, %eBox, 0, 1, 2, 3, 0, 255, 128, 0)
TBGL_EntitySetPos(%sScene, %eBox, 0, 0, 0)
TBGL_ResetKeyState()
Long flag
' -- Main loop
While TBGL_IsWindow(hWnd)
FrameRate = TBGL_GetFrameRate
TBGL_ClearFrame
TBGL_EntityTurn(%sScene, %eBox, 0, -45/FrameRate, 0)
TBGL_SceneRender(%sScene)
TBGL_DrawFrame
' -- ESCAPE key to exit application
If TBGL_GetWindowKeyState(hWnd, %VK_ESCAPE) Then Exit While
flag = IsEntityVisible(%sScene, %eBox, 1, 2, 3)
PrintL IIf$(flag, "Visible", "Off screen")
TBGL_EntitySetUse(%sScene, %eBox, flag) ' -- Hide if not needed to be rendered
If TBGL_GetWindowKeyState(hWnd, %VK_W) Then TBGL_EntityMove(%sScene, %eBox, 0, 0,-5/FrameRate)
If TBGL_GetWindowKeyState(hWnd, %VK_A) Then TBGL_EntityMove(%sScene, %eBox,-5/FrameRate, 0, 0)
If TBGL_GetWindowKeyState(hWnd, %VK_S) Then TBGL_EntityMove(%sScene, %eBox, 0, 0, 5/FrameRate)
If TBGL_GetWindowKeyState(hWnd, %VK_D) Then TBGL_EntityMove(%sScene, %eBox, 5/FrameRate, 0, 0)
Wend
TBGL_DestroyWindow
End Function
Function IsEntityVisible(scene As Long, entity As Long, width As Single, height As Single, length As Single) As Long
Dim cornerPoints(8) As TBGL_TVECTOR3D
Single x, y, z
' -- Safety margins
width *= 1.1
height *= 1.1
length *= 1.1
' -- Tracking point in each corner of the occlusion box
TBGL_EntityTrackPos(scene, entity, -width/2, -height/2, -length/2, cornerPoints(1).x, cornerPoints(1).y, cornerPoints(1).z)
TBGL_EntityTrackPos(scene, entity, width/2, -height/2, -length/2, cornerPoints(2).x, cornerPoints(2).y, cornerPoints(2).z)
TBGL_EntityTrackPos(scene, entity, -width/2, height/2, -length/2, cornerPoints(3).x, cornerPoints(3).y, cornerPoints(3).z)
TBGL_EntityTrackPos(scene, entity, width/2, height/2, -length/2, cornerPoints(4).x, cornerPoints(4).y, cornerPoints(4).z)
TBGL_EntityTrackPos(scene, entity, -width/2, -height/2, length/2, cornerPoints(5).x, cornerPoints(5).y, cornerPoints(5).z)
TBGL_EntityTrackPos(scene, entity, width/2, -height/2, length/2, cornerPoints(6).x, cornerPoints(6).y, cornerPoints(6).z)
TBGL_EntityTrackPos(scene, entity, -width/2, height/2, length/2, cornerPoints(7).x, cornerPoints(7).y, cornerPoints(7).z)
TBGL_EntityTrackPos(scene, entity, width/2, height/2, length/2, cornerPoints(8).x, cornerPoints(8).y, cornerPoints(8).z)
Long i
For i = 1 To 8
If TBGL_IsPointVisible(cornerPoints(i).x, cornerPoints(i).y, cornerPoints(i).z) Then
Return TRUE
End If
Next
Return Return FALSE
End Function
Bookmarks