测试
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1
using System;
2
using System.Drawing;
3
using System.Windows.Forms;
4
using Microsoft.DirectX;
5
using Microsoft.DirectX.Direct3D;
6![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
7
namespace Chapter11Code
8![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
9![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
10
/// Summary description for Form1.
11
/// </summary>
12
public class Form1 : System.Windows.Forms.Form
13![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
14
private Device device = null;
15
private Mesh mesh = null;
16
private Material[] meshMaterials;
17
private Texture[] meshTextures;
18![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
private Effect effect = null;
20
// Matrices
21
private Matrix worldMatrix;
22
private Matrix viewMatrix;
23
private Matrix projMatrix;
24![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
25![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
26
/// Required designer variable.
27
/// </summary>
28
private System.ComponentModel.Container components = null;
29
private float angle = 0.0f;
30![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
31
public Form1()
32![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
33
//
34
// Required for Windows Form Designer support
35
//
36
InitializeComponent();
37![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
38
this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true);
39
}
40![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
41![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
42
/// We will initialize our graphics device here
43
/// </summary>
44
public bool InitializeGraphics()
45![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
46
// Set our presentation parameters
47
PresentParameters presentParams = new PresentParameters();
48![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
49
presentParams.Windowed = true;
50
presentParams.SwapEffect = SwapEffect.Discard;
51
presentParams.AutoDepthStencilFormat = DepthFormat.D16;
52
presentParams.EnableAutoDepthStencil = true;
53![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
54![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
55
bool canDoShaders = true;
56
// Does a hardware device support shaders?
57
Caps hardware = Manager.GetDeviceCaps(0, DeviceType.Hardware);
58
if ((hardware.VertexShaderVersion >= new Version(1, 1)) &&
59
(hardware.PixelShaderVersion >= new Version(1, 1)))
60![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
61
// Default to software processing
62
CreateFlags flags = CreateFlags.SoftwareVertexProcessing;
63![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
64
// Use hardware if it's available
65
if (hardware.DeviceCaps.SupportsHardwareTransformAndLight)
66
flags = CreateFlags.HardwareVertexProcessing;
67![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
68
// Use pure if it's available
69
if (hardware.DeviceCaps.SupportsPureDevice)
70
flags |= CreateFlags.PureDevice;
71![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
72
// Yes, Create our device
73
device = new Device(0, DeviceType.Hardware, this, flags, presentParams);
74
}
75
else
76![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
77
// No shader support
78
canDoShaders = false;
79![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
80
// Create a reference device
81
device = new Device(0, DeviceType.Reference, this,
82
CreateFlags.SoftwareVertexProcessing, presentParams);
83
}
84![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
85
// Create our effect
86
//2005/07/11. TheZBuffer.com. Updated for June 2005 SDK
87
//effect = Effect.FromFile(device, @"..\..\simple.fx", null, ShaderFlags.None, null);
88
effect = Effect.FromFile(device, @"..\..\simple.fx", null, "", ShaderFlags.None, null);
89
effect.Technique = "TransformTexture";
90![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
91
// Store our project and view matrices
92
projMatrix = Matrix.PerspectiveFovLH((float)Math.PI / 4,
93
this.Width / this.Height, 1.0f, 10000.0f);
94![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
95
viewMatrix = Matrix.LookAtLH(new Vector3(0,0, 580.0f), new Vector3(),
96
new Vector3(0,1,0));
97![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
98
// Load our mesh
99
LoadMesh(@"..\..\tiny.x");
100![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
101
return canDoShaders;
102
}
103![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
104
private void LoadMesh(string file)
105![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
106
ExtendedMaterial[] mtrl;
107![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
108
// Load our mesh
109
mesh = Mesh.FromFile(file, MeshFlags.Managed, device, out mtrl);
110![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
111
// If we have any materials, store them
112
if ((mtrl != null) && (mtrl.Length > 0))
113![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
114
meshMaterials = new Material[mtrl.Length];
115
meshTextures = new Texture[mtrl.Length];
116![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
117
// Store each material and texture
118
for (int i = 0; i < mtrl.Length; i++)
119![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
120
meshMaterials[i] = mtrl[i].Material3D;
121
if ((mtrl[i].TextureFilename != null) && (mtrl[i].TextureFilename != string.Empty))
122![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
123
// We have a texture, try to load it
124
meshTextures[i] = TextureLoader.FromFile(device, @"..\..\" + mtrl[i].TextureFilename);
125
}
126
}
127
}
128
}
129![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
130
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
131![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
132
device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.CornflowerBlue, 1.0f, 0);
133![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
134
device.BeginScene();
135![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
136
// Draw our Mesh
137
DrawMesh(angle / (float)Math.PI, angle / (float)Math.PI * 2.0f,
138
angle / (float)Math.PI / 4.0f, 0.0f, 0.0f, 0.0f);
139![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
140
device.EndScene();
141![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
142
device.Present();
143![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
144
this.Invalidate();
145
}
146![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
147
private void DrawMesh(float yaw, float pitch, float roll, float x, float y, float z)
148![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
149
angle += 0.01f;
150![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
151
worldMatrix = Matrix.RotationYawPitchRoll(yaw, pitch, roll)
152
* Matrix.Translation(x, y, z);
153![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
154
Matrix worldViewProj = worldMatrix * viewMatrix * projMatrix;
155
effect.SetValue("WorldViewProj", worldViewProj);
156![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
157
int numPasses = effect.Begin(0);
158
for (int iPass = 0; iPass < numPasses; iPass++)
159![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
160
//2005/07/11. TheZBuffer.com. Updated for June 2005 SDK
161
//effect.Pass(iPass);
162
effect.BeginPass(iPass);
163
for (int i = 0; i < meshMaterials.Length; i++)
164![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
165
device.SetTexture(0, meshTextures[i]);
166
mesh.DrawSubset(i);
167
}
168
//2005/07/11. TheZBuffer.com. Updated for June 2005 SDK
169
//Added![](https://www.cnblogs.com/Images/dot.gif)
170
effect.EndPass();
171
}
172
effect.End();
173
}
174![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
175
protected override void OnKeyPress(KeyPressEventArgs e)
176![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
177
switch (e.KeyChar)
178![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
179
case '1':
180
effect.Technique = "TransformTexture";
181
break;
182
case '2':
183
effect.Technique = "TransformInverseTexture";
184
break;
185
case '3':
186
effect.Technique = "TransformTextureNoBlue";
187
break;
188
case '4':
189
effect.Technique = "TransformTextureOnlyBlue";
190
break;
191
}
192
base.OnKeyPress (e);
193
}
194![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
195![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
196
/// Clean up any resources being used.
197
/// </summary>
198
protected override void Dispose( bool disposing )
199![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
200
if( disposing )
201![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
202
if (components != null)
203![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
204
components.Dispose();
205
}
206
}
207
base.Dispose( disposing );
208
}
209![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
210![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
Windows Form Designer generated code#region Windows Form Designer generated code
211![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
212
/// Required method for Designer support - do not modify
213
/// the contents of this method with the code editor.
214
/// </summary>
215
private void InitializeComponent()
216![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
217
this.components = new System.ComponentModel.Container();
218
this.Size = new Size(800,600);
219
this.Text = "Form1";
220
}
221
#endregion
222![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
223![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
224
/// The main entry point for the application.
225
/// </summary>
226
static void Main()
227![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
228
using (Form1 frm = new Form1())
229![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
230
// Show our form and initialize our graphics engine
231
frm.Show();
232
if (!frm.InitializeGraphics())
233![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
234
MessageBox.Show("Your card does not support shaders. " +
235
"This application will run in ref mode instead.");
236
}
237
Application.Run(frm);
238
}
239
}
240
}
241
}
242![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
posted @
2009-03-08 19:45
江水
阅读(
223)
评论()
编辑
收藏
举报