I'm rendering various elements in OpenGL ES 2 on Android, and I'd like to pack the vertex attributes of the whole scene into a single vertex buffer object (VBO).
My question is, if these objects have different sets of attributes, strides and offsets, can I still pack this data into a single VBO and draw everything using it?
Most of these elements have 2 floats for position and 2 floats for texture coords (X, Y, S, T) as vertex attributes, and they do get drawn correctly using this:
@Override
public void onDrawFrame(GL10 glUnused) {
glClear(GL_COLOR_BUFFER_BIT);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer.getBufferId());
drawObject1();
drawObject2();
drawObject3();
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
public void drawObject1() {
glUseProgram(object1Program);
object1Program.setUniforms(finalMatrixObject1, object1Texture);
glVertexAttribPointer(object1Program.getPositionAttributeLocation(), 2, GL_FLOAT, false, 16, 0);
glEnableVertexAttribArray(object1Program.getPositionAttributeLocation());
glVertexAttribPointer(object1Program.getTextureCoordinatesAttributeLocation(), 2, GL_FLOAT, false, 16, 8);
glEnableVertexAttribArray(object1Program.getTextureCoordinatesAttributeLocation());
glDrawArrays(GL_TRIANGLES, 0, 6);
}
public void drawObject2() {
glUseProgram(object2Program);
object2Program.setUniforms(finalMatrixObject2, object2Texture);
glVertexAttribPointer(object2Program.getPositionAttributeLocation(), 2, GL_FLOAT, false, 16, 0);
glEnableVertexAttribArray(object2Program.getPositionAttributeLocation());
glVertexAttribPointer(object2Program.getTextureCoordinatesAttributeLocation(), 2, GL_FLOAT, false, 16, 8);
glEnableVertexAttribArray(object2Program.getTextureCoordinatesAttributeLocation());
glDrawArrays(GL_TRIANGLES, 6, 6);
}
But I have weird artifacts and coords showing (if at all) when I try to render an element with a different pattern of vertex attributes, for example just 2 floats for position (X Y), or 3 floats for position and 2 floats for texture coord (X Y Z S T).
public void drawObject3() {
glUseProgram(object3Program);
object3Program.setUniforms(finalMatrixObject3);
glVertexAttribPointer(object3Program.getPositionAttributeLocation(), 2, GL_FLOAT, false, 8, 0);
glEnableVertexAttribArray(object3Program.getPositionAttributeLocation());
glDrawArrays(GL_TRIANGLES, 12, 6);
}
To resume the situation, I have a VBO packed this way (it differs a bit from my previous example):
6 * (X, Y, S, T)
6 * (X, Y, S, T)
6 * (X, Y, S, T)
6 * (X, Y)
36 * (X, Y, Z, S, T)
Is it possible to do what I'm trying to do? Or do I have to use different VBOs for every element with a unique set of vertex attributes?
If it's possible, what am I doing wrong? Before moving to VBO everything was working fine.