Hello OpenGL experts,
I am asking for some help to get multiple buffer objects working.
I have an animated scene with mutliple ellipses & spheres, and it is starting to go slow if I keep sending all the vertices to OpenGL for each frame. So I want to re-use buffer data across scene objects since I have repeats of the same data & just a different model-view transform.
The same shader is used throughout. The code works fine if I set (objectNotInCache) to always return TRUE, so the buffers get recreated each time round the loop. It also works fine if I only use one geometry & therefore keep retrieving the same one from my cache.
I can’t get this working with mutliple goemetries in the same scene. The minute I introduce a mix of geometries (i.e. use multiple buffers), I get garbage on screen - so obviously I am missing something in how to correctly set up and swap between buffers.
Thanks in advance for any assistance. Can anyone advise what I am doing wrong?
=========
Pseudocode
Loadshader ()
SetShaderUniforms();
for (each object in scene)
{
SetShaderModelView();
if (objectNotInCache)
{
glGenVertexArrays( 1, &vao );
glBindVertexArray( vao );
glGenBuffers (1, &bufferID)
glBindBuffer (GL_ARRAY_BUFFER, bufferID);
// Set up buffers for drawing facets. I know interleaved will be faster but want to get
// a simpler buffer layout working first.
glBufferData( GL_ARRAY_BUFFER,
vertexbuffersize + normalbuffersize,
NULL, GL_STATIC_DRAW );
glBufferSubData( GL_ARRAY_BUFFER,
0, vertexBufferSize(), pVertices);
glBufferSubData( GL_ARRAY_BUFFER,
vertexbuffersize , normalbuffersize, pNormals);
// vPosition & vNormal are the shader variables for vertex position & normal
// the positions are retrieved up where I load the shader. I know this is working bcs the
// program works when I use a single buffer or use only one geometry and thus one buffer
glEnableVertexAttribArray( vPosition );
glEnableVertexAttribArray( vNormal );
// set up the vertex arrays for drawing facets
glVertexAttribPointer( vPosition, 4, GL_FLOAT, GL_FALSE, 0,
BUFFER_OFFSET(0) );
glVertexAttribPointer( vNormal, 3, GL_FLOAT, GL_FALSE, 0,
BUFFER_OFFSET(vertexbuffersize) );
cacheVaoAndBuffer (vao, bufferID);
}
else
{
getVaoAndBufferfromCache (&vao, &bufferID);
glBindVertexArray( vao );
glBindBuffer (GL_ARRAY_BUFFER, bufferID);
// I supect I don't need the following 4 calls but ended up putting them in
// a vain attempt to get some output.
glEnableVertexAttribArray( vPosition );
glEnableVertexAttribArray( vNormal );
glVertexAttribPointer( vPosition, 4, GL_FLOAT, GL_FALSE, 0,
BUFFER_OFFSET(0) );
glVertexAttribPointer( vNormal, 3, GL_FLOAT, GL_FALSE, 0,
BUFFER_OFFSET(vertexbuffersize) );
}
glDrawArrays( GL_TRIANGLES, 0, vertexCount);
}