続・男は黙ってナマポリゴン

■座標がわかりにくい!
前回の「男は黙ってナマポリゴン 」では、頂点の座標が画面に対して -1.0 〜 1.0 で指定するので解りにくいという指摘を受け、以下のような変更になりました。

namespace WindowsGame
{
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        ContentManager content;

        VertexPositionColor[] vertices =
        {
            new VertexPositionColor( new Vector3(   0.0f,   0.0f, 0.0f ),new Color(255, 255,  50, 100) ),
            new VertexPositionColor( new Vector3( 400.0f,  20.0f, 0.0f ),new Color(255, 255,  50, 255) ),
            new VertexPositionColor( new Vector3(  20.0f, 200.0f, 0.0f ),new Color(255, 255, 255, 100) ),
        };
        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)
        {
            graphics.GraphicsDevice.Clear(Color.CornflowerBlue);

            basicEffect.Begin();
            basicEffect.CurrentTechnique.Passes[0].Begin();

            graphics.GraphicsDevice.VertexDeclaration = vdecl;
            graphics.GraphicsDevice.DrawUserPrimitives<VertexPositionColor>(
                PrimitiveType.TriangleList, vertices, 0, 1);

            basicEffect.CurrentTechnique.Passes[0].End();
            basicEffect.End();

            base.Draw(gameTime);
        }
    }
}

■ソース説明
頂点を設定します。画面左上が ( 0, 0 )として設定します。

 VertexPositionColor[] vertices =
 {
     new VertexPositionColor( new Vector3(   0.0f,   0.0f, 0.0f ),new Color(255, 255,  50, 100) ),
     new VertexPositionColor( new Vector3( 400.0f,  20.0f, 0.0f ),new Color(255, 255,  50, 255) ),
     new VertexPositionColor( new Vector3(  20.0f, 200.0f, 0.0f ),new Color(255, 255, 255, 100) ),
 };

「World」と「View」のマトリックスを設定します。これにより画面の座標と一致します。

 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 );                          // ファークリップの距離

原点( 0, 0 )を画面中央に設定したい場合は以下のように変更してください。

 //原点( 0, 0 )を画面中央に設定
 basicEffect.Projection = Matrix.CreateOrthographic(
     this.Window.ClientBounds.Width,   // Window幅
     this.Window.ClientBounds.Height,  // Window高さ
     0.0f,                            // ニアクリップの距離
     1.0f );                           // ファークリップの距離

なにも画面サイズに変更を加えていないのなら

this.Window.ClientBounds.Width :800
this.Window.ClientBounds.Height :600

となっています。
■実行すると以下のような画面が現れれば成功です。

(サポート:神林)