0

基本的に 36 個の頂点の配列、原点 (0, 0, 0) の周りにブロックを構築するコンストラクター、および更新用のメソッドを含む Block クラスがあります。

ブロックを操作するために物理ライブラリを使用しています。ボディとコリジョン スキンをブロックに割り当て、更新ごとに物理ステップを更新します。更新するたびに、ブロックの新しい位置、向きなどを含むマトリックスが返されます。

さて、私が理解できないのは、マトリックスをブロックに適用し、フレームごとに頂点バッファーを効率的に更新するための最良の方法です。

これは基本的に私が現時点で持っているものです。より良い方法があるとほぼ確信しています...

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);
    }
}
4

1 に答える 1

1

シェーダーに任せてください...シェーダーのパラメーターの1つは通常WorldMatrixです...

それを描くには:

effect.Parameters["WorldMatrix"].SetValue(World);
effect.Apply(0);

ブロックの描画に問題がある場合は...自分でワールド変換マトリックスを作成してテストしてみてください...

effect.Parameters["WorldMatrix"].SetValue(Matrix.CreateRotationZ(angle));
effect.Apply(0);

ブロックのインスタンスが複数ある場合は、インスタンス化を使用して変換行列を vertexBuffer でシェーダーに渡すことができます...

ここで、インスタンス化に関する情報を見つけることができます: http://blogs.msdn.com/b/shawnhar/archive/2010/06/17/drawinstancedprimitives-in-xna-game-studio-4-0.aspx

于 2012-06-05T08:18:16.107 に答える