MatrixExample.cs
上传用户:lslight
上传日期:2022-01-10
资源大小:14248k
文件大小:15k
- ////////////////////////////////////////////////////////////////////////
- // ■■■■ ■■■■■ ■■■■ ■ ■ //
- // ■ ■ ■ ■ ■ //
- // ■ ■ ■ ■■■ ■ ■ //
- // ■ ■ ■ ■ ■ ■ //
- // ■■■■ ■ ■■■■ ■■■■ //
- // Copyright (c) 三峡大学水利与环境学院 肖泽云. All rights reserved. //
- ////////////////////////////////////////////////////////////////////////
- using System;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Windows.Forms;
- using Microsoft.DirectX;
- using Microsoft.DirectX.Direct3D;
- namespace 矩阵
- {
- public partial class MatrixExample : Form
- {
- Device device = null;//定义绘图设备
- Sprite sprite;
- Microsoft.DirectX.Direct3D.Font myFont;
- System.Drawing.Font currentFont;
- Matrix view;
- Matrix project;
- public MatrixExample()
- {
- this.ClientSize = new Size(800, 600);//指定窗体尺寸
- this.Text = "矩阵应用";//指定窗体标题
- }
- public bool InitializeDirect3D()
- {
- try
- {
- PresentParameters presentParams = new PresentParameters();
- presentParams.Windowed = true; //指定以Windows窗体形式显示
- presentParams.SwapEffect = SwapEffect.Discard; //当前屏幕绘制后它将自动从内存中删除
- device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams); //实例化device对象
- currentFont = new System.Drawing.Font("Times New Roman", 16, FontStyle.Regular);
- sprite = new Sprite(device);
- myFont = new Microsoft.DirectX.Direct3D.Font(device, currentFont);
-
- view=Matrix.LookAtLH(new Vector3(0f,0f,-50f),new Vector3(0f,0f,0f),new Vector3(0,1,0));
- project = Matrix.PerspectiveFovLH((float)(Math.PI / 4), this.Width/this.Height, 1f, 1000f);
- device.Transform.View=view;
- device.Transform.Projection=project;
- ShowMatrix(Matrix.Identity);//显示单位矩阵
- Console.WriteLine("矩阵行列式为:" + Matrix.Identity.Determinant.ToString());//显示矩阵行列式
- Matrix invertMat = Matrix.Identity;
- invertMat.M12 = 2f;
- Console.WriteLine("矩阵为:");
- ShowMatrix(invertMat);//显示原矩阵
- invertMat.Invert();//计算逆矩阵
- Console.WriteLine("矩阵的逆矩阵为:");
- ShowMatrix(invertMat);//显示逆矩阵
- Matrix matrix1 = Matrix.Identity;
- Matrix matrix2 = Matrix.Identity;
- matrix1.M12 = 3.5f;
- matrix2.M21 = -0.7f;
- Console.WriteLine("第一个矩阵为:");
- ShowMatrix(matrix1);//显示第一个矩阵
- Console.WriteLine("第二个矩阵为:");
- ShowMatrix(matrix2);//显示第二个矩阵
- Matrix addResult=Matrix.Add(matrix1, matrix2);//矩阵相加
- Console.WriteLine("矩阵相加结果为:");
- ShowMatrix(addResult);//显示相加结果
-
- Matrix matrix3 = Matrix.Identity;
- Matrix matrix4 = Matrix.Identity;
- matrix3.M12 = 3.5f;
- matrix4.M21 = -2f;
- Console.WriteLine("第一个矩阵为:");
- ShowMatrix(matrix3);//显示第一个矩阵
- Console.WriteLine("第二个矩阵为:");
- ShowMatrix(matrix4);//显示第二个矩阵
- Matrix multiResult = Matrix.Multiply(matrix3, matrix4);//矩阵相乘
- Console.WriteLine("矩阵相乘结果为:");
- ShowMatrix(multiResult);//显示相乘结果
- Vector3 cameraPosition = new Vector3(0.0f, 5.0f, 10.0f);
- Vector3 cameraTarget = new Vector3(0.2f, 0.0f, 0.0f);
- Vector3 cameraUpVector = new Vector3(0, 1, 0);
- Matrix viewMatrixComp = Matrix.LookAtLH(cameraPosition, cameraTarget, cameraUpVector);
- Console.WriteLine("视图矩阵为:");
- ShowMatrix(viewMatrixComp);//显示视图矩阵
- Vector3 cameraZaxis = Vector3.Normalize(cameraTarget-cameraPosition);
- Vector3 cameraXaxis = Vector3.Normalize(Vector3.Cross(cameraUpVector, cameraZaxis));
- Vector3 cameraYaxis = Vector3.Cross(cameraZaxis, cameraXaxis);
- Matrix viewMatrix=Matrix.Zero;
- viewMatrix.M11 = cameraXaxis.X; viewMatrix.M12 = cameraYaxis.X; viewMatrix.M13 = cameraZaxis.X;
- viewMatrix.M21 = cameraXaxis.Y; viewMatrix.M22 = cameraYaxis.Y; viewMatrix.M23 = cameraZaxis.Y;
- viewMatrix.M31 = cameraXaxis.Z; viewMatrix.M32 = cameraYaxis.Z; viewMatrix.M33 = cameraZaxis.Z;
- viewMatrix.M41 = -Vector3.Dot(cameraXaxis, cameraPosition);
- viewMatrix.M42 = -Vector3.Dot(cameraYaxis, cameraPosition);
- viewMatrix.M43 = -Vector3.Dot(cameraZaxis, cameraPosition);
- viewMatrix.M44 = 1;
- Console.WriteLine("视图矩阵为:");
- ShowMatrix(viewMatrix);//显示视图矩阵
- Matrix projMatrix = Matrix.PerspectiveFovLH((float)Math.PI / 4, this.Width / this.Height, 1f, 100f);
- Console.WriteLine("投影矩阵为:");
- ShowMatrix(projMatrix);//显示投影矩阵
- Matrix transMatrix = Matrix.Translation(1.5f, 2.1f, -3.2f);
- Console.WriteLine("形成的平移矩阵为:");
- ShowMatrix(transMatrix);//显示平移矩阵
- Matrix rotXMatrix = Matrix.RotationX((float)Math.PI / 4);
- Console.WriteLine("形成的绕X轴旋转45度的矩阵:");
- ShowMatrix(rotXMatrix);//显示旋转矩阵
- Matrix rotAxisMatrix = Matrix.RotationAxis(new Vector3(1f,1f,0f),(float)Math.PI/4);
- Console.WriteLine("形成绕指定轴旋转的矩阵:");
- ShowMatrix(rotAxisMatrix);//显示旋转矩阵
- Matrix scaleMatrix = Matrix.Scaling(1f, 1.5f, 2f);
- Console.WriteLine("形成的缩放矩阵:");
- ShowMatrix(scaleMatrix);//显示缩放矩阵
- Quaternion quat1 = new Quaternion(1f, 2f, 3f, 1f);
- Quaternion quat2 = Quaternion.Identity;
- quat2.RotateAxis(new Vector3(0, 1, 0), (float)Math.PI / 2);
- Console.WriteLine("绕Y轴旋转90°的四元数:X=" + quat2.X.ToString() + ",Y=" + quat2.Y.ToString() + ",Z=" + quat2.Z.ToString() + ",W=" + quat2.W.ToString());
- Matrix quatMatrix = Matrix.RotationQuaternion(quat2);
- Console.WriteLine("由四元数形成的旋转矩阵:");
- ShowMatrix(quatMatrix);
- Matrix rotMatrix = Matrix.RotationAxis(new Vector3(0, 1, 0), (float)Math.PI / 2);
- Console.WriteLine("直接由轴和旋转角形成的旋转矩阵:");
- ShowMatrix(rotMatrix);
- return true;
- }
- catch (DirectXException e)
- {
- MessageBox.Show(e.ToString(), "Error"); //处理异常
- return false;
- }
- }
- public void Render()
- {
- if (device == null) //如果device为空则不渲染
- {
- return;
- }
- device.Clear(ClearFlags.Target, Color.DarkSlateBlue, 1.0f, 0); //清除windows界面为深蓝色
- device.BeginScene();
- //在此添加渲染图形代码
- /*
- Vector3 eye = new Vector3(0.0f, 5.0f, 10.0f);
- Vector3 at = new Vector3(0.2f, 0.0f, 0.0f);
- Vector3 up = new Vector3(0, 1, 0);
- Matrix viewMatrix = Matrix.LookAtLH(eye, at, up);
- Matrix projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, this.Width / this.Height, 1.0f, 1000.0f);
- device.Transform.Projection = projection;
- device.Transform.View = viewMatrix;
- */
-
- //在此添加渲染图形代码
- Vector3 cameraPosition = new Vector3(0, -10,-20);
- Vector3 cameraTarget = new Vector3(0f, 0.0f, 0.0f);
- Vector3 cameraUpVector = new Vector3(0, 1, 0);
- Matrix viewMatrixComp = Matrix.LookAtLH(cameraPosition, cameraTarget, cameraUpVector);
- device.Transform.View = viewMatrixComp;
- Matrix projMatrix = Matrix.PerspectiveFovLH((float)Math.PI / 2, (float)this.Width / this.Height, 1.0f, 1000.0f);
- //device.Transform.Projection = projMatrix;
- Matrix orthoMatrix = Matrix.OrthoLH(40f, 30f, 1f, 1000f);
- device.Transform.Projection = orthoMatrix;
- device.RenderState.CullMode = Cull.None;
- device.RenderState.Lighting = false;
- device.RenderState.FillMode = FillMode.Solid;
- CustomVertex.PositionColored[] vertices = new CustomVertex.PositionColored[3];//定义顶点
- vertices[0].Position = new Vector3(0f, 0f, 0f);
- vertices[0].Color = Color.Red.ToArgb();
- vertices[1].Position = new Vector3(5f, 10f, 0f);
- vertices[1].Color = Color.Green.ToArgb();
- vertices[2].Position = new Vector3(10f, 0f, 0f);
- vertices[2].Color = Color.Yellow.ToArgb();
- device.VertexFormat = CustomVertex.PositionColored.Format;
- device.DrawUserPrimitives(PrimitiveType.TriangleList, 1, vertices);
-
- sprite.Begin(SpriteFlags.AlphaBlend);
- Matrix matrix01 = projMatrix;// viewMatrixComp;// Matrix.Identity;
- string matrixString = "";
- matrixString = matrix01.M11.ToString()+" "+matrix01.M12.ToString() + " " +
- matrix01.M13.ToString() + " " + matrix01.M14.ToString() + "n" +
- matrix01.M21.ToString() + " " + matrix01.M22.ToString() + " " +
- matrix01.M23.ToString() + " " + matrix01.M24.ToString() + "n" +
- matrix01.M31.ToString() + " " + matrix01.M32.ToString() + " " +
- matrix01.M33.ToString() + " " + matrix01.M34.ToString() + "n" +
- matrix01.M41.ToString() + " " + matrix01.M42.ToString() + " " +
- matrix01.M43.ToString() + " " + matrix01.M44.ToString() + "n";
- //myFont.DrawText(sprite, matrixString, 200, 100, Color.Yellow);
-
- //vertices[0].Position.
- //myFont.DrawText(sprite, "□", this.Width / 2, this.Height / 2, Color.Yellow);
- /*
- Vector4 calV1 = new Vector4(0f,0f,0f,1f);
- calV1 =Vector4.Transform(calV1, viewMatrixComp);
- int x1 = (int)(((float)calV1.X*this.Height/(float)(2 * calV1.Z * Math.Tan((float)Math.PI / 4))) + this.Width / 2);
- int y1 = (int)(-calV1.Y * (float)this.Height / (2 * calV1.Z * Math.Tan((float)Math.PI / 4)) + this.Height / 2);
- myFont.DrawText(sprite, "1", x1, y1, Color.Yellow);
- Vector4 calV2 = new Vector4(5f, 10f, 0, 1);
- calV2 = Vector4.Transform(calV2, viewMatrixComp);
- int x2 = (int)(((float)calV2.X * this.Height / (float)(2 * calV2.Z * Math.Tan((float)Math.PI / 4))) + this.Width / 2);
- int y2 = -(int)(calV2.Y * (float)this.Height / (2 * calV2.Z * Math.Tan((float)Math.PI / 4))) + this.Height / 2;
- myFont.DrawText(sprite, "2", x2, y2, Color.Yellow);
-
- Vector4 calV3 = new Vector4(10f, 0f, 0, 1);
- calV3 = Vector4.Transform(calV3, viewMatrixComp);
- int x3 = (int)(((float)calV3.X * this.Height / (float)(2 * calV3.Z * Math.Tan((float)Math.PI / 4))) + this.Width / 2);
- int y3 = (int)(-calV3.Y * (float)this.Height / (2 * calV3.Z * Math.Tan((float)Math.PI / 4)) + this.Height / 2);
- myFont.DrawText(sprite, "3", x3, y3, Color.Yellow);
- Vector4 VectorOP = new Vector4(5f, 10f, 0f, 1f);
- VectorOP = Vector4.Transform(VectorOP, viewMatrixComp);
- VectorOP = Vector4.Transform(VectorOP, projMatrix);
- Matrix ScreenMatrix = Matrix.Zero;
- ScreenMatrix.M11 = ScreenMatrix.M41= (float)this.Width / 2; ScreenMatrix.M22 = -(float)this.Height / 2;
- ScreenMatrix.M42 = (float)this.Height / 2;
- VectorOP = Vector4.Multiply(VectorOP, 1.0f / VectorOP.W);
- VectorOP = Vector4.Transform(VectorOP, ScreenMatrix);
- myFont.DrawText(sprite, "+", (int)VectorOP.X, (int)VectorOP.Y, Color.Yellow);
- */
- Vector4 VectorOPO = new Vector4(5f, 10f, 0f, 1f);
- VectorOPO = Vector4.Transform(VectorOPO, viewMatrixComp);
- VectorOPO = Vector4.Transform(VectorOPO, orthoMatrix);
- Matrix ScreenMatrix = Matrix.Zero;
- ScreenMatrix.M11 = ScreenMatrix.M41 = (float)this.Width / 2; ScreenMatrix.M22 = -(float)this.Height / 2;
- ScreenMatrix.M42 = (float)this.Height / 2;
- VectorOPO = Vector4.Transform(VectorOPO, ScreenMatrix);
- myFont.DrawText(sprite, "+", (int)VectorOPO.X, (int)VectorOPO.Y, Color.Yellow);
-
- sprite.End();
-
- device.EndScene();
- device.Present();
- }
- /// <summary>
- /// 要显示的矩阵
- /// </summary>
- /// <param name="matrix"></param>
- private void ShowMatrix(Matrix matrix)
- {
- string matrixString = "";
- matrixString = matrix.M11.ToString("F3") + " " + matrix.M12.ToString("F3") + " " +
- matrix.M13.ToString("F3") + " " + matrix.M14.ToString("F3") + "n" +
- matrix.M21.ToString("F3") + " " + matrix.M22.ToString("F3") + " " +
- matrix.M23.ToString("F3") + " " + matrix.M24.ToString("F3") + "n" +
- matrix.M31.ToString("F3") + " " + matrix.M32.ToString("F3") + " " +
- matrix.M33.ToString("F3") + " " + matrix.M34.ToString("F3") + "n" +
- matrix.M41.ToString("F3") + " " + matrix.M42.ToString("F3") + " " +
- matrix.M43.ToString("F3") + " " + matrix.M44.ToString("F3") + "n";
- Console.WriteLine(matrixString);
- }
- static void Main()
- {
- MatrixExample MatrixExample = new MatrixExample(); //创建窗体对象
- MatrixExample.FormBorderStyle = FormBorderStyle.None;//设置窗体无框架
- if (MatrixExample.InitializeDirect3D() == false) //检查Direct3D是否启动
- {
- MessageBox.Show("无法启动Direct3D!", "错误!");
- return;
- }
- MatrixExample.Show(); //如果一切都初始化成功,则显示窗体
- while (MatrixExample.Created) //设置一个循环用于实时更新渲染状态
- {
- MatrixExample.Render(); //保持device渲染,直到程序结束
- Application.DoEvents(); //处理键盘鼠标等输入事件
- }
- }
- }
- }