I'm going through the OGLdev tutorials, and I'm getting stuck on getting Vertex Array Objects to work. Here's the relevant code:
glBindBuffer(GL_ARRAY_BUFFER, buffers[POS_VB]);
FloatBuffer posBuf = BufferUtils.createFloatBuffer(positions.size() * 3);
for (Vector3f v : positions) {
posBuf.put(v.toFloatArray());
}
posBuf.flip();
glBufferData(GL_ARRAY_BUFFER, posBuf, GL_STATIC_DRAW);
POS_VB
is 1, and positions
is an ArrayList filled with the positions (as Vector3f
's) of the mesh. v.toFloatArray()
just returns a float array with the members of the vector.
Having checked the code where glGetError()
starts generating anything other than 0, I've found that this line:
glBufferData(GL_ARRAY_BUFFER, posBuf, GL_STATIC_DRAW);
is the culprit. However, checking the documentation, GL_INVALID_OPERATION
is only generated when the first parameter is set to the reserved value (0). This is obviously not the case, so what's happening here?
There are only two conditions where glBufferData()
will trigger a GL_INVALID_OPERATION
error. This is from the OpenGL 4.5 spec:
An INVALID_OPERATION error is generated by BufferData if zero is bound to target.
An INVALID_OPERATION error is generated if the BUFFER_IMMUTABLE_STORAGE flag of the buffer object is TRUE.
The second error condition only applies to OpenGL 4.4 and later, where immutable buffers can be allocated with glBufferStorage()
.
Therefore, the only logical explanation in your case is that you have 0 bound for GL_ARRAY_BUFFER
. You're binding the buffer in the first line of the posted code:
glBindBuffer(GL_ARRAY_BUFFER, buffers[POS_VB]);
This means that buffer[POS_VB]
is 0 at this point. Buffer ids need to be generated with glGenBuffers()
before being used. It looks like either you missed the glGenBuffers()
call, or used it improperly.