LWJGL 2.8.4
nVidia GTS 450 - driver 306.97
I have a LWJGL based program with working shaders (yay!), IBOs, and VBOs.
Everything is working, I can render 1 model with DrawElements and it looks great!
But there is a problem when I try to combine models into one large VBO.
Here are 3 examples of trying to render two models stored in the same VBO.
1. When multiple models are combined into one VBO, only the first model renders correctly.
Models are combined by adding one model after another into the VBO:
VBO [vertex a.1][vertexb a.2]...[vertex a.n][vertex b.1][vertex b.2]...[vertex b.n] (is implemented as interleaving vertex attributes)
IBO [a.0, a.1, ..., a.n, b.0, b.1, ..., b.n] (format of element/index array)
Rendering with glDrawElementsBaseVertex:
// 2 quads, 4 vertices each, 6 indices each
glDrawElementsBaseVertex(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0, 0); // Quad 1
glDrawElementsBaseVertex(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 6, 4); // Quad 2 with 'a.n' offset
...
However, only the first draw call renders model 'a' correctly, the other models either disappear or are jumbled messes of vertices.
2. Another idea is to setup the IBO format [a.0, a.1, ..., a.n, a.n+b.0, a.n+b.1, ..., a.n+b.n]
Where indices are pre-offset before being added to the IBO.
Rendered like this:
glDrawElementsBaseVertex(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0, 0); // Quad 1
glDrawElementsBaseVertex(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 6, 0); // Quad 2 (assuming 4 vertices and 6 indices)
...
This does not work, only the first draw call renders correctly.
3. However, using the pre-offset index format and rendering everything in one call (no index offset or base vertex):
glDrawElementsBaseVertex(GL_TRIANGLES, 12, GL_UNSIGNED_INT, 0, 0); // Render both quads in the same call
...
The result are correct (two quads).
Attached are images of drawing two models (a cube and a quad),
The first image draws the cube first, quad second, I checked and all sides of the cube are rendered correctly, but as you can see, the quad is 'broken'.
The second image draws the quad first, cube second (notice the one 'broken' triangle on the top of the cube, further 'broken' triangles cannot be seen on the back of the cube)
The third image draws the cube and quad in one call (using offset indices), and both render correctly.
I could work around this problem in very simple programs (by drawing everything in one call with pre-offset index arrays).
But this work around is not manageable for more complex rendering, such as when materials are involved.
Any help or insite someone can provide would be greatly appreciated.
I assume I'm just calling DrawElements* incorrectly, since the third example works great.
Any ideas are welcome!
Update:
See the below post for solution, the index offset in the glDrawElements* calls was being treated incorrectly like an integer offset, and needed to be treated like a byte offset.