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

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 MatrixExample : Form
  19.     {
  20.         Device device = null;//定义绘图设备
  21.         Sprite sprite;
  22.         Microsoft.DirectX.Direct3D.Font myFont;
  23.         System.Drawing.Font currentFont;
  24.         Matrix view;
  25.         Matrix project;
  26.         public MatrixExample()
  27.         {
  28.             this.ClientSize = new Size(800, 600);//指定窗体尺寸
  29.             this.Text = "矩阵应用";//指定窗体标题
  30.         }
  31.         public bool InitializeDirect3D()
  32.         {
  33.             try
  34.             {
  35.                 PresentParameters presentParams = new PresentParameters();
  36.                 presentParams.Windowed = true; //指定以Windows窗体形式显示
  37.                 presentParams.SwapEffect = SwapEffect.Discard; //当前屏幕绘制后它将自动从内存中删除
  38.                 device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams); //实例化device对象
  39.                 currentFont = new System.Drawing.Font("Times New Roman", 16, FontStyle.Regular);
  40.                 sprite = new Sprite(device);
  41.                 myFont = new Microsoft.DirectX.Direct3D.Font(device, currentFont);
  42.                 
  43.                 view=Matrix.LookAtLH(new Vector3(0f,0f,-50f),new Vector3(0f,0f,0f),new Vector3(0,1,0));
  44.                 project = Matrix.PerspectiveFovLH((float)(Math.PI / 4), this.Width/this.Height, 1f, 1000f);
  45.                 device.Transform.View=view;
  46.                 device.Transform.Projection=project;
  47.                 ShowMatrix(Matrix.Identity);//显示单位矩阵
  48.                 Console.WriteLine("矩阵行列式为:" + Matrix.Identity.Determinant.ToString());//显示矩阵行列式
  49.                 Matrix invertMat = Matrix.Identity;
  50.                 invertMat.M12 = 2f;
  51.                 Console.WriteLine("矩阵为:");
  52.                 ShowMatrix(invertMat);//显示原矩阵
  53.                 invertMat.Invert();//计算逆矩阵
  54.                 Console.WriteLine("矩阵的逆矩阵为:");
  55.                 ShowMatrix(invertMat);//显示逆矩阵
  56.                 Matrix matrix1 = Matrix.Identity;
  57.                 Matrix matrix2 = Matrix.Identity;
  58.                 matrix1.M12 = 3.5f;
  59.                 matrix2.M21 = -0.7f;
  60.                 Console.WriteLine("第一个矩阵为:");
  61.                 ShowMatrix(matrix1);//显示第一个矩阵
  62.                 Console.WriteLine("第二个矩阵为:");
  63.                 ShowMatrix(matrix2);//显示第二个矩阵
  64.                 Matrix addResult=Matrix.Add(matrix1, matrix2);//矩阵相加                
  65.                 Console.WriteLine("矩阵相加结果为:");
  66.                 ShowMatrix(addResult);//显示相加结果
  67.                 
  68.                 Matrix matrix3 = Matrix.Identity;
  69.                 Matrix matrix4 = Matrix.Identity;
  70.                 matrix3.M12 = 3.5f;
  71.                 matrix4.M21 = -2f;
  72.                 Console.WriteLine("第一个矩阵为:");
  73.                 ShowMatrix(matrix3);//显示第一个矩阵
  74.                 Console.WriteLine("第二个矩阵为:");
  75.                 ShowMatrix(matrix4);//显示第二个矩阵
  76.                 Matrix multiResult = Matrix.Multiply(matrix3, matrix4);//矩阵相乘              
  77.                 Console.WriteLine("矩阵相乘结果为:");
  78.                 ShowMatrix(multiResult);//显示相乘结果
  79.                 Vector3 cameraPosition = new Vector3(0.0f, 5.0f, 10.0f);
  80.                 Vector3 cameraTarget = new Vector3(0.2f, 0.0f, 0.0f);
  81.                 Vector3 cameraUpVector = new Vector3(0, 1, 0);
  82.                 Matrix viewMatrixComp = Matrix.LookAtLH(cameraPosition, cameraTarget, cameraUpVector);
  83.                 Console.WriteLine("视图矩阵为:");
  84.                 ShowMatrix(viewMatrixComp);//显示视图矩阵
  85.                 Vector3 cameraZaxis = Vector3.Normalize(cameraTarget-cameraPosition);
  86.                 Vector3 cameraXaxis = Vector3.Normalize(Vector3.Cross(cameraUpVector, cameraZaxis));
  87.                 Vector3 cameraYaxis = Vector3.Cross(cameraZaxis, cameraXaxis);
  88.                 Matrix viewMatrix=Matrix.Zero;
  89.                 viewMatrix.M11 = cameraXaxis.X; viewMatrix.M12 = cameraYaxis.X; viewMatrix.M13 = cameraZaxis.X;
  90.                 viewMatrix.M21 = cameraXaxis.Y; viewMatrix.M22 = cameraYaxis.Y; viewMatrix.M23 = cameraZaxis.Y;
  91.                 viewMatrix.M31 = cameraXaxis.Z; viewMatrix.M32 = cameraYaxis.Z; viewMatrix.M33 = cameraZaxis.Z;
  92.                 viewMatrix.M41 = -Vector3.Dot(cameraXaxis, cameraPosition);
  93.                 viewMatrix.M42 = -Vector3.Dot(cameraYaxis, cameraPosition);
  94.                 viewMatrix.M43 = -Vector3.Dot(cameraZaxis, cameraPosition);
  95.                 viewMatrix.M44 = 1;
  96.                 Console.WriteLine("视图矩阵为:");
  97.                 ShowMatrix(viewMatrix);//显示视图矩阵
  98.                 Matrix projMatrix = Matrix.PerspectiveFovLH((float)Math.PI / 4, this.Width / this.Height, 1f, 100f);
  99.                 Console.WriteLine("投影矩阵为:");
  100.                 ShowMatrix(projMatrix);//显示投影矩阵
  101.                 Matrix transMatrix = Matrix.Translation(1.5f, 2.1f, -3.2f);
  102.                 Console.WriteLine("形成的平移矩阵为:");
  103.                 ShowMatrix(transMatrix);//显示平移矩阵
  104.                 Matrix rotXMatrix = Matrix.RotationX((float)Math.PI / 4);
  105.                 Console.WriteLine("形成的绕X轴旋转45度的矩阵:");
  106.                 ShowMatrix(rotXMatrix);//显示旋转矩阵
  107.                 Matrix rotAxisMatrix = Matrix.RotationAxis(new Vector3(1f,1f,0f),(float)Math.PI/4);
  108.                 Console.WriteLine("形成绕指定轴旋转的矩阵:");
  109.                 ShowMatrix(rotAxisMatrix);//显示旋转矩阵
  110.                 Matrix scaleMatrix = Matrix.Scaling(1f, 1.5f, 2f);
  111.                 Console.WriteLine("形成的缩放矩阵:");
  112.                 ShowMatrix(scaleMatrix);//显示缩放矩阵
  113.                 Quaternion quat1 = new Quaternion(1f, 2f, 3f, 1f);
  114.                 Quaternion quat2 = Quaternion.Identity;
  115.                 quat2.RotateAxis(new Vector3(0, 1, 0), (float)Math.PI / 2);
  116.                 Console.WriteLine("绕Y轴旋转90°的四元数:X=" + quat2.X.ToString() + ",Y=" + quat2.Y.ToString() + ",Z=" + quat2.Z.ToString() + ",W=" + quat2.W.ToString());
  117.                 Matrix quatMatrix = Matrix.RotationQuaternion(quat2);
  118.                 Console.WriteLine("由四元数形成的旋转矩阵:");
  119.                 ShowMatrix(quatMatrix);
  120.                 Matrix rotMatrix = Matrix.RotationAxis(new Vector3(0, 1, 0), (float)Math.PI / 2);
  121.                 Console.WriteLine("直接由轴和旋转角形成的旋转矩阵:");
  122.                 ShowMatrix(rotMatrix);
  123.                 return true;
  124.             }
  125.             catch (DirectXException e)
  126.             {
  127.                 MessageBox.Show(e.ToString(), "Error"); //处理异常
  128.                 return false;
  129.             }
  130.         }
  131.         public void Render()
  132.         {
  133.             if (device == null)   //如果device为空则不渲染
  134.             {
  135.                 return;
  136.             }
  137.             device.Clear(ClearFlags.Target, Color.DarkSlateBlue, 1.0f, 0);  //清除windows界面为深蓝色
  138.             device.BeginScene();
  139.             //在此添加渲染图形代码
  140.             /*
  141.             Vector3 eye = new Vector3(0.0f, 5.0f, 10.0f);
  142.             Vector3 at = new Vector3(0.2f, 0.0f, 0.0f);
  143.             Vector3 up = new Vector3(0, 1, 0);
  144.             Matrix viewMatrix = Matrix.LookAtLH(eye, at, up);
  145.             Matrix projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, this.Width / this.Height, 1.0f, 1000.0f);
  146.             device.Transform.Projection = projection;
  147.             device.Transform.View = viewMatrix;
  148.             */
  149.             
  150.             //在此添加渲染图形代码
  151.             Vector3 cameraPosition = new Vector3(0, -10,-20);
  152.             Vector3 cameraTarget = new Vector3(0f, 0.0f, 0.0f);
  153.             Vector3 cameraUpVector = new Vector3(0, 1, 0);
  154.             Matrix viewMatrixComp = Matrix.LookAtLH(cameraPosition, cameraTarget, cameraUpVector);
  155.             device.Transform.View = viewMatrixComp;
  156.             Matrix projMatrix = Matrix.PerspectiveFovLH((float)Math.PI / 2, (float)this.Width / this.Height, 1.0f, 1000.0f);
  157.             //device.Transform.Projection = projMatrix;
  158.             Matrix orthoMatrix = Matrix.OrthoLH(40f, 30f, 1f, 1000f);
  159.             device.Transform.Projection = orthoMatrix;
  160.             device.RenderState.CullMode = Cull.None;
  161.             device.RenderState.Lighting = false;
  162.             device.RenderState.FillMode = FillMode.Solid;
  163.             CustomVertex.PositionColored[] vertices = new CustomVertex.PositionColored[3];//定义顶点
  164.             vertices[0].Position = new Vector3(0f, 0f, 0f);
  165.             vertices[0].Color = Color.Red.ToArgb();
  166.             vertices[1].Position = new Vector3(5f, 10f, 0f);
  167.             vertices[1].Color = Color.Green.ToArgb();
  168.             vertices[2].Position = new Vector3(10f, 0f, 0f);
  169.             vertices[2].Color = Color.Yellow.ToArgb();
  170.             device.VertexFormat = CustomVertex.PositionColored.Format;
  171.             device.DrawUserPrimitives(PrimitiveType.TriangleList, 1, vertices);
  172.             
  173.             sprite.Begin(SpriteFlags.AlphaBlend);
  174.             Matrix matrix01 = projMatrix;// viewMatrixComp;// Matrix.Identity;
  175.             string matrixString = "";
  176.             matrixString = matrix01.M11.ToString()+"  "+matrix01.M12.ToString() + "  " +
  177.                 matrix01.M13.ToString() + "  " + matrix01.M14.ToString() + "n" +
  178.                 matrix01.M21.ToString() + "  " + matrix01.M22.ToString() + "  " +
  179.                 matrix01.M23.ToString() + "  " + matrix01.M24.ToString() + "n" +
  180.                 matrix01.M31.ToString() + "  " + matrix01.M32.ToString() + "  " +
  181.                 matrix01.M33.ToString() + "  " + matrix01.M34.ToString() + "n" +
  182.                 matrix01.M41.ToString() + "  " + matrix01.M42.ToString() + "  " +
  183.                 matrix01.M43.ToString() + "  " + matrix01.M44.ToString() + "n";
  184.             //myFont.DrawText(sprite, matrixString, 200, 100, Color.Yellow);  
  185.           
  186.             //vertices[0].Position.
  187.             //myFont.DrawText(sprite, "□", this.Width / 2, this.Height / 2, Color.Yellow);
  188.             /*
  189.             Vector4 calV1 = new Vector4(0f,0f,0f,1f);
  190.             calV1 =Vector4.Transform(calV1, viewMatrixComp);
  191.             int x1 = (int)(((float)calV1.X*this.Height/(float)(2 * calV1.Z * Math.Tan((float)Math.PI / 4))) + this.Width / 2);
  192.             int y1 = (int)(-calV1.Y * (float)this.Height / (2 * calV1.Z * Math.Tan((float)Math.PI / 4)) + this.Height / 2);
  193.             myFont.DrawText(sprite, "1", x1, y1, Color.Yellow);
  194.             Vector4 calV2 = new Vector4(5f, 10f, 0, 1);
  195.             calV2 = Vector4.Transform(calV2, viewMatrixComp);
  196.             int x2 = (int)(((float)calV2.X * this.Height / (float)(2 * calV2.Z * Math.Tan((float)Math.PI / 4))) + this.Width / 2);
  197.             int y2 = -(int)(calV2.Y * (float)this.Height / (2 * calV2.Z * Math.Tan((float)Math.PI / 4))) + this.Height / 2;
  198.             myFont.DrawText(sprite, "2", x2, y2, Color.Yellow);
  199.             
  200.             Vector4 calV3 = new Vector4(10f, 0f, 0, 1);
  201.             calV3 = Vector4.Transform(calV3, viewMatrixComp);
  202.             int x3 = (int)(((float)calV3.X * this.Height / (float)(2 * calV3.Z * Math.Tan((float)Math.PI / 4))) + this.Width / 2);
  203.             int y3 = (int)(-calV3.Y * (float)this.Height / (2 * calV3.Z * Math.Tan((float)Math.PI / 4)) + this.Height / 2);
  204.             myFont.DrawText(sprite, "3", x3, y3, Color.Yellow);
  205.             Vector4 VectorOP = new Vector4(5f, 10f, 0f, 1f);
  206.             VectorOP = Vector4.Transform(VectorOP, viewMatrixComp);
  207.             VectorOP = Vector4.Transform(VectorOP, projMatrix);
  208.             Matrix ScreenMatrix = Matrix.Zero;
  209.             ScreenMatrix.M11 = ScreenMatrix.M41= (float)this.Width / 2; ScreenMatrix.M22 = -(float)this.Height / 2;
  210.             ScreenMatrix.M42 = (float)this.Height / 2;
  211.             VectorOP = Vector4.Multiply(VectorOP, 1.0f / VectorOP.W);
  212.             VectorOP = Vector4.Transform(VectorOP, ScreenMatrix);
  213.             myFont.DrawText(sprite, "+", (int)VectorOP.X, (int)VectorOP.Y, Color.Yellow);
  214.              */
  215.             Vector4 VectorOPO = new Vector4(5f, 10f, 0f, 1f);
  216.             VectorOPO = Vector4.Transform(VectorOPO, viewMatrixComp);
  217.             VectorOPO = Vector4.Transform(VectorOPO, orthoMatrix);
  218.             Matrix ScreenMatrix = Matrix.Zero;
  219.             ScreenMatrix.M11 = ScreenMatrix.M41 = (float)this.Width / 2; ScreenMatrix.M22 = -(float)this.Height / 2;
  220.             ScreenMatrix.M42 = (float)this.Height / 2;
  221.             VectorOPO = Vector4.Transform(VectorOPO, ScreenMatrix);
  222.             myFont.DrawText(sprite, "+", (int)VectorOPO.X, (int)VectorOPO.Y, Color.Yellow);
  223.             
  224.             sprite.End();
  225.             
  226.             device.EndScene();
  227.             device.Present();
  228.         }
  229.         /// <summary>
  230.         /// 要显示的矩阵
  231.         /// </summary>
  232.         /// <param name="matrix"></param>
  233.         private void ShowMatrix(Matrix matrix)
  234.         {
  235.             string matrixString = "";
  236.             matrixString = matrix.M11.ToString("F3") + "  " + matrix.M12.ToString("F3") + "  " +
  237.                 matrix.M13.ToString("F3") + "  " + matrix.M14.ToString("F3") + "n" +
  238.                 matrix.M21.ToString("F3") + "  " + matrix.M22.ToString("F3") + "  " +
  239.                 matrix.M23.ToString("F3") + "  " + matrix.M24.ToString("F3") + "n" +
  240.                 matrix.M31.ToString("F3") + "  " + matrix.M32.ToString("F3") + "  " +
  241.                 matrix.M33.ToString("F3") + "  " + matrix.M34.ToString("F3") + "n" +
  242.                 matrix.M41.ToString("F3") + "  " + matrix.M42.ToString("F3") + "  " +
  243.                 matrix.M43.ToString("F3") + "  " + matrix.M44.ToString("F3") + "n";
  244.             Console.WriteLine(matrixString);
  245.         }
  246.         static void Main()
  247.         {
  248.             MatrixExample MatrixExample = new MatrixExample(); //创建窗体对象
  249.             MatrixExample.FormBorderStyle = FormBorderStyle.None;//设置窗体无框架
  250.             if (MatrixExample.InitializeDirect3D() == false) //检查Direct3D是否启动
  251.             {
  252.                 MessageBox.Show("无法启动Direct3D!", "错误!");
  253.                 return;
  254.             }
  255.             MatrixExample.Show(); //如果一切都初始化成功,则显示窗体
  256.             while (MatrixExample.Created) //设置一个循环用于实时更新渲染状态
  257.             {
  258.                 MatrixExample.Render(); //保持device渲染,直到程序结束
  259.                 Application.DoEvents(); //处理键盘鼠标等输入事件
  260.             }
  261.         }
  262.     }
  263. }