単純な形状のMeshが既定で用意されており、これを使用することで簡単に3Dのオブジェクトを表示できます。
なおMeshを使用するには、Direct3D Extensions library (D3DX) を参照に追加する必要があります。
ボックス (Box)![]() |
public static Mesh Box(
Device device,
float width, // X軸方向の大きさ
float height, // Y軸方向の大きさ
float depth // Z軸方向の大きさ
)
|
円柱 (Cylinder)![]() |
public static Mesh Cylinder(
Device device,
float radius1, // 面の半径 (Z軸の負の方向側)
float radius2, // 面の半径 (Z軸の正の方向側)
float length, // 円柱の長さ (Z軸方向)
int slices, // 主軸に平行な方向の分割数
int stacks // 主軸に垂直な方向の分割数
)
|
多角形 (Polygon)![]() |
public static Mesh Polygon(
Device device,
float length, // 各辺の長さ
int sides // 辺の数 (sides角形)
)
|
球 (Sphere)![]() |
public static Mesh Sphere(
Device device,
float radius, // 球の半径 [0より大きな値]
int slices, // 主軸に平行な方向の分割数 [2以上]
int stacks // 主軸に垂直な方向の分割数 [2以上]
)
|
トーラス (Torus)![]() |
public static Mesh Torus(
Device device,
float innerRadius, // 環状部の半径
float outerRadius, // 全体の半径
int sides, // 環状部の円の辺の数
int rings // 全体の辺の数
)
|
ティーポット (Teapot)![]() |
public static Mesh Teapot(
Device device
)
|
public void DrawSubset(
int attributeID
)
using System;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
public class Form1 : Form
{
Device device;
Mesh mesh;
public Form1()
{
PresentParameters presentParameters = new PresentParameters();
presentParameters.Windowed = true;
presentParameters.SwapEffect = SwapEffect.Discard;
this.device = new Device( 0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParameters );
// メッシュの作成
this.mesh = Mesh.Teapot( this.device );
}
protected override void OnPaint( PaintEventArgs e )
{
this.device.Clear( ClearFlags.Target, Color.Black, 1.0f, 0 );
this.device.BeginScene();
// 視野錐台
float fieldOfViewY = ( float )Math.PI / 2.0f;
float aspectRatio = 1.0f;
float znearPlane = 0.0f;
float zfarPlane = 1.0f;
this.device.Transform.Projection = Matrix.PerspectiveFovLH( fieldOfViewY, aspectRatio, znearPlane, zfarPlane );
// 視野
Vector3 cameraPosition = new Vector3( 1.3f, 1.3f, 1.3f );
Vector3 cameraTarget = new Vector3( 0.0f, 0.0f, 0.0f );
Vector3 cameraUpVector = new Vector3( 0.0f, 1.0f, 0.0f );
this.device.Transform.View = Matrix.LookAtLH( cameraPosition, cameraTarget, cameraUpVector );
// ライト
this.device.Lights[ 0 ].Direction = new Vector3( 0.0f, -1.0f, -1.0f );
this.device.Lights[ 0 ].Diffuse = Color.White;
this.device.Lights[ 0 ].Enabled = true;
this.device.Lights[ 0 ].Update();
// マテリアル
Material material = new Material();
material.Diffuse = Color.White;
this.device.Material = material;
this.device.RenderState.Lighting = true;
// メッシュの描画
this.mesh.DrawSubset( 0 );
this.device.EndScene();
this.device.Present();
}
static void Main()
{
Form1 form1 = new Form1();
form1.Show();
while( form1.Created )
{
Application.DoEvents();
}
}
}

実行結果