3Dでポリゴン表示

■単純なポリゴン表示。
XNAで3Dのポリゴンを表示する簡単なサンプルが欲しかったので作ってみました。

■ソース

namespace WindowsGame2_01
{
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        private VertexDeclaration vertexDeclaration = null;
        private VertexPositionColor[] vertices = null;
        private BasicEffect basicEffect = null;
        private double dAng;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        protected override void Initialize()
        {
            // エフェクトを作成
            basicEffect = new BasicEffect(graphics.GraphicsDevice, null);
            // ビューマトリックス
            basicEffect.View = Matrix.CreateLookAt(
                    new Vector3((float)(10.0 * Math.Sin(0)), 5.0f, (float)(10.0 * Math.Cos(0))),
                    new Vector3(0, 0, 0),
                    Vector3.Up
                );
            // ワールドマトリックス
            basicEffect.World = Matrix.CreateTranslation(0, 0, 0);
            // プロジェクションマトリックス
            basicEffect.Projection = Matrix.CreatePerspectiveFieldOfView(
                    MathHelper.ToRadians(45.0f),
                    (float)graphics.GraphicsDevice.Viewport.Width / (float)graphics.GraphicsDevice.Viewport.Height,
                    1.0f,
                    100.0f
                );
            //  頂点カラーを有効にする
            basicEffect.VertexColorEnabled = true;
            // 頂点データ定義を作成
            vertexDeclaration = new VertexDeclaration(graphics.GraphicsDevice, VertexPositionColor.VertexElements);
            // 頂点データを作成する
            vertices = new VertexPositionColor[4];
            vertices[0] = new VertexPositionColor(new Vector3(-1.0f, 0.0f, 1.0f), Color.White);
            vertices[1] = new VertexPositionColor(new Vector3(-1.0f, 0.0f, -1.0f), Color.AliceBlue);
            vertices[2] = new VertexPositionColor(new Vector3(1.0f, 0.0f, 1.0f), Color.Green);
            vertices[3] = new VertexPositionColor(new Vector3(1.0f, 0.0f, -1.0f), Color.Pink);
            dAng = 0;
            base.Initialize();
        }

        protected override void Update(GameTime gameTime)
        {
            // ビューマトリックス
            basicEffect.View = Matrix.CreateLookAt(
                    new Vector3((float)(10.0 * Math.Sin(dAng)), 5.0f, (float)(10.0 * Math.Cos(dAng))),
                    new Vector3(0, 0, 0),
                    Vector3.Up
                );
            dAng += 0.01;
            if (dAng > 3.141592 * 2)
                dAng -= 3.141592 * 2;
            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            graphics.GraphicsDevice.Clear(Color.CornflowerBlue);

            basicEffect.Begin();
            basicEffect.CurrentTechnique.Passes[0].Begin();
            graphics.GraphicsDevice.VertexDeclaration = vertexDeclaration;
            graphics.GraphicsDevice.DrawUserPrimitives<VertexPositionColor>(PrimitiveType.TriangleStrip, vertices, 0, 2);
            basicEffect.CurrentTechnique.Passes[0].End();
            basicEffect.End();

            base.Draw(gameTime);
        }
    }
}

■描画設定
描画にあたっては「basicEffect」に「View」「World」「Projection」の3つのマトリックスを設定する必要があります。

// エフェクトを作成
basicEffect = new BasicEffect(graphics.GraphicsDevice, null);
// ビューマトリックス
basicEffect.View = Matrix.CreateLookAt(
    new Vector3((float)(10.0 * Math.Sin(0)), 5.0f + 2.0f, (float)(10.0 * Math.Cos(0))),
    new Vector3(0, 2, 0),
    Vector3.Up
    );
// ワールドマトリックス
basicEffect.World = Matrix.CreateTranslation(0, 0, 0);
// プロジェクションマトリックス
basicEffect.Projection = Matrix.CreatePerspectiveFieldOfView(
    MathHelper.ToRadians(45.0f),
    (float)graphics.GraphicsDevice.Viewport.Width / (float)graphics.GraphicsDevice.Viewport.Height,
    1.0f,
    100.0f);

・今回表示するポリゴンは頂点カラーを使用するので、頂点カラーを使用する設定をします。

basicEffect.VertexColorEnabled = true;

■モデルデータの設定
・プログラムでは以下のように定義します。
vertexDeclaration:モデルデータのデータフォーマットを定義
vertices:実際のモデルデータ

private VertexDeclaration vertexDeclaration = null;
private VertexPositionColor[] vertices = null;

・今回は、小さいデータなので直接プログラムに記入しています。

// 頂点データ定義を作成
vertexDeclaration = new VertexDeclaration(graphics.GraphicsDevice, VertexPositionColor.VertexElements);
// 頂点データを作成する
vertices = new VertexPositionColor[4];
vertices[0] = new VertexPositionColor(new Vector3(-1.0f, 0.0f, 1.0f), Color.White);
vertices[1] = new VertexPositionColor(new Vector3(-1.0f, 0.0f, -1.0f), Color.AliceBlue);
vertices[2] = new VertexPositionColor(new Vector3(1.0f, 0.0f, 1.0f), Color.Green);
vertices[3] = new VertexPositionColor(new Vector3(1.0f, 0.0f, -1.0f), Color.Pink);

■描画

graphics.GraphicsDevice.Clear(Color.CornflowerBlue);

basicEffect.Begin();
basicEffect.CurrentTechnique.Passes[0].Begin();
graphics.GraphicsDevice.VertexDeclaration = vertexDeclaration;
graphics.GraphicsDevice.DrawUserPrimitives<VertexPositionColor>(PrimitiveType.TriangleStrip, vertices, 0, 2);
basicEffect.CurrentTechnique.Passes[0].End();
basicEffect.End();

・設定するデータの型を指定

graphics.GraphicsDevice.VertexDeclaration = vertexDeclaration;

・モデルデータを設定します。

graphics.GraphicsDevice.DrawUserPrimitives<VertexPositionColor>(PrimitiveType.TriangleStrip, vertices, 0, 2);


■動かすと