I'm having a hard time getting multiple vertex array objects to render multiple primitives. Nearly all the OpenGL tutorials I've found online show using only a single VAO, so I'm not sure what I might be doing wrong.
I'm using Qt-OpenGL and trying to render a square and a cube (on multiple VAOs).
Using the following code, I'm only getting one primitive displayed on the screen (whichever one is initialized second). I can see either primitive when I turn off initialization of the other one, but not at the same time.
Data Struct:
struct VBO : public QOpenGLBuffer {
};
struct VAO : public QOpenGLVertexArrayObject {
VBO vbo[1];
};
enum { CIRCLE, RECT, NUM_VAOs };
enum { POS, NUM_VBOs };
VAO vao[NUM_VAOs];
Initialization:
static void init_objects() {
for(int i = 0; i < NUM_VAOs; ++i) {
vao[i].create();
}
vao[CIRCLE].bind();
for(int i = 0; i < NUM_VBOs; ++i) {
vao[CIRCLE].vbo[i].create();
vao[CIRCLE].vbo[i].setUsagePattern( QOpenGLBuffer::StaticDraw );
}
vao[CIRCLE].vbo[POS].bind();
vao[CIRCLE].vbo[POS].allocate(circle.getVertexData(), circle.getNumVertices()*3*sizeof(float));
vao[CIRCLE].release();
// repeat for RECTANGLE instead of CIRCLE
vao[RECT].bind();
for(int i = 0; i < NUM_VBOs; ++i) {
vao[RECT].vbo[i].create();
vao[RECT].vbo[i].setUsagePattern( QOpenGLBuffer::StaticDraw );
}
vao[RECT].vbo[POS].bind();
vao[RECT].vbo[POS].allocate(circle.getVertexData(), circle.getNumVertices()*3*sizeof(float));
}
Rendering Code:
void game::paintGL() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
vao[RECT].bind();
vao[RECT].vbo[POS].bind();
QMatrix4x4 id;
Game.setMVP( id );
Game.setColor( Colour(0.0, 1.0, 0.0) );
glDrawElements(GL_TRIANGLES, rect.getSolidIndices().size()*sizeof(unsigned int),
GL_UNSIGNED_INT, &(rect.getSolidIndices()[0]));
glFinish();
vao[RECT].release();
// Now for circle:
vao[CIRCLE].bind();
vao[CIRCLE].vbo[POS].bind();
Game.setMVP( id );
Game.setColor( Colour(1.0, 0.0, 0.0) );
glDrawElements(GL_TRIANGLES, circle.getSolidIndices().size()*sizeof(unsigned int),
GL_UNSIGNED_INT, &(circle.getSolidIndices()[0]));
glFlush();
}
I've tried reading the data from the buffers before rendering (they are distinct and what I would expect for each primitive), so I know the write occurred properly. I'm guessing now I might be binding the buffers wrong, or missing a step while rendering.
Aucun commentaire:
Enregistrer un commentaire