PDA

View Full Version : TBGL, culling and occlusion culling



Michael Hartlef
23-04-2016, 11:37
That is great for you (and us) :-)

Not sure if it fits here. Does TBGL do automatic render culling?

Petr Schreiber
23-04-2016, 16:51
Hi Mike,

thanks :) Culling is not enabled in TBGL, as in OpenGL, by default, so not triangles are discarded.
When needed, you can use thinBasic_gl.inc and enable it via glEnable(%GL_CULL_FACE), glCullFace(%GL_BACK).


Petr

Michael Hartlef
23-04-2016, 19:47
Sorry, I ment objects that are not in the view. Is that setting effecting it too?

Petr Schreiber
23-04-2016, 23:18
This is called occlusion culling and one should implement this according to needs of the application.

It can be achieved with TBGL by tracking for example corner points of bounding box of an object, and querying their visibility via TBGL_IsPointVisible, TBGL_IsPointBehindView.
Another approach is to render each object in scene with different color and then search for that color in frame. The possibilities are infinite, but I did not find any to fit all scenarios, so TBGL does not do this by default.

As we are diverging from m15, I am moving the posts to new thread (as it is interesting, just not related to m15).


Petr

Petr Schreiber
24-04-2016, 00:14
Here little example of occlusion culling:


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


Petr

Michael Hartlef
24-04-2016, 00:20
As we are diverging from m15, I am moving the posts to new thread (as it is interesting, just not related to m15).


Petr

Sorry, I will try to avoid this in the future.