[英]XNA Buffer Best Practice
I am currently converting a 2D game to 3D, never having worked with 3D before I am trying to learn as I go. 我目前正在将2D游戏转换为3D,在尝试学习之前从未使用过3D。 I am wondering what the best way to use vertex/index buffers is. 我想知道使用顶点/索引缓冲区的最佳方法是什么。
Currently I am looping through a grid and creating a set of buffers for each tile type, these are then used in the draw method. 目前,我正在遍历网格并为每种图块类型创建一组缓冲区,然后在draw方法中使用它们。 Im presuming I shouldnt use multiple sets of vertex buffers but dont know of another way to do this, as each tile is a different texture. 我想我不应该使用多组顶点缓冲区,但不知道另一种方法,因为每个图块都是不同的纹理。
Grid loop 网格循环
for (int i = Convert.ToInt32(xPos / 2) - 30; i < Convert.ToInt32(xPos / 2) + 30; i++)
{
for (int a = Convert.ToInt32(yPos / 2); a < Convert.ToInt32(yPos / 2) + 50; a++)
{
if (mapXtile > 0 && mapYtile > 0 && mapXtile < Globals.mapsizex && mapYtile < Globals.mapsizey)
{
int currentTile = Globals.levelArray[level, mapXtile, mapYtile].TyleType;
if (currentTile == tyleType.grass)
{
createCubeVertices(i * 2, a * 2, 1, 0f, count, grassVertices);
SetUpBufferIndices(count, grassIndices);
count++;
}
if (currentTile == tyleType.water)
{
createCubeVertices(i * 2, a * 2, 1, 0f, count, waterVertices);
SetUpBufferIndices(count, waterIndices);
waterCount++;
}
}
mapYtile = mapYtile + 1;
//mapYtile++;
}
mapXtile = mapXtile + 1;
mapYtile = Convert.ToInt32(yPos / 2);
}
CopyToBuffers(grassVertices, grassIndices);
CopyToWaterBuffers(waterVertices, waterIndices);
Draw code 抽奖码
foreach (EffectPass pass in grassEffect.CurrentTechnique.Passes)
{
pass.Apply();
device.Indices = myIndexBuffer;
device.SetVertexBuffer(myVertexBuffer);
device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, grassVertices.Length, 0, grassIndices.Length/3);
}
foreach (EffectPass pass in mouseEffect.CurrentTechnique.Passes)
{
pass.Apply();
device.Indices = myWaterIndexBuffer;
device.SetVertexBuffer(myWaterVertexBuffer);
device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 10000, 0, 10000);
}
Any advice is greatly appreciated. 任何意见是极大的赞赏。
如果为绘画它们分配相同的效果,则可以使用图集纹理并将所有图元分组到一个唯一的顶点缓冲区中,从而允许在一次调用中绘制所有这些图元。
Your options: 您的选择:
Single atlas texture. 单个地图集纹理。 Advantages: easy, fast, default shaders can be used. 优点:可以轻松,快速地使用默认着色器。 Disadvantages: texture seams. 缺点:纹理接缝。
Multitexturing. 多纹理。 Advantages: powerful and flexible, you control everything. 优点:强大而灵活,您可以控制一切。 Disadvantages: more complex, custom shaders, slower. 缺点:更复杂,自定义着色器,速度较慢。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.