もっとナマポリゴン-その2

もっとナマポリゴン」と同じような結果になるのですが、表示させる方法を変えてみました。VIEWを変更するのではなく、頂点を設定するバッファを書き換え登録する方法で表示させてみました。

namespace WindowsGame
{
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        ContentManager content;
        private const int VERTICES_SIZE = 4;    // リテラルは原則として使用しない
        VertexPositionColor[] vertices = new VertexPositionColor[VERTICES_SIZE];
        VertexDeclaration vdecl;
        BasicEffect basicEffect;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            content = new ContentManager(Services);
        }

        protected override void Initialize()
        {
            vdecl = new VertexDeclaration(graphics.GraphicsDevice, VertexPositionColor.VertexElements);
            basicEffect = new BasicEffect(graphics.GraphicsDevice, null);
            basicEffect.VertexColorEnabled = true;

            basicEffect.World = Matrix.Identity;
            basicEffect.View = Matrix.Identity;
            //原点( 0, 0 )を画面右上に設定
            basicEffect.Projection = Matrix.CreateOrthographicOffCenter(
                0.0f,                            // 左座標
                this.Window.ClientBounds.Width,  // 右座標 Window幅
                this.Window.ClientBounds.Height, // 下座標 Window高さ
                0.0f,              // 上座標
                0.0f,                            // ニアクリップの距離
                1.0f);                         // ファークリップの距離
            base.Initialize();
        }

        protected override void Draw(GameTime gameTime)
        {
            Color boxColor = new Color(50, 100, 50);

            graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
            for (int iy = 5; iy < this.Window.ClientBounds.Height; iy += 100)
            {
                for (int ix = 5; ix < this.Window.ClientBounds.Width; ix += 100)
                {
                    Box( ix , iy , 90, 90, boxColor);
                }
            }
            base.Draw(gameTime);
        }

        public void Box(float boxPosX, float boxPosY, float boxWidth, float boxHeight, Color boxColor)
        {
            vertices[0].Position.X = boxPosX;
            vertices[0].Position.Y = boxPosY;
            vertices[0].Position.Z = 0;
            vertices[0].Color = boxColor;

            vertices[1].Position.X = boxPosX + boxWidth;
            vertices[1].Position.Y = boxPosY;
            vertices[1].Position.Z = 0;
            vertices[1].Color = boxColor;

            vertices[2].Position.X = boxPosX;
            vertices[2].Position.Y = boxPosY + boxHeight;
            vertices[2].Position.Z = 0;
            vertices[2].Color = boxColor;

            vertices[3].Position.X = boxPosX + boxWidth;
            vertices[3].Position.Y = boxPosY + boxHeight;
            vertices[3].Position.Z = 0;
            vertices[3].Color = boxColor;

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

この方法はCPUですべて計算するのでプログラム的な小回りがききますが、メモリーのコピーが多くなりパフォーマンスに問題があるのではないかと思っています。(こんど、時間があるときにパフォーマンスレビューをしたいと思います。)