Reputation: 143
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.
Upvotes: 0
Views: 3881
Reputation: 474266
glDrawElements(GL_TRIANGLES, 3, GL_FLOAT, 0);
Your indices are GLushort
s, not floats. You should have gotten a GL_INVALID_ENUM
error; you should always check for GL errors.
Also, shaderProgram->disableAttributeArray(0);
is completely unnecessary. The enable/disable state is part of the VAO (Qt's idiotic wrapper to the contrary). Disabling the attribute won't change the VAO, since you unbound it. And you don't want to disable it anyway.
Lastly, there's this:
shaderProgram->enableAttributeArray(0); shaderProgram->setAttributeBuffer(positionLocation, GL_FLOAT, 0, 3, 0);
The first parameter of both of these functions should be the same. They probably are, so this is more of a benign bug. But it's still wrong.
Upvotes: 3