1

I'm working on my first steps with OpenGL (3.x) and I plan to use VAO & VBO's to draw several figures (triangle, cube,...):

Now i got the following setup:

VAO --> VBO [vertices, colors] + VBO [indices]

Where "vertices" has [3 * 3floats vertices of triangle, 8*3floats of cube], "colors" has RGB per vertex (no alpha), "indices" has [ triangle: 0,1,2, square: 0,1,2, 0,2,3, ...]

Now if I draw using "glDrawElements". i only see the first figure on the series drawn correctly (and getting the right color), the second one doesn't works as it should.

So if i render the triangle data first, it goes like:

triangle then cube

And if i render the cube first, it goes like:

cube then triangle

Note: triangle is red, and cube is colourful, so the first figure is always shown as I expected

This worked ok drawing arrays with "glDrawArrays" (with offset + trianglecount) instead of drawElements but, of course, indexing makes the data arrays muuuuuch smaller. So i wanted to do the move.

How could i draw the same setup but with indexing? should i call another method?

This is the code I use to prepare the VBO's data, in case of doubt.

// VBOS solid
    glBindBuffer(GL_ARRAY_BUFFER, vboSolid[0]);
    glBufferData(GL_ARRAY_BUFFER, vertices.size()*sizeof(GLfloat),&vertices[0], GL_STATIC_DRAW);
    glVertexAttribPointer((GLuint)0, 3, GL_FLOAT, GL_FALSE, 0, 0);
    glEnableVertexAttribArray(0);

    glBindBuffer(GL_ARRAY_BUFFER, vboSolid[1]);
    glBufferData(GL_ARRAY_BUFFER, colors.size()*sizeof(GLfloat),&colors[0], GL_STATIC_DRAW);
    glVertexAttribPointer((GLuint)1, 3, GL_FLOAT, GL_FALSE, 0, 0);
    glEnableVertexAttribArray(1);

    glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, vboIndex );
    glBufferData( GL_ELEMENT_ARRAY_BUFFER, indices.size()*sizeof(GLuint),&indices[0], GL_STATIC_DRAW);
    glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, 0 );

And this is the call I do when rendering. In this loop, the triangle would have indicesCount=3 & offset=0, while cube would have 24 & 3 respectively.

glDrawElements(
   GL_TRIANGLES,      
   primitives[i]->indicesCount,    
   GL_UNSIGNED_INT,  
   (void*) (primitives[i]->offsetIndices*sizeof(GLuint))   
);
Ragnagard
  • 191
  • 5
  • 16

1 Answers1

1

Since i was still testing after posting, i finally made a working attempt, using:

glDrawElementsBaseVertex(
   GL_TRIANGLES.
   __INDICES_TO_RENDER__,
   GL_UNSIGNED_INT,
   (void*) (__INDICES_RENDERED___*sizeof(GLuint)),
   _VERTICES_RENDERED_BEFORE_   
);

where "INDICES_TO_RENDER" and "INDICES_RENDERED_" are values counting 3f, so for a cube you use 24floats as value "TO_RENDER", and 0 to "_RENDERED".

"_VERTICES_RENDERED_BEFORE_" worked being a human value of vertices rendered, so before cube is rendered, it is 0, after, it should be 8.

Kinda weird (probably i did something wrong), but it worked in my tests:

wooot

It was said at Using an offset with VBOs in OpenGL, but i tried several times till i found what to put into the call.

If an expert could verify this, i would be happier :P.

Community
  • 1
  • 1
Ragnagard
  • 191
  • 5
  • 16