public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
this.InitialDX();
}
private Device device;
private Vector3 camPosition = new Vector3(0, 30, -50);
private Vector3 camTarget = new Vector3(0, 0, 0);
private Vector3 camUp = new Vector3(0, 1, 0);
private float angle = 0.01f;
private void InitialDX()
{
PresentParameters presentParams = new PresentParameters();
presentParams.Windowed = true;
presentParams.SwapEffect = SwapEffect.Discard;
device = new Device(
0,
DeviceType.Hardware,
this,
CreateFlags.SoftwareVertexProcessing,
presentParams);
device.RenderState.Lighting = false;
device.RenderState.CullMode = Cull.None;
device.Transform.View = Matrix.LookAtLH(
camPosition,
camTarget,
camUp);
device.Transform.Projection = Matrix.PerspectiveFovLH(
(float)Math.PI / 4,
this.Width / this.Height,
10,
500);
Texture texture = Texture.FromBitmap(
device,
(Bitmap)Image.FromFile("w.png"),
Usage.None,
Pool.Managed);
device.SetTexture(0, texture);
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
this.DrawDX();
}
private void DrawDX()
{
device.Clear(ClearFlags.Target, Color.Teal, 1f, 0);
device.BeginScene();
this.DrawMyGraphics();
device.EndScene();
device.Present();
}
private void DrawMyGraphics()
{
CustomVertex.PositionTextured[] vertexs = new CustomVertex.PositionTextured[4];
vertexs[0].Position = new Vector3(10f, 0, 10f);
vertexs[1].Position = new Vector3(10f, 0, -10f);
vertexs[2].Position = new Vector3(-10f, 0, -10f);
vertexs[3].Position = new Vector3(-10f, 0, 10f);
vertexs[0].Tu = 1f;
vertexs[0].Tv = 0f;
vertexs[1].Tu = 1f;
vertexs[1].Tv = 1f;
vertexs[2].Tu = 0f;
vertexs[2].Tv = 1f;
vertexs[3].Tu = 0f;
vertexs[3].Tv = 0f;
device.VertexFormat = CustomVertex.PositionTextured.Format;
device.DrawUserPrimitives(PrimitiveType.TriangleFan,
2,
vertexs);
}
protected override void OnKeyDown(KeyEventArgs e)
{
base.OnKeyDown(e);
Vector4 tempV;
switch (e.KeyCode)
{
case Keys.Add: //放大
{
camPosition.Subtract(camTarget);
camPosition.Scale(0.95f);
camPosition.Add(camTarget);
}
break;
case Keys.Subtract: //缩小
{
camPosition.Subtract(camTarget);
camPosition.Scale(1.05f);
camPosition.Add(camTarget);
}
break;
case Keys.Up: //摄像机绕X轴顺时针旋转(目标逆时针旋转)
{
camPosition.Subtract(camTarget);
tempV = Vector3.Transform(
camPosition,
Matrix.RotationQuaternion(
Quaternion.RotationAxis(new Vector3(
device.Transform.View.M11,
device.Transform.View.M21,
device.Transform.View.M31),
angle)));
camPosition.X = tempV.X + camTarget.X;
camPosition.Y = tempV.Y + camTarget.Y;
camPosition.Z = tempV.Z + camTarget.Z;
}
break;
case Keys.Down: //摄像机绕X轴逆时针旋转
{
camPosition.Subtract(camTarget);
tempV = Vector3.Transform(
camPosition,
Matrix.RotationQuaternion(
Quaternion.RotationAxis(new Vector3(
device.Transform.View.M11,
device.Transform.View.M21,
device.Transform.View.M31),
-angle)));
camPosition.X = tempV.X + camTarget.X;
camPosition.Y = tempV.Y + camTarget.Y;
camPosition.Z = tempV.Z + camTarget.Z;
}
break;
case Keys.Left: //摄像机绕Y轴顺时针旋转(目标逆时针旋转)
{
camPosition.Subtract(camTarget);
tempV = Vector3.Transform(
camPosition,
Matrix.RotationQuaternion(Quaternion.RotationAxis(
new Vector3(device.Transform.View.M12,
device.Transform.View.M22,
device.Transform.View.M32),
angle)));
camPosition.X = tempV.X + camTarget.X;
camPosition.Y = tempV.Y + camTarget.Y;
camPosition.Z = tempV.Z + camTarget.Z;
}
break;
case Keys.Right: //摄像机绕Y轴逆时针旋转
{
camPosition.Subtract(camTarget);
tempV = Vector3.Transform(
camPosition,
Matrix.RotationQuaternion(Quaternion.RotationAxis(
new Vector3(device.Transform.View.M12,
device.Transform.View.M22,
device.Transform.View.M32),
-angle)));
camPosition.X = tempV.X + camTarget.X;
camPosition.Y = tempV.Y + camTarget.Y;
camPosition.Z = tempV.Z + camTarget.Z;
}
break;
case Keys.S: //左平移
{
camPosition.Subtract(camTarget);
camTarget.X -= 1f;
camPosition.Add(camTarget);
}
break;
case Keys.F: //右平移
{
camPosition.Subtract(camTarget);
camTarget.X += 1f;
camPosition.Add(camTarget);
}
break;
}
device.Transform.View = Matrix.LookAtLH(
camPosition,
camTarget,
camUp);
this.Invalidate();
}
}
效果: