  $ FileName "t.exe"
 'uses rtl64
  $ title     "Index Buffer Objects"
  uses OpenglSceneFrame
  uses glo2\ShaderSupport

  'types
  '====
  '
  type vert float x,y,z
  type tex  float u,v
  type rgba byte  r,g,b,a
  '
  '
  'globals
  '=====
  '
  indexbase 1
  sys VBOs 'flag VBOs in use
  sys VertBuf,NormBuf,TexBuf,IndexBuf
  sys VertCount
  sys Texname
  int vbo[16]

  sub BuildVertexArrays()
  =======================
   '
  indexbase 1
  '
  'CREATE DATA BUFFERS
  '
  VertCount=4
  sys n=100
  '
  VertBuf=getmemory n*sizeof vert
  NormBuf=getmemory n*sizeof vert
  TexBuf=getmemory n*sizeof tex
  IndexBuf=getmemory n*sizeof int
  '
  float l=1.0, n=-1.0, o=0.0
  float v at VertBuf
  v={n,n,o, l,n,o, l,l,o, n,l,o}
  float v at NormBuf
  v={o,o,l, o,o,l, o,o,l, o,o,l}
  float v at Texbuf
  v={ o,o,  l,0, l,l, 0,l}
  int i at IndexBuf
  i={0,1,2,3}
  '
  if VBOs
    SetupExtFunctions
    '
    glGenBuffers( 4, vbo )
    '
    macro setup(i,n,type,buf,mo)
      glBindBuffer mo, vbo[i]
      glBufferData mo, n*sizeof(type), buf, GL_STATIC_DRAW
    end macro
    '
    int n=4 'VertCount
    setup 1,n,Vert,VertBuf,GL_ARRAY_BUFFER
    setup 2,n,Vert,NormBuf,GL_ARRAY_BUFFER
    setup 3,n,Tex,TexBuf,GL_ARRAY_BUFFER
    setup 4,n,Int,IndexBuf,GL_ELEMENT_ARRAY_BUFFER
    '
    freememory VertBuf
    freememory NormBuf
    freememory TexBuf
    freememory IndexBuf
  end if
  end sub


  function MakeSimpleTexture(int*texture)
  =======================================
  string tex
  tex=nuls (128*128*4)
  int c,i
  sys e=128*128
  int q at strptr tex
  for i=0 to <e
    q=0xffc08000+c*2
    'q=0xb0c08000+c*2
     @q+=4
     c+=1
  next
  sys p=strptr tex
  CreateTexture2D(128,128,p,texture)
  end function


  sub DrawVertexArrays(optional int m)
  ====================================
  '
  glEnableClientState( GL_VERTEX_ARRAY )						// Enable Vertex Arrays
  glEnableClientState( GL_NORMAL_ARRAY )						// Enable Vertex Arrays
  glEnableClientState( GL_TEXTURE_COORD_ARRAY )
  '
  if VBOs
    static int loaded
    if m then loaded=0 'reload buffers
    if not loaded
      glBindBuffer( GL_ARRAY_BUFFER, vbo[1] )
      glVertexPointer( 3, GL_FLOAT, 0, null )
      '
      glBindBuffer( GL_ARRAY_BUFFER, vbo[2] )
      glNormalPointer( GL_FLOAT, 0, null )
      '
      glBindBuffer( GL_ARRAY_BUFFER, vbo[3] )
      glTexCoordPointer( 2, GL_FLOAT, 0, null )
      '
      glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, vbo[4] )
      loaded=1
    end if
    '
    'glDrawArrays( GL_QUADS, 0, VertCount )
    glDrawElements( GL_QUADS, VertCount, GL_UNSIGNED_INT, 0 )
  else 'CLIENT SIDE BUFFERS
    glVertexPointer  ( 3, GL_FLOAT, 0, VertBuf )
    glNormalPointer( GL_FLOAT, 0, NormBuf )
    glTexCoordPointer( 2, GL_FLOAT, 0, TexBuf )
    'glDrawArrays( GL_QUADS, 0, VertCount )
    glDrawElements( GL_QUADS, VertCount, GL_UNSIGNED_INT, IndexBuf )
  end if
  'Disable Pointers
  glDisableClientState( GL_VERTEX_ARRAY )
  glDisableClientState( GL_NORMAL_ARRAY )						// Enable Vertex Arrays
  glDisableClientState( GL_TEXTURE_COORD_ARRAY )
  end sub


  string ti

  sub initialize(sys hWnd)
  '=======================
  zstring glext at glGetString GL_EXTENSIONS
  'putfile "t.txt", glExt
  finit
  MakeSimpleTexture(TexName)
  VBOs=instrword glExt, "GL_ARB_vertex_buffer_object"
  if VBOs
    ti="Using GPU vertex buffer objects"
  else
    ti="Using clent side vertex buffer"
  end if
  BuildVertexArrays()
  end sub


  sub Release(sys hwnd)
  '====================
  glDeleteTextures 1,TexName
  if VBOs
    glDeleteBuffers  4, vbo 
  else
    freememory VertBuf
    freememory NormBuf
    freememory TexBuf
    freememory IndexBuf
  end if
  end sub



  sub scene(sys hWnd)
  '==================
  '
  static single s1,s2,ang1,angi1=1
  '
  if ti then SetWindowText hWnd,ti
  ActiveFrame
  '
  glcolor4f 1.0, 1.0, 1.0, 1.0
  glrotatef ang1, 0,0,1
  '
  glEnable GL_TEXTURE_2D
  glBindTexture GL_TEXTURE_2D,TexName
  glTranslatef 0.0, 0.0, -3.0
  DrawVertexArrays()
  '
  'UPDATE ROTATION ANGLES
  '
  ang1+=angi1
  if ang1>360 then ang1-=360
  sleep 10
  '
  end sub
