rendering crowds in OpenGL with VBOs

220 Views Asked by At

I'm trying to render a large number of copies of the same mesh in OpenGL. I know I can use a small vertex buffer with an index buffer, but each copy will have it's own transformation matrix. I want to pass each matrix into my shader, but vertices belonging to the same copy will use the same matrix. Therefore, the matrix buffer will be shorter than the vertex buffer.

The mesh has 4 vertices, so I need to somehow send a new matrix every four vertices. Any ideas?

1

There are 1 best solutions below

1
On BEST ANSWER

You could use Instanced rendering: https://www.opengl.org/sdk/docs/man/html/glDrawArraysInstanced.xhtml

or

https://www.opengl.org/sdk/docs/man3/xhtml/glDrawElementsInstanced.xml

you can create a VBO filled with all the transformation matrices for each index.

set the divisor to four so that the matrix changes after four vertices have been sent:

https://www.opengl.org/sdk/docs/man3/xhtml/glVertexAttribDivisor.xml

You can use the gl_instanceId in the vertex shader to figure out which instance is being rendered, and can generate the transformation matrix that way rather than sending every transformation matrix to the shaders.