Unable to obtain buffer object data through glGetBufferSubData

363 views Asked by At

I was hoping to make a tower of hanoi game using opengl. Eventually i came up to the problem of processing & transfering data from one buffer object to another. I have successfully stored my vertices in a buffer object and bound it with a vertex array . And now want to move that to another created buffer object bound to another vertex array. The problem is that when i try to obtain my vertices using glGetBufferSubData my data array shows all of its elements to be zero (I printed it on the console). I have double checked to see weather my buffer object has its data right and so it seems to be. Please help me i am on the end of my rope here.

My code :

struct Stack
{
    unsigned int top;
    VA vao; 
};

void transfer(Stack& s1, Stack& s2)
{
    float data[20];//collects data 
    s1.vao.Bind();
    glGetBufferSubData(GL_ARRAY_BUFFER, 0,  20* sizeof(float), data);`

    for (int i = 0; i < 20; i+=5)//prints data collected
    {
        cout << i+1 << "th loop" << endl;
        for (int j = i; j < i + 5; j++)
        {
            cout << data[j]<<"\t";
        }
        cout << endl;
    }

    s2.vao.Bind();
    glBufferSubData(GL_ARRAY_BUFFER, 0, 20 * sizeof(float), data);
}

Output Screen hopefully

2

There are 2 answers

0
Rabbid76 On BEST ANSWER

For the use of glGetBufferSubData and glBufferSubData, thus you have to bind the Buffer Object rather than the Vertex Array Object. This operation reads from the in the buffer object which is currently bound to the specified target.

The GL_ARRAY_BUFFER is a global state. In state vector of the VAO references to VBOs are stored, but binding the VAO does not bind any VBO. If the buffer target would be GL_ELEMENT_ARRAY_BUFFER, then you would have to bind the VAO, because the Index Buffer binding is stored within the VAO.

Anyway, if you want to copy data from one buffer to another, then you can use glCopyBufferSubData/glCopyNamedBufferSubData, which copies the data store of a buffer object to the data store of another buffer object directly, with out loading the data from the GPU to the CPU.

0
Yakov Galka On

GL_ARRAY_BUFFER is not part of the VAO state. Therefore binding the VAO does not change the GL_ARRAY_BUFFER binding.

You should keep the id of the buffer around to bind it prior to reading the data. If for some reason you don't have the original buffer id, you can retrieve it with

glGetVertexAttribiv(attrib, GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING, &id)

Notice how each attribute of the VAO may have a different buffer.