OpenGL 2.1 中的顶点缓冲区对象
(我指定了 2.1,因为我的笔记本电脑不会超过该版本。自从 3.x 及以后强制引入着色器以来,我可能会这样做?)。
感谢维基百科:http://en.wikipedia.org/wiki/Vertex_Buffer_Object 我开始明白它是多么简单是使用VBO(我仍然对IBO持怀疑态度?)。到目前为止,我了解到使用它们的主要原因是由于数据现在存储在视频内存中而获得了性能提升。
我想知道的是我应该如何在实际环境中使用它们。例如,我所看到的都是设置一个顶点缓冲区对象并绘制一个三角形或一个立方体等。如果我想绘制2个或更多怎么办?我是否为每个要绘制的实体设置一个新的 VBO?或者我是否神奇地附加到我早期设置的一些静态 VBO?
(I specified 2.1 because my laptop won't go past that version. I would have probably done this anyway since 3.x and on introduces shaders as mandatory?).
Thanks to Wikipedia: http://en.wikipedia.org/wiki/Vertex_Buffer_Object I am starting to grasp how simple it can be to use VBO's (I am still not positive about IBO's?). What I have understood so far is the primary reason to use them is a performance boost gained due to the fact that data is now stored in video memory.
What I would like to know is how I am supposed to use them in a practical context. For instance, all of what I have seen has been setting up one Vertex Buffer Object and drawing one triangle or one cube, etc. What if I want to draw 2 or more? Do I set up a new VBO for each entity that I want to draw? Or do I magically append to some static VBO that I setup early on?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这要看情况,因为这个问题相当广泛。
使用一个 VBO 作为顶点属性,使用一个 VBO 作为每个模型/对象/实体/网格的索引是一种直接的方法。您可能会受益于将所有模型存储在单个 VBO 中,因此您不必过于频繁地绑定缓冲区,但是当这些模型不是那么静态时,这会带来其他问题。另外,您可以为每个对象使用多个 VBO,可能一个用于动态数据,一个用于静态数据,或者一个用于几何数据(位置...),一个用于材质相关数据(texCoords...)。
每个对象使用一个 VBO 可能有它的(缺点)优点,并且使用单个巨大的 VBO 可能不是一个好主意。这仅取决于具体的应用以及这些概念与它的契合程度。但对于一开始,直接的每个对象一个 VBO 方法并不是一个坏主意。只是不要每个三角形使用一个 VBO ;)
It depends, as the question is quite broad.
Using one VBO for the vertex attributes and one for the indices for each model/object/entity/mesh is the straight forward approach. You may benefit from storing all models in a single VBO, so you don't have to bind buffers too often, but this brings in other problems when those models are not that static. Also, you may use multiple VBOs per object, maybe one for dynamic data and one for static data, or one for geometry data (position...) and one for material related data (texCoords...).
Using one VBO per object may have it's (dis)advantages, as well as using a single huge VBO may not be that good an idea. It just depends on the concrete application and how well those concepts fit into it. But for the beginning, the straight-forward one-VBO-per-object approach is not that bad an idea. Just don't use one VBO per triangle ;)
这应该非常简单 - 只需向 VBO 添加更多数据即可。
因此,例如,如果您想渲染 3 个三角形而不是 1 个,请确保将 9 个顶点发送到
glBufferData
。然后,要渲染所有这些,请使用调用glDrawArrays(GL_TRIANGLES, 0, 9);
它真的应该这么简单。然而,关于 IBO 的一点是,它们实际上与 VBO 没有太大区别,而且我真的会习惯使用它们。这确实是使用 VBO 的实用方法。由于多个三角形通常意味着覆盖同一表面(例如,一个立方体需要十二个三角形),因此仅使用 VBO 会导致指定顶点数据时出现大量重复。通过使用索引缓冲区,事情会更加高效,因为任何重复都会从 VBO 中删除。我发现 this 它提供了从仅 VBO 迁移到 VBO/IBO 的简洁示例,并且还很好地解释了这一变化。
It should be really easy - just add more data to the VBO.
So, for example, if you wanted to render 3 triangles instead of just 1, make sure that you send 9 vertices to
glBufferData
. Then, to render all of them, use the callglDrawArrays(GL_TRIANGLES, 0, 9);
It really should be that simple.However, a point about IBOs is that they're really not much different from VBOs at all, and I would really get used to using them. That is really the practical way of using VBOs. Since multiple triangles are usually meant to cover the same surface (e.g. a cube needs twelve triangles), using only VBOs causes a lot of repetition in specifying vertex data. By using index buffer as well, things are even more efficient, since any repetition is removed from the VBO. I found this which provides a concise example of moving from VBO-only to VBO/IBO and also gives a good explanation of the change.