Sprache: C#
Dieses Snippet enthält eine Klasse namens DXObject, welche ein mit DirectX zu zeichnendes Objekt definiert, bestehend aus:
Mesh Model : Das DirectX-Mesh
Vector3 Position : Die Position des Objekts
Material MeshMaterial : Das Material des Objekts
Außerdem eine Klasse namens DirectXForm, die von System.Windows.Forms.Form erbt und DirectX implementiert. Sie besitzt folgende öffentliche Member:
Device D3DDevice : Das DirectX-Device
event Rendered : Wird ausgelöst, wenn eine Szene gerendert wurde
List<DXObject> Meshes : Die zu zeichnende Liste von DXObjects
Vector3 CameraPosition : Die Position der Kamera
Vector3 LookAtPoint : Der Look-At-Vektor
Microsoft.DirectX.DirectInput.Device DXKeyboard : Die Tastatur
Color BackgroundColor : Die Hintergrundfarbe
Anderen Änderungen lassen sich direkt am Device D3DDevice ändern.
Nötig sind folgende Assemblyverweise (alle aus dem DirectX-SDK):
Microsoft.DirectX,
Microsoft.DirectX.Direct3DX,
Microsoft.DirectX.DirectInput
und folgende Using-Direktiven:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
using Microsoft.DirectX.DirectInput;
Verwendet wird es wie eine normale Form.
Dieses Snippet soll jediglich die Verwendung von DirectX vereinfachen!
Hier möchte ich auf das C#-DirectX Tutorial auf
http://www.riemers.net/eng/Tutorials/dxcsharp.php
verweisen, welches mir das nötige Wissen verschafft hat, ohne welches ich dieses Snippet nicht hätte schreiben können.
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
}
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
}
Alte URL:
/snippet/directx-form/1321