Note that there are some explanatory texts on larger screens.

plurals
  1. POOpenGL 3 VBO Indexing Multiple figures (glDrawElements)
    primarykey
    data
    text
    <p>I'm working on my first steps with OpenGL (3.x) and I plan to use VAO &amp; VBO's to draw several figures (triangle, cube,...):</p> <p>Now i got the following setup:</p> <p>VAO --> VBO [vertices, colors] + VBO [indices]</p> <p>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, ...]</p> <p>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.</p> <p>So if i render the triangle data first, it goes like:</p> <p><img src="https://i.stack.imgur.com/yKBZT.png" alt="triangle then cube"></p> <p>And if i render the cube first, it goes like:</p> <p><img src="https://i.stack.imgur.com/uBOM5.png" alt="cube then triangle"></p> <p><em>Note: triangle is red, and cube is colourful, so the first figure is always shown as I expected</em></p> <p>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.</p> <p><strong>How could i draw the same setup but with indexing? should i call another method?</strong></p> <p>This is the code I use to prepare the VBO's data, in case of doubt.</p> <pre><code>// VBOS solid glBindBuffer(GL_ARRAY_BUFFER, vboSolid[0]); glBufferData(GL_ARRAY_BUFFER, vertices.size()*sizeof(GLfloat),&amp;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),&amp;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),&amp;indices[0], GL_STATIC_DRAW); glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, 0 ); </code></pre> <p>And this is the call I do when rendering. In this loop, the triangle would have indicesCount=3 &amp; offset=0, while cube would have 24 &amp; 3 respectively.</p> <pre><code>glDrawElements( GL_TRIANGLES, primitives[i]-&gt;indicesCount, GL_UNSIGNED_INT, (void*) (primitives[i]-&gt;offsetIndices*sizeof(GLuint)) ); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload