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

DirextX编程

开发平台:

C#

  1. ////////////////////////////////////////////////////////////////////////
  2. //      ■■■■     ■■■■■       ■■■■       ■       ■      //
  3. //    ■                 ■         ■               ■       ■      //
  4. //    ■                 ■         ■    ■■■     ■       ■      //
  5. //    ■                 ■         ■       ■      ■       ■      //
  6. //      ■■■■         ■           ■■■■         ■■■■       //
  7. // Copyright (c) 三峡大学水利与环境学院 肖泽云. All rights reserved.  //
  8. ////////////////////////////////////////////////////////////////////////
  9. using System;
  10. using System.Collections.Generic;
  11. using System.ComponentModel;
  12. using System.Drawing;
  13. using System.Data;
  14. using System.Text;
  15. using System.Windows.Forms;
  16. using Microsoft.DirectX;
  17. using Microsoft.DirectX.Direct3D;
  18. using System.Collections;
  19. namespace CTGU.XZY
  20. {
  21.     public partial class XSceneControl : UserControl
  22.     {
  23.         public Vector3 CamPostion = new Vector3(0, 100, 100);//定义摄像机位置
  24.         public Vector3 CamTarget = new Vector3(125, 30, 125);//定义摄像机目标位置
  25.         private Device device = null;//定义绘图设备
  26.         private ArrayList lastCamPosAndTarg = new ArrayList();//记录上一次摄像机位置
  27.         private int sceneIndex = 0;//记录当前场景的序号
  28.         private float angleY = 0.01f;//定义绕Y轴旋转变量
  29.         private int mouseLastX, mouseLastY;//记录鼠标按下时的坐标位置
  30.         private bool isRotateByMouse = false;//记录是否由鼠标控制旋转
  31.         private bool isMoveByMouse = false;//记录是否由鼠标控制移动
  32.         private CustomVertex.PositionTextured[] vertices;//定义顶点变量
  33.         private Texture texture;//定义贴图变量
  34.         private Material material;//定义材质变量
  35.         private VertexBuffer vertexBuffer;//定义顶点缓冲变量
  36.         private IndexBuffer indexBuffer;//定义索引缓冲变量
  37.         private int[] indices;//定义索引号变量
  38.         private int xCount = 5, yCount = 4;//定义横向和纵向网格数目
  39.         private float cellHeight = 1f, cellWidth = 1f;//定义单元的宽度和长度
  40.         public string texturePath = Application.StartupPath + "\heightMap.BMP";// = @"E:DirectXDirectX_C#MediacolorMap.jpg";//定义贴图路径
  41.         public string heightMapPath = Application.StartupPath + "\colorMap.jpg";// = @"E:DirectXDirectX_C#MediaheightMap.BMP";//定义高度图路径
  42.         public XSceneControl()
  43.         {
  44.             InitializeComponent();
  45.         }
  46.         public bool InitializeDirect3D()
  47.         {
  48.             try
  49.             {
  50.                 PresentParameters presentParams = new PresentParameters();
  51.                 presentParams.Windowed = true; //指定以Windows窗体形式显示
  52.                 presentParams.SwapEffect = SwapEffect.Discard; //当前屏幕绘制后它将自动从内存中删除
  53.                 presentParams.AutoDepthStencilFormat = DepthFormat.D16;
  54.                 presentParams.EnableAutoDepthStencil = true;
  55.                 presentParams.PresentationInterval = PresentInterval.Immediate;
  56.                 device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams); //实例化device对象
  57.                 Matrix viewMatrix = Matrix.LookAtLH(CamPostion, CamTarget, new Vector3(0, 1, 0));
  58.                 device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, this.Width / this.Height, 1f, 1000f);
  59.                 device.Transform.View = viewMatrix;
  60.                 lastCamPosAndTarg.Add(device.Transform.View);//记录这次摄像机的位置
  61.                 //VertexDeclaration();//定义顶点
  62.                 //IndicesDeclaration();//定义索引缓冲
  63.                 //LoadTexturesAndMaterials();//导入贴图和材质
  64.                 return true;
  65.             }
  66.             catch (DirectXException e)
  67.             {
  68.                 MessageBox.Show(e.ToString(), "Error"); //处理异常
  69.                 return false;
  70.             }
  71.         }
  72.         public void BuildTerrain(string heightmapPath,string texturemapPath)
  73.         {
  74.             heightMapPath = heightmapPath;
  75.             texturePath = texturemapPath;
  76.             VertexDeclaration();//定义顶点
  77.             IndicesDeclaration();//定义索引缓冲
  78.             LoadTexturesAndMaterials();//导入贴图和材质
  79.             Render();
  80.         }
  81.         private void VertexDeclaration()//定义顶点
  82.         {
  83.             string bitmapPath = heightMapPath;
  84.             Bitmap bitmap = new Bitmap(bitmapPath);
  85.             xCount = (bitmap.Width - 1) / 2;
  86.             yCount = (bitmap.Height - 1) / 2;
  87.             cellWidth = bitmap.Width / xCount;
  88.             cellHeight = bitmap.Height / yCount;
  89.             vertexBuffer = new VertexBuffer(typeof(CustomVertex.PositionTextured), (xCount + 1) * (yCount + 1), device,
  90.                 Usage.Dynamic | Usage.WriteOnly, CustomVertex.PositionColored.Format, Pool.Default);
  91.             vertices = new CustomVertex.PositionTextured[(xCount + 1) * (yCount + 1)];//定义顶点
  92.             for (int i = 0; i < yCount + 1; i++)
  93.             {
  94.                 for (int j = 0; j < xCount + 1; j++)
  95.                 {
  96.                     Color color = bitmap.GetPixel((int)(j * cellWidth), (int)(i * cellHeight));
  97.                     float height = float.Parse(color.R.ToString()) + float.Parse(color.G.ToString()) + float.Parse(color.B.ToString());
  98.                     height /= 10;
  99.                     vertices[j + i * (xCount + 1)].Position = new Vector3(j * cellWidth, height, i * cellHeight);
  100.                     vertices[j + i * (xCount + 1)].Tu = (float)j / (xCount + 1);
  101.                     vertices[j + i * (xCount + 1)].Tv = (float)i / (yCount + 1);
  102.                 }
  103.             }
  104.             vertexBuffer.SetData(vertices, 0, LockFlags.None);
  105.             CamTarget = new Vector3(bitmap.Width / 2, 0f, bitmap.Height / 2);//设置摄像机目标位置
  106.         }
  107.         private void IndicesDeclaration()//定义索引
  108.         {
  109.             indexBuffer = new IndexBuffer(typeof(int), 6 * xCount * yCount, device, Usage.WriteOnly, Pool.Default);
  110.             indices = new int[6 * xCount * yCount];
  111.             for (int i = 0; i < yCount; i++)
  112.             {
  113.                 for (int j = 0; j < xCount; j++)
  114.                 {
  115.                     indices[6 * (j + i * xCount)] = j + i * (xCount + 1);
  116.                     indices[6 * (j + i * xCount) + 1] = j + (i + 1) * (xCount + 1);
  117.                     indices[6 * (j + i * xCount) + 2] = j + i * (xCount + 1) + 1;
  118.                     indices[6 * (j + i * xCount) + 3] = j + i * (xCount + 1) + 1;
  119.                     indices[6 * (j + i * xCount) + 4] = j + (i + 1) * (xCount + 1);
  120.                     indices[6 * (j + i * xCount) + 5] = j + (i + 1) * (xCount + 1) + 1;
  121.                 }
  122.             }
  123.             indexBuffer.SetData(indices, 0, LockFlags.None);
  124.         }
  125.         private void LoadTexturesAndMaterials()//导入贴图和材质
  126.         {
  127.             material = new Material();
  128.             material.Diffuse = Color.White;
  129.             material.Specular = Color.LightGray;
  130.             material.SpecularSharpness = 15.0F;
  131.             device.Material = material;
  132.             texture = TextureLoader.FromFile(device, texturePath);
  133.         }
  134.         /// <summary>
  135.         /// 渲染场景
  136.         /// </summary>
  137.         public void Render()
  138.         {
  139.             if (device == null)   //如果device为空则不渲染
  140.             {
  141.                 return;
  142.             }
  143.             device.Clear(ClearFlags.Target|ClearFlags.ZBuffer, Color.DarkSlateBlue, 1.0f, 0);  //清除windows界面为深蓝色
  144.             device.BeginScene();
  145.             //在此添加渲染图形代码
  146.             device.RenderState.CullMode = Cull.None;
  147.             device.RenderState.FillMode = FillMode.Solid;
  148.             device.RenderState.Lighting = false;
  149.             if (indices != null)
  150.             {
  151.                 device.SetTexture(0, texture);//设置贴图
  152.                 device.VertexFormat = CustomVertex.PositionTextured.Format;
  153.                 device.SetStreamSource(0, vertexBuffer, 0);
  154.                 device.Indices = indexBuffer;
  155.                 device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, (xCount + 1) * (yCount + 1), 0, indices.Length / 3);
  156.             }
  157.             device.EndScene();
  158.             device.Present();
  159.         }
  160.         protected override void OnKeyDown(KeyEventArgs e)
  161.         {
  162.             Vector4 tempV4;
  163.             Matrix currentView = device.Transform.View;//当前摄像机的视图矩阵
  164.             switch (e.KeyCode)
  165.             {
  166.                 case Keys.Left:
  167.                     CamPostion.Subtract(CamTarget);
  168.                     tempV4 = Vector3.Transform(CamPostion, Matrix.RotationQuaternion(
  169.                             Quaternion.RotationAxis(new Vector3(currentView.M12, currentView.M22, currentView.M32), -angleY)));
  170.                     CamPostion.X = tempV4.X + CamTarget.X;
  171.                     CamPostion.Y = tempV4.Y + CamTarget.Y;
  172.                     CamPostion.Z = tempV4.Z + CamTarget.Z;
  173.                     break;
  174.                 case Keys.Right:
  175.                     CamPostion.Subtract(CamTarget);
  176.                     tempV4 = Vector3.Transform(CamPostion, Matrix.RotationQuaternion(
  177.                             Quaternion.RotationAxis(new Vector3(currentView.M12, currentView.M22, currentView.M32), angleY)));
  178.                     CamPostion.X = tempV4.X + CamTarget.X;
  179.                     CamPostion.Y = tempV4.Y + CamTarget.Y;
  180.                     CamPostion.Z = tempV4.Z + CamTarget.Z;
  181.                     break;
  182.                 case Keys.Up:
  183.                     CamPostion.Subtract(CamTarget);
  184.                     tempV4 = Vector3.Transform(CamPostion, Matrix.RotationQuaternion(
  185.                        Quaternion.RotationAxis(new Vector3(device.Transform.View.M11
  186.                        , device.Transform.View.M21, device.Transform.View.M31), -angleY)));
  187.                     CamPostion.X = tempV4.X + CamTarget.X;
  188.                     CamPostion.Y = tempV4.Y + CamTarget.Y;
  189.                     CamPostion.Z = tempV4.Z + CamTarget.Z;
  190.                     break;
  191.                 case Keys.Down:
  192.                     CamPostion.Subtract(CamTarget);
  193.                     tempV4 = Vector3.Transform(CamPostion, Matrix.RotationQuaternion(
  194.                        Quaternion.RotationAxis(new Vector3(device.Transform.View.M11
  195.                        , device.Transform.View.M21, device.Transform.View.M31), angleY)));
  196.                     CamPostion.X = tempV4.X + CamTarget.X;
  197.                     CamPostion.Y = tempV4.Y + CamTarget.Y;
  198.                     CamPostion.Z = tempV4.Z + CamTarget.Z;
  199.                     break;
  200.                 case Keys.Add:
  201.                     CamPostion.Subtract(CamTarget);
  202.                     CamPostion.Scale(0.95f);
  203.                     CamPostion.Add(CamTarget);
  204.                     break;
  205.                 case Keys.Subtract:
  206.                     CamPostion.Subtract(CamTarget);
  207.                     CamPostion.Scale(1.05f);
  208.                     CamPostion.Add(CamTarget);
  209.                     break;
  210.             }
  211.             Matrix viewMatrix = Matrix.LookAtLH(CamPostion, CamTarget, new Vector3(0, 1, 0));
  212.             device.Transform.View = viewMatrix;
  213.             Render();
  214.         }
  215.         protected override void OnMouseDown(MouseEventArgs e)
  216.         {
  217.             if (e.Button == MouseButtons.Left)
  218.             {
  219.                 mouseLastX = e.X;
  220.                 mouseLastY = e.Y;
  221.                 isRotateByMouse = true;
  222.             }
  223.             else if (e.Button == MouseButtons.Middle)
  224.             {
  225.                 mouseLastX = e.X;
  226.                 mouseLastY = e.Y;
  227.                 isMoveByMouse = true;
  228.             }
  229.             this.Focus();
  230.         }
  231.         protected override void OnMouseUp(MouseEventArgs e)
  232.         {
  233.             isRotateByMouse = false;
  234.             isMoveByMouse = false;
  235.             lastCamPosAndTarg.Add(device.Transform.View);//记录这次摄像机的位置
  236.             if (lastCamPosAndTarg.Count > 10)
  237.             {
  238.                 lastCamPosAndTarg.RemoveAt(0);
  239.             }
  240.             sceneIndex = lastCamPosAndTarg.Count;
  241.         }
  242.         protected override void OnMouseMove(MouseEventArgs e)
  243.         {
  244.             if (isRotateByMouse)
  245.             {
  246.                 Matrix currentView = device.Transform.View;//当前摄像机的视图矩阵
  247.                 float tempAngleY = 2 * (float)(e.X - mouseLastX) / this.Width;
  248.                 CamPostion.Subtract(CamTarget);
  249.                 Vector4 tempV4 = Vector3.Transform(CamPostion, Matrix.RotationQuaternion(
  250.                     Quaternion.RotationAxis(new Vector3(currentView.M12, currentView.M22, currentView.M32), tempAngleY)));
  251.                 CamPostion.X = tempV4.X;
  252.                 CamPostion.Y = tempV4.Y;
  253.                 CamPostion.Z = tempV4.Z;
  254.                 float tempAngleX = 4 * (float)(e.Y - mouseLastY) / this.Height;
  255.                 tempV4 = Vector3.Transform(CamPostion, Matrix.RotationQuaternion(
  256.                     Quaternion.RotationAxis(new Vector3(currentView.M11, currentView.M21, currentView.M31), tempAngleX)));
  257.                 CamPostion.X = tempV4.X + CamTarget.X;
  258.                 CamPostion.Y = tempV4.Y + CamTarget.Y;
  259.                 CamPostion.Z = tempV4.Z + CamTarget.Z;
  260.                 Matrix viewMatrix = Matrix.LookAtLH(CamPostion, CamTarget, new Vector3(0, 1, 0));
  261.                 device.Transform.View = viewMatrix;
  262.                 mouseLastX = e.X;
  263.                 mouseLastY = e.Y;
  264.                 Render();
  265.             }
  266.             else if (isMoveByMouse)
  267.             {
  268.                 Matrix currentView = device.Transform.View;//当前摄像机的视图矩阵
  269.                 float moveFactor = 0.01f;
  270.                 CamTarget.X += -moveFactor * ((e.X - mouseLastX) * currentView.M11 - (e.Y - mouseLastY) * currentView.M12);
  271.                 CamTarget.Y += -moveFactor * ((e.X - mouseLastX) * currentView.M21 - (e.Y - mouseLastY) * currentView.M22);
  272.                 CamTarget.Z += -moveFactor * ((e.X - mouseLastX) * currentView.M31 - (e.Y - mouseLastY) * currentView.M32);
  273.                 CamPostion.X += -moveFactor * ((e.X - mouseLastX) * currentView.M11 - (e.Y - mouseLastY) * currentView.M12);
  274.                 CamPostion.Y += -moveFactor * ((e.X - mouseLastX) * currentView.M21 - (e.Y - mouseLastY) * currentView.M22);
  275.                 CamPostion.Z += -moveFactor * ((e.X - mouseLastX) * currentView.M31 - (e.Y - mouseLastY) * currentView.M32);
  276.                 Matrix viewMatrix = Matrix.LookAtLH(CamPostion, CamTarget, new Vector3(0, 1, 0));
  277.                 device.Transform.View = viewMatrix;
  278.                 mouseLastX = e.X;
  279.                 mouseLastY = e.Y;
  280.                 Render();
  281.             }
  282.         }
  283.         protected override void OnMouseWheel(MouseEventArgs e)
  284.         {
  285.             float scaleFactor = -(float)e.Delta / 2000 + 1f;
  286.             CamPostion.Subtract(CamTarget);
  287.             CamPostion.Scale(scaleFactor);
  288.             CamPostion.Add(CamTarget);
  289.             Matrix viewMatrix = Matrix.LookAtLH(CamPostion, CamTarget, new Vector3(0, 1, 0));
  290.             device.Transform.View = viewMatrix;
  291.             Render();
  292.         }
  293.         
  294.         /// <summary>
  295.         /// 返回上一视图
  296.         /// </summary>
  297.         public void LastScene()//上一次场景
  298.         {
  299.             if (sceneIndex >= 2)
  300.             {
  301.                 sceneIndex = sceneIndex - 2;
  302.                 Matrix viewMatrix = (Matrix)lastCamPosAndTarg[sceneIndex];
  303.                 device.Transform.View = viewMatrix;
  304.                 Render();
  305.             }
  306.         }
  307.         /// <summary>
  308.         /// 返回下一视图
  309.         /// </summary>
  310.         public void NextScene()//下一次场景
  311.         {
  312.             if (sceneIndex < lastCamPosAndTarg.Count - 1)
  313.             {
  314.                 sceneIndex = sceneIndex + 1;
  315.                 Matrix viewMatrix = (Matrix)lastCamPosAndTarg[sceneIndex];
  316.                 device.Transform.View = viewMatrix;
  317.                 Render();
  318.             }
  319.         }
  320.         /// <summary>
  321.         /// 查看附视图
  322.         /// </summary>
  323.         public void TopView()//附视图
  324.         {
  325.             CamPostion = new Vector3(100f, 50f, 100.001f);
  326.             CamTarget = new Vector3(100f, 0f, 100f);
  327.             Matrix viewMatrix = Matrix.LookAtLH(CamPostion, CamTarget, new Vector3(0, 1, 0));
  328.             device.Transform.Projection = Matrix.OrthoLH((float)this.Width, (float)this.Height, 0.1f, 1000f);
  329.             device.Transform.View = viewMatrix;
  330.             Render();
  331.         }
  332.         /// <summary>
  333.         /// 查看左视图
  334.         /// </summary>
  335.         public void LeftView()//左视图
  336.         {
  337.             CamPostion = new Vector3(-100f, 0f, 100.0f);
  338.             CamTarget = new Vector3(100f, 0f, 100f);
  339.             Matrix viewMatrix = Matrix.LookAtLH(CamPostion, CamTarget, new Vector3(0, 1, 0));
  340.             device.Transform.View = viewMatrix;
  341.             device.Transform.Projection = Matrix.OrthoLH((float)this.Width, (float)this.Height, 0.1f, 1000f);
  342.             Render();
  343.         }
  344.         /// <summary>
  345.         /// 查看前视图
  346.         /// </summary>
  347.         public void FrontView()//前视图
  348.         {
  349.             CamPostion = new Vector3(100f, 00f, -100.0f);
  350.             CamTarget = new Vector3(100f, 0f, 100f);
  351.             Matrix viewMatrix = Matrix.LookAtLH(CamPostion, CamTarget, new Vector3(0, 1, 0));
  352.             device.Transform.View = viewMatrix;
  353.             device.Transform.Projection = Matrix.OrthoLH((float)this.Width, (float)this.Height, 0.1f, 1000f);
  354.             Render();
  355.         }
  356.         /// <summary>
  357.         /// 放大视图
  358.         /// </summary>
  359.         public void ZoomIn()
  360.         {
  361.             float scaleFactor = 0.95f;
  362.             CamPostion.Subtract(CamTarget);
  363.             CamPostion.Scale(scaleFactor);
  364.             CamPostion.Add(CamTarget);
  365.             Matrix viewMatrix = Matrix.LookAtLH(CamPostion, CamTarget, new Vector3(0, 1, 0));
  366.             device.Transform.View = viewMatrix;
  367.             Render();
  368.         }
  369.         /// <summary>
  370.         /// 缩小视图
  371.         /// </summary>
  372.         public void ZoomOut()
  373.         {
  374.             float scaleFactor = 1.05f;
  375.             CamPostion.Subtract(CamTarget);
  376.             CamPostion.Scale(scaleFactor);
  377.             CamPostion.Add(CamTarget);
  378.             Matrix viewMatrix = Matrix.LookAtLH(CamPostion, CamTarget, new Vector3(0, 1, 0));
  379.             device.Transform.View = viewMatrix;
  380.             Render();
  381.         }
  382.         /// <summary>
  383.         /// 向左旋转
  384.         /// </summary>
  385.         public void RotateLeft()
  386.         {
  387.             Vector4 tempV4;
  388.             Matrix currentView = device.Transform.View;//当前摄像机的视图矩阵
  389.             CamPostion.Subtract(CamTarget);
  390.             tempV4 = Vector3.Transform(CamPostion, Matrix.RotationQuaternion(
  391.                     Quaternion.RotationAxis(new Vector3(currentView.M12, currentView.M22, currentView.M32), -angleY)));
  392.             CamPostion.X = tempV4.X + CamTarget.X;
  393.             CamPostion.Y = tempV4.Y + CamTarget.Y;
  394.             CamPostion.Z = tempV4.Z + CamTarget.Z;
  395.             Matrix viewMatrix = Matrix.LookAtLH(CamPostion, CamTarget, new Vector3(0, 1, 0));
  396.             device.Transform.View = viewMatrix;
  397.             Render();
  398.         }
  399.         /// <summary>
  400.         /// 向右旋转
  401.         /// </summary>
  402.         public void RotateRight()
  403.         {
  404.             Vector4 tempV4;
  405.             Matrix currentView = device.Transform.View;//当前摄像机的视图矩阵
  406.             CamPostion.Subtract(CamTarget);
  407.             tempV4 = Vector3.Transform(CamPostion, Matrix.RotationQuaternion(
  408.                     Quaternion.RotationAxis(new Vector3(currentView.M12, currentView.M22, currentView.M32), angleY)));
  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.             Render();
  415.         }
  416.         /// <summary>
  417.         /// 向上旋转
  418.         /// </summary>
  419.         public void RotateUp()
  420.         {
  421.             Vector4 tempV4;
  422.             Matrix currentView = device.Transform.View;//当前摄像机的视图矩阵
  423.             CamPostion.Subtract(CamTarget);
  424.             tempV4 = Vector3.Transform(CamPostion, Matrix.RotationQuaternion(
  425.                Quaternion.RotationAxis(new Vector3(device.Transform.View.M11
  426.                , device.Transform.View.M21, device.Transform.View.M31), -angleY)));
  427.             CamPostion.X = tempV4.X + CamTarget.X;
  428.             CamPostion.Y = tempV4.Y + CamTarget.Y;
  429.             CamPostion.Z = tempV4.Z + CamTarget.Z;
  430.             Matrix viewMatrix = Matrix.LookAtLH(CamPostion, CamTarget, new Vector3(0, 1, 0));
  431.             device.Transform.View = viewMatrix;
  432.             Render();
  433.         }
  434.         /// <summary>
  435.         /// 向下旋转
  436.         /// </summary>
  437.         public void RotateDown()
  438.         {
  439.             Vector4 tempV4;
  440.             Matrix currentView = device.Transform.View;//当前摄像机的视图矩阵
  441.             CamPostion.Subtract(CamTarget);
  442.             tempV4 = Vector3.Transform(CamPostion, Matrix.RotationQuaternion(
  443.                Quaternion.RotationAxis(new Vector3(device.Transform.View.M11
  444.                , device.Transform.View.M21, device.Transform.View.M31), -angleY)));
  445.             CamPostion.X = tempV4.X + CamTarget.X;
  446.             CamPostion.Y = tempV4.Y + CamTarget.Y;
  447.             CamPostion.Z = tempV4.Z + CamTarget.Z;
  448.             Matrix viewMatrix = Matrix.LookAtLH(CamPostion, CamTarget, new Vector3(0, 1, 0));
  449.             device.Transform.View = viewMatrix;
  450.             Render();
  451.         }
  452.         /// <summary>
  453.         /// 回到初始视图状态
  454.         /// </summary>
  455.         public void ResetView()
  456.         {
  457.             CamPostion = new Vector3(0, 100, 100);//定义摄像机位置
  458.             CamTarget = new Vector3(125, 30, 125);//定义摄像机目标位置
  459.             Matrix viewMatrix = Matrix.LookAtLH(CamPostion, CamTarget, new Vector3(0, 1, 0));
  460.             device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, this.Width / this.Height, 0.1f, 1000f);
  461.             device.Transform.View = viewMatrix;
  462.             Render();
  463.         }
  464.         protected override void OnPaint(PaintEventArgs e)
  465.         {
  466.             Render();
  467.         }
  468.         
  469.         protected override void OnResize(EventArgs e)
  470.         {
  471.             InitializeDirect3D();
  472.             if (indices != null)
  473.             {
  474.                 BuildTerrain(heightMapPath, texturePath);
  475.             }
  476.             Render();
  477.         }  
  478.          
  479.     }
  480. }