Draw a trigonal polygon using a vertex buffer
summary
By using vertex buffers, polygons can be drawn quickly.
Operating environment
Prerequisites
Supported XNA Versions |
|
Supported Platforms |
|
Windows Required Vertex Shader Version | 2.0 |
Windows Required Pixel Shader Version | 2.0 |
Operating environment
platform |
|
substance
When you draw a polygon, you can draw it using something called a "vertex buffer". The vertex buffer can be filled with vertex data to be used when drawing primitives such as polygons. The vertex buffer allows vertex data to be placed on video memory, and the drawing pipeline can exchange data much faster than it does from normal main memory, so it can be expected to improve the drawing speed.
In the case of a single polygon like this one, you can't feel that you are drawing at high speed, but I think that the effect is visible when drawing thousands or tens of thousands of polygons.
field
<summary>
頂点バッファ
</summary>
private VertexBuffer vertexBuffer = null;
To use a vertex buffer, use the "VertexBuffer" class. If user-defined vertex data was sent to the pipeline every time it was drawn, the vertex data had to be kept at all times, but if a vertex buffer was created, the vertex buffer manages the vertex data, so there is no need to keep the vertex data.
Creating a Vertex Buffer
// 頂点の数
int vertexCount = 3;
// 頂点バッファ作成
this.vertexBuffer = new VertexBuffer(this.GraphicsDevice,
typeof(VertexPositionColor), vertexCount, BufferUsage.None);
In the LoadGraphicsContent method, we are creating a VertexBuffer.
The first argument is a GraphicsDevice.
The second argument specifies the type of structure that contains the vertex data, or the vertex declaration of the vertex data to be used. Here, typeof is passed in the type of the structure. Since "vertex position" and "vertex color" are used for vertex data, the "VertexPositionColor" structure provided by the XNA Framework is specified as standard.
The third argument specifies the number of vertices to create.
The fourth argument specifies the vertex buffer usage, but if there is nothing in particular, you can specify "BufferUsage.None".
VertexBuffer
constructor
Create an instance of the class "VertexBuffer" for creating vertex data.
graphicsDevice | GraphicsDevice | Specifies the GraphicsDevice to associate with the vertex buffer. |
vertexType | Type | Specifies the type of vertex data to use. |
vertexCount | int | Specifies the number of vertices to create. |
usage | BufferUsage | Specifies the usage of the vertex buffer. If there is nothing in particular, specify "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(pointList);
After creating the vertex buffer, set the vertex buffer with vertex data. To set vertex data, use the "VertexBuffer.SetData" method.
Once the vertex data is set in the vertex buffer, it is managed by the vertex buffer, so if you do not use it anymore, you can discard it as it is.
VertexBuffer.SetData
method
Set the vertex data in the vertex buffer.
T | ValueType | Specifies the structure of the vertex data to use. |
data | T[] | Specifies an array of vertex data to be set in the vertex buffer. The size specified in the vertex buffer must match the size of the vertex data to be set. |
drawing
// 描画に使用する頂点バッファをセットします
this.GraphicsDevice.SetVertexBuffer(this.vertexBuffer);
// パスの数だけ繰り返し描画
foreach (EffectPass pass in this.basicEffect.CurrentTechnique.Passes)
{
// パスの開始
pass.Apply();
// 三角形を描画する
this.GraphicsDevice.DrawPrimitives(PrimitiveType.TriangleList, 0, 1);
}
To draw a polygon, set the vertex buffer to be used for drawing. The GraphicsDevice has a "SetVertexBuffer" method that sets the vertex buffer to it.
GraphicsDevice.SetVertexBuffer
method
Specifies the vertex buffer to use for drawing.
vertexBuffer | VertexBuffer | Specifies the vertex buffer to use for drawing. |
To draw the actual polygons, use the "GraphicsDevice.DrawPrimitives" method. If you want to use a vertex buffer, you will draw it with this method. Unlike the DrawUserPrimitives method, which specifies the user-defined point data as it is, this method is used to draw using a vertex buffer.
The first argument is the type of primitive to draw, the second argument is the vertex index to start drawing, and the third argument is the number of primitives to draw.
GraphicsDevice.DrawPrimitives
method
Draws a primitive using the specified vertex buffer.
primitiveType | PrimitiveType | Specifies the type of primitive to draw |
startVertex | int | The first index of the vertex to load |
primitiveCount | int | Number of primitives to draw |
All Codes
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 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 VertexBuffer vertexBuffer = 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 = 3;
// 頂点バッファ作成
this.vertexBuffer = new VertexBuffer(this.GraphicsDevice,
typeof(VertexPositionColor), 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(pointList);
}
<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.vertexBuffer);
// パスの数だけ繰り返し描画
foreach (EffectPass pass in this.basicEffect.CurrentTechnique.Passes)
{
// パスの開始
pass.Apply();
// 三角形を描画する
this.GraphicsDevice.DrawPrimitives(PrimitiveType.TriangleList, 0, 1);
}
// 登録された DrawableGameComponent を描画する
base.Draw(gameTime);
}
}
}