Afișarea poligoanelor adiacente

Pagina actualizată :
Data creării paginii :

rezumat

Desenarea unui poligon continuu. Există două moduri de a afișa poligoane adiacente.

連続したポリゴンの表示

Mediul de operare

Cerințe preliminare

Versiuni XNA acceptate
  • 4.0
Platforme acceptate
  • Windows (XP SP2 sau o versiune ulterioară, Vista, 7)
  • Xbox 360
  • Windows Phone 7
Versiunea Vertex Shader necesară pentru Windows 2.0
Versiunea Pixel Shader necesară pentru Windows 2.0

Mediul de operare

peron
  • Windows 7
  • Xbox 360
  • Windows Phone 7 Emulator

substanță

Dacă utilizați patru sau mai multe noduri, puteți desena poligoane consecutiv. Există două moduri de a desena ca model: "TriangleList" și "TriangleStrip". Apropo, "TriangleFan", care era până la XNA Framework 3.1, a dispărut. Acest lucru se datorează specificațiilor DirectX 10 și versiunilor ulterioare.

Listă triunghiulară

TriangleList

"TriangleList" desenează un poligon triunghiular pentru fiecare trei. Acesta este modelul cel mai frecvent utilizat, cum ar fi forma unui model complex.

Bandă triunghiulară

TriangleStrip

"TriangleStrip" este o formă care desenează poligoane triunghiulare alternativ. Gândiți-vă la asta ca la desenarea poligoanelor înfășurându-le unul după altul. Când este desenat cu un TriangleStrip, poligonul par este orientat înapoi, astfel încât conducta de desen compensează acest lucru și îl desenează.

Acest model este uneori folosit atunci când poligoanele sunt aliniate în mod regulat, cum ar fi în câmpuri sau clădiri.

desen

Pentru a desena poligoane în fiecare model, utilizați metoda GraphicsDevice.DrawPrimitives (sau orice altă metodă de desen) pentru a specifica tipul primitiv: PrimitiveType.TriangleList și, respectiv, PrimitiveType.TriangleStrip.

Numărul de primitive din al treilea argument specifică numărul de poligoane care trebuie desenate, astfel încât numărul specificat diferă în funcție de tipul de primitivă.

// TriangleList で描画する
this.GraphicsDevice.DrawPrimitives(PrimitiveType.TriangleList, 0, 2);
// TriangleStrip で描画する
this.GraphicsDevice.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 4);

Toate codurile

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 SequencingTriangle
{
    /// <summary>
    /// ゲームメインクラス
    /// </summary>
    public class GameMain : Microsoft.Xna.Framework.Game
    {
        /// <summary>
        /// グラフィックデバイス管理クラス
        /// </summary>
        private GraphicsDeviceManager graphics = null;

        /// <summary>
        /// スプライトのバッチ化クラス
        /// </summary>
        private SpriteBatch spriteBatch = null;

        /// <summary>
        /// TriangleList 用頂点バッファ
        /// </summary>
        private VertexBuffer triangleListVertexBuffer = null;

        /// <summary>
        /// TriangleStrip 用頂点バッファ
        /// </summary>
        private VertexBuffer triangleStripVertexBuffer = 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, 12) から原点を見る)
            this.basicEffect.View = Matrix.CreateLookAt(
                    new Vector3(0.0f, 0.0f, 12.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
                );

            // TriangleList 用頂点バッファ作成
            this.triangleListVertexBuffer = new VertexBuffer(this.GraphicsDevice,
                VertexPositionColor.VertexDeclaration, 6, BufferUsage.None);

            // TriangleList 用頂点データを作成する
            VertexPositionColor[] triangleListVertices = new VertexPositionColor[6];

            triangleListVertices[0] = new VertexPositionColor(new Vector3(-2.5f, 0.5f, 0.0f),
                                                              Color.Blue);
            triangleListVertices[1] = new VertexPositionColor(new Vector3(-1.5f, 2.5f, 0.0f),
                                                              Color.Green);
            triangleListVertices[2] = new VertexPositionColor(new Vector3(-0.5f, 0.5f, 0.0f),
                                                              Color.Red);
            triangleListVertices[3] = new VertexPositionColor(new Vector3(0.5f, 0.5f, 0.0f),
                                                              Color.Yellow);
            triangleListVertices[4] = new VertexPositionColor(new Vector3(1.5f, 2.5f, 0.0f),
                                                              Color.Black);
            triangleListVertices[5] = new VertexPositionColor(new Vector3(2.5f, 0.5f, 0.0f),
                                                              Color.White);

            // 頂点データを頂点バッファに書き込む
            this.triangleListVertexBuffer.SetData(triangleListVertices);

            // TriangleStrip 用頂点バッファ作成
            this.triangleStripVertexBuffer = new VertexBuffer(this.GraphicsDevice,
                VertexPositionColor.VertexDeclaration, 6, BufferUsage.None);

            // TriangleStrip 用頂点データを作成する
            VertexPositionColor[] triangleStripVertices = new VertexPositionColor[6];

            triangleStripVertices[0] = new VertexPositionColor(new Vector3(-2.5f, -2.5f, 0.0f),
                                                               Color.Red);
            triangleStripVertices[1] = new VertexPositionColor(new Vector3(-1.5f, -0.5f, 0.0f),
                                                               Color.Blue);
            triangleStripVertices[2] = new VertexPositionColor(new Vector3(-0.5f, -2.5f, 0.0f),
                                                               Color.Green);
            triangleStripVertices[3] = new VertexPositionColor(new Vector3(0.5f, -0.5f, 0.0f),
                                                               Color.Yellow);
            triangleStripVertices[4] = new VertexPositionColor(new Vector3(1.5f, -2.5f, 0.0f),
                                                               Color.Black);
            triangleStripVertices[5] = new VertexPositionColor(new Vector3(2.5f, -0.5f, 0.0f),
                                                               Color.White);

            // 頂点データを頂点バッファに書き込む
            this.triangleStripVertexBuffer.SetData(triangleStripVertices);
        }

        /// <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.triangleListVertexBuffer);

            // パスの数だけ繰り替えし描画
            foreach (EffectPass pass in this.basicEffect.CurrentTechnique.Passes)
            {
                // パスの開始
                pass.Apply();
                
                // TriangleList で描画する
                this.GraphicsDevice.DrawPrimitives(PrimitiveType.TriangleList, 0, 2);
            }

            // 描画に使用する頂点バッファをセットします
            this.GraphicsDevice.SetVertexBuffer(this.triangleStripVertexBuffer);
            
            // パスの数だけ繰り替えし描画
            foreach (EffectPass pass in this.basicEffect.CurrentTechnique.Passes)
            {
                // パスの開始
                pass.Apply();
                
                // TriangleStrip で描画する
                this.GraphicsDevice.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 4);
            }

            // 登録された DrawableGameComponent を描画する
            base.Draw(gameTime);
        }
    }
}