Index Buffers in OpenGL?

BlitzMax Forums/OpenGL Module/Index Buffers in OpenGL?

StuC(Posted 2005) [#1]
I'm sure it does, just wondering what the API is for an "index buffer" (term used in DirectX)?

In DirectX, the index buffer is a set of indices referring to vertices in a vertex buffer to make up a triangle. So, defining a cube requires 8 vertices and 36 indices - results in significant memory savings and potential performance gains too.

Cheers,

Stu


N(Posted 2005) [#2]
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.


StuC(Posted 2005) [#3]
Cheers mate - thanks for the excellent (and informative) reply.

Stu


N(Posted 2005) [#4]
I should note that when using vertex/index buffers, you pass the data pointer as a byte pointer to zero (C's NULL) (or zero + the amount of bytes into the buffer you want to start at).


ImaginaryHuman(Posted 2005) [#5]
I'm not sure if BlitzMax fully supports proper index buffers ... for example I found that glColorLogicOp is defined but glLogicOp (for index mode) is not.


N(Posted 2005) [#6]
Daniel, those aren't even in my GL reference manual. What version are those in? Are the extensions?