1

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);
skiwi
  • 66,971
  • 31
  • 131
  • 216
Tustin2121
  • 2,058
  • 2
  • 25
  • 38

1 Answers1

11

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:

private ByteBuffer indexBuffer; //change to
private ShortBuffer indexBuffer; //or
private IntBuffer indexBuffer;

and these:

glDrawElements(GL_TRIANGLES, indexCount, GL_UNSIGNED_BYTE, 0); //fine for up to 256
glDrawElements(GL_TRIANGLES, indexCount, GL_UNSIGNED_SHORT, 0); //up to 65535
glDrawElements(GL_TRIANGLES, indexCount, GL_UNSIGNED_INT, 0); //up to 4294967295

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.)

Tustin2121
  • 2,058
  • 2
  • 25
  • 38