3Dでポリゴン表示(テクスチャ)

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

■ソース

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

        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 = false;
            basicEffect.TextureEnabled = true;
            // 頂点データ定義を作成
            vertexDeclaration = new VertexDeclaration(graphics.GraphicsDevice, VertexPositionTexture.VertexElements);
            // 頂点データを作成する
            vertices = new VertexPositionTexture[4];
            vertices[0] = new VertexPositionTexture(new Vector3(-1.0f, 0.0f, 1.0f), new Vector2(0.0f, 1.0f));
            vertices[1] = new VertexPositionTexture(new Vector3(-1.0f, 0.0f, -1.0f), new Vector2(0.0f, 0.0f));
            vertices[2] = new VertexPositionTexture(new Vector3(1.0f, 0.0f, 1.0f), new Vector2(1.0f, 1.0f));
            vertices[3] = new VertexPositionTexture(new Vector3(1.0f, 0.0f, -1.0f), new Vector2(1.0f, 0.0f));
            dAng = 0;
            base.Initialize();
        }

        protected override void LoadContent()
        {
            texture = Content.Load<Texture2D>("mimi");
        }

        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.Textures[0] = texture;
            graphics.GraphicsDevice.VertexDeclaration = vertexDeclaration;
            graphics.GraphicsDevice.DrawUserPrimitives<VertexPositionTexture>(PrimitiveType.TriangleStrip, vertices, 0, 2);
            basicEffect.CurrentTechnique.Passes[0].End();
            basicEffect.End();

            base.Draw(gameTime);
        }
    }
}

■設定
今回は頂点カラーは使用ぜず、テクスチャを使うので以下のような設定をしておきます。

basicEffect.VertexColorEnabled = false;
basicEffect.TextureEnabled = true;

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

private VertexDeclaration vertexDeclaration = null;
private VertexPositionTexture[] vertices = null;
private Texture2D texture = null;

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

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

■描画

graphics.GraphicsDevice.Clear(Color.CornflowerBlue);

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

・テクスチャ指定

graphics.GraphicsDevice.Textures[0] = texture;

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

graphics.GraphicsDevice.VertexDeclaration = vertexDeclaration;

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

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


■動かすと

■アルファを抜きたい場合は、描画の時に以下の3行を足して

graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
graphics.GraphicsDevice.RenderState.AlphaBlendEnable = true;
graphics.GraphicsDevice.RenderState.SourceBlend = Blend.SourceAlpha;
graphics.GraphicsDevice.RenderState.DestinationBlend = Blend.InverseSourceAlpha;

basicEffect.Begin();
   :
   :
basicEffect.End();

こんな感じです。