Question

I am experimenting with the implementation of Vertex Buffer Objects (VBO) as a way to render in 3D using openGL via the Lightweight Java Game Library (LWJGL). My particular program allows every object with graphical representation to render and draw itself to the screen. My question is not about whether this rendering management is a good idea under which circumstances. I am rather interested in the drawing process of LWJGL itself. I have prepared to simple drawing methods of, let us say for example, a point cloud:

public void drawVBO(){
        // enable Vertex Arrays (VBOs)
        glEnableClientState(GL_VERTEX_ARRAY);

        // tell OpenGL to draw the data supplied by the pointer method
        // as points.length amount of points
        glDrawArrays(GL_POINTS, 0, points.length);
        // otherwise use GL_LINES, GL_TRIANGLES, GL_QUADS, etc. 
        // (http://wiki.delphigl.com/index.php/glDrawArrays)

        // Disable Vertex Arrays (VBOs)
        glDisableClientState(GL_VERTEX_ARRAY);
    }

    public void drawVBOBound(){
        // enable Vertex Arrays (VBOs)
        glEnableClientState(GL_VERTEX_ARRAY);
        // tell OpenGL to use our VBO
        glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObject);

        // tell OpenGL to draw the data supplied by the pointer method
        // as points.length amount of points
        glDrawArrays(GL_POINTS, 0, points.length);
        // otherwise use GL_LINES, GL_TRIANGLES, GL_QUADS, etc. 
        // (http://wiki.delphigl.com/index.php/glDrawArrays)

        // Unbind the VBO
        glBindBuffer(GL_ARRAY_BUFFER, 0);
        // Disable Vertex Arrays (VBOs)
        glDisableClientState(GL_VERTEX_ARRAY);
    }

The difference between those to is the binding and unbinding of the objects own VBO. In a scenario where I render and draw lots of different point cloud objects, I will have multiple vertex buffers bound. This indicates that I have to explicitly tell openGL which vertex buffer to use during the rendering process (as seen in method drawVBOBound). Otherwise openGL would draw every single bound vertex buffer by means of the current drawing object which equals multiple drawing during one renderin/drawing cycle of the program (nobody would want that). Is this assumption correct?

Was it helpful?

Solution

Your two code variants are missing the main point: the vertex attribute pointers.

glDraw* functions do never care what is currently bound at the GL_ARRAY_BUFFER binding point. What is does care about are the vertex arrays (and, for the glDrawElements* variants, the currently bound GL_ELEMENT_ARRAY_POINTER).

What you are currently missing is that the GL_ARRAY_BUFFER binding is relevant when you set up the vertex attrob pointers (probably via glVertexPointer in your case). The reference to the VBO bound at setup-time will become part of the pointer.

So, when you really want to draw from different VBOs, you must respecify the attrib pointers (and, for that, also rebind the VBOs). Note that modern GL uses VAOs which encapsulate the vertex array state and allow swithcing between different sets of VA pointers with a single function call.

As a side note: you also don't need to enable and disable those attrib pointers all the time. You can leave it on as long as you don't plan to do any call to glDraw* without a GL_VERTEX attribute array. As Martijn Courteaux already pointed out, it does not enable or disable VBOs, it just tells the GL for which attributes an array is to be used, and for which it will use the current value (which will be constant for all vertices of the draw call, then).

OTHER TIPS

Nope. There can only one VBO be bound at a time. That is the last one you bound using

glBindBuffer(GL_ARRAY_BUFFER, vbo);

If you do not unbind a buffer, then it will be unbound automatically as you bind the next one. This means you won't draw "multiple VBOs at once".

Next, GL_VERTEX_ARRAY is the name for the built-in vertex attribute that holds the coordinates of the position. So your comment:

// Disable Vertex Arrays (VBOs)

Isn't technically correct. You disable the attribute in the shader, which doesn't really disable VBO's.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top