PDA

View Full Version : creating vertex buffer object



saman_artorious
11-16-2013, 02:38 AM
Is there any way to use an array directly from CPU and not read it everytime from GPU?
In the example below, a VBO is created and the buffer is transfered to GPU, so every time the screen gets rendered,
it reads from GPU and not CPU. How can we modify this implementation if we don't want to keep the buffer in GPU and instead read it every time from CPU?



Vector3f Vertices[1];
Vertices[0] = Vector3f(0.0f, 0.0f, 0.0f);

glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);

mhagain
11-16-2013, 08:57 AM
That's client-side vertex arrays, which have been available since OpenGL 1.1. They're deprecated in modern GL_VERSIONs which means - in practice - that they may not work seamlessly with newer functionality, but if you're coding to a pre-GL 3 target you can freely use them.


// ensure that no buffer is bound
glBindBuffer (GL_ARRAY_BUFFER, 0);

// data to draw with
float data[] = {/* vertex data goes here */};

// set up arrays
glEnableVertexAttribArray (0);
glVertexAttribPointer (0, 3, GL_FLOAT, GL_FALSE, sizeof (float) * 3, data);

// draw a single triangle from the enabled array pointers
glDrawArrays (GL_TRIANGLES, 0, 3);

If all of your vertex data is static - i.e. it doesn't need to change - there's no advantage to this method. Since the GPU does the drawing it makes sense to store your vertex data on the GPU. If you're dealing with dynamic data then you have a number of options available, such as client-side arrays, immediate mode, buffer-object streaming, or even the MultiDraw commands (which may be useful in cases where the data is static but individual polys may or may not be drawn).

Osbios
11-16-2013, 09:37 AM
My guess would be that, except on very old hardware, using client-side vertex arrays is no difference then using glBufferData with GL_STREAM_DRAW as usage hint.
With client side arrays the driver only has two options: Stall the CPU until the GPU is in sync and has drawn the array or just copy the data and put the draw command on the GPU work queue. Guess what the driver will most likely do?