Results 1 to 6 of 6

Thread: Saving and reading binary geometry files with TBGL

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,153
    Rep Power
    736

    Lightbulb Saving and reading binary geometry files with TBGL

    Rene's wish is to save geometry in form of ThinBASIC TBGL script and then load it by interpreting. While this is one of the possible approaches, there is one way which will probably will be slightly slower, but much more space efficient. Each TBGL command (for now just a few) is encoded using binary signature (4 byte) + all parameters are in binary form.

    Take for example "TBGL_Vertex 1.100, 2.256, 3.456", this would take 32 bytes to save in script form. With binary form, it is packed to SizeOf(signature) + 3 * SizeOf(single) = 16 bytes.
    We saved 50% on size and reading binary value does not involve string-to-number conversion, which speeds up the load time too.

    I attach the unit file, fileformat_BG.tBasicU, + test script. The usage is as simple as:
    #INCLUDE "fileFormat_BG.tBasicU"
    
    Uses "TBGL"                      
    
    $BG_FILE = APP_SourcePath + "test.bg"
    
    Function TBMain()
      Local hWnd      As DWord
      Local FrameRate As Double
      
      ' -- Create and show window
      hWnd = TBGL_CreateWindowEx("Test of BG file format - press ESC to quit", 640, 480, 32, %TBGL_WS_WINDOWED Or %TBGL_WS_CLOSEBOX) 
      TBGL_ShowWindow 
    
      ' -- Define geometry to file
      BG_BeginSaveFile($BG_FILE)
      
        BG_BeginPoly(%GL_TRIANGLES)
          BG_Color(255,   0,   0)
          BG_Vertex(-1,-1, 0)
          
          BG_Color(  0, 255,   0)
          BG_Vertex( 1,-1, 0)
          
          BG_Color(  0,   0, 255)
          BG_Vertex( 0, 1, 0)
        BG_EndPoly
        
        BG_BeginPoly(%GL_QUADS)
          BG_Color(255, 255, 255)  
          
          BG_Vertex(-2, 2, -1)
          BG_Vertex( 2, 2, -1)      
          
          BG_Color(0, 0, 0)  
          BG_Vertex( 2,-2, -1)
          BG_Vertex(-2,-2, -1)
        BG_EndPoly
      
      BG_EndSaveFile()      
      
      ' -- Load it back
      %listHouse = BG_LoadFileToList($BG_FILE)
      
      ' -- Resets status of all keys 
      TBGL_ResetKeyState()
    
      ' -- Main loop
      While TBGL_IsWindow(hWnd) 
        FrameRate = TBGL_GetFrameRate
    
        TBGL_ClearFrame 
          TBGL_Camera(0, 0, 5, 0, 0, 0)
          
          TBGL_CallList(%listHouse)
    
        TBGL_DrawFrame 
    
        ' -- ESCAPE key to exit application
        If TBGL_GetWindowKeyState(hWnd, %VK_ESCAPE) Then Exit While 
    
      Wend 
    
      TBGL_DestroyWindow
    End Function
    
    Of course, more features can be added, this is just a concept for possible expansion.


    Petr
    Attached Images Attached Images
    Attached Files Attached Files
    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

Similar Threads

  1. clicking on Geometry Figures
    By zak in forum UI (User Interface)
    Replies: 25
    Last Post: 29-03-2011, 17:59
  2. Binary Files
    By Michael Clease in forum thinBasic General
    Replies: 3
    Last Post: 23-05-2007, 14:20

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
  •