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

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