📄 form1.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
namespace Triangle_with_HLSL
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
private VertexBuffer vb = null;
private Effect effect = null;
private VertexDeclaration decl = null;
private Device device= null;
//our matrices
private Matrix worldMatrix;
private Matrix viewMatrix;
private Matrix projMatrix;
private float angle = 0.0f;
public bool InitializeGraphics()
{
// Set our presentation parameters
PresentParameters presentParams = new PresentParameters();
presentParams.Windowed = true;
presentParams.SwapEffect = SwapEffect.Discard;
presentParams.AutoDepthStencilFormat = DepthFormat.D16;
presentParams.EnableAutoDepthStencil = true;
bool canDoShaders = true;
// Does a hardware device support shaders?
Caps hardware = Manager.GetDeviceCaps(0, DeviceType.Hardware);
if (hardware.VertexShaderVersion >= new Version(1, 1))
{
// Default to software processing
CreateFlags flags = CreateFlags.SoftwareVertexProcessing;
// Use hardware if it's available
if (hardware.DeviceCaps.SupportsHardwareTransformAndLight)
flags = CreateFlags.HardwareVertexProcessing;
// Use pure if it's available
if (hardware.DeviceCaps.SupportsPureDevice)
flags |= CreateFlags.PureDevice;
// Yes, Create our device
device = new Device(0, DeviceType.Hardware, this, flags, presentParams);
}
else
{
// No shader support
canDoShaders = false;
// Create a reference device
device = new Device(0, DeviceType.Reference, this,
CreateFlags.SoftwareVertexProcessing, presentParams);
}
effect = Effect.FromFile(device,@"..\..\simple.fx",null,ShaderFlags.None,null);
effect.Technique = "TransformDiffuse";
// Create our vertex data
vb = new VertexBuffer(typeof(CustomVertex.PositionOnly), 3, device,
Usage.Dynamic | Usage.WriteOnly, CustomVertex.PositionOnly.Format,
Pool.Default);
vb.Created += new EventHandler(this.OnVertexBufferCreate);
OnVertexBufferCreate(vb, null);
// Store our project and view matrices
projMatrix = Matrix.PerspectiveFovLH((float)Math.PI / 4,
this.Width / this.Height, 1.0f, 100.0f);
viewMatrix = Matrix.LookAtLH(new Vector3(0,0, 5.0f), new Vector3(),
new Vector3(0,1,0));
// Create our vertex declaration
VertexElement[] elements = new VertexElement[]
{
new VertexElement(0, 0, DeclarationType.Float3,
DeclarationMethod.Default,
DeclarationUsage.Position, 0),
VertexElement.VertexDeclarationEnd
};
decl = new VertexDeclaration(device, elements);
return canDoShaders;
}
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true);
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.Size = new System.Drawing.Size(300,300);
this.Text = "Form1";
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
using (Form1 frm = new Form1())
{
// Show our form and initialize our graphics engine
frm.Show();
if (!frm.InitializeGraphics())
{
MessageBox.Show("Your card does not support shaders. " +
"This application will run in ref mode instead.");
}
Application.Run(frm);
}
}
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
UpdateWorld();
device.Clear(ClearFlags.Target| ClearFlags.ZBuffer,Color.CornflowerBlue,1.0f,0);
device.BeginScene();
device.SetStreamSource(0, vb, 0);
device.VertexDeclaration = decl;
int numPasses = effect.Begin(0);
for (int i = 0; i < numPasses; i++)
{
effect.BeginPass(i);
device.DrawPrimitives(PrimitiveType.TriangleList, 0, 1);
effect.EndPass();
}
effect.End();
device.EndScene();
device.Present();
this.Invalidate();
}
private void UpdateWorld()
{
worldMatrix = Matrix.RotationAxis(new Vector3(angle/((float)Math.PI * 2.0f),angle/((float)Math.PI*4.0f),
angle/((float)Math.PI*6.0f)),angle/(float)Math.PI);
angle += 0.1f;
Matrix worldViewProj = worldMatrix * viewMatrix * projMatrix;
effect.SetValue("Time", (float)Math.Sin(angle / 5.0f));
effect.SetValue("WorldViewProj", worldViewProj);
}
private void OnVertexBufferCreate(object sender, EventArgs e)
{
VertexBuffer buffer = (VertexBuffer)sender;
CustomVertex.PositionOnly[] verts = new CustomVertex.PositionOnly[3];
verts[0].Position = new Vector3(0.0f,1.0f,1.0f);
verts[1].Position = new Vector3(-1.0f,-1.0f,1.0f);
verts[2].Position = new Vector3(1.0f,-1.0f,1.0f);
buffer.SetData(verts,0,LockFlags.None);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -