コードスペランカー

ゲーム開発日誌など

XNA4.0でのポリゴン描画

とりあえず、単純にポリゴンを描画できるところまで来たのでメモする

//頂点データの定義
VertexBuffer vb = new VertexBuffer
  (_graphics.GraphicsDevice, typeof(VertexPositionColor), 
  _vatexData.Length, BufferUsage.None);
//エフェクト宣言
BasicEffect effect = new BasicEffect(_graphics.GraphicsDevice);
//ワールド座標変換行列
Matrix world = Matrix.Identity;
//ビュー座標変換行列
Matrix view = Matrix.CreateLookAt
  (new Vector3(3, 3, 3), new Vector3(0, 0, 0), new Vector3(0, 1, 0));
//射影変換行列
Matrix projection = Matrix.CreatePerspectiveFieldOfView
  (MathHelper.ToRadians(45), _graphics.GraphicsDevice.Viewport.Width /
  _graphics.GraphicsDevice.Viewport.Height, 0.1f, 1000f);

//ワールド座標変換指定
effect.World = world;
//ビュー座標変換指定
effect.View = view;
//射影変換指定
effect.Projection = projection;

//頂点データ定義のセット
_graphics.GraphicsDevice.SetVertexBuffer(vb);

//描画
foreach (EffectPass pass in effect.CurrentTechnique.Passes)
{
  pass.Apply();
  _graphics.GraphicsDevice.DrawUserPrimitives<VertexPositionNormalTexture>
  (PrimitiveType.TriangleList, _vatexData, 0, 1);
}

っと、まぁ、こんな感じで書けばよいらしい
大きな変更点としては頂点データの定義方法になるだろう。
_vatexData.Lengthの部分は表示するポリゴンの頂点数になる。
他には前回書いたようにeffect.Begin()〜effect.End()の表記がばっさり消えている。
そしてpass.Begin()〜pass.End()の代わりに開始時にpass.Apply()という変更が主だったものになる。
RenderStateについては、別の機会に

2010/12/12追記
RenderStateについてはXNA4.0でのポリゴン描画その4で記述した