MeshForm.cs
上传用户:lslight
上传日期:2022-01-10
资源大小:14248k
文件大小:24k
源码类别:

DirextX编程

开发平台:

C#

  1. ///////////////////////////////////////////////////////////////////////
  2. //      ■■■■     ■■■■■       ■■■■       ■       ■      //
  3. //    ■                 ■         ■               ■       ■      //
  4. //    ■                 ■         ■    ■■■     ■       ■      //
  5. //    ■                 ■         ■       ■      ■       ■      //
  6. //      ■■■■         ■           ■■■■         ■■■■       //
  7. // Copyright (c) 三峡大学水利与环境学院 肖泽云. All rights reserved.  //
  8. ////////////////////////////////////////////////////////////////////////
  9. using System;
  10. using System.ComponentModel;
  11. using System.Data;
  12. using System.Drawing;
  13. using System.Windows.Forms;
  14. using Microsoft.DirectX;
  15. using Microsoft.DirectX.Direct3D;
  16. namespace Mesh网格
  17. {
  18.     public partial class MeshForm : Form
  19.     {
  20.         Device device = null;//定义绘图设备
  21.         private CustomVertex.PositionTextured[] vertices;//定义顶点变量
  22.         private Texture texture;//定义贴图变量
  23.         private Material material;//定义材质变量
  24.         private float angleY=0.01f;//定义绕Y轴旋转变量
  25.         private Vector3 CamPostion = new Vector3(0, 30, -30);//定义摄像机位置
  26.         private Vector3 CamTarget = new Vector3(0, 0, 0);//定义摄像机目标位置
  27.         private int mouseLastX,mouseLastY;//记录鼠标按下时的坐标位置
  28.         private bool isRotateByMouse=false;//记录是否由鼠标控制旋转
  29.         private bool isMoveByMouse = false;//记录是否由鼠标控制移动
  30.         private Mesh meshObj;//定义网格对象
  31.         private Material[] meshMaterials; //定义网格材质对象
  32.         private Texture[] meshTextures;   // 定义网格贴图对象
  33.         private Mesh meshObjCopy;//定义复制的网格对象
  34.         private ProgressiveMesh progMesh;//定义渐进网格对象
  35.         private int numFaces;//定义渐进网格的面数目
  36.         private AnimationRootFrame rootFrame;
  37.         private Mesh[] meshArrayObj;//定义网格模型数组
  38.         private Matrix[] meshPosition;//定义网格模型空间坐标数组
  39.         public MeshForm()
  40.         {
  41.             this.ClientSize = new Size(800, 600);//指定窗体尺寸
  42.             this.Text = "Mesh网格";//指定窗体标题
  43.         }
  44.         public bool InitializeDirect3D()
  45.         {
  46.             try
  47.             {
  48.                 PresentParameters presentParams = new PresentParameters();
  49.                 presentParams.Windowed = true; //指定以Windows窗体形式显示
  50.                 presentParams.SwapEffect = SwapEffect.Discard; //当前屏幕绘制后它将自动从内存中删除
  51.                 presentParams.AutoDepthStencilFormat = DepthFormat.D16;
  52.                 presentParams.EnableAutoDepthStencil = true;
  53.                 presentParams.PresentationInterval = PresentInterval.Immediate;
  54.                 device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams); //实例化device对象
  55.                 
  56.                 //meshObj = Mesh.Box(device, 5f, 3f, 6f);//定义长方体几何对象
  57.                 meshObj = Mesh.Cylinder(device, 10f, 10f, 50f, 20, 20);//定义圆柱体对象
  58.                 //meshObj = Mesh.Polygon(device, 10f, 5);//定义五边形对象
  59.                 //meshObj = Mesh.Sphere(device, 10f, 20, 20);//定义球体对象
  60.                 //meshObj = Mesh.Torus(device, 2f, 6f, 20, 40);//定义圆环对象
  61.                 //meshObj = Mesh.Teapot(device);//定义茶壶对象
  62.                 //MeshDeclaration(20);//定义网格
  63.                 /*
  64.                 Vector3[] points = new Vector3[8];
  65.                 points[0] = new Vector3(0f, 3f, 0f);
  66.                 points[1] = new Vector3(0f, 3f, 6f);
  67.                 points[2] = new Vector3(5f, 3f, 6f);
  68.                 points[3] = new Vector3(5f, 3f, 0f);
  69.                 points[4] = new Vector3(0f, 0f, 0f);
  70.                 points[5] = new Vector3(0f, 0f, 6f);
  71.                 points[6] = new Vector3(5f, 0f, 6f);
  72.                 points[7] = new Vector3(5f, 0f, 0f);
  73.                 DrawHexClass drawHexClass = new DrawHexClass(device, points);
  74.                 //meshObj = drawHexClass.MeshObject;
  75.                 meshObj = DrawHexClass.DrawHexObject(device, points);
  76.                 */
  77.                 //从X文件导入网格
  78.                 //meshObj = Mesh.FromFile(@"E:DirectXDirectX_C#MediaModelsbox03.X", MeshFlags.SystemMemory, device);
  79.                 //MeshFromFile(@"E:DirectXDirectX_C#MediaModels", "box03.X");
  80.                 //meshObjCopy=meshObj.Clone(meshObj.Options.Value, meshObj.VertexFormat, device);
  81.                 int[] adjacency=new int[meshObj.NumberFaces*3];
  82.                 meshObj.GenerateAdjacency(0.01f,adjacency);
  83.                 progMesh = new ProgressiveMesh(meshObj, adjacency, meshObj.NumberFaces/30, MeshFlags.SimplifyFace);
  84.                 numFaces = meshObj.NumberFaces / 30;//设置渐进网格初始值
  85.                 //三维文字网格
  86.                 //System.Drawing.Font myFont = new System.Drawing.Font("Times New Roman", 28, FontStyle.Regular);//设置字体
  87.                 //meshObj = Mesh.TextFromFont(device, myFont, "Hello,DirectX!", 0.01f, 0.1f);
  88.                 VertexDeclaration();//定义顶点
  89.                 LoadTexturesAndMaterials();//导入贴图和材质
  90.                 //定义模型
  91.                 meshArrayObj = new Mesh[5];
  92.                 meshArrayObj[0] = Mesh.Cylinder(device, 1f, 1f, 5f, 10, 10);//定义圆柱体对象
  93.                 meshArrayObj[1] = Mesh.Polygon(device, 1f, 5);//定义五边形对象
  94.                 meshArrayObj[2] = Mesh.Sphere(device, 1f, 10, 10);//定义球体对象
  95.                 meshArrayObj[3] = Mesh.Torus(device, 0.2f, 0.6f, 10, 20);//定义圆环对象
  96.                 meshArrayObj[4] = Mesh.Teapot(device);//定义茶壶对象
  97.                 //定义模型坐标
  98.                 meshPosition = new Matrix[5];
  99.                 //meshPosition[0] = Matrix.Translation(0f,0f,-10f);
  100.                 meshPosition[0] = Matrix.RotationX((float)Math.PI/2) * Matrix.Translation(0f, 0f, -10f);
  101.                 meshPosition[1] = Matrix.Translation(-20f, 0f, 0f);
  102.                 meshPosition[2] = Matrix.Translation(-10f, 0f, 0f);
  103.                 //meshPosition[3] = Matrix.Translation(0f, 0f, 0f);
  104.                 meshPosition[3] = Matrix.RotationX((float)Math.PI / 2) * Matrix.Translation(0f, 0f, 0f);
  105.                 meshPosition[4] = Matrix.Translation(10f, 0f, 10f);
  106.                 return true;
  107.             }
  108.             catch (DirectXException e)
  109.             {
  110.                 MessageBox.Show(e.ToString(), "Error"); //处理异常
  111.                 return false;
  112.             }
  113.         }
  114.         private void MeshDeclaration(int count)//定义网格
  115.         {
  116.             //定义索引
  117.             Int16[] arrayIndices = new Int16[(count - 1) * (count - 1) * 6];
  118.             //定义顶点
  119.             CustomVertex.PositionColored[] arrayVertices=new CustomVertex.PositionColored[count*count];
  120.             //定义属性
  121.             AttributeRange attributeRange=new AttributeRange();
  122.             //实例化网格对象
  123.             meshObj = new Mesh(arrayIndices.Length / 3, arrayVertices.Length, MeshFlags.SystemMemory, CustomVertex.PositionColored.Format, device);
  124.             //设置顶点坐标值
  125.             for (int i = 0; i < count; i++)
  126.             {
  127.                 for (int j = 0; j < count; j++)
  128.                 {                  
  129.                     arrayVertices[j + i * count].X = (float)j;
  130.                     arrayVertices[j + i * count].Y = 0f;
  131.                     arrayVertices[j + i * count].Z = (float)i;
  132.                     arrayVertices[j + i * count].Color = Color.DarkGreen.ToArgb();
  133.                 }
  134.             }
  135.             //设置索引
  136.             for (int i = 0; i < count - 1; i++)
  137.             {
  138.                 for (int j = 0; j < count - 1; j++)
  139.                 {                    
  140.                     int num=6*(i*(count-1)+j);
  141.                     arrayIndices[num] = (Int16)(i * count + j);
  142.                     arrayIndices[num + 1] = (Int16)(i * count + j + 1);
  143.                     arrayIndices[num + 2] = (Int16)((i + 1) * count + j);
  144.                     arrayIndices[num + 3] = (Int16)(i * count + j + 1);
  145.                     arrayIndices[num + 4] = (Int16)((i + 1) * count + j + 1);
  146.                     arrayIndices[num + 5] = (Int16)((i + 1) * count + j);                   
  147.                 }
  148.             }
  149.             //设置属性
  150.             attributeRange.AttributeId = 0;
  151.             attributeRange.FaceStart = 0;
  152.             attributeRange.FaceCount = arrayIndices.Length / 3;
  153.             attributeRange.VertexStart = 0;
  154.             attributeRange.VertexCount = arrayVertices.Length;
  155.             //设置网格对象的索引缓冲和顶点缓冲数据
  156.             meshObj.VertexBuffer.SetData(arrayVertices, 0, LockFlags.None);
  157.             meshObj.IndexBuffer.SetData(arrayIndices, 0, LockFlags.None);
  158.             meshObj.SetAttributeTable(new AttributeRange[] { attributeRange }); 
  159.         }
  160.         private void MeshFromFile(string xFilePath,string xFileName)//从文件导入网格
  161.         {
  162.             ExtendedMaterial[] materials;
  163.             //从X文件导入网格
  164.             meshObj = Mesh.FromFile(xFilePath + "\" + xFileName, MeshFlags.SystemMemory, device, out materials);
  165.             if (meshTextures == null)
  166.             {
  167.                 meshTextures = new Texture[materials.Length];
  168.                 meshMaterials = new Material[materials.Length];
  169.                 for (int i = 0; i < materials.Length; i++)
  170.                 {
  171.                     meshMaterials[i] = materials[i].Material3D;
  172.                     meshMaterials[i].Ambient = meshMaterials[i].Diffuse;
  173.                     // 创建贴图
  174.                     Console.WriteLine(i.ToString() + ":" + materials[i].ToString());
  175.                     meshTextures[i] = TextureLoader.FromFile(device, xFilePath + "\" + materials[i].TextureFilename);                    
  176.                 }
  177.             }            
  178.         }
  179.         private void VertexDeclaration()//定义顶点
  180.         {
  181.             vertices = new CustomVertex.PositionTextured[6];
  182.             vertices[0].Position = new Vector3(10f, 0f, 10f);
  183.             vertices[0].Tu = 1;
  184.             vertices[0].Tv = 0;
  185.             vertices[1].Position = new Vector3(-10f, 0f, -10f);
  186.             vertices[1].Tu = 0;
  187.             vertices[1].Tv = 1;
  188.             vertices[2].Position = new Vector3(10f,0f,-10f);
  189.             vertices[2].Tu = 1;
  190.             vertices[2].Tv = 1;            
  191.             vertices[3].Position = new Vector3(-10f, 0f,-10f);
  192.             vertices[3].Tu = 0;
  193.             vertices[3].Tv = 1;
  194.             vertices[4].Position = new Vector3(10f, 0f, 10f);
  195.             vertices[4].Tu = 1;
  196.             vertices[4].Tv = 0;
  197.             vertices[5].Position = new Vector3(-10f, 0f, 10f);
  198.             vertices[5].Tu = 0;
  199.             vertices[5].Tv = 0;               
  200.         }
  201.         private void LoadTexturesAndMaterials()//导入贴图和材质
  202.         {
  203.             material = new Material();
  204.             material.Diffuse = Color.White;
  205.             material.Ambient = Color.White;
  206.             material.Specular = Color.LightGray;
  207.             material.SpecularSharpness = 15.0F;
  208.             device.Material = material;
  209.             texture = TextureLoader.FromFile(device, "CTGU.jpg");
  210.         }
  211.         public void Render()
  212.         {
  213.             if (device == null)   //如果device为空则不渲染
  214.             {
  215.                 return;
  216.             }
  217.             SetUpCamera();
  218.             device.Clear(ClearFlags.Target|ClearFlags.ZBuffer, Color.DarkSlateBlue, 1.0f, 0);  //清除windows界面为深蓝色
  219.             device.BeginScene();
  220.             //在此添加渲染图形代码
  221.             //device.RenderState.FillMode = FillMode.WireFrame;
  222.             /*
  223.             AttributeRange[] attributeRange = meshObj.GetAttributeTable();
  224.             for (int i = 0; i < attributeRange.Length; i++)
  225.             {
  226.                 Console.WriteLine("attributeRange[{0}].AttributeId:" + attributeRange[i].AttributeId.ToString(), i);
  227.                 Console.WriteLine("attributeRange[{0}].FaceCount:" + attributeRange[i].FaceCount.ToString(), i);
  228.                 Console.WriteLine("attributeRange[{0}].FaceStart:" + attributeRange[i].FaceStart.ToString(), i);
  229.                 Console.WriteLine("attributeRange[{0}].VertexCount:" + attributeRange[i].VertexCount.ToString(), i);
  230.                 Console.WriteLine("attributeRange[{0}].VertexStart:" + attributeRange[i].VertexStart.ToString(), i);
  231.             }
  232.             //获取邻接信息
  233.             int[] adjacency = new int[meshObj.NumberFaces * 3];
  234.             meshObj.GenerateAdjacency(0.01f, adjacency);
  235.             for (int i = 0; i < adjacency.Length; i++)
  236.             {
  237.                 Console.Write("adjacency[{0}]:{1}     ", i,adjacency[i]);
  238.             }
  239.              */
  240.             //绘制原始网格
  241.             //device.Transform.World = Matrix.Add(device.Transform.World, Matrix.Translation(100f, 0f, 0f));
  242.             //meshObj.DrawSubset(0);
  243.             /*
  244.             for (int i = 0; i < meshObj.NumberAttributes; i++)
  245.             {
  246.                 //设置网格子集的材质和贴图
  247.                 device.Material = meshMaterials[i];
  248.                 device.SetTexture(0, meshTextures[i]);
  249.                 //绘制网格子集
  250.                 meshObj.DrawSubset(i);
  251.             }
  252.              */ 
  253.             //绘制复制的网格
  254.             //device.Transform.World = Matrix.Subtract(device.Transform.World, Matrix.Translation(100f, 0f, 0f));
  255.             //progMesh.DrawSubset(0);
  256.             //meshObjCopy.DrawSubset(0);
  257.                         
  258.             //灯光设置            
  259.             device.RenderState.Lighting = true;
  260.             device.Lights[0].Type = LightType.Directional;
  261.             device.Lights[0].Diffuse = System.Drawing.Color.Red;
  262.             device.Lights[0].Direction = new Vector3(-50.0f,50.0f,50.0f);
  263.             device.Lights[0].Enabled = true; //打开灯光
  264.             device.Lights[1].Type = LightType.Directional;
  265.             device.Lights[1].Diffuse = System.Drawing.Color.Green;
  266.             device.Lights[1].Direction = new Vector3(50f,-50.0f,50.0f);
  267.             device.Lights[1].Enabled = true; //打开灯光            
  268.             device.RenderState.Ambient = Color.SlateGray;           
  269.             
  270.             device.RenderState.CullMode=Cull.None;
  271.             //device.SetTexture(0, texture);//设置贴图
  272.             //device.VertexFormat = CustomVertex.PositionTextured.Format;
  273.             //device.DrawUserPrimitives(PrimitiveType.TriangleList, 2, vertices);
  274.             //device.RenderState.Lighting = false;
  275.             for (int i = 0; i < meshArrayObj.Length; i++)
  276.             {
  277.                 //设置当前世界矩阵
  278.                 device.Transform.World = meshPosition[i];
  279.                 meshArrayObj[i].DrawSubset(0);
  280.             }
  281.             device.EndScene();
  282.             device.Present();
  283.         }
  284.         private void SetUpCamera()//摄像机
  285.         {
  286.             Matrix viewMatrix = Matrix.LookAtLH(CamPostion, CamTarget, new Vector3(0, 1, 0));
  287.             device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, this.Width / this.Height, 1f, 500f);
  288.             device.Transform.View = viewMatrix;
  289.         }
  290.         protected override void OnKeyDown(KeyEventArgs e)
  291.         {
  292.             Vector4 tempV4;
  293.             Matrix currentView = device.Transform.View;//当前摄像机的视图矩阵
  294.             if (Control.ModifierKeys== Keys.Control && e.KeyCode == Keys.S)
  295.             {
  296.                 //定义衔接关系
  297.                 int[] adjacency = new int[meshObj.NumberFaces * 3];
  298.                 meshObj.GenerateAdjacency(0.01f, adjacency);
  299.                 //定义材质属性
  300.                 ExtendedMaterial[] exmaterials = new ExtendedMaterial[meshMaterials.Length];
  301.                 //设置材质
  302.                 for (int i = 0; i < meshMaterials.Length; i++)
  303.                 {
  304.                     exmaterials[i].Material3D = meshMaterials[i];
  305.                     exmaterials[i].TextureFilename = "";
  306.                 }                
  307.                 meshObj.Save("SaveBox012.x", adjacency, exmaterials, XFileFormat.Text);
  308.                 MessageBox.Show("保存文件成功!");
  309.             } 
  310.             switch (e.KeyCode)
  311.             {
  312.                 case Keys.Left:
  313.                     CamPostion.Subtract(CamTarget);
  314.                     tempV4 = Vector3.Transform(CamPostion, Matrix.RotationQuaternion(
  315.                             Quaternion.RotationAxis(new Vector3(currentView.M12, currentView.M22, currentView.M32), -angleY)));
  316.                     CamPostion.X = tempV4.X + CamTarget.X;
  317.                     CamPostion.Y = tempV4.Y + CamTarget.Y;
  318.                     CamPostion.Z = tempV4.Z + CamTarget.Z;
  319.                     break;
  320.                 case Keys.Right:
  321.                     CamPostion.Subtract(CamTarget);
  322.                     tempV4 = Vector3.Transform(CamPostion, Matrix.RotationQuaternion(
  323.                             Quaternion.RotationAxis(new Vector3(currentView.M12, currentView.M22, currentView.M32), angleY)));
  324.                     CamPostion.X = tempV4.X + CamTarget.X;
  325.                     CamPostion.Y = tempV4.Y + CamTarget.Y;
  326.                     CamPostion.Z = tempV4.Z + CamTarget.Z;
  327.                     break;
  328.                 case Keys.Up:
  329.                     CamPostion.Subtract(CamTarget);
  330.                     tempV4 = Vector3.Transform(CamPostion, Matrix.RotationQuaternion(
  331.                        Quaternion.RotationAxis(new Vector3(device.Transform.View.M11
  332.                        , device.Transform.View.M21, device.Transform.View.M31), -angleY)));
  333.                     CamPostion.X = tempV4.X + CamTarget.X;
  334.                     CamPostion.Y = tempV4.Y + CamTarget.Y;
  335.                     CamPostion.Z = tempV4.Z + CamTarget.Z;
  336.                     break;
  337.                 case Keys.Down:
  338.                     CamPostion.Subtract(CamTarget);
  339.                     tempV4 = Vector3.Transform(CamPostion, Matrix.RotationQuaternion(
  340.                        Quaternion.RotationAxis(new Vector3(device.Transform.View.M11
  341.                        , device.Transform.View.M21, device.Transform.View.M31), angleY)));
  342.                     CamPostion.X = tempV4.X + CamTarget.X;
  343.                     CamPostion.Y = tempV4.Y + CamTarget.Y;
  344.                     CamPostion.Z = tempV4.Z + CamTarget.Z;
  345.                     break;
  346.                 case Keys.Add:
  347.                     CamPostion.Subtract(CamTarget);
  348.                     CamPostion.Scale(0.95f);
  349.                     CamPostion.Add(CamTarget);
  350.                     numFaces++;
  351.                     progMesh.NumberFaces = numFaces;
  352.                     break;
  353.                 case Keys.Subtract:
  354.                     CamPostion.Subtract(CamTarget);
  355.                     CamPostion.Scale(1.05f);
  356.                     CamPostion.Add(CamTarget);
  357.                     numFaces--;
  358.                     progMesh.NumberFaces = numFaces;
  359.                     break;
  360.             }
  361.             Console.WriteLine(numFaces.ToString());
  362.             Matrix viewMatrix = Matrix.LookAtLH(CamPostion, CamTarget, new Vector3(0, 1, 0));
  363.             device.Transform.View = viewMatrix;
  364.         }
  365.         protected override void OnMouseDown(MouseEventArgs e)
  366.         {
  367.             if (e.Button == MouseButtons.Left)
  368.             {
  369.                 mouseLastX = e.X;
  370.                 mouseLastY = e.Y;
  371.                 isRotateByMouse = true;
  372.             }
  373.             else if (e.Button == MouseButtons.Middle)
  374.             {
  375.                 mouseLastX = e.X;
  376.                 mouseLastY = e.Y;
  377.                 isMoveByMouse=true;
  378.             }
  379.         }
  380.         
  381.         protected override void OnMouseUp(MouseEventArgs e)
  382.         {
  383.             //base.OnMouseUp(e);
  384.             /*float tempAngleY = 2*(float)(e.X - mouseLastX) / this.Width;
  385.             Vector4 tempV4 = Vector3.Transform(CamPostion, Matrix.RotationQuaternion(
  386.                 Quaternion.RotationAxis(new Vector3(0, 1, 0), tempAngleY)));
  387.             CamPostion.X = tempV4.X; CamPostion.Y = tempV4.Y; CamPostion.Z = tempV4.Z;
  388.             Matrix viewMatrix = Matrix.LookAtLH(CamPostion, CamTarget, new Vector3(0, 1, 0));
  389.             device.Transform.View = viewMatrix;
  390.              */
  391.             isRotateByMouse = false;
  392.             isMoveByMouse = false;
  393.         }
  394.         protected override void OnMouseMove(MouseEventArgs e)
  395.         {
  396.             if (isRotateByMouse)
  397.             {
  398.                 Matrix currentView = device.Transform.View;//当前摄像机的视图矩阵
  399.                 float tempAngleY = 2 * (float)(e.X - mouseLastX) / this.Width;
  400.                 CamPostion.Subtract(CamTarget);
  401.                 Vector4 tempV4 = Vector3.Transform(CamPostion, Matrix.RotationQuaternion(
  402.                     Quaternion.RotationAxis(new Vector3(currentView.M12, currentView.M22, currentView.M32), tempAngleY)));
  403.                 CamPostion.X = tempV4.X;
  404.                 CamPostion.Y = tempV4.Y;
  405.                 CamPostion.Z = tempV4.Z;
  406.                 float tempAngleX = 4 * (float)(e.Y - mouseLastY) / this.Height;
  407.                 tempV4 = Vector3.Transform(CamPostion, Matrix.RotationQuaternion(
  408.                     Quaternion.RotationAxis(new Vector3(currentView.M11, currentView.M21, currentView.M31), tempAngleX)));
  409.                 CamPostion.X = tempV4.X + CamTarget.X;
  410.                 CamPostion.Y = tempV4.Y + CamTarget.Y;
  411.                 CamPostion.Z = tempV4.Z + CamTarget.Z;
  412.                 Matrix viewMatrix = Matrix.LookAtLH(CamPostion, CamTarget, new Vector3(0, 1, 0));
  413.                 device.Transform.View = viewMatrix;
  414.                 mouseLastX = e.X;
  415.                 mouseLastY = e.Y;
  416.             }
  417.             else if (isMoveByMouse)
  418.             {
  419.                 Matrix currentView = device.Transform.View;//当前摄像机的视图矩阵
  420.                 float moveFactor=0.01f;
  421.                 CamTarget.X += -moveFactor * ((e.X - mouseLastX) * currentView.M11 - (e.Y - mouseLastY) * currentView.M12);
  422.                 CamTarget.Y += -moveFactor * ((e.X - mouseLastX) * currentView.M21 - (e.Y - mouseLastY) * currentView.M22);
  423.                 CamTarget.Z += -moveFactor * ((e.X - mouseLastX) * currentView.M31 - (e.Y - mouseLastY) * currentView.M32);
  424.                 CamPostion.X +=- moveFactor * ((e.X - mouseLastX) * currentView.M11 - (e.Y - mouseLastY) * currentView.M12);
  425.                 CamPostion.Y += -moveFactor * ((e.X - mouseLastX) * currentView.M21 - (e.Y - mouseLastY) * currentView.M22);
  426.                 CamPostion.Z += -moveFactor * ((e.X - mouseLastX) * currentView.M31 - (e.Y - mouseLastY) * currentView.M32);
  427.              
  428.                 Matrix viewMatrix = Matrix.LookAtLH(CamPostion, CamTarget, new Vector3(0, 1, 0));
  429.                 device.Transform.View = viewMatrix;
  430.                 mouseLastX = e.X;
  431.                 mouseLastY = e.Y;
  432.             }
  433.         }
  434.         protected override void OnMouseWheel(MouseEventArgs e)
  435.         {
  436.             float scaleFactor = -(float)e.Delta / 2000 + 1f;
  437.             CamPostion.Subtract(CamTarget);
  438.             CamPostion.Scale(scaleFactor);
  439.             CamPostion.Add(CamTarget);
  440.             Matrix viewMatrix = Matrix.LookAtLH(CamPostion, CamTarget, new Vector3(0, 1, 0));
  441.             device.Transform.View = viewMatrix;
  442.         }
  443.         static void Main()
  444.         {
  445.             MeshForm CameraTransform = new MeshForm(); //创建窗体对象
  446.             if (CameraTransform.InitializeDirect3D() == false) //检查Direct3D是否启动
  447.             {
  448.                 MessageBox.Show("无法启动Direct3D!", "错误!");
  449.                 return;
  450.             }
  451.             CameraTransform.Show(); //如果一切都初始化成功,则显示窗体
  452.             while (CameraTransform.Created) //设置一个循环用于实时更新渲染状态
  453.             {
  454.                 CameraTransform.Render(); //保持device渲染,直到程序结束
  455.                 //CameraTransform.SetUpCamera();//建立摄像机
  456.                 Application.DoEvents(); //处理键盘鼠标等输入事件
  457.             }
  458.         }
  459.     }
  460. }