Draw many of the same object quickly in OpenGL

前端 未结 2 1287
时光取名叫无心
时光取名叫无心 2021-02-04 08:16

So I\'m working on a game and I need to draw a lot of the same object. Same shape, same size, same color, just different locations.

Right now my setup is like this.

2条回答
  •  伪装坚强ぢ
    2021-02-04 08:52

    On ~modern hardware, instancing is the way to go.

    The idea is that you send the geometry to your GPU once (one draw call), specifying how many instances you want to draw (primCount parameter).

    Then in the vertex shader you can use the intrinsic input variable gl_InstanceID to learn which instance is being rendered and then use the appropriate transformation for it. This approach implies that you should have the transformations for all your instances available in the vertex shader, for example in an Uniform Buffer Object.

    Edit: The glVertexAttribDivisor function is very useful together with instancing; it basically allows to have some per-vertex attributes together with some per-instance attributes.

提交回复
热议问题