I am trying to combine a vertex array object with a vertex buffer & an index buffer using Qt.
The following code combines a VAO and a VBO, working perfectly:
// Create buffers
void Tile::setupBuffers() {
vbo.create();
vbo.bind();
vbo.setUsagePattern(QOpenGLBuffer::StaticDraw);
vbo.allocate(vertices.constData(), vertices.size() * sizeof(QVector3D));
vbo.release();
vao.create();
shaderProgram->bind();
vao.bind();
vbo.bind();
shaderProgram->enableAttributeArray(0);
shaderProgram->setAttributeBuffer(positionLocation, GL_FLOAT, 0, 3, 0);
vao.release();
shaderProgram->disableAttributeArray(0);
vbo.release();
shaderProgram->release();
}
// Draw triangle
void Tile::draw() {
shaderProgram->bind();
vao.bind();
glDrawArrays(GL_TRIANGLES, 0, 3);
vao.release();
shaderProgram->release();
}
When I'm now trying to add an index buffer nothing gets drawn except for the clear color. Here's the code:
// Create buffers
void Tile::setupBuffers() {
vbo.create();
vbo.bind();
vbo.setUsagePattern(QOpenGLBuffer::StaticDraw);
vbo.allocate(vertices.constData(), vertices.size() * sizeof(QVector3D));
vbo.release();
ibo.create();
ibo.bind();
ibo.setUsagePattern(QOpenGLBuffer::StaticDraw);
ibo.allocate(indices.constData(), indices.size() * sizeof(GLushort));
ibo.release();
vao.create();
shaderProgram->bind();
vao.bind();
vbo.bind();
shaderProgram->enableAttributeArray(0);
shaderProgram->setAttributeBuffer(positionLocation, GL_FLOAT, 0, 3, 0);
ibo.bind();
vao.release();
shaderProgram->disableAttributeArray(0);
vbo.release();
ibo.release();
shaderProgram->release();
}
void Tile::draw() {
shaderProgram->bind();
vao.bind();
glDrawElements(GL_TRIANGLES, 3, GL_FLOAT, 0);
vao.release();
shaderProgram->release();
}
Here's the content of my vertex vector & index vector:
vertices = {
QVector3D(1.0f, -1.0f, 0.0f),
QVector3D(1.0f, 1.0f, 0.0f),
QVector3D(-1.0f, -1.0f, 0.0f)
};
indices = {
0, 1, 2
};
I assume the order of binding the VAO/VBO/IBO in the second code block is wrong, I just don't know how it's done right. Any help is appreciated.