简体   繁体   English

OpenGL VBO批处理最佳做法

[英]OpenGL VBO batch best practices

I am creating a 2d game engine as a side project. 我正在创建2D游戏引擎作为附带项目。 I've been doing some experimenting and research and have come across a lot of people suggesting that you batch (store multiple objects to draw) in the same VBO. 我一直在做一些实验和研究,发现很多人建议您在同一VBO中批处理(存储要绘制的多个对象)。 For example, if I my scene had a lot of trees I could put all the trees in the same VBO because they have the same memory footprint and then use a single glDrawArrays to draw all of them. 例如,如果我的场景中有很多树,则可以将所有树放入同一VBO中,因为它们具有相同的内存占用量,然后使用单个glDrawArrays来绘制所有树。

This is fine and it makes total sense... but then I started to wonder how I can send the different transforms for each tree? 很好,这完全有意义...但是后来我开始怀疑如何为每棵树发送不同的转换? How do I get that to the shader? 如何将其传递给着色器? Or is this approach assuming I do the calculations on CPU and send the entire VBO each draw? 还是这种方法是假设我在CPU上进行计算并在每次抽奖时将整个VBO发送出去?

Here are two of the main questions i've been looking at: 这是我一直关注的两个主要问题:

OpenGL VAO best practices OpenGL VAO最佳做法

OpenGL How Many VAOs OpenGL多少个VAO

The term you're looking for is Instancing . 您要查找的术语是Instancing

You create a VAO that contains the model of the tree, and then instead of passing the model matrix through the uniforms, you put it as an instanced vertex attribute in the VAO. 您创建一个包含树模型的VAO,然后将其作为实例顶点属性放置在VAO中,而不是通过统一模型传递模型矩阵。 Then each instance of your tree will get drawn with a different model matrix. 然后,将使用不同的模型矩阵来绘制树的每个实例。

You can utilize compute shaders or transform feedback to update and store the model-view product in the VAO once per instance per frame (rather than calculating it per each vertex of each instance). 您可以利用计算着色器或转换反馈来将模型视图乘积每帧每实例更新和存储在VAO中(而不是针对每个实例的每个顶点进行计算)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM