Riadkové zobrazenie
súhrn
Kreslenie primitívov. Zobrazí sa riadok "LineList" oddelený pre každú postupnosť vrcholov a pripojená čiara "LineStrip".
Prevádzkové prostredie
Predpoklady
| Podporované verzie XNA | 
 | 
| Podporované platformy | 
 | 
| Windows vyžaduje verziu tieňovača vrcholov | 2.0 | 
| Windows vyžaduje verziu Pixel Shader | 2.0 | 
Prevádzkové prostredie
| nástupište | 
 | 
látka
Kreslenie čiary používa údaje vrcholov rovnakým spôsobom ako kreslenie bodov alebo mnohouholníkov, ale existujú dva vzory na kreslenie čiar. Ľavá strana obrázku nižšie je nakreslená pomocou "LineList" a pravá strana je nakreslená "LineStrip".
"LineList" nakreslí jednu čiaru s dvoma vrcholmi a čiary sa navzájom nespájajú.
"LineStrip" kreslí čiary spojené poradím vrcholov. Obaja dosahujú kresbu s rovnakým počtom vrcholov.
Ak chcete nakresliť čiaru, použite metódu GraphicsDevice.DrawPrimitives (alebo akúkoľvek inú metódu kreslenia) na určenie primitívneho typu: PrimitiveType.LineList a PrimitiveType.LineStrip.
Počet primitív v treťom argumente určuje počet riadkov, ktoré sa majú nakresliť, takže zadaný počet sa líši medzi LineList a LineStrip.
// LineList で描画する
this.GraphicsDevice.DrawPrimitives(PrimitiveType.LineList, 0, 3);
// LineStrip で描画する
this.GraphicsDevice.DrawPrimitives(PrimitiveType.LineStrip, 0, 5);
Všetky kódy
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
#if WINDOWS_PHONE
using Microsoft.Xna.Framework.Input.Touch;
#endif
namespace DrawLine
{
     <summary>
     ゲームメインクラス
     </summary>
    public class GameMain : Microsoft.Xna.Framework.Game
    {
         <summary>
         グラフィックデバイス管理クラス
         </summary>
        private GraphicsDeviceManager graphics = null;
         <summary>
         スプライトのバッチ化クラス
         </summary>
        private SpriteBatch spriteBatch = null;
         <summary>
         LineList 用頂点バッファ
         </summary>
        private VertexBuffer lineListVertexBuffer = null;
         <summary>
         LineStrip 用頂点バッファ
         </summary>
        private VertexBuffer lineStripVertexBuffer = null;
         <summary>
         基本エフェクト
         </summary>
        private BasicEffect basicEffect = null;
         <summary>
         GameMain コンストラクタ
         </summary>
        public GameMain()
        {
            // グラフィックデバイス管理クラスの作成
            this.graphics = new GraphicsDeviceManager(this);
            // ゲームコンテンツのルートディレクトリを設定
            this.Content.RootDirectory = "Content";
#if WINDOWS_PHONE
            // Windows Phone のデフォルトのフレームレートは 30 FPS
            this.TargetElapsedTime = TimeSpan.FromTicks(333333);
            // バックバッファサイズの設定
            this.graphics.PreferredBackBufferWidth = 480;
            this.graphics.PreferredBackBufferHeight = 800;
            // フルスクリーン表示
            this.graphics.IsFullScreen = true;
#endif
        }
         <summary>
         ゲームが始まる前の初期化処理を行うメソッド
         グラフィック以外のデータの読み込み、コンポーネントの初期化を行う
         </summary>
        protected override void Initialize()
        {
            // TODO: ここに初期化ロジックを書いてください
            // コンポーネントの初期化などを行います
            base.Initialize();
        }
         <summary>
         ゲームが始まるときに一回だけ呼ばれ
         すべてのゲームコンテンツを読み込みます
         </summary>
        protected override void LoadContent()
        {
            // テクスチャーを描画するためのスプライトバッチクラスを作成します
            this.spriteBatch = new SpriteBatch(this.GraphicsDevice);
            // エフェクトを作成
            this.basicEffect = new BasicEffect(this.GraphicsDevice);
            // エフェクトで頂点カラーを有効にする
            this.basicEffect.VertexColorEnabled = true;
            // ビューマトリックスをあらかじめ設定 ((0, 0, 15) から原点を見る)
            this.basicEffect.View = Matrix.CreateLookAt(
                    new Vector3(0.0f, 0.0f, 15.0f),
                    Vector3.Zero,
                    Vector3.Up
                );
            // プロジェクションマトリックスをあらかじめ設定
            this.basicEffect.Projection = Matrix.CreatePerspectiveFieldOfView(
                    MathHelper.ToRadians(45.0f),
                    (float)this.GraphicsDevice.Viewport.Width /
                        (float)this.GraphicsDevice.Viewport.Height,
                    1.0f,
                    100.0f
                );
            // 頂点の数
            int vertexCount = 6;
            // LineList 用頂点バッファ作成
            this.lineListVertexBuffer = new VertexBuffer(this.GraphicsDevice,
                typeof(VertexPositionColor), vertexCount, BufferUsage.None);
            // LineList 用頂点データを作成する
            VertexPositionColor[] lineListVertices = new VertexPositionColor[vertexCount];
            lineListVertices[0] = new VertexPositionColor(new Vector3(-2.5f, 2.5f, 0.0f),
                                                          Color.Red);
            lineListVertices[1] = new VertexPositionColor(new Vector3(-1.5f, 0.5f, 0.0f),
                                                          Color.Blue);
            lineListVertices[2] = new VertexPositionColor(new Vector3(-0.5f, 2.5f, 0.0f),
                                                          Color.Green);
            lineListVertices[3] = new VertexPositionColor(new Vector3(0.5f, 0.5f, 0.0f),
                                                          Color.Yellow);
            lineListVertices[4] = new VertexPositionColor(new Vector3(1.5f, 2.5f, 0.0f),
                                                          Color.Black);
            lineListVertices[5] = new VertexPositionColor(new Vector3(2.5f, 0.5f, 0.0f),
                                                          Color.White);
            // 頂点データを頂点バッファに書き込む
            this.lineListVertexBuffer.SetData(lineListVertices);
            // LineStrip 用頂点バッファ作成
            this.lineStripVertexBuffer = new VertexBuffer(this.GraphicsDevice,
                typeof(VertexPositionColor), vertexCount, BufferUsage.None);
            // LineStrip 用頂点データを作成する
            VertexPositionColor[] lineStripVertices = new VertexPositionColor[vertexCount];
            lineStripVertices[0] = new VertexPositionColor(new Vector3(-2.5f, -0.5f, 0.0f),
                                                           Color.Red);
            lineStripVertices[1] = new VertexPositionColor(new Vector3(-1.5f, -2.5f, 0.0f),
                                                           Color.Blue);
            lineStripVertices[2] = new VertexPositionColor(new Vector3(-0.5f, -0.5f, 0.0f),
                                                           Color.Green);
            lineStripVertices[3] = new VertexPositionColor(new Vector3(0.5f, -2.5f, 0.0f),
                                                           Color.Yellow);
            lineStripVertices[4] = new VertexPositionColor(new Vector3(1.5f, -0.5f, 0.0f),
                                                           Color.Black);
            lineStripVertices[5] = new VertexPositionColor(new Vector3(2.5f, -2.5f, 0.0f),
                                                           Color.White);
            // 頂点データを頂点バッファに書き込む
            this.lineStripVertexBuffer.SetData(lineStripVertices);
        }
         <summary>
         ゲームが終了するときに一回だけ呼ばれ
         すべてのゲームコンテンツをアンロードします
         </summary>
        protected override void UnloadContent()
        {
            // TODO: ContentManager で管理されていないコンテンツを
            //       ここでアンロードしてください
        }
         <summary>
         描画以外のデータ更新等の処理を行うメソッド
         主に入力処理、衝突判定などの物理計算、オーディオの再生など
         </summary>
         <param name="gameTime">このメソッドが呼ばれたときのゲーム時間</param>
        protected override void Update(GameTime gameTime)
        {
            // Xbox 360 コントローラ、Windows Phone の BACK ボタンを押したときに
            // ゲームを終了させます
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            {
                this.Exit();
            }
            // TODO: ここに更新処理を記述してください
            // 登録された GameComponent を更新する
            base.Update(gameTime);
        }
         <summary>
         描画処理を行うメソッド
         </summary>
         <param name="gameTime">このメソッドが呼ばれたときのゲーム時間</param>
        protected override void Draw(GameTime gameTime)
        {
            // 画面を指定した色でクリアします
            this.GraphicsDevice.Clear(Color.CornflowerBlue);
            // 描画に使用する頂点バッファをセットします
            this.GraphicsDevice.SetVertexBuffer(this.lineListVertexBuffer);
            // パスの数だけ繰り替えし描画
            foreach (EffectPass pass in this.basicEffect.CurrentTechnique.Passes)
            {
                // パスの開始
                pass.Apply();
                // LineList で描画する
                this.GraphicsDevice.DrawPrimitives(PrimitiveType.LineList, 0, 3);
            }
            // 描画に使用する頂点バッファをセットします
            this.GraphicsDevice.SetVertexBuffer(this.lineStripVertexBuffer);
            // パスの数だけ繰り替えし描画
            foreach (EffectPass pass in this.basicEffect.CurrentTechnique.Passes)
            {
                // パスの開始
                pass.Apply();
                // LineStrip で描画する
                this.GraphicsDevice.DrawPrimitives(PrimitiveType.LineStrip, 0, 5);
            }
            // 登録された DrawableGameComponent を描画する
            base.Draw(gameTime);
        }
    }
}