Hi, i implemented vertex buffer objects for drawing an octree world, and i seems to work fine, but i have a question:
when i init my program, i do something like this:
// bind and generate
glGenBuffersARB( 1, &uiVBODataID);
glBindBufferARB( GL_ARRAY_BUFFER_ARB, uiVBODataID );
// Load The Data
glBufferDataARB( GL_ARRAY_BUFFER_ARB, octreeWorldNumberOfVerts*3*sizeof(float), pOctreeWorldVertices, GL_STREAM_DRAW_ARB );
then, the code for the drawing the octree is something like this:
void COctree::DrawOctree(COctree *pOctreeNode)
{
if( pOctreeNode->IsSubdivided() )
{
// draw recursively all the nodes
DrawOctree( pOctreeNode->m_pOctreeNode[TOP_LEFT_FRONT] );
DrawOctree( pOctreeNode->m_pOctreeNode[TOP_LEFT_BACK] );
DrawOctree( pOctreeNode->m_pOctreeNode[TOP_RIGHT_FRONT] );
DrawOctree( pOctreeNode->m_pOctreeNode[TOP_RIGHT_BACK] );
DrawOctree( pOctreeNode->m_pOctreeNode[BOTTOM_LEFT_FRONT] );
DrawOctree( pOctreeNode->m_pOctreeNode[BOTTOM_LEFT_BACK] );
DrawOctree( pOctreeNode->m_pOctreeNode[BOTTOM_RIGHT_FRONT] );
DrawOctree( pOctreeNode->m_pOctreeNode[BOTTOM_RIGHT_BACK] );
}
else
{
if( !pOctreeNode->m_pvVertices )
return;
glBindBufferARB( GL_ARRAY_BUFFER_ARB, uiVBODataID );
glVertexPointer( 3, GL_FLOAT, 0, (char *) NULL );
glEnableClientState( GL_VERTEX_ARRAY );
glDrawArrays( GL_TRIANGLES, 0, octreeWorldNumberOfVerts );
glDisableClientState( GL_VERTEX_ARRAY );
}
}
Before using VBO, i used vertex arrays for doing it, and if i have more than one node (i.e the octree is subdivided), the framerate decreased a lot because it was drawing ALL the vertices while the octree was still subdivided.
With VBO’s it seems to be the same problem
Because i have this problem, i tried to do what i had when i was using vertex arrays, and that was creating the necessary arrays for every subdivided node in my Init code, so then, when in every frame i call the draw function, it only drawed the vertex data that is in every node.
I’m doing it like this:
void COctree::CreateVBOs(COctree *pOctreeNode, UINT uiVBOoffsetID)
{
if( !pOctreeNode )
return;
if( pOctreeNode->IsSubdivided() )
{
CreateVBOs( pOctreeNode->m_pOctreeNode[TOP_LEFT_FRONT ],uiVBOoffsetID );
CreateVBOs( pOctreeNode->m_pOctreeNode[TOP_LEFT_BACK ],uiVBOoffsetID );
CreateVBOs( pOctreeNode->m_pOctreeNode[TOP_RIGHT_FRONT],uiVBOoffsetID );
CreateVBOs( pOctreeNode->m_pOctreeNode[TOP_RIGHT_BACK ],uiVBOoffsetID );
CreateVBOs(pOctreeNode->m_pOctreeNode[BOTT_LEFT_FRONT],uiVBOoffsetID );
CreateVBOs( pOctreeNode->m_pOctreeNode[BOTT_LEFT_BACK],uiVBOoffsetID );
CreateVBOs( pOctreeNode->m_pOctreeNode[BOTT_RIGHT_FRONT],uiVBOoffsetID);
CreateVBOs( pOctreeNode->m_pOctreeNode[BOTT_RIGHT_BACK],uiVBOoffsetID );
}
else
{
if( !pOctreeNode->m_pvVertices )
return;
// increase the VBO ID
pOctreeNode->m_uiVBODataID += uiVBOoffsetID;
// bind and generate
glGenBuffersARB( 1, &pOctreeNode->m_uiVBODataID);
glBindBufferARB( GL_ARRAY_BUFFER_ARB, pOctreeNode->m_uiVBODataID );
// Load The Data
glBufferDataARB( GL_ARRAY_BUFFER_ARB,
pOctreeNode->GetTriangleCount()*sizeof(float),
pOctreeNode->m_pvVertices, GL_STREAM_DRAW_ARB );
}
}
and then, when drawing:
if( pOctreeNode->IsSubdivided() )
{
// draw the nodes recursively
DrawOctree( pOctreeNode->m_pOctreeNode[TOP_LEFT_FRONT] );
DrawOctree( pOctreeNode->m_pOctreeNode[TOP_LEFT_BACK] );
DrawOctree( pOctreeNode->m_pOctreeNode[TOP_RIGHT_FRONT] );
DrawOctree( pOctreeNode->m_pOctreeNode[TOP_RIGHT_BACK] );
DrawOctree( pOctreeNode->m_pOctreeNode[BOTTOM_LEFT_FRONT] );
DrawOctree( pOctreeNode->m_pOctreeNode[BOTTOM_LEFT_BACK] );
DrawOctree( pOctreeNode->m_pOctreeNode[BOTTOM_RIGHT_FRONT] );
DrawOctree( pOctreeNode->m_pOctreeNode[BOTTOM_RIGHT_BACK] );
}
else
{
if( !pOctreeNode->m_pvVertices )
return;
glBindBufferARB( GL_ARRAY_BUFFER_ARB, pOctreeNode->m_uiVBODataID );
glVertexPointer( 3, GL_FLOAT, 0, (char *) NULL );
glEnableClientState( GL_VERTEX_ARRAY );
glDrawArrays( GL_TRIANGLES, 0, pOctreeNode->m_TriangleCount/3 );
glDisableClientState( GL_VERTEX_ARRAY );
}
}
But if i do that, it only draws some portions of the world, then…
what am i doing wrong?