Search code examples
python-moderngl

ModernGL: How can I use a new VBO with my VAO?


According to my understanding of what a VAO is, it acts as a descriptor for the format of vertex data, and if you have vertex buffers that share the same format, you can use the same VAO for both. right?

All the VBOs in my application will use the same format so I want to use one VAO for all of them.

but ModernGL won't let me create a VAO without specifying a VBO, and I don't have access to the all the VBOs when I'm creating the VAO so I try giving it an empty VBO initally:

self.vao = self.ctx.vertex_array(self.program, self.ctx.buffer(b'\x00' * 24), 'position', 'texture')

and then attempt to bind my new VBO later (I have a format with 3 floats for position and 3 for texture: u, v, layer):

self.vao.bind(0, 'f', my_vbo, '3f', 0, 24)
self.vao.bind(1, 'f', my_vbo, '3f', 12, 24)

but it's not working, nothing appears on screen and every tutorial I can find related to ModernGL assumes you want a new VAO for each VBO, or is not in-depth to the point of needing to dynamically bind VBOs to VAOs.

thanks, sorry if question is stupid, I just figured I'd get it out there..


Solution

  • Per Einarf (ModernGL dev) on Discord, not being able to add buffers to a VertexArray after it's created is a limitation of ModernGL at the moment,

    You must create a new VAO for each of your VBOs, or re-create the same VAO with the new VBOs each time, which isn't even worth the complexity, because VertexArrays are small metadata objects, as Einarf says.

    I was seeking an answer to this merely because I wanted to be as correct as possible with my code. I ended up just having a VAO for each VBO.