1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
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
}
|