【发布时间】:2012-06-04 13:43:24
【问题描述】:
我有一个 Block 类,它基本上包含一个 36 个顶点的数组,一个围绕原点 (0, 0, 0) 构建块的构造函数,以及一个更新方法。
我正在使用物理库来操作块。我为块分配了一个身体和碰撞皮肤,并在每次更新时更新物理步骤。每次更新后,我都会返回一个矩阵,其中包含块的新位置、方向等。
现在,我无法理解的是,将矩阵应用于块并有效地每帧更新顶点缓冲区的最佳方法。
这基本上是我目前所拥有的,我几乎肯定有更好的方法......
class Block
{
const float FullBlockSize = 20f;
const float HalfBlockSize = FullBlockSize / 2;
public VertexPositionNormalTexture [] vertices = new VertexPositionNormalTexture[36];
public VertexPositionNormalTexture [] currentVertices = new VertexPositionNormalTexture[36];
public DynamicVertexBuffer vBuffer;
Vector3 position;
public Block(Vector3 blockPosition)
{
position = blockPosition * FullBlockSize;
/*
* Build the 6 faces of the block here.
* The block is built around the origin,
* (0, 0, 0) being the center of the block.
*/
vBuffer = new DynamicVertexBuffer(Game.Device, VertexPositionNormalTexture.VertexDeclaration, vertices.Length, BufferUsage.WriteOnly);
vBuffer.SetData(vertices);
}
public Matrix WorldMatrix
{
get
{
return Matrix.CreateTranslation(position);
}
}
public void Update()
{
currentVertices = (VertexPositionNormalTexture[])vertices.Clone();
Matrix currentWorld = WorldMatrix;
for(int i = 0; i < currentVertices.Length; ++i)
{
currentVertices[i].Position = Vector3.Transform(currentVertices[i].Position, currentWorld);
}
vBuffer.SetData(currentVertices);
}
}
【问题讨论】:
-
为什么要修改顶点数据?这将变得非常缓慢。存储原始顶点和变换矩阵就完全足够了。
-
@NicoSchertler 这就是我的想法,但将变换矩阵传递给 GPU 并绘制三角形似乎并不奏效。他们都只是停留在原点。