Results 1 to 2 of 2

Thread: glDrawArrays example + GBuffers example

Share/Bookmark
  1. #1

    glDrawArrays example + GBuffers example

    1- glDrawArrays : to draw simple flat shark data, the purpose of Z = RND(0,1) in line 73 is to draw two parallel sharks. the shark is too tall so i have used gluPerspective(60.0, 800/600, 1.0, 100.0) to see it all
    i have added data to the code because it is not too big
    Uses "tbgl", "MATH"
        
      #INCLUDE "%app_includepath%\thinbasic_gl.inc"
      #INCLUDE "%app_includepath%\thinbasic_glu.inc"
      
      Type Point3D
      x As Single
      y As Single
      z As Single 
      red As Single
      green As Single
      blue As Single
      End Type 
       
      Dim hwnd As DWord  
      Global Nb As DWord = 10053 
      
      Global Vertex(Nb) As Point3D  
      'msgbox 0, SizeOf(Point3D))
          
      hwnd = TBGL_CreateWindowEx("glDrawArrays example - esc to exit", 600, 600, 32, 0)
      TBGL_ShowWindow
      TBGL_ResetKeyState()
      'TBGL_SetDrawDistance 550
      
      glPointSize( 2.0 ) ' just to show the curve in bold
      
      FillArray ' fill Vertex(...) with position data and color data
      
      While TBGL_IsWindow(hwnd)
         TBGL_ClearFrame
        
        display
        TBGL_DrawFrame
        If TBGL_GetWindowKeyState( hwnd, %VK_ESCAPE) Then Exit While
      Wend
        
      TBGL_DestroyWindow
    
    '=================================================================================
    
    '--------------------------------------------------------
    '--------------------------------------------------------   
    Sub display()
    
    glMatrixMode(%GL_PROJECTION)
    glLoadIdentity()
    gluPerspective(60.0, 800/600, 1.0, 100.0)
    glMatrixMode(%GL_MODELVIEW)
    glTranslatef(0, 0, -40)
    glShadeModel(%GL_SMOOTH) 
    glEnable(%GL_DEPTH_TEST)
    gluLookAt( 0, 1, 1,
               0,  1, 0,
                0, 1,  0 ) 
        glclear(%gl_color_buffer_bit)
          
        TBGL_Rotate GetTickCount/30,0,1,0
         
     'glRotatef(1, 0, 1, 0)' -- 0,1,0 rotate around Y
          
     glClear(%GL_COLOR_BUFFER_BIT Or %GL_DEPTH_BUFFER_BIT)
     glClearColor(1, 1, 1, 1)
          'glRotatef_(1, 0, 1, 0)
     glEnableClientState(%GL_VERTEX_ARRAY )
     glEnableClientState(%GL_COLOR_ARRAY)
     glVertexPointer(3, %GL_FLOAT,SizeOf(Point3D),VarPtr(Vertex(1).x))
     glColorPointer(3, %GL_FLOAT, SizeOf(Point3D), VarPtr(Vertex(1).red))
          
     'glDrawArrays(%GL_LINE_STRIP, 1, 10053)
     glDrawArrays(%GL_POINTS, 1, 10053 )
          
     glDisableClientState(%GL_COLOR_ARRAY)
     glDisableClientState(%GL_VERTEX_ARRAY)  
      '**************************************************
          
      
      End Sub
      
      Sub FillArray()
      DWord n = 1 : Double t = 0
      Dim x, y, z As Single      
                    
            While t <= 2 * Pi
              If n>10053 Then Exit While
              
              x = -18/5 *Sin(3/4-30 *t)-7/2 *Sin(2/3-20 *t)-129/4 *Sin(6/7-6 *t)-285/11 *Sin(1/15-5 *t)+2749/3 *Sin(t+11/6)+556/3 *Sin(2 *t+8/3)+625/12 *Sin(3 *t+6/5)+318/5 *Sin(4 *t+7/2)+257/6 *Sin(7 *t+20/7)+35 *Sin(8 *t+3/5)+118/7 *Sin(9 *t+3/5)+48/5 *Sin(10 *t+1/3)+96/5 *Sin(11 *t+5/4)+77/6 *Sin(12 *t+9/2)+91/4 *Sin(13 *t+7/3)+19/3 *Sin(14 *t+8/5)+56/11 *Sin(15 *t+9/5)+55/7 *Sin(16 *t+13/3)+37/9 *Sin(17 *t+13/5)+13/4 *Sin(18 *t+1/4)+27/5 *Sin(19 *t+7/3)+25/6 *Sin(21 *t+8/5)+5/6 *Sin(22 *t+7/3)+49/5 *Sin(23 *t+12/5)+11/4 *Sin(24 *t+5/3)+18/5 *Sin(25 *t+3)+23/4 *Sin(26 *t+5/2)+8/3 *Sin(27 *t+3/4)+5/3 *Sin(28 *t+35/17)+11/4 *Sin(29 *t+2/3)
              y = -7/5 *Sin(3/4-30 *t)-8/7 *Sin(4/3-27 *t)-Sin(2/3-26 *t)-2/3 *Sin(1/3-25 *t)-15/4 *Sin(3/4-23 *t)-83/7 *Sin(4/3-16 *t)-99/7 *Sin(5/4-13 *t)-19/3 *Sin(6/5-12 *t)-32 *Sin(8/5-9 *t)-298/9 *Sin(2/3-8 *t)-129/5 *Sin(1/3-7 *t)-76/3 *Sin(6/5-6 *t)+11/4 *Sin(29 *t)+873/4 *Sin(t+17/5)+364/11 *Sin(2 *t+26/9)+268/5 *Sin(3 *t+4/5)+167/3 *Sin(4 *t+25/6)+208/3 *Sin(5 *t+21/5)+31/4 *Sin(10 *t+5/4)+21 *Sin(11 *t+11/5)+27/5 *Sin(14 *t+25/6)+81/10 *Sin(15 *t+31/8)+10/3 *Sin(17 *t+9/2)+3/2 *Sin(18 *t+2)+91/9 *Sin(19 *t+39/10)+3/2 *Sin(20 *t+7/5)+25/8 *Sin(21 *t+1)+4 *Sin(22 *t+5/4)+3 *Sin(24 *t+13/3)+Sin(28 *t+26/9)
              z = Rnd(0,1)
              'Vertex(n).red = Rnd(0,1) : Vertex(n).green = Rnd(0,1) : Vertex(n).blue = Rnd(0,1)
              If z = 1 Then
              Vertex(n).red = 1 :Vertex(n).green = 0 :Vertex(n).blue = 0
              Else  
              Vertex(n).red = 0 :Vertex(n).green = 1 :Vertex(n).blue = 0.3
              End If
              
              Vertex(n).x = x*0.02
              Vertex(n).y = y*0.02 
              Vertex(n).z = z*2
            
              
              n + 1 : t + 0.001
            
            Wend
                    
      End Sub
    
    Click image for larger version. 

Name:	shark.PNG 
Views:	25 
Size:	7.0 KB 
ID:	9369

    2- Gbuffers method to draw the shark in two parallel sharks, the shark is too tall and parts of it is not displayed so i have find this TBGL function: TBGL_SetDrawDistance 250 , i set it to 250 meters, so we can see the shark in full. you can see the GBuffers example are much shorter if we delete the commented lines
    Uses "TBGL"
    Uses "math"
     
    Function TBMain()
      Local hWnd      As DWord
      Local FrameRate As Double
       
      ' -- Create and show window
      hWnd = TBGL_CreateWindowEx("Plotting 3D Data in an array using GBuffers - press ESC to quit", 600, 600, 32, %TBGL_WS_WINDOWED Or %TBGL_WS_CLOSEBOX) 
      TBGL_ShowWindow  
       
      TBGL_BackColor 255,255,255 
      TBGL_SetDrawDistance 250
     
      ' -- Create 3D points buffer
      Dim  gbPoints As DWord = TBGL_GBufferCreate(%TBGL_POINTS, %TBGL_3D)
      'Dim  gbPoints As DWord = TBGL_GBufferCreate(%TBGL_LINES, %TBGL_3D)
      
      Global Nb As DWord = 10053 '30159 '62831
      
      ' -- Define data for it
      Global VertexA(Nb) As TBGL_TVECTOR3F
      Global ColorA(Nb)  As TBGL_TRGB
      
      FillArrays ' call the sub to fill VertexA , ColorA  arrays
     
      ' -- Create buffer dynamically linked to the arrays above
      TBGL_GBufferDefineFromArray(gbPoints, %TBGL_DYNAMIC, CountOf(VertexA), VertexA(1), ColorA(1))
      
      ' -- Resets status of all keys 
      TBGL_ResetKeyState()
      TBGL_PointSize 2
      
      ' -- Main loop
      While TBGL_IsWindow(hWnd)
      'init 
        FrameRate = TBGL_GetFrameRate
        
        TBGL_ClearFrame
          TBGL_Camera(0, 0, 150, 0, 0, 0)
           
          ' -- Turn triangle
          TBGL_Rotate GetTickCount/50, 0, 1, 0
          'TBGL_Scale 0.6, 0.6, 0.6
                                         
          ' -- Render it                              
          TBGL_GBufferRender(gbPoints)   
            
        TBGL_DrawFrame
     
        ' -- ESCAPE key to exit application
        If TBGL_GetWindowKeyState(hWnd, %VK_ESCAPE) Then Exit While
     
      Wend        
      ' -- Destroying the buffer is not necessary,
      ' -- the garbage collector will take care of it
     
      ' -- Destroy window
      TBGL_DestroyWindow
    End Function  
    
    Sub FillArrays()
    
    
    Dim x, y, z, t As Single
    
    Dim N As DWord = 1
    
    While t <= 2*Pi
              
              x = -18/5 *Sin(3/4-30 *t)-7/2 *Sin(2/3-20 *t)-129/4 *Sin(6/7-6 *t)-285/11 *Sin(1/15-5 *t)+2749/3 *Sin(t+11/6)+556/3 *Sin(2 *t+8/3)+625/12 *Sin(3 *t+6/5)+318/5 *Sin(4 *t+7/2)+257/6 *Sin(7 *t+20/7)+35 *Sin(8 *t+3/5)+118/7 *Sin(9 *t+3/5)+48/5 *Sin(10 *t+1/3)+96/5 *Sin(11 *t+5/4)+77/6 *Sin(12 *t+9/2)+91/4 *Sin(13 *t+7/3)+19/3 *Sin(14 *t+8/5)+56/11 *Sin(15 *t+9/5)+55/7 *Sin(16 *t+13/3)+37/9 *Sin(17 *t+13/5)+13/4 *Sin(18 *t+1/4)+27/5 *Sin(19 *t+7/3)+25/6 *Sin(21 *t+8/5)+5/6 *Sin(22 *t+7/3)+49/5 *Sin(23 *t+12/5)+11/4 *Sin(24 *t+5/3)+18/5 *Sin(25 *t+3)+23/4 *Sin(26 *t+5/2)+8/3 *Sin(27 *t+3/4)+5/3 *Sin(28 *t+35/17)+11/4 *Sin(29 *t+2/3) ' shark
              y = -7/5 *Sin(3/4-30 *t)-8/7 *Sin(4/3-27 *t)-Sin(2/3-26 *t)-2/3 *Sin(1/3-25 *t)-15/4 *Sin(3/4-23 *t)-83/7 *Sin(4/3-16 *t)-99/7 *Sin(5/4-13 *t)-19/3 *Sin(6/5-12 *t)-32 *Sin(8/5-9 *t)-298/9 *Sin(2/3-8 *t)-129/5 *Sin(1/3-7 *t)-76/3 *Sin(6/5-6 *t)+11/4 *Sin(29 *t)+873/4 *Sin(t+17/5)+364/11 *Sin(2 *t+26/9)+268/5 *Sin(3 *t+4/5)+167/3 *Sin(4 *t+25/6)+208/3 *Sin(5 *t+21/5)+31/4 *Sin(10 *t+5/4)+21 *Sin(11 *t+11/5)+27/5 *Sin(14 *t+25/6)+81/10 *Sin(15 *t+31/8)+10/3 *Sin(17 *t+9/2)+3/2 *Sin(18 *t+2)+91/9 *Sin(19 *t+39/10)+3/2 *Sin(20 *t+7/5)+25/8 *Sin(21 *t+1)+4 *Sin(22 *t+5/4)+3 *Sin(24 *t+13/3)+Sin(28 *t+26/9)
              z = Rnd(0,1)
              VertexA(N).x = x*0.05
              VertexA(N).y = y*0.05
              VertexA(N).z = z*10
              'ColorA(N).r = 255 :ColorA(N).g = 0 :ColorA(N).b = 0 
              If z = 1 Then
              ColorA(N).r = 255 :ColorA(N).g = 0 :ColorA(N).b = 0
              Else  
              ColorA(N).r = 0 :ColorA(N).g = 255 :ColorA(N).b = 100
              End If
                    
            
        t + 0.001 : N + 1
        
    Wend
        
         
     
    End Sub  
    
    Function theta(a As Single) As Single
       If a<=0 Then
        Function = 0
        Else
        Function = 1
       End If
    
    End Function
    
    Attached Files Attached Files

  2. #2
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    6,718
    Blog Entries
    3
    Rep Power
    684
    Really nice

    Please note that if object is too big, you can always scale it via TBGL_Scale to fit better, without need to increase draw distance.
    With greater draw distance comes less precision when performing depth tests - this is why I try to keep it minimal.

    Few tips
    When declaring local variables, you can use shortcut syntax:
    DWord gbPoints = TBGL_GBufferCreate(%TBGL_POINTS, %TBGL_3D)
    ...
    DWord Nb = 10053
    ...
    Single x, y, z, t
    ...
    DWord N = 1
    
    Functions with simple IFs can be made into one liners:
    If a<=0 Then
      Function = 0
    Else
      Function = 1
    End If
    
    becomes
    return iif(a<=0, 0, 1)
    
    Petr
    Last edited by Petr Schreiber; 22-11-2015 at 14:21.
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 7 64bit - Intel Core 2 Duo T6600 @ 2.2GHz - 4 GB RAM - NVIDIA GeForce G210M 512MB
    Windows 8 64bit - Intel Core i5-3350P @ 3.1GHz - 8 GB RAM - NVIDIA GeForce GT640 3GB

Similar Threads

  1. How to combine GBuffers & Entities?
    By ReneMiner in forum TBGL General
    Replies: 7
    Last Post: 12-11-2014, 22:05
  2. The silence inside the fractal - GBuffers (how many may be used ?)
    By RobbeK in forum TBGL Scripts and Projects
    Replies: 35
    Last Post: 24-01-2014, 12:18
  3. Preview: GBuffers for TBGL
    By Petr Schreiber in forum TBGL module by Petr Schreiber
    Replies: 11
    Last Post: 04-03-2010, 11:33

Posting Permissions

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