zoukankan      html  css  js  c++  java
  • 摄像机矩阵变换

    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();
           }
       }

    效果:

    image放大 image缩小image平移image俯仰image旋转

  • 相关阅读:
    【转】Redis和Memcache对比及选择
    Ubuntu下php环境的搭建
    【HTML和CSS】总结
    【python】 The different between ' %r ' and ' %s '
    Learn Python The Hard Way
    Vim 插件配置及快捷键
    sublime-text 插件配置
    mysql-5.7在CentOS-7下的rpm安装
    oracle pdb基本管理
    Oracle 12cR2 Installation On CentOS-7
  • 原文地址:https://www.cnblogs.com/sharpfeng/p/1954085.html
Copyright © 2011-2022 走看看