1

I'm preparing some buffers with 2f vertices, 2f texvertices and 4f colors. It's displayed right. The whole thing is in one class. If I have more instances (every generating it's own buffer id, never passed in a function so it's not cleaned up, wrapped as pointers in a std::list) only during the first draw (paused after first draw using gdb and I see all buffered things) all buffered data is visible. In the next draw only the last drawn buffer is visible.

I prepare them by generate, bind and then fill the buffer with data with this call:

glBufferData( GL_ARRAY_BUFFER, Size * 8 * sizeof( float ), f, GL_STATIC_DRAW );

where Size is a std::size_t with the number of vertices and f the float-Array. To draw the buffer I bind it, activate the clientstates: GL_VERTEX_ARRAY, GL_TEXTURE_COORD_ARRAY, GL_COLOR_ARRAY.

glDrawArrays( Mode, 0, Size );

where Mode is a GLenum with GL_TRIANGLES.

I fixed it by calling glBufferData before glDrawArrays every frame but that's not how it supposed to be. It supposed to be generating, binding, filling and then to draw by just binding and calling glDrawArrays, isn't it?

If necessary: I'm working with C++, gcc on a Windows 7 x64. I was asked for more code:

void Buffer::CopyToGPU( )
{
    glBindBuffer( GL_ARRAY_BUFFER, Object );
    float* f = new float[ Size * 8 ];
    for ( std::size_t s( 0 ) ; s < Size ; ++s )
        CopyVertexToFloatArray( &f[ s * 8 ], Vortex[ s ] );
    glBufferData( GL_ARRAY_BUFFER, Size * 8 * sizeof( float ), f, GL_STATIC_DRAW );
    delete[] f;

    glVertexPointer( 2, GL_FLOAT, 8 * sizeof( float ), NULL );
    glTexCoordPointer( 2, GL_FLOAT, 8 * sizeof( float ), (char*)( 2 * sizeof( float ) ) );
    glColorPointer( 4, GL_FLOAT, 8 * sizeof( float ), (char*)( 4 * sizeof( float ) ) );
}
void Buffer::Render( )
{
    glBindBuffer( GL_ARRAY_BUFFER, Object );
    glEnableClientState( GL_VERTEX_ARRAY );
    glEnableClientState( GL_TEXTURE_COORD_ARRAY );
    glEnableClientState( GL_COLOR_ARRAY );

    //Actually draw the triangle, giving the number of vertices provided
    glDrawArrays( Mode, 0, Size );

    glDisableClientState( GL_VERTEX_ARRAY );
    glDisableClientState( GL_TEXTURE_COORD_ARRAY );
    glDisableClientState( GL_COLOR_ARRAY );
}
int main( ... ) // stripped buffer sfml2 initialization etc.
{
    glClearColor( 0, 0, 0, 1 );

    glEnable( GL_ALPHA_TEST );
    glAlphaFunc( GL_GREATER , 0.1 );
    glEnable( GL_TEXTURE_2D );
    glEnable( GL_BLEND );
    glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );

    while ( win.isOpen( ) ) // sf::Window
    {
        /// draw
        glClear( GL_COLOR_BUFFER_BIT );
        MVP.Apply( );
        CallDraw( );
        win.display( );
    }
}
7
  • Could you clarify "But when I'm having more of those buffers in the first frame all of them are displayed and then only the last drawn is visible" a little? Maybe show some more code? May 27, 2013 at 15:31
  • Nice. So if I understand this correctly: You are drawing several things (several Buffers) side-by-side (to the same frame), but only the last drawn object is shown? May 27, 2013 at 15:52
  • @VictorSand You get it right. May 27, 2013 at 16:00
  • How does your rendering loop look? When are you calling glClearColor etc? Also, since you're using deprecated functionality, I would suggest having a look at arcsynthesis.org/gltut/Basics/Tut02%20Vertex%20Attributes.html. May 27, 2013 at 16:03
  • @VictorSand Didn't knew that buffers are deprecated. I thought only the direct way is deprecated, but I'll have a look at attributes. Added drawing routine. May 27, 2013 at 16:15

1 Answer 1

2

You seem to specify the attrib pointers when you update the buffer object. This is not how it works. The vertex attrib pointers are (depending on the GL version) either global state, or per-VAO state, but never per-VBO state. Currently, when you do something like

bufferA.CopyToGPU();
bufferB.CopyToGPU();
while(true) {
    bufferA.render();
    bufferB.render();
}

only buffer B will be used (leaving potential for out-ouf-bounds accesses as you think you use buffer A when rendering it), as the vertex array state is set to buffer B in the second call, overwriting any attrib pointers set in the first call. You need either to respecify the pointers when you draw each object, or use Vertex Array Objects to encapsulate those pointers. Note that the latter path is mandatory on GL >= 3.X core profile.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.