
※この Tips は XNA Game Studio 2.0 をベースに説明しています。
ポリゴンを描画するとき、「頂点バッファ」というものを使用して描画することができます。頂点バッファにはポリゴンなどのプリミティブを描画する際に使用する頂点データを登録することができます。頂点バッファを使用すると頂点データをビデオメモリ上に配置することができ、描画パイプラインはビデオメモリ上のデータからアクセスした方が、通常のメインメモリからアクセスするようりも非常に高速にデータのやり取りができるため、描画速度の向上が見込めます。
今回のようにポリゴン1枚の場合ですと、高速に描画できているという実感はできませんが、数千、数万のポリゴンを描画する場合にその効果は目に見えるかと思います。
/// <summary> /// 頂点バッファ /// </summary> private VertexBuffer vertexBuffer = null;
頂点バッファを使用するには「VertexBuffer」クラスを使用します。ユーザー定義の頂点データを描画時に毎回パイプラインに送って描画していた場合は、頂点データを常に保持しておかなければいけなかったのですが、頂点バッファを作成した場合は頂点バッファが頂点データを管理するのでわざわざ頂点データを保持しておく必要はありません。
// 頂点の数 int vertexCount = 3; // 頂点バッファ作成 this.vertexBuffer = new VertexBuffer(this.GraphicsDevice, VertexPositionColor.SizeInBytes * vertexCount, BufferUsage.None);
LoadGraphicsContent メソッド内で VertexBuffer を作成しています。
第1引数には GraphicsDevice を指定します。
第2引数には作成する頂点データのトータルバイトサイズ(byte)を指定します。今回描画するポリゴンは「位置」と「色」の情報を持つ頂点データ(VertexPositionColor)を使用しますが、「VertexPositionColor.SizeInBytes」プロパティで頂点データのバイトサイズが取得できるのでそれを使用しています。
さらに三角形ポリゴンを1枚作成には頂点データを3つ必要とするので、頂点データのサイズに頂点数をかけています。
第3引数には頂点バッファ使用法を指定しますが、特に何もなければ「BufferUsage.None」を指定できます。
| VertexBuffer コンストラクタ | ||
|---|---|---|
| 頂点データを作成するためのクラス「VertexBuffer」のインスタンスを作成します。 | ||
| graphicsDevice | GraphicsDevice | 頂点バッファと関連付ける GraphicsDevice を指定します。 |
| sizeInBytes | int | 頂点バッファに割り当てるバイトサイズを指定します。通常ひとつの頂点データのサイズ×使用する頂点数を指定します。 |
| usage | BufferUsage | 頂点バッファの使用法を指定します。特に何もない場合は「BufferUsage.None」を指定します。 |
| MSDN ライブラリの Web ページへリンク | ||
// 頂点データを作成する VertexPositionColor[] pointList = new VertexPositionColor[vertexCount]; pointList[0] = new VertexPositionColor(new Vector3(0.0f, 3.0f, 0.0f), Color.Red); pointList[1] = new VertexPositionColor(new Vector3(3.0f, -2.0f, 0.0f), Color.Blue); pointList[2] = new VertexPositionColor(new Vector3(-3.0f, -2.0f, 0.0f), Color.Green); // 頂点データを頂点バッファに書き込む this.vertexBuffer.SetData<VertexPositionColor>(pointList);
頂点バッファを作成したら、頂点バッファに頂点データをセットします。頂点データをセットするには「VertexBuffer.SetData」メソッドを使用します。
一度頂点バッファにセットした頂点データは頂点バッファに管理されるので、もう使用することがないのであればそのまま破棄してかまいません。
| VertexBuffer.SetData メソッド | ||
|---|---|---|
| 頂点バッファに頂点データをセットします。 | ||
| T | ValueType | 使用する頂点データの構造体を指定します。 |
| data | T[] | 頂点バッファにセットする頂点データの配列を指定します。頂点バッファに指定したサイズとセットする頂点データのサイズは一致させる必要があります。 |
| MSDN ライブラリの Web ページへリンク | ||
// 描画する頂点データの定義を設定 this.GraphicsDevice.VertexDeclaration = this.vertexDeclaration; // 描画に使用する頂点バッファをセットします this.GraphicsDevice.Vertices[0].SetSource( this.vertexBuffer, 0, VertexPositionColor.SizeInBytes); // エフェクトの使用を開始します this.basicEffect.Begin(); // パスの数だけ繰り返し描画 foreach (EffectPass pass in this.basicEffect.CurrentTechnique.Passes) { // パスの開始 pass.Begin(); // 三角形を描画する this.GraphicsDevice.DrawPrimitives(PrimitiveType.TriangleList, 0, 1); // パスの終了 pass.End(); } // エフェクトの使用を終了する this.basicEffect.End();
ポリゴンを描画するために、描画に使用する頂点バッファをセットします。GraphicsDevice には「VertexStreamCollection」プロパティがあり、複数の「VertexStream」を使用できます。ここでは最初のインデックスを使用して「SetSource」メソッドで頂点バッファをセットします。
第1引数にはセットする頂点バッファを指定します。
第2引数は何バイト目から頂点バッファを使用するかを指定しますが通常は 0 でいいです。
第3引数にはひとつの頂点のサイズを指定します。
| VertexStream.SetSource メソッド | ||
|---|---|---|
| 頂点ストリームに頂点バッファをセットします。 | ||
| vb | VertexBuffer | セットする頂点バッファ |
| offsetInBytes | int | 頂点バッファの何バイト目から使用するかを指定する。 |
| vertexStride | int | ひとつの頂点データのバイトサイズ |
| MSDN ライブラリの Web ページへリンク | ||
ちなみに GraphicsDevice が複数の頂点ストリームを持っているのは、描画時に一度に複数の頂点データを使用することができるからです。ただし、実行しているPCの環境によって最大数が違うので、複数の頂点バッファを同時に使う場合は事前に使用できるか調べる必要があります。
実際のポリゴンの描画には「GraphicsDevice.DrawPrimitives」メソッドを使用します。頂点バッファを使用する場合はにこのメソッドで描画することになります。
第1引数に描画するプリミティブの種類、第2引数に描画開始頂点インデックス、第3引数に描画するプリミティブの数を指定します。
| GraphicsDevice.DrawPrimitives メソッド | ||
|---|---|---|
| 指定された頂点バッファを使用してプリミティブを描画します。 | ||
| primitiveType | PrimitiveType | 描画するプリミティブの種類を指定します |
| startVertex | int | 読み込む頂点の最初のインデックス |
| primitiveCount | int | 描画するプリミティブの数 |
| MSDN ライブラリの Web ページへリンク | ||
| ファイル | サイズ | 対応XNAバージョン | プラットフォーム | 作成日 |
|---|---|---|---|---|
| xna_tips_vertexbuffertriangle_3_0_exe.zip | 6.5 KB | 3.0 | Windows (XP SP2 以降, Vista) | 2009/01/04 |
| xna_tips_vertexbuffertriangle_2_0_exe.zip | 6.3 KB | 2.0 | Windows (XP SP2, Vista) | 2008/01/02 |
| xna_tips_vertexbuffertriangle_1_0_ref_exe.zip | 6.2 KB | 1.0 Refresh | Windows (XP SP2, Vista) | 2007/09/01 |
| ファイル | サイズ | 対応XNAバージョン | プラットフォーム | 作成日 |
|---|---|---|---|---|
| xna_tips_vertexbuffertriangle_3_0_project.zip | 21.0 KB | 3.0 | Windows (XP SP2 以降, Vista), Xbox 360 | 2009/01/04 |
| xna_tips_vertexbuffertriangle_2_0_project.zip | 19.3 KB | 2.0 | Windows (XP SP2, Vista), Xbox 360 | 2008/01/02 |
| xna_tips_vertexbuffertriangle_1_0_ref_project.zip | 19.9 KB | 1.0 Refresh | Windows (XP SP2, Vista), Xbox 360 | 2007/09/01 |
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 VertexBufferTriangle { /// <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> /// 頂点バッファ /// </summary> private VertexBuffer vertexBuffer = 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 ); // 頂点の数 int vertexCount = 3; // 頂点バッファ作成 this.vertexBuffer = new VertexBuffer(this.GraphicsDevice, VertexPositionColor.SizeInBytes * vertexCount, BufferUsage.None); // 頂点データを作成する VertexPositionColor[] pointList = new VertexPositionColor[vertexCount]; pointList[0] = new VertexPositionColor(new Vector3(0.0f, 3.0f, 0.0f), Color.Red); pointList[1] = new VertexPositionColor(new Vector3(3.0f, -2.0f, 0.0f), Color.Blue); pointList[2] = new VertexPositionColor(new Vector3(-3.0f, -2.0f, 0.0f), Color.Green); // 頂点データを頂点バッファに書き込む this.vertexBuffer.SetData<VertexPositionColor>(pointList); } /// <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; // 描画に使用する頂点バッファをセットします this.GraphicsDevice.Vertices[0].SetSource( this.vertexBuffer, 0, VertexPositionColor.SizeInBytes); // エフェクトの使用を開始します this.basicEffect.Begin(); // パスの数だけ繰り返し描画 foreach (EffectPass pass in this.basicEffect.CurrentTechnique.Passes) { // パスの開始 pass.Begin(); // 三角形を描画する this.GraphicsDevice.DrawPrimitives(PrimitiveType.TriangleList, 0, 1); // パスの終了 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/01 | ページ作成 |