r/GraphicsProgramming • u/leech6666 • Mar 22 '21
Rendering 2 shapes from 2 different VBOs
Hey guys,
i am new in openGL
i am trying to render two shapes int the window, with two different Vertex Buffers.
i use glDrawArrays twice, but only one shape appears in the screen
if any can help me, i would be grateful :)
thanks
2
u/danmarell Mar 22 '21
I'm assuming you also have a Vertex Array Object (they are needed in modern opengl). If you have two VBOs then you would have two VOAs that reference those vertex buffers. You might be forgetting to switch which VOA is bound.
Vertex Array Objects are different from Vertex Buffer Objects. Buffers (VBOs) themselves stores the data, and the array objects (VAOs) store which buffers are bound and info for marshalling the data to the shaders.
So things I would check are....
- do you have two VAOs?
- are you switching the vao to the second one before your second draw call?
Another thing I would say is if the objects are using the same shader, just stick them into the same buffer. Even if not, you can still stick them into the same buffer (if they share the same vertex format) and call the draw() function with an offset and count to index into the buffer where the second object starts.
Of course if you are just learning, then its good to get the basics down.
here is a blog series I've started (not finished yet) which the post on buffers and VAOs. (if you scroll down to the bottom, you'll see a diagram that shows how the buffers and VOA's relate to each other).
https://dokipen.com/modern-opengl-part-5-feeding-vertex-data-to-shaders/
Be aware, that in that post, I am using Direct State Access which is a different way of using opengl (introduced officially in opengl 4.5) that avoids some of the pitfalls of using a bind based state machine.
Good luck!
3
u/leech6666 Mar 22 '21
i found the bug
i didnt understand how layout (location = 0) works
so i was having layout (location = 0) for the position of the first shape and layout (location = 1) for the position of the second shape
in the main function i thought using gl_Position twice would render both shapes, but that was my mistake
thank u tho
4
u/TimJoijers Mar 22 '21 edited Mar 22 '21
I recommend you download RenderDoc and spend some time to learn to use it. It is really good tool to figure out why something does not work.
2
u/shadergremlin Mar 22 '21
You’re probably missing something silly, you might be able to use glGetError to figure out what you are missing. You can also look at this example to see if you are missing something.
5
u/Sir_Kero Mar 22 '21
If only the shape of the second draw call is appearing, then my guess would be that you clear your color and/or depth buffer inbetween.