DrawArrays work fine, yet DrawElements doesn't?

So, this is my first ‘problem’ thread. So I’ve been toying with VBOs, and after successfully using modified Riven’s VBO tutorial to draw a .OBJ file on the screen with DrawArrays, I decided to move onto DrawElements, then interleaving, etc.

However, when it came time to DrawElements, my OBJ wouldn’t render properly. So I comment’d that code out, and put in simple code to draw a triangle. This worked.

DrawElements vs. DrawArrays

This is my draw code: http://pastebin.java-gaming.org/fa705631367
And my init code: http://pastebin.java-gaming.org/a705373176f
If you zoom upon the Elements, you’ll see a few pixel of white being rendered at the bottom.

Please look over documentation on both OpenGL functions

http://www.opengl.org/sdk/docs/man/xhtml/glDrawArrays.xml
http://www.opengl.org/sdk/docs/man/xhtml/glDrawElements.xml

Your problem most likely stems from the fact that you’re not passing a proper indices array when using DrawElements, so it doesn’t know the order or the obj’s vertices. glDrawArrays assumes an order depending on the set mode.

I also recommend using GL_TRIANGLE_STRIP instead of GL_TRIANGLES for efficiency.

So after reading that I realized that I was using element count where I should have used index count.
But unfortunately, it hasn’t changed anything.

Why in the world are you uploading the VBO every single frame?! The glBufferData function should only be called once in your init code since you’re using GL_STATIC_DRAW.

Oh, thanks!
This is the first time I’ve really played with VBOs(only really used displaylists, prior), so what I’m doing is all rather new to me.

Anyway are you sure your OBJ only needs 3 indices and 2 normals? :slight_smile:
The few white pixels is the 1 triangle only being drawn instead of the hundreds it may have.

OBJ files don’t have indices in the first place btw. That vBuffer that was returned was the only thing you needed to upload once and then use glDrawArrays since there are no elements to possibly upload (unless you want to generate your own ;D).

So using Draw Elements has no advantage?

You only use drawElements when you have elements. You don’t have elements with this OBJ model therefore use drawArrays. There is no advantage to one over the other it’s just whether you have/want to use indices/elements or not.

with glDrawElements you can take advantange of the vertex cache of the GPU, allowing you to (in the most optimal case) only run the vertex shader once for every unique index.

Ah yes there are speed/memory advantages of course, I meant no advantage in terms of usage/complexity. :slight_smile: