测试
测试
1
using System;2
using System.Drawing;3
using System.Windows.Forms;4
using Microsoft.DirectX;5
using Microsoft.DirectX.Direct3D;6

7
namespace Chapter11Code8


{9

/**//// <summary>10
/// Summary description for Form1.11
/// </summary>12
public class Form1 : System.Windows.Forms.Form13

{14
private Device device = null;15
private Mesh mesh = null;16
private Material[] meshMaterials;17
private Texture[] meshTextures;18

19
private Effect effect = null;20
// Matrices21
private Matrix worldMatrix;22
private Matrix viewMatrix;23
private Matrix projMatrix;24

25

/**//// <summary>26
/// Required designer variable.27
/// </summary>28
private System.ComponentModel.Container components = null;29
private float angle = 0.0f;30

31
public Form1()32

{33
//34
// Required for Windows Form Designer support35
//36
InitializeComponent();37

38
this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true);39
}40

41

/**//// <summary>42
/// We will initialize our graphics device here43
/// </summary>44
public bool InitializeGraphics()45

{46
// Set our presentation parameters47
PresentParameters presentParams = new PresentParameters();48

49
presentParams.Windowed = true;50
presentParams.SwapEffect = SwapEffect.Discard;51
presentParams.AutoDepthStencilFormat = DepthFormat.D16;52
presentParams.EnableAutoDepthStencil = true;53

54

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

{61
// Default to software processing62
CreateFlags flags = CreateFlags.SoftwareVertexProcessing;63

64
// Use hardware if it's available65
if (hardware.DeviceCaps.SupportsHardwareTransformAndLight)66
flags = CreateFlags.HardwareVertexProcessing;67

68
// Use pure if it's available69
if (hardware.DeviceCaps.SupportsPureDevice)70
flags |= CreateFlags.PureDevice;71

72
// Yes, Create our device73
device = new Device(0, DeviceType.Hardware, this, flags, presentParams);74
}75
else76

{77
// No shader support78
canDoShaders = false;79

80
// Create a reference device81
device = new Device(0, DeviceType.Reference, this, 82
CreateFlags.SoftwareVertexProcessing, presentParams);83
}84

85
// Create our effect86
//2005/07/11. TheZBuffer.com. Updated for June 2005 SDK87
//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

91
// Store our project and view matrices92
projMatrix = Matrix.PerspectiveFovLH((float)Math.PI / 4, 93
this.Width / this.Height, 1.0f, 10000.0f);94

95
viewMatrix = Matrix.LookAtLH(new Vector3(0,0, 580.0f), new Vector3(), 96
new Vector3(0,1,0));97

98
// Load our mesh99
LoadMesh(@"..\..\tiny.x");100

101
return canDoShaders;102
}103

104
private void LoadMesh(string file)105

{106
ExtendedMaterial[] mtrl;107

108
// Load our mesh109
mesh = Mesh.FromFile(file, MeshFlags.Managed, device, out mtrl);110

111
// If we have any materials, store them112
if ((mtrl != null) && (mtrl.Length > 0))113

{114
meshMaterials = new Material[mtrl.Length];115
meshTextures = new Texture[mtrl.Length];116

117
// Store each material and texture118
for (int i = 0; i < mtrl.Length; i++)119

{120
meshMaterials[i] = mtrl[i].Material3D;121
if ((mtrl[i].TextureFilename != null) && (mtrl[i].TextureFilename != string.Empty))122

{123
// We have a texture, try to load it124
meshTextures[i] = TextureLoader.FromFile(device, @"..\..\" + mtrl[i].TextureFilename);125
}126
}127
}128
}129

130
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)131

{132
device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.CornflowerBlue, 1.0f, 0);133

134
device.BeginScene();135

136
// Draw our Mesh137
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

140
device.EndScene();141

142
device.Present();143

144
this.Invalidate();145
}146

147
private void DrawMesh(float yaw, float pitch, float roll, float x, float y, float z)148

{149
angle += 0.01f;150

151
worldMatrix = Matrix.RotationYawPitchRoll(yaw, pitch, roll) 152
* Matrix.Translation(x, y, z);153

154
Matrix worldViewProj = worldMatrix * viewMatrix * projMatrix;155
effect.SetValue("WorldViewProj", worldViewProj);156

157
int numPasses = effect.Begin(0);158
for (int iPass = 0; iPass < numPasses; iPass++)159

{160
//2005/07/11. TheZBuffer.com. Updated for June 2005 SDK161
//effect.Pass(iPass);162
effect.BeginPass(iPass);163
for (int i = 0; i < meshMaterials.Length; i++)164

{165
device.SetTexture(0, meshTextures[i]);166
mesh.DrawSubset(i);167
}168
//2005/07/11. TheZBuffer.com. Updated for June 2005 SDK169
//Added
170
effect.EndPass();171
}172
effect.End();173
}174

175
protected override void OnKeyPress(KeyPressEventArgs e)176

{177
switch (e.KeyChar)178

{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

195

/**//// <summary>196
/// Clean up any resources being used.197
/// </summary>198
protected override void Dispose( bool disposing )199

{200
if( disposing )201

{202
if (components != null) 203

{204
components.Dispose();205
}206
}207
base.Dispose( disposing );208
}209

210

Windows Form Designer generated code#region Windows Form Designer generated code211

/**//// <summary>212
/// Required method for Designer support - do not modify213
/// the contents of this method with the code editor.214
/// </summary>215
private void InitializeComponent()216

{217
this.components = new System.ComponentModel.Container();218
this.Size = new Size(800,600);219
this.Text = "Form1";220
}221
#endregion222

223

/**//// <summary>224
/// The main entry point for the application.225
/// </summary>226
static void Main() 227

{228
using (Form1 frm = new Form1())229

{230
// Show our form and initialize our graphics engine231
frm.Show();232
if (!frm.InitializeGraphics())233

{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


浙公网安备 33010602011771号