开发者

Using DynamicVertexBuffer in XNA 4.0

开发者 https://www.devze.com 2023-02-04 18:01 出处:网络
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 b

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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消