
※この Tips は XNA Game Studio 2.0 をベースに説明しています。
4つ以上の頂点を使用した場合、ポリゴンを連続して描画することができます。そのパターンとして「TriangleList」「TriangleStrip」「TriangleFan」の3つのパターンで描画する方法があります。
TriangleList

「TriangleList」は3つのごとに三角形ポリゴンをひとつ描画します。複雑なモデルの形状など、もっともよく使われるパターンです。
TriangleStrip

「TriangleStrip」は交互に三角形ポリゴンを描画する形になります。ポリゴンを次々と折り返して描画するようなイメージだと思ってください。TriangleStrip で描画した場合、偶数枚目のポリゴンが裏を向いてしまうので描画パイプラインはこれを補正して描画するようにしています。
このパターンはフィールドや建物など規則的にポリゴンが並ぶ場合に使用されることがあります。
TriangleFan

「TriangleFan」は最初の頂点を常に共有する形でポリゴンを描画していきます。扇みたいな形になることから Fan という名前が付いています。このパターンはほとんど使われることがなく、DirectX10 では存在しないので使わない方がいいでしょう。
それぞれのパターンでポリゴンを描画するには、「GraphicsDevice.DrawPrimitives」メソッド(またはその他の描画メソッド)で、プリミティブの種類の指定に「PrimitiveType.TriangleList」「PrimitiveType.TriangleStrip」「PrimitiveType.TriangleFan」をそれぞれ指定します。
第3引数のプリミティブ数は描画するポリゴンの数を指定するので、プリミティブの種類によって指定する数が違います。
// TriangleList で描画する this.GraphicsDevice.DrawPrimitives(PrimitiveType.TriangleList, 0, 2);
// TriangleStrip で描画する this.GraphicsDevice.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 4);
// TriangleFan で描画する this.GraphicsDevice.DrawPrimitives(PrimitiveType.TriangleFan, 0, 4);
| ファイル | サイズ | 対応XNAバージョン | プラットフォーム | 作成日 |
|---|---|---|---|---|
| xna_tips_sequencingtriangle_3_0_exe.zip | 6.7 KB | 3.0 | Windows (XP SP2 以降, Vista) | 2009/01/04 |
| xna_tips_sequencingtriangle_2_0_exe.zip | 6.6 KB | 2.0 | Windows (XP SP2, Vista) | 2008/01/02 |
| xna_tips_sequencingtriangle_1_0_ref_exe.zip | 6.4 KB | 1.0 Refresh | Windows (XP SP2, Vista) | 2007/09/15 |
| ファイル | サイズ | 対応XNAバージョン | プラットフォーム | 作成日 |
|---|---|---|---|---|
| xna_tips_sequencingtriangle_3_0_project.zip | 22.4 KB | 3.0 | Windows (XP SP2 以降, Vista), Xbox 360 | 2009/01/04 |
| xna_tips_sequencingtriangle_2_0_project.zip | 20.7 KB | 2.0 | Windows (XP SP2, Vista), Xbox 360 | 2008/01/02 |
| xna_tips_sequencingtriangle_1_0_ref_project.zip | 21.2 KB | 1.0 Refresh | Windows (XP SP2, Vista), Xbox 360 | 2007/09/15 |
using System; using System.Collections.Generic; 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.Net; using Microsoft.Xna.Framework.Storage; namespace SequencingTriangle { /// <summary> /// ゲームメインクラス /// </summary> public class GameMain : Microsoft.Xna.Framework.Game { /// <summary> /// グラフィックデバイス管理クラス /// </summary> private GraphicsDeviceManager graphics = null; /// <summary> /// スプライトのバッチ化クラス /// </summary> private SpriteBatch spriteBatch = null; /// <summary> /// 頂点データ定義 /// </summary> private VertexDeclaration vertexDeclaration = null; /// <summary> /// TriangleList 用頂点バッファ /// </summary> private VertexBuffer triangleListVertexBuffer = null; /// <summary> /// TriangleStrip 用頂点バッファ /// </summary> private VertexBuffer triangleStripVertexBuffer = null; /// <summary> /// TriangleFan 用頂点バッファ /// </summary> private VertexBuffer triangleFanVertexBuffer = null; /// <summary> /// 基本エフェクト /// </summary> private BasicEffect basicEffect = null; /// <summary> /// GameMain コンストラクタ /// </summary> public GameMain() { // グラフィックデバイス管理クラスの作成 this.graphics = new GraphicsDeviceManager(this); // ゲームコンテンツのルートディレクトリを設定 this.Content.RootDirectory = "Content"; } /// <summary> /// ゲームが始まる前の初期化処理を行うメソッド /// グラフィック以外のデータの読み込み、コンポーネントの初期化を行う /// </summary> protected override void Initialize() { // TODO: ここに初期化ロジックを書いてください // コンポーネントの初期化などを行います base.Initialize(); } /// <summary> /// ゲームが始まるときに一回だけ呼ばれ /// すべてのゲームコンテンツを読み込みます /// </summary> protected override void LoadContent() { // テクスチャーを描画するためのスプライトバッチクラスを作成します this.spriteBatch = new SpriteBatch(this.GraphicsDevice); // 頂点定義データを作成 this.vertexDeclaration = new VertexDeclaration( this.GraphicsDevice, VertexPositionColor.VertexElements); // エフェクトを作成 this.basicEffect = new BasicEffect(this.GraphicsDevice, null); // エフェクトで頂点カラーを有効にする this.basicEffect.VertexColorEnabled = true; // ビューマトリックスをあらかじめ設定 ((0, 0, 10) から原点を見る) this.basicEffect.View = Matrix.CreateLookAt( new Vector3(0.0f, 0.0f, 10.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.SizeInBytes * 6, BufferUsage.None); // TriangleList 用頂点データを作成する VertexPositionColor[] triangleListVertices = new VertexPositionColor[6]; triangleListVertices[0] = new VertexPositionColor(new Vector3(-4.5f, 0.5f, 0.0f), Color.Blue); triangleListVertices[1] = new VertexPositionColor(new Vector3(-3.5f, 2.5f, 0.0f), Color.Green); triangleListVertices[2] = new VertexPositionColor(new Vector3(-2.5f, 0.5f, 0.0f), Color.Red); triangleListVertices[3] = new VertexPositionColor(new Vector3(-1.5f, 0.5f, 0.0f), Color.Yellow); triangleListVertices[4] = new VertexPositionColor(new Vector3(-0.5f, 2.5f, 0.0f), Color.Black); triangleListVertices[5] = new VertexPositionColor(new Vector3(0.5f, 0.5f, 0.0f), Color.White); // 頂点データを頂点バッファに書き込む this.triangleListVertexBuffer.SetData<VertexPositionColor>(triangleListVertices); // TriangleStrip 用頂点バッファ作成 this.triangleStripVertexBuffer = new VertexBuffer(this.GraphicsDevice, VertexPositionColor.SizeInBytes * 6, BufferUsage.None); // TriangleStrip 用頂点データを作成する VertexPositionColor[] triangleStripVertices = new VertexPositionColor[6]; triangleStripVertices[0] = new VertexPositionColor(new Vector3(-4.5f, -2.5f, 0.0f), Color.Red); triangleStripVertices[1] = new VertexPositionColor(new Vector3(-3.5f, -0.5f, 0.0f), Color.Blue); triangleStripVertices[2] = new VertexPositionColor(new Vector3(-2.5f, -2.5f, 0.0f), Color.Green); triangleStripVertices[3] = new VertexPositionColor(new Vector3(-1.5f, -0.5f, 0.0f), Color.Yellow); triangleStripVertices[4] = new VertexPositionColor(new Vector3(-0.5f, -2.5f, 0.0f), Color.Black); triangleStripVertices[5] = new VertexPositionColor(new Vector3(0.5f, -0.5f, 0.0f), Color.White); // 頂点データを頂点バッファに書き込む this.triangleStripVertexBuffer.SetData<VertexPositionColor>(triangleStripVertices); // TriangleFan 用頂点バッファ作成 this.triangleFanVertexBuffer = new VertexBuffer(this.GraphicsDevice, VertexPositionColor.SizeInBytes * 6, BufferUsage.None); // TriangleFan 用頂点データを作成する VertexPositionColor[] triangleFanVertices = new VertexPositionColor[6]; triangleFanVertices[0] = new VertexPositionColor(new Vector3(2.0f, 0.0f, 0.0f), Color.Red); triangleFanVertices[1] = new VertexPositionColor(new Vector3(2.0f, 2.0f, 0.0f), Color.Blue); triangleFanVertices[2] = new VertexPositionColor(new Vector3(3.0f, 1.5f, 0.0f), Color.Green); triangleFanVertices[3] = new VertexPositionColor(new Vector3(3.5f, 0.5f, 0.0f), Color.Yellow); triangleFanVertices[4] = new VertexPositionColor(new Vector3(3.5f, -0.5f, 0.0f), Color.Black); triangleFanVertices[5] = new VertexPositionColor(new Vector3(3.0f, -1.5f, 0.0f), Color.White); // 頂点データを頂点バッファに書き込む this.triangleFanVertexBuffer.SetData<VertexPositionColor>(triangleFanVertices); } /// <summary> /// ゲームが終了するときに一回だけ呼ばれ /// すべてのゲームコンテンツをアンロードします /// </summary> protected override void UnloadContent() { // TODO: ContentManager で管理されていないコンテンツを // ここでアンロードしてください } /// <summary> /// 描画以外のデータ更新等の処理を行うメソッド /// 主に入力処理、衝突判定などの物理計算、オーディオの再生など /// </summary> /// <param name="gameTime">このメソッドが呼ばれたときのゲーム時間</param> protected override void Update(GameTime gameTime) { // Xbox360 コントローラの 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.VertexDeclaration = this.vertexDeclaration; // TriangleList 用の頂点バッファをセットします this.GraphicsDevice.Vertices[0].SetSource( this.triangleListVertexBuffer, 0, VertexPositionColor.SizeInBytes); // エフェクトの使用を開始します this.basicEffect.Begin(); // パスの数だけ繰り替えし描画 foreach (EffectPass pass in this.basicEffect.CurrentTechnique.Passes) { // パスの開始 pass.Begin(); // TriangleList で描画する this.GraphicsDevice.DrawPrimitives(PrimitiveType.TriangleList, 0, 2); // パスの終了 pass.End(); } // エフェクトの使用を終了する this.basicEffect.End(); // TriangleStrip 用の頂点バッファをセットします this.GraphicsDevice.Vertices[0].SetSource( this.triangleStripVertexBuffer, 0, VertexPositionColor.SizeInBytes); // エフェクトの使用を開始します this.basicEffect.Begin(); // パスの数だけ繰り替えし描画 foreach (EffectPass pass in this.basicEffect.CurrentTechnique.Passes) { // パスの開始 pass.Begin(); // TriangleStrip で描画する this.GraphicsDevice.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 4); // パスの終了 pass.End(); } // エフェクトの使用を終了する this.basicEffect.End(); // TriangleFan 用の頂点バッファをセットします this.GraphicsDevice.Vertices[0].SetSource( this.triangleFanVertexBuffer, 0, VertexPositionColor.SizeInBytes); // エフェクトの使用を開始します this.basicEffect.Begin(); // パスの数だけ繰り替えし描画 foreach (EffectPass pass in this.basicEffect.CurrentTechnique.Passes) { // パスの開始 pass.Begin(); // TriangleFan で描画する this.GraphicsDevice.DrawPrimitives(PrimitiveType.TriangleFan, 0, 4); // パスの終了 pass.End(); } // エフェクトの使用を終了する this.basicEffect.End(); // 登録された DrawableGameComponent を描画する base.Draw(gameTime); } } }
| 更新日時 | 更新内容 |
|---|---|
| 2009/01/04 | XNA Game Studio 3.0 のプロジェクト追加 |
| 2008/05/18 | 文章・プログラムの校正 |
| 2008/01/02 | XNA Game Studio 2.0 用に修正 |
| 2007/09/15 | ページ作成 |