忍者ブログ

Memeplexes

プログラミング、3DCGとその他いろいろについて

かんたんXNA その13 IndexBuffer

このページは古いです
最新版はこちら

頂点にパフォーマンスを上げるVertexBufferがあるのと同じように
インデックスにもパフォーマンスを上げる(しかしめんどくさい)IndexBufferがあります。
 


using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
 

public class MyGame : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;
    BasicEffect effect;

    VertexPositionColor[] vertices = new VertexPositionColor[4];
    VertexBuffer vertexBuffer;

    int[] indices = new int[6];
    IndexBuffer indexBuffer;


    public MyGame()
    {
        graphics = new GraphicsDeviceManager(this);
    }

    protected override void Initialize()
    {
        vertices[0] = new VertexPositionColor(new Vector3(1, 0, 0), Color.White);
        vertices[1] = new VertexPositionColor(new Vector3(-1, 0, 0), Color.Red);
        vertices[2] = new VertexPositionColor(new Vector3(0, 1, 0), Color.Navy);
        vertices[3] = new VertexPositionColor(new Vector3(0, -1, 0), Color.Navy);

        indices[0] = 0;
        indices[1] = 1;
        indices[2] = 2;
        indices[3] = 0;
        indices[4] = 3;
        indices[5] = 1;


        base.Initialize();
    }

    protected override void LoadGraphicsContent(bool loadAllContent)
    {
        if (loadAllContent)
        {
            effect = new BasicEffect(graphics.GraphicsDevice, null);
            effect.VertexColorEnabled = true;
            effect.Projection = Matrix.CreatePerspectiveFieldOfView(
                MathHelper.ToRadians(45),
                Window.ClientBounds.Width / (float)Window.ClientBounds.Height,
                1,
                100
                );
            effect.View = Matrix.CreateLookAt(
                new Vector3(0, 0, 3),
                new Vector3(0, 0, 0),
                new Vector3(0, 1, 0)
            );

            vertexBuffer = new VertexBuffer(
                graphics.GraphicsDevice,
                VertexPositionColor.SizeInBytes * vertices.Length,
                ResourceUsage.None
                );
            vertexBuffer.SetData<VertexPositionColor>(vertices);
            indexBuffer = new IndexBuffer(
                graphics.GraphicsDevice,
                sizeof(int) * indices.Length,
                ResourceUsage.None,
                IndexElementSize.ThirtyTwoBits
                );
            indexBuffer.SetData<int>(indices);
        }
    }

    protected override void Draw(GameTime gameTime)
    {
        graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
        graphics.GraphicsDevice.VertexDeclaration = new VertexDeclaration(
            graphics.GraphicsDevice,
            VertexPositionColor.VertexElements
            );
 
        effect.Begin();
        foreach (EffectPass pass in effect.CurrentTechnique.Passes)
        {
            pass.Begin();
            graphics.GraphicsDevice.Vertices[0].SetSource(
                vertexBuffer,
                0,  //The starting offset.
                VertexPositionColor.SizeInBytes
                );
            graphics.GraphicsDevice.Indices = indexBuffer;
            graphics.GraphicsDevice.DrawIndexedPrimitives(
                PrimitiveType.TriangleList, //primitiveType
                0,  //baseIndex
                0,  //minVertexIndex
                vertices.Length,    //numVertices
                0,  //startIndex
                2   //primitiveCount
                );

            pass.End();
        }
        effect.End();
    }
}

 

 

 

f692fe71.JPG
見た目は普通に描画したときと全く変わりませんが、内部ではIndexBufferを使っており、
ほんの少しパフォーマンスがよくなっているはずです。
(しかしやはり、こういう単純な例では全くありがたみがありません。)
 

拍手[0回]

PR