View Full Version : OpenGl Incorrect Rendering using VAO/VBO/IBO

02-13-2017, 07:46 AM
Hey there. I am sorry for the long post in advance :(. I am relatively new to OpenGL, I managed to create my first program following some online tutorials, but now I am facing a really weird bug.
Since it is hard to explain the bug I'll post a link to a video on youtube, that displays it.

With a cube model: https://www.youtube.com/watch?v=aGqc_7H3ylU
With a pyramid model: https://www.youtube.com/watch?v=wJCidaUUwjY

In the videos I show the original model that I have in blender (a simple cube and pyramid primitives), and then I show how my program displays them.
I have set it to draw lines so that you can see the model structure a bit better.
All I am doing in the video is rotating them around the X axis (left-right) and this is what I get.

If someone needs it, I can give the source code, but since there is quite a lot of other stuff in it I will try to explain only the drawing-related parts

First, I export my model from blender and grab the vertex coordinates and put them in a GLfloat array. I do the same with the Vertex Indecies but I put them in a GLushort array.
Next I create a VAO, VBO and IBO. (I have abstracted them in my own classes so it looks like Buffer * cVBO = new Buffer(cubeVertecies, 3, 8 * 3);

cubeVertecies is the GLfloat[] are the vertex coordinates
3 is the component count (I use it to set the size later when I call glVertexAttribPointer() in my VAO)
8 * 3 is the total size of the buffer (I also multiply it by sizeof(GLfloat) in my class)

I also have the exact same buffer, but for vertex colors, that I use for testing purposes (Im hopint this is not relevant to the bug)
After that I create an IndexBuffer from my Glushort[] and bind the data to it.

After this I add my VBO and ColorBuffer to the VAO and proceed inside the main loop to rendering.
In the loop I do the following stuff:

Bind the VAO
Bind the IBO
call glDrawElements() (I pass the IBO's size for the count and 0 for the indicies)
Unbind the IBO
Unbind the VAO

And when I render I get the odd artifact, shown in the video :(
When rendering 2D rectangles everything seems to work fine but with 3d models it breaks.

*I believe it has something to do with my IBO's since if I reorded some of the indices I get different parts of the model appearing/disappearing.
** Again, if its necessary, I can post my source code.