PDA

View Full Version : Vertex Buffer Object's Mapping



MeshMan
04-12-2004, 01:49 PM
Hi guys.

Im starting to use VBO's and ive been reading the CGI spec doc and the NVidia doc on them over and over. They seem to be the fastest way that GL has to offer in drawing vertices to the screen (at least thats what i think). Anyway, now my problem...

Whats this business about mapping a buffer? Whats it for? When should it be done? Why is it done? Is it a good thing?

Help would be apprecitaed.
Regards,
MeshMan

yooyo
04-12-2004, 02:25 PM
If you scene contains any dynamic vertex data it will be very usefull for you. Dynamic vertex data are vertices that app change every frame.
This kind of vertices driver must handle on different way than static vertex data.

yooyo

CrazyButcher
04-12-2004, 11:14 PM
yipp
with the mapping you basically get a pointer to the currently bound vbo, so that you can change the data just like if it was a normal array in RAM.

MeshMan
04-13-2004, 05:21 AM
Sounds very handy. So could you explain what the best method is:

1) Map the VBO at the start of my game scene, and when my gane is finished, unmap it.

2) Map/Unmap every frame.

Also, im experiencing some very frustrating problems. For some reason when i switch my code to use VBO's, some polys dont get drawn =(
For example, my bitmap font class works without the VBO and with standard glBegin()/glEnd() calls but when i apply the VBO and try to render "Hello World", all i get is "orld" or something stupid. Alot of people i speak to who have dealt with VBO also complain that they are very buggy...is this true? Can i trust to hard-code my renderer driver for my engine to use VBO's all the time?

harsman
04-13-2004, 06:20 AM
If the data in the VBO is static, there is no need to map it at all.

However, if you have some process that generates vertices on the CPU and upload using the normal BufferSubData or whatever it's called, the driver has to copy from your buffer into its own internal buffer (the VBO) which can cost some performance. If this is a bottleneck for you, you can instead map the VBO giving you a pointer that allows you to write straight to the VBO, eliminating the extra copy. You should only do this write when the data changes.

I'm not perfectly sure your question makes sense, if you map the VBO at the start of the game you won't be able to render from it until you unmap it.

In general, unless you upload lots of dynamic geometry into VBOs every frame there is no need to bother with mapping.

MeshMan
04-13-2004, 06:42 AM
Ok, thats helped, thanks alot. But i'm still not getting any joy with my VBO objects. I get crashes and all sorts of weird happenings.

Visit here to see the small source snippet of my class.
http://rafb.net/paste/results/g1061326.html

After i call my Create(), i call Lock() to start filling my VB, but after about writing 5 vertices, it crashes with Access Violation. Do i only have to call glBufferDataARB when i want to 'reserve' memory for my VBO, and then map it to fill it with data? Still reading through the spec doc for this but doesnt seem to be helping.

zeckensack
04-13-2004, 09:43 AM
You must bind the buffer object before doing anything with it. In your Create function:

<...>
drvOpenGL::glGenBuffersARB(1, &amp;m_BufferID);

//new line
drvOpenGL::glBindBufferARB(GL_ARRAY_BUFFER_ARB,m_B ufferID);

drvOpenGL::glBufferDataARB(GL_ARRAY_BUFFER_ARB, m_VertexSize*m_Count, m_pData, g_glBufferUsage[m_Usage]);
<...>You are currently crashing because you map a buffer object of undefined size.

jwatte
04-13-2004, 09:46 AM
First, you cannot draw with a buffer when it is mapped. That's an error, according to the spec.

Second, you need to call BufferDataARB() to size the buffer before it's mapped, else the driver doesn't know how much memory to allocate for you. Pass "NULL" for the pointer argument of BufferDataARB().

Third, you need to bind the buffer before working on it, else you're working on whatever the previously bound buffer was.

MeshMan
04-14-2004, 03:35 AM
Aha, thats a bit obvious, should of spotted that. Thanks for the help guys, all my engine rendering is purely VBO's now :p .

Next up...dynamic textures.