public class DXObject
{
public DXObject() { }
public DXObject(Mesh model, Vector3 position, Material meshMaterial)
{
Model = model;
Position = position;
MeshMaterial = meshMaterial;
}
public Mesh Model { get; set; }
public Vector3 Position { get; set; }
public Material MeshMaterial { get; set; }
}
public class DirectXForm : System.Windows.Forms.Form
{
#region Private members
private Vector3 cameraPosition = new Vector3(0, 0, -25);
private Vector3 lookAtPoint = new Vector3(0, 0, 0);
private Vector3 upVector = new Vector3(0, 1, 0);
Microsoft.DirectX.DirectInput.Device keyboard = null;
private Color backgroundColor = Color.Blue;
#endregion
#region Public members
public event EventHandler Rendered;
public Microsoft.DirectX.Direct3D.Device D3DDevice = null;
public List<DXObject> Meshes = new List<DXObject>();
public Vector3 CameraPostition
{
get { return cameraPosition; }
set { cameraPosition = value; }
}
public Vector3 LookAtPoint
{
get { return lookAtPoint; }
set { lookAtPoint = value; }
}
public Microsoft.DirectX.DirectInput.Device DXKeyboard
{
get { return keyboard; }
}
public Color BackgroundColor
{
get { return backgroundColor; }
set { backgroundColor = value; }
}
#endregion
#region Constructors
public DirectXForm() : this(new Size(640, 480)) { }
public DirectXForm(Size size)
{
this.ClientSize = size;
this.Text = "";
this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true);
this.FormBorderStyle = FormBorderStyle.FixedDialog;
Caps caps = Microsoft.DirectX.Direct3D.Manager.GetDeviceCaps(Microsoft.DirectX.Direct3D.Manager.Adapters.Default.Adapter, Microsoft.DirectX.Direct3D.DeviceType.Hardware);
CreateFlags flags;
if (caps.DeviceCaps.SupportsHardwareTransformAndLight)
flags = CreateFlags.HardwareVertexProcessing;
else
flags = CreateFlags.SoftwareVertexProcessing;
PresentParameters presParams = new PresentParameters();
presParams.BackBufferFormat = Format.Unknown;
presParams.SwapEffect = SwapEffect.Discard;
presParams.Windowed = true;
presParams.EnableAutoDepthStencil = true;
presParams.AutoDepthStencilFormat = DepthFormat.D16;
D3DDevice = new Microsoft.DirectX.Direct3D.Device(0, Microsoft.DirectX.Direct3D.DeviceType.Hardware, this, flags, presParams);
D3DDevice.DeviceReset += new System.EventHandler(this.OnResetDevice);
OnResetDevice(D3DDevice, null);
D3DDevice.RenderState.Lighting = true;
D3DDevice.Lights[0].Type = LightType.Directional;
D3DDevice.Lights[0].Direction = new Vector3(0, 0, 1f);
D3DDevice.Lights[0].Diffuse = Color.White;
D3DDevice.Lights[0].Enabled = true;
D3DDevice.RenderState.FillMode = FillMode.Solid;
keyboard = new Microsoft.DirectX.DirectInput.Device(SystemGuid.Keyboard);
keyboard.SetCooperativeLevel(this, CooperativeLevelFlags.Background | CooperativeLevelFlags.NonExclusive);
keyboard.Acquire();
}
#endregion
#region Methods
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
this.Render();
this.Invalidate();
}
protected void OnResetDevice(object sender, EventArgs e)
{
Microsoft.DirectX.Direct3D.Device device = (Microsoft.DirectX.Direct3D.Device)sender;
device.Transform.Projection =
Matrix.PerspectiveFovLH(Geometry.DegreeToRadian(45.0f),
(float)this.ClientSize.Width / this.ClientSize.Height,
0.1f, 100.0f);
device.RenderState.CullMode = Cull.None;
}
private void Render()
{
D3DDevice.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.DarkBlue, 1.0f, 0);
D3DDevice.BeginScene();
D3DDevice.Transform.View = Matrix.LookAtLH(cameraPosition, lookAtPoint, new Vector3(0.0f, 1.0f, 0.0f));
foreach (DXObject dxo in this.Meshes)
{
D3DDevice.Transform.World = Matrix.Translation(dxo.Position);
D3DDevice.Material = dxo.MeshMaterial;
dxo.Model.DrawSubset(0);
}
D3DDevice.EndScene();
D3DDevice.Present();
if (this.Rendered != null)
Rendered(this, new EventArgs());
}
#endregion
}