I don’t understand what’s going on! I have this mesh loader, and when I load small meshes with it, my program works fine, drawing the whole mesh. But now that I tested the program with a big mesh (upwards of 100,000 vertices) it only draws a tiny section of it! Is it something with my graphics card, like some insanely small limit or something?
Using LWJGL, some code I picked up from their tutorials:
private ByteBuffer indexBuffer;
...
// Create a new VBO for the indices and select it (bind)
indxBufId = glGenBuffers();
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indxBufId);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexBuffer, GL_STATIC_DRAW);
...
// Bind to the index VBO that has all the information about the order of the vertices
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indxBufId);
// Draw the vertices
glDrawElements(GL_TRIANGLES, indexCount, GL_UNSIGNED_BYTE, 0);
// Put everything back to default (deselect)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
No, past self, it is not a problem with your graphics card or anything, it is a problem with your indexes. You’re using BYTES for your index buffer, and that’s perfectly fine, and likely preferable due to its small size, when working with small models like 12-triangled cubes.
But if your model runs over 256 indexes into your vertex array, you’re gonna get wrap around. A LOT. Especially when you’re asking for index 125,647.
If your model is bigger than 256 verts, its time to up to a short or an int. Remember to change the following things:
and these:
Remember, though Java’s ints, shorts, and bytes are all signed, OpenGL is working with unsigned, and will convert the stuff in your buffers accordingly.
(Though these examples are LWJGL, this goes for normal OpenGL in C languages as well.)