在 XNA 4.0 中使用 DynamicVertexBuffer

发布于 2024-10-12 08:39:33 字数 375 浏览 11 评论 0 原文

我读到了有关 DynamicVertexBuffer 的内容,以及它如何更好地处理经常变化的数据。我有一个由立方体构建的世界,我需要将立方体的顶点存储在这个缓冲区中以将它们绘制到屏幕上。

然而,并非所有立方体都有顶点(有些是空气,是透明的),也不是立方体的所有面都需要绘制(它们彼此面对),所以我如何跟踪哪些顶点存储在缓冲?此外,某些面需要最后绘制,即具有透明度的面(如玻璃或树叶),并且这些面也需要按从后到前的顺序绘制,以免扰乱 Alpha 混合。

如果所有这些顶点都任意存储在该缓冲区中,我如何知道哪些顶点在哪里?

另外,顶点的数量可以改变,但 DynamicVertexBuffer 对我来说似乎不太动态,因为我根本无法改变它的大小。每次需要添加或删除面时是否都必须重新创建缓冲区?

I read about DynamicVertexBuffer, and how it's supposed to be better for data that changes often. I have a world built up by cubes, and I need to store the cubes' vertices in this buffer to draw them to the screen.

However, not all cubes have vertices (some are air, which is transparent) and not all faces of the cubes need to be drawn either (they are facing each other), so how do I keep track of what vertices are stored where in the buffer? Also, certain faces need to be drawn last, namely the ones with transparency in them (like glass or leaves), and these faces also need to be drawn in a back-to-front order to not mess up the alpha blending.

If all of these vertices are stored arbitrarily in this buffer, how do I know what vertices are where?

Also, the number of vertices can change, but the DynamicVertexBuffer doesn't seem very dynamic to me, since I can't change it's size at all. Do I have to recreate the buffer every time I need to add or remove faces?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

残龙傲雪 2024-10-19 08:39:33

听起来你正在以错误的方式处理这个问题 - 假设你的世界中只有微不足道的立方体。您应该将世界(及其立方体)存储在自定义数据结构中,以便您可以根据从给定点向给定方向查看时的世界规则快速确定哪些立方体(和面)可见。

然后,每次渲染场景时,都会生成这些面的批量顶点缓冲区。因此,不要使用顶点缓冲区作为存储世界的整个几何图形的基础。顶点缓冲区是一种渲染工具,而不是世界场景图工具。

这些大规模可见性问题在代码中的运行速度比在 GPU 中快得多。例如,如果你坐在原点,看着+x,你可以立即忽略-ve x方向上的所有立方体,这是一个非常简单的例子。

有关八叉树渲染的更完整示例,请搜索。这种渲染会很好地匹配你的世界布局。

最后的提示 - 当我说生成批量顶点缓冲区时 - 我的意思是以最小化 GPU 状态变化的方式将立方体批量在一起(例如相同的纹理、相同的着色器等)。最大限度地减少 GPU 状态的变化是优化渲染的关键 - 一旦您首先从渲染中剔除面就可以了。

Sounds like you are approaching this in the wrong way - assuming you have anything more than a trivial number of cubes in your world. You should store the world (and it's cubes) in a custom data structure that lets you rapidly determine which cubes (and faces) are visible based on the rules of your world from a given point when looking in a given direction.

Then each time you render a scene generate batches of vertex buffers of just these faces. So don't use vertex buffers as the basis for storing the entire geometry of your world. Vertex buffers are a rendering tool, not a world scene graph tool.

These kind of large scale visibility issues are much faster run in code than by the GPU. For example if you are sat at the origin, looking +x, you can immediately ignore all cubes in the -ve x direction, this is a very simple example.

For a more complete example search on oct-tree rendering. This kind of rendering would match your world layout quite nicely.

Final tip - when I say generate batches of vertex buffers - I mean batch you cubes together in ways that minimize changes in the state of the GPU (e.g. same texture, same shader etc). Minimizing changes to the state of the GPU is key to optimizing the rendering - once you've gone as far as you can with culling faces from the render in the first place.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文