In OpenGL, you have (basically) two options for index buffers -- 1. Use an array of indices (normal old BlitzMax arrays work, just send a pointer to the first element to glDrawElements/glDrawRangeElements) or 2. Use a hardware buffer.
Here's examples of both:
Strict
'' CHANGE THIS TO USE EITHER THE HARDWARE OR SOFTWARE VERSIONS
Const USE_HARDWARE = 0
bglCreateContext( 800, 600, 0, 0, BGL_BACKBUFFER )
glewInit( )
Local vertices:Float[] =..
[ -64.0, -64.0,..
64.0, -64.0,..
0.0, 64.0 ]
Local colors:Byte[] =..
[ 255:Byte, 0:Byte, 0:Byte, 255:Byte,..
0:Byte, 255:Byte, 0:Byte, 255:Byte,..
0:Byte, 0:Byte, 255:Byte, 255:Byte ]
Local indices:Short[] = [ 0:Short, 1:Short, 2:Short ]
Local buffer:Int = 0 ' Hardware buffer name
'' Hardware buffer
If USE_HARDWARE
'' Create the buffer
glGenBuffersARB( 1, Varptr buffer )
'' Bind it as an index buffer
glBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, buffer )
'' Set its data to the previously declared indices
glBufferDataARB( GL_ELEMENT_ARRAY_BUFFER_ARB, indices.Length * 2, Varptr indices[0], GL_STATIC_DRAW_ARB )
EndIf
'' Setup matrices so you can actually see the triangles
glMatrixMode( GL_PROJECTION )
glLoadIdentity( )
glOrtho( -400, 400, -300, 300, -10, 10 )
glMatrixMode( GL_MODELVIEW )
glLoadIdentity( )
While Not KeyHit( KEY_ESCAPE )
glClear( GL_COLOR_BUFFER_BIT )
glEnableClientState( GL_VERTEX_ARRAY )
glEnableClientState( GL_COLOR_ARRAY )
glVertexPointer( 2, GL_FLOAT, 0, Varptr vertices[0] )
glColorPointer( 4, GL_UNSIGNED_BYTE, 0, Varptr colors[0] )
If USE_HARDWARE
glBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, buffer )
glDrawElements( GL_TRIANGLES, 3, GL_UNSIGNED_SHORT, Byte Ptr( 0 ) )
Else
glDrawElements( GL_TRIANGLES, 3, GL_UNSIGNED_SHORT, Varptr indices[0] )
EndIf
glDisableClientState( GL_VERTEX_ARRAY )
glDisableClientState( GL_COLOR_ARRAY )
bglSwapBuffers( )
Wend
If USE_HARDWARE
glDeleteBuffersARB( 1, Varptr buffer )
EndIf
bglDeleteContext( )
Change the constant USE_HARDWARE (at the top of the code) to switch between software and hardware buffers.
|