javaopengllwjgl

How to get data stored in VBO


I need to get back a float[] of vertices which is stored in attrList in vbo in vao.
I do:

float[] b = new float[vertexCount];
glBindVertexArray(vaoId);
long pointer;
glEnableVertexAttribArray(0);
pointer = glGetVertexAttribPointer(0,  GL_VERTEX_ATTRIB_ARRAY_POINTER);
glBindBuffer(GL_ARRAY_BUFFER, (int) pointer);
glGetBufferSubData(GL_ARRAY_BUFFER, 0, b);

All data in vaos and vbos is stored correctly, I can draw things perfectly.
The problem here is that variable pointer returns 0.
What am I doing wrong?


Solution

  • No, your code is nonsense.

    From the documentation of glGetVertexAttribPointer:

    The pointer returned is a byte offset into the data store of the buffer object that was bound to the GL_ARRAY_BUFFER target (see glBindBuffer) when the desired pointer was previously specified.

    You should thus pass pointer as the offset parameter of your glGetBufferSubData call. Your code treats it as the name of the VBO that was bound (because you call glBindBuffer); if you want to retrieve that you can ask using glGetVertexAttrib(0, GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING). Same goes for the stride, divisor, etc.

    But all of this smells like a giant XY problem: you should know the values that were used at setup time, no?