I just started learning OpenGL and decided to use the LWJGL in java to begin. I'm following the tutorials on their wiki, but I'm confused about something in the DrawElements tutorial.
In the tutorial, they create a VBO containing the vertices (four corners) of a rectangle and add it to a VAO. Then they create a VBO that contains the order the vertices are to be drawn, but this one isn't added to the VAO.
Here's the code where they're created:
float[] vertices =
{
-0.5f, 0.5f, 0.0f,
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
0.5f, 0.5f, 0.0f
};
vertexCount = vertices.length;
FloatBuffer verticesBuffer = BufferUtils.createFloatBuffer(vertexCount);
verticesBuffer.put(vertices);
verticesBuffer.flip();
byte[] indices =
{
0, 1, 2,
2, 3, 0
};
indicesCount = indices.length;
ByteBuffer indicesBuffer = BufferUtils.createByteBuffer(indicesCount);
indicesBuffer.put(indices);
indicesBuffer.flip();
vaoId = GL30.glGenVertexArrays();
GL30.glBindVertexArray(vaoId);
addedVBO = GL15.glGenBuffers();
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, addedVBO);
GL15.glBufferData(GL15.GL_ARRAY_BUFFER, verticesBuffer, GL15.GL_STATIC_DRAW);
GL20.glVertexAttribPointer(0, 3, GL11.GL_FLOAT, false, 0, 0); //Vbo is added to Vao
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
GL30.glBindVertexArray(0);
unaddedVBO = GL15.glGenBuffers();
GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, unaddedVBO);
GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, indicesBuffer, GL15.GL_STATIC_DRAW);
GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);
Here's the code where they're rendered:
GL30.glBindVertexArray(vaoId);
GL20.glEnableVertexAttribArray(0);
GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, unaddedVBO);
GL11.glDrawElements(GL11.GL_TRIANGLES, indicesCount, GL11.GL_UNSIGNED_BYTE, 0);
GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);
GL20.glDisableVertexAttribArray(0);
GL30.glBindVertexArray(0);
Is there a reason one VBO is added to the VAO and one isn't? Would it make a difference if it was added?Also, it occurs to me as I write this that maybe adding one to the VAO allows you to use both VBOs at the same time during rendering?