Results 1 to 6 of 6

Thread: TBGL, culling and occlusion culling

  1. #1
    thinBasic MVPs Michael Hartlef's Avatar
    Join Date
    Sep 2006
    Location
    Germany
    Age
    58
    Posts
    3,299
    Rep Power
    348

    TBGL, culling and occlusion culling

    That is great for you (and us) :-)

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

  2. #2
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,153
    Rep Power
    736
    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
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  3. #3
    thinBasic MVPs Michael Hartlef's Avatar
    Join Date
    Sep 2006
    Location
    Germany
    Age
    58
    Posts
    3,299
    Rep Power
    348
    Sorry, I ment objects that are not in the view. Is that setting effecting it too?

  4. #4
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,153
    Rep Power
    736
    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
    Last edited by Petr Schreiber; 23-04-2016 at 23:23.
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  5. #5
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,153
    Rep Power
    736
    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
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  6. #6
    thinBasic MVPs Michael Hartlef's Avatar
    Join Date
    Sep 2006
    Location
    Germany
    Age
    58
    Posts
    3,299
    Rep Power
    348
    Quote Originally Posted by Petr Schreiber View Post
    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.

Similar Threads

  1. New TBGL related article that r.o.c.k.s, on TBGL website!
    By Petr Schreiber in forum TBGL module by Petr Schreiber
    Replies: 6
    Last Post: 09-11-2007, 10:03

Members who have read this thread: 0

There are no members to list at the moment.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •