3Dの基本ということで、キューブを作ってみましょう。
頂点データ
- 3D頂点座標、頂点色
- CustomVertex.PositionColored
- 頂点数:8
CustomVertex.PositionColored[] v = new CustomVertex.PositionColored[ 8 ];
v[ 0 ] = new CustomVertex.PositionColored( -0.5f, 0.5f, -0.5f, Color.Red.ToArgb() );
v[ 1 ] = new CustomVertex.PositionColored( 0.5f, 0.5f, -0.5f, Color.Magenta.ToArgb() );
v[ 2 ] = new CustomVertex.PositionColored( 0.5f, -0.5f, -0.5f, Color.Blue.ToArgb() );
v[ 3 ] = new CustomVertex.PositionColored( -0.5f, -0.5f, -0.5f, Color.Black.ToArgb() );
v[ 4 ] = new CustomVertex.PositionColored( -0.5f, 0.5f, 0.5f, Color.Yellow.ToArgb() );
v[ 5 ] = new CustomVertex.PositionColored( 0.5f, 0.5f, 0.5f, Color.White.ToArgb() );
v[ 6 ] = new CustomVertex.PositionColored( 0.5f, -0.5f, 0.5f, Color.Cyan.ToArgb() );
v[ 7 ] = new CustomVertex.PositionColored( -0.5f, -0.5f, 0.5f, Color.FromArgb( 0, 255, 0 ).ToArgb() );
vertex = new VertexBuffer( typeof(CustomVertex.PositionColored), v.Length, device, Usage.None, CustomVertex.PositionColored.Format, Pool.Managed );
using( GraphicsStream data = vertex.Lock( 0, 0, LockFlags.None ) )
{
data.Write( v );
vertex.Unlock();
}
インデックスデータ
インデックスバッファを使ってみます。
Int16[] i = new Int16[]{
0, 1, 2,
0, 2, 3,
1, 5, 6,
1, 6, 2,
5, 4, 7,
5, 7, 6,
4, 0, 3,
4, 3, 7,
4, 5, 1,
4, 1, 0,
3, 2, 6,
3, 6, 7
};
index = new IndexBuffer( device, i.Length * 2, Usage.None, Pool.Managed, true );
using( GraphicsStream data = index.Lock( 0, 0, LockFlags.None ) )
{
data.Write( i );
index.Unlock();
}
描画
device.SetStreamSource( 0, vertex, 0 );
device.Indices = index;
device.SetTexture( 0, null );
device.VertexFormat = CustomVertex.PositionColored.Format;
device.DrawIndexedPrimitives( PrimitiveType.TriangleList, 0, 0, 頂点数, 0, プリミティブ数 );