Vertex Buffer Command

BlitzMax Forums/BlitzMax OpenGL Programming/Vertex Buffer Command

Has anyone had any luck using the Vertex Buffer Extension, or for that matter.. can you use it in B.Max ??

This is the bit that I dont understand:
GLuint buffer = 0;
glGenBuffersARB(1, &buffer);
glBindBufferARB(GL_ARRAY_BUFFER_ARB, buffer);
glBufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(data), data, GL_STATIC_DRAW_ARB);

I can see what its doing in C, but using B.Max !?

I've not checked out VBOs yet, but I think Terrabits 3D Level Demo uses them (if supported), check this post:

http://www.blitzbasic.com/Community/posts.php?topic=41577

As for the code, GLuint is an Unsigned INT, in max just use an INT

At a guess, the code means something like this:

Local buffer:Int = 0
glGenBuffersARB(1, VarPtr(buffer)) 'Get a buffer name (name = a pointer)
glBindBufferARB(GL_ARRAY_BUFFER_ARB, buffer) 'Tell GL buffer is of type 'GL_ARRAY_BUFFER_ARB'
glBufferDataARB(GL_ARRAY_BUFFER_ARB, SizeOf(data), data, GL_STATIC_DRAW_ARB) 'Point the buffer to some data

If 'data' is a vertex array, I'm not sure if SizeOf(data) would need to be 'data.length' instead.

Tom

Thanks Tom, thats a good place for me to start.

After a few days of head scratching, finally got this baby up and running..

Here's a small example that might come in useful:
bglCreateContext(800,600,32,0,BGL_BACKBUFFER|BGL_DEPTHBUFFER|BGL_FULLSCREEN)

glClearColor(0.0, 0.0, 0.0, 0.0)
glColor3f(0.0, 0.0, 1.0)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(45.0, 800/600, 0.1, 100.0)

glewInit

Global data:Float[]=[ 0.0, 1.0,-10.0,-1.0,-1.0,-10.0,1.0,-1.0,-10.0 ]
Global buffer:Int = 0
Global sglfloat:Int = Sizeof(GL_FLOAT)

glEnableClientState(GL_VERTEX_ARRAY)

glGenBuffersARB(1, Varptr(buffer))
glBindBufferARB(GL_ARRAY_BUFFER_ARB, buffer)
glBufferDataARB(GL_ARRAY_BUFFER_ARB, sglfloat*Sizeof(data), data, GL_STATIC_DRAW_ARB)

While Not KeyDown(KEY_ESCAPE)

glClear (GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
glEnableClientState(GL_VERTEX_ARRAY)
glBindBufferARB(GL_ARRAY_BUFFER_ARB, buffer)
glVertexPointer(3, GL_FLOAT, 0, Int Ptr 0)
glDrawArrays(GL_TRIANGLES, 0, 3)

FlushMem
bglSwapBuffers

Wend 
End

What is it ?, and why do you need it ??

The vertex data is stored in the graphics card memory, rather than the systems - (which is transfered across every time its used). Huge speedups can be had.. the only downside being its quite a new command, so some of the older graphics cards might not be able to support it.

Cheers Paul.


b.t.w, here's how to check for supported extentions:


'Anywhere after bglCreateContext()
If Not ExtensionSupported("GL_ARB_vertex_buffer_object") RuntimeError "VBOs not supported" 'Or use vertex arrays instead e.t.c


...

Function ExtensionSupported(ext$) 'thanks to Lee for this one
	Local RetStr:String=String.FromCString$(Byte Ptr(glGetString( GL_EXTENSIONS )))
	If Instr(retstr$,ext$)<>0 Return True
	Return False
End Function


Well done Tom. This is a handy little function.

Thanks.