スキスキ、ライン文

ソリッドポリゴンやラインだけで、3Dゲームを作るのが好きな私にとって欠かせない部分です。

namespace WindowsGame
{
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        ContentManager content;
        private const int VERTICES_SIZE = 2;
        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 lineColor = new Color(0, 0, 0);

            graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
            for( int i = 0 ; i < this.Window.ClientBounds.Height ; i+=10 )
                Line(0, 0, this.Window.ClientBounds.Width, i, lineColor );
            base.Draw(gameTime);
        }

        public void Line(float linePosX1, float linePosY1, float linePosX2, float linePosY2, Color lineColor)
        {
            vertices[0].Position.X = linePosX1;
            vertices[0].Position.Y = linePosY1;
            vertices[0].Position.Z = 0;
            vertices[0].Color = lineColor;

            vertices[1].Position.X = linePosX2;
            vertices[1].Position.Y = linePosY2;
            vertices[1].Position.Z = 0;
            vertices[1].Color = lineColor;

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

■結果、以下のような画面になります。