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

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 贴图
  17. {
  18.     public partial class TextureForm : Form
  19.     {
  20.         Device device = null;//定义绘图设备
  21.         //private CustomVertex.PositionTextured[] vertices;//定义顶点变量
  22.         //private Texture verticesTexture;//定义三角形贴图
  23.         private Mesh meshObj;//定义网格模型对象
  24.         private Texture meshTexture;//定义模型贴图
  25.         private Material material;//定义材质变量
  26.         private float angleY=0.01f;//定义绕Y轴旋转变量
  27.         private Vector3 CamPostion = new Vector3(0, 30, -30);//定义摄像机位置
  28.         private Vector3 CamTarget = new Vector3(0, 0, 0);//定义摄像机目标位置
  29.         private int mouseLastX,mouseLastY;//记录鼠标按下时的坐标位置
  30.         private bool isRotateByMouse=false;//记录是否由鼠标控制旋转
  31.         private bool isMoveByMouse = false;//记录是否由鼠标控制移动
  32.         public TextureForm()
  33.         {
  34.             this.ClientSize = new Size(800, 600);//指定窗体尺寸
  35.             this.Text = "贴图";//指定窗体标题
  36.         }
  37.         public bool InitializeDirect3D()
  38.         {
  39.             try
  40.             {
  41.                 PresentParameters presentParams = new PresentParameters();
  42.                 presentParams.Windowed = true; //指定以Windows窗体形式显示
  43.                 presentParams.SwapEffect = SwapEffect.Discard; //当前屏幕绘制后它将自动从内存中删除
  44.                 presentParams.AutoDepthStencilFormat = DepthFormat.D16;
  45.                 presentParams.EnableAutoDepthStencil = true;
  46.                 presentParams.PresentationInterval = PresentInterval.Immediate;
  47.                 device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams); //实例化device对象
  48.                 /*
  49.                 //定义顶点坐标及贴图坐标
  50.                 vertices = new CustomVertex.PositionTextured[6];
  51.                 //左上角顶点
  52.                 vertices[0].Position = new Vector3(-10f, 0f, 10f);
  53.                 vertices[0].Tu = 0;vertices[0].Tv = 0;
  54.                 //右下角顶点
  55.                 vertices[1].Position = new Vector3(10f, 0f, -10f);
  56.                 vertices[1].Tu = 1;vertices[1].Tv = 1;
  57.                 //左下角顶点
  58.                 vertices[2].Position = new Vector3(-10f, 0f, -10f);
  59.                 vertices[2].Tu = 0;vertices[2].Tv = 1;
  60.                 //左上角顶点
  61.                 vertices[3].Position = new Vector3(-10f, 0f, 10f);
  62.                 vertices[3].Tu = 0;vertices[3].Tv = 0;
  63.                 //右上角顶点
  64.                 vertices[4].Position = new Vector3(10f, 0f, 10f);
  65.                 vertices[4].Tu = 1;vertices[4].Tv = 0;
  66.                 //右下角顶点
  67.                 vertices[5].Position = new Vector3(10f, 0f, -10f);
  68.                 vertices[5].Tu = 1;vertices[5].Tv = 1;
  69.                 //导入贴图
  70.                 verticesTexture = TextureLoader.FromFile(device, @"E:DirectXDirectX_C#MediaCTGU.jpg");
  71.                 */
  72.                 //定义网格模型
  73.                 Vector3[] points = new Vector3[8];
  74.                 points[0] = new Vector3(0f, 6f, 0f);
  75.                 points[1] = new Vector3(0f, 6f, 6f);
  76.                 points[2] = new Vector3(6f, 6f, 6f);
  77.                 points[3] = new Vector3(6f, 6f, 0f);
  78.                 points[4] = new Vector3(0f, 0f, 0f);
  79.                 points[5] = new Vector3(0f, 0f, 6f);
  80.                 points[6] = new Vector3(6f, 0f, 6f);
  81.                 points[7] = new Vector3(6f, 0f, 0f);
  82.                 meshObj = DrawTextureHexClass.DrawTextureHexObject(device, points, @"E:DirectXDirectX_C#MediaMetalPlate.jpg", out meshTexture);
  83.                 //定义材质
  84.                 material = new Material();
  85.                 material.Ambient = Color.FromArgb(0,255,255,255);//设置环境光
  86.                 material.Diffuse = Color.White;//设置漫反射
  87.                 material.Emissive = Color.FromArgb(0, 0, 0, 0);//设置自发光
  88.                 material.Specular = Color.DarkRed;//设置镜面反射光
  89.                 material.SpecularSharpness = 15.0f;//反射高光清晰度
  90.                 return true;
  91.             }
  92.             catch (DirectXException e)
  93.             {
  94.                 MessageBox.Show(e.ToString(), "Error"); //处理异常
  95.                 return false;
  96.             }
  97.         }
  98.  
  99.         public void Render()
  100.         {
  101.             if (device == null)   //如果device为空则不渲染
  102.             {
  103.                 return;
  104.             }
  105.             SetUpCamera();
  106.             device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.DarkSlateBlue, 1.0f, 0);  //清除windows界面为深蓝色
  107.             device.BeginScene();
  108.             //在此添加渲染图形代码
  109.             device.RenderState.Lighting = true;
  110.             device.Lights[0].Type = LightType.Directional;
  111.             device.Lights[0].Diffuse = System.Drawing.Color.White;
  112.             device.Lights[0].Direction = new Vector3(-1.0f, 1.0f, 1.0f);
  113.             device.Lights[0].Enabled = true; //打开灯光
  114.             device.Lights[1].Type = LightType.Directional;
  115.             device.Lights[1].Diffuse = System.Drawing.Color.LightGray;
  116.             device.Lights[1].Direction = new Vector3(1f, -1.0f, 1.0f);
  117.             device.Lights[1].Enabled = true; //打开灯光            
  118.             device.RenderState.Ambient = Color.SlateGray;
  119.             device.RenderState.CullMode = Cull.None;
  120.             //设置绘图设备当前的材质属性
  121.             device.Material = material;
  122.             //device.SetTexture(0, verticesTexture);//设置贴图
  123.             //device.VertexFormat = CustomVertex.PositionTextured.Format;
  124.             //device.DrawUserPrimitives(PrimitiveType.TriangleList, 2, vertices);
  125.             
  126.             device.SetTexture(0, meshTexture);//设置贴图
  127.             meshObj.DrawSubset(0);//绘制模型
  128.             
  129.             device.EndScene();
  130.             device.Present();
  131.         }
  132.         private void SetUpCamera()//摄像机
  133.         {
  134.             Matrix viewMatrix = Matrix.LookAtLH(CamPostion, CamTarget, new Vector3(0, 1, 0));
  135.             device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, this.Width / this.Height, 0.3f, 500f);
  136.             device.Transform.View = viewMatrix;
  137.         }
  138.         protected override void OnKeyDown(KeyEventArgs e)
  139.         {
  140.             Vector4 tempV4;
  141.             Matrix currentView = device.Transform.View;//当前摄像机的视图矩阵
  142.             switch (e.KeyCode)
  143.             {
  144.                 case Keys.Left:
  145.                     CamPostion.Subtract(CamTarget);
  146.                     tempV4 = Vector3.Transform(CamPostion, Matrix.RotationQuaternion(
  147.                             Quaternion.RotationAxis(new Vector3(currentView.M12, currentView.M22, currentView.M32), -angleY)));
  148.                     CamPostion.X = tempV4.X + CamTarget.X;
  149.                     CamPostion.Y = tempV4.Y + CamTarget.Y;
  150.                     CamPostion.Z = tempV4.Z + CamTarget.Z;
  151.                     break;
  152.                 case Keys.Right:
  153.                     CamPostion.Subtract(CamTarget);
  154.                     tempV4 = Vector3.Transform(CamPostion, Matrix.RotationQuaternion(
  155.                             Quaternion.RotationAxis(new Vector3(currentView.M12, currentView.M22, currentView.M32), angleY)));
  156.                     CamPostion.X = tempV4.X + CamTarget.X;
  157.                     CamPostion.Y = tempV4.Y + CamTarget.Y;
  158.                     CamPostion.Z = tempV4.Z + CamTarget.Z;
  159.                     break;
  160.                 case Keys.Up:
  161.                     CamPostion.Subtract(CamTarget);
  162.                     tempV4 = Vector3.Transform(CamPostion, Matrix.RotationQuaternion(
  163.                        Quaternion.RotationAxis(new Vector3(device.Transform.View.M11
  164.                        , device.Transform.View.M21, device.Transform.View.M31), -angleY)));
  165.                     CamPostion.X = tempV4.X + CamTarget.X;
  166.                     CamPostion.Y = tempV4.Y + CamTarget.Y;
  167.                     CamPostion.Z = tempV4.Z + CamTarget.Z;
  168.                     break;
  169.                 case Keys.Down:
  170.                     CamPostion.Subtract(CamTarget);
  171.                     tempV4 = Vector3.Transform(CamPostion, Matrix.RotationQuaternion(
  172.                        Quaternion.RotationAxis(new Vector3(device.Transform.View.M11
  173.                        , device.Transform.View.M21, device.Transform.View.M31), angleY)));
  174.                     CamPostion.X = tempV4.X + CamTarget.X;
  175.                     CamPostion.Y = tempV4.Y + CamTarget.Y;
  176.                     CamPostion.Z = tempV4.Z + CamTarget.Z;
  177.                     break;
  178.                 case Keys.Add:
  179.                     CamPostion.Subtract(CamTarget);
  180.                     CamPostion.Scale(0.95f);
  181.                     CamPostion.Add(CamTarget);
  182.                     break;
  183.                 case Keys.Subtract:
  184.                     CamPostion.Subtract(CamTarget);
  185.                     CamPostion.Scale(1.05f);
  186.                     CamPostion.Add(CamTarget);
  187.                     break;
  188.             }
  189.             Matrix viewMatrix = Matrix.LookAtLH(CamPostion, CamTarget, new Vector3(0, 1, 0));
  190.             device.Transform.View = viewMatrix;
  191.         }
  192.         protected override void OnMouseDown(MouseEventArgs e)
  193.         {
  194.             if (e.Button == MouseButtons.Left)
  195.             {
  196.                 mouseLastX = e.X;
  197.                 mouseLastY = e.Y;
  198.                 isRotateByMouse = true;
  199.             }
  200.             else if (e.Button == MouseButtons.Middle)
  201.             {
  202.                 mouseLastX = e.X;
  203.                 mouseLastY = e.Y;
  204.                 isMoveByMouse=true;
  205.             }
  206.         }
  207.         
  208.         protected override void OnMouseUp(MouseEventArgs e)
  209.         {
  210.             isRotateByMouse = false;
  211.             isMoveByMouse = false;
  212.         }
  213.         protected override void OnMouseMove(MouseEventArgs e)
  214.         {
  215.             if (isRotateByMouse)
  216.             {
  217.                 Matrix currentView = device.Transform.View;//当前摄像机的视图矩阵
  218.                 float tempAngleY = 2 * (float)(e.X - mouseLastX) / this.Width;
  219.                 CamPostion.Subtract(CamTarget);
  220.                 Vector4 tempV4 = Vector3.Transform(CamPostion, Matrix.RotationQuaternion(
  221.                     Quaternion.RotationAxis(new Vector3(currentView.M12, currentView.M22, currentView.M32), tempAngleY)));
  222.                 CamPostion.X = tempV4.X;
  223.                 CamPostion.Y = tempV4.Y;
  224.                 CamPostion.Z = tempV4.Z;
  225.                 float tempAngleX = 4 * (float)(e.Y - mouseLastY) / this.Height;
  226.                 tempV4 = Vector3.Transform(CamPostion, Matrix.RotationQuaternion(
  227.                     Quaternion.RotationAxis(new Vector3(currentView.M11, currentView.M21, currentView.M31), tempAngleX)));
  228.                 CamPostion.X = tempV4.X + CamTarget.X;
  229.                 CamPostion.Y = tempV4.Y + CamTarget.Y;
  230.                 CamPostion.Z = tempV4.Z + CamTarget.Z;
  231.                 Matrix viewMatrix = Matrix.LookAtLH(CamPostion, CamTarget, new Vector3(0, 1, 0));
  232.                 device.Transform.View = viewMatrix;
  233.                 mouseLastX = e.X;
  234.                 mouseLastY = e.Y;
  235.             }
  236.             else if (isMoveByMouse)
  237.             {
  238.                 Matrix currentView = device.Transform.View;//当前摄像机的视图矩阵
  239.                 float moveFactor=0.01f;
  240.                 CamTarget.X += -moveFactor * ((e.X - mouseLastX) * currentView.M11 - (e.Y - mouseLastY) * currentView.M12);
  241.                 CamTarget.Y += -moveFactor * ((e.X - mouseLastX) * currentView.M21 - (e.Y - mouseLastY) * currentView.M22);
  242.                 CamTarget.Z += -moveFactor * ((e.X - mouseLastX) * currentView.M31 - (e.Y - mouseLastY) * currentView.M32);
  243.                 CamPostion.X +=- moveFactor * ((e.X - mouseLastX) * currentView.M11 - (e.Y - mouseLastY) * currentView.M12);
  244.                 CamPostion.Y += -moveFactor * ((e.X - mouseLastX) * currentView.M21 - (e.Y - mouseLastY) * currentView.M22);
  245.                 CamPostion.Z += -moveFactor * ((e.X - mouseLastX) * currentView.M31 - (e.Y - mouseLastY) * currentView.M32);
  246.              
  247.                 Matrix viewMatrix = Matrix.LookAtLH(CamPostion, CamTarget, new Vector3(0, 1, 0));
  248.                 device.Transform.View = viewMatrix;
  249.                 mouseLastX = e.X;
  250.                 mouseLastY = e.Y;
  251.             }
  252.         }
  253.         protected override void OnMouseWheel(MouseEventArgs e)
  254.         {
  255.             float scaleFactor = -(float)e.Delta / 2000 + 1f;
  256.             CamPostion.Subtract(CamTarget);
  257.             CamPostion.Scale(scaleFactor);
  258.             CamPostion.Add(CamTarget);
  259.             Matrix viewMatrix = Matrix.LookAtLH(CamPostion, CamTarget, new Vector3(0, 1, 0));
  260.             device.Transform.View = viewMatrix;
  261.         }
  262.         static void Main()
  263.         {
  264.             TextureForm textureForm = new TextureForm(); //创建窗体对象
  265.             if (textureForm.InitializeDirect3D() == false) //检查Direct3D是否启动
  266.             {
  267.                 MessageBox.Show("无法启动Direct3D!", "错误!");
  268.                 return;
  269.             }
  270.             textureForm.Show(); //如果一切都初始化成功,则显示窗体
  271.             while (textureForm.Created) //设置一个循环用于实时更新渲染状态
  272.             {
  273.                 textureForm.Render(); //保持device渲染,直到程序结束
  274.                 Application.DoEvents(); //处理键盘鼠标等输入事件
  275.             }
  276.         }
  277.     }
  278. }