ClickRay.cs
上传用户:lslight
上传日期:2022-01-10
资源大小:14248k
文件大小:13k
- ///////////////////////////////////////////////////////////////////////
- // ■■■■ ■■■■■ ■■■■ ■ ■ //
- // ■ ■ ■ ■ ■ //
- // ■ ■ ■ ■■■ ■ ■ //
- // ■ ■ ■ ■ ■ ■ //
- // ■■■■ ■ ■■■■ ■■■■ //
- // 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 ClickRay : Form
- {
- Device device = null;//定义绘图设备
- private float angleY=0.01f;//定义绕Y轴旋转变量
- private Vector3 CamPostion = new Vector3(0, 30, -30);//定义摄像机位置
- private Vector3 CamTarget = new Vector3(0, 0, 0);//定义摄像机目标位置
- private int mouseLastX,mouseLastY;//记录鼠标按下时的坐标位置
- private bool isRotateByMouse=false;//记录是否由鼠标控制旋转
- private bool isMoveByMouse = false;//记录是否由鼠标控制移动
- private CustomVertex.PositionColored[] rayVertices;//定义射线顶点变量
- public ClickRay()
- {
- this.ClientSize = new Size(800, 600);//指定窗体尺寸
- this.Text = "点击射线";//指定窗体标题
- }
- public bool InitializeDirect3D()
- {
- try
- {
- PresentParameters presentParams = new PresentParameters();
- presentParams.Windowed = true; //指定以Windows窗体形式显示
- presentParams.SwapEffect = SwapEffect.Discard; //当前屏幕绘制后它将自动从内存中删除
- presentParams.AutoDepthStencilFormat = DepthFormat.D16;
- presentParams.EnableAutoDepthStencil = true;
- presentParams.PresentationInterval = PresentInterval.Immediate;
- device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams); //实例化device对象
- return true;
- }
- catch (DirectXException e)
- {
- MessageBox.Show(e.ToString(), "Error"); //处理异常
- return false;
- }
- }
- 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);
- }
- public void Render()
- {
- if (device == null) //如果device为空则不渲染
- {
- return;
- }
- SetUpCamera();
- device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.DarkSlateBlue, 1.0f, 0); //清除windows界面为深蓝色
- device.BeginScene();
- //在此添加渲染图形代码
- device.RenderState.Lighting = false;
- if (rayVertices != null)
- {
- device.VertexFormat = CustomVertex.PositionColored.Format;
- device.DrawUserPrimitives(PrimitiveType.LineList, rayVertices.Length / 2, rayVertices);
- }
- device.EndScene();
- device.Present();
- }
- private void SetUpCamera()//摄像机
- {
- Matrix viewMatrix = Matrix.LookAtLH(CamPostion, CamTarget, new Vector3(0, 1, 0));
- device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, this.ClientSize.Width / this.ClientSize.Height, 0.3f, 500f);
- device.Transform.View = viewMatrix;
- }
- protected override void OnKeyDown(KeyEventArgs e)
- {
- Vector4 tempV4;
- Matrix currentView = device.Transform.View;//当前摄像机的视图矩阵
- switch (e.KeyCode)
- {
- case Keys.Left:
- CamPostion.Subtract(CamTarget);
- tempV4 = Vector3.Transform(CamPostion, Matrix.RotationQuaternion(
- Quaternion.RotationAxis(new Vector3(currentView.M12, currentView.M22, currentView.M32), -angleY)));
- CamPostion.X = tempV4.X + CamTarget.X;
- CamPostion.Y = tempV4.Y + CamTarget.Y;
- CamPostion.Z = tempV4.Z + CamTarget.Z;
- break;
- case Keys.Right:
- CamPostion.Subtract(CamTarget);
- tempV4 = Vector3.Transform(CamPostion, Matrix.RotationQuaternion(
- Quaternion.RotationAxis(new Vector3(currentView.M12, currentView.M22, currentView.M32), angleY)));
- CamPostion.X = tempV4.X + CamTarget.X;
- CamPostion.Y = tempV4.Y + CamTarget.Y;
- CamPostion.Z = tempV4.Z + CamTarget.Z;
- break;
- case Keys.Up:
- CamPostion.Subtract(CamTarget);
- tempV4 = Vector3.Transform(CamPostion, Matrix.RotationQuaternion(
- Quaternion.RotationAxis(new Vector3(device.Transform.View.M11
- , device.Transform.View.M21, device.Transform.View.M31), -angleY)));
- CamPostion.X = tempV4.X + CamTarget.X;
- CamPostion.Y = tempV4.Y + CamTarget.Y;
- CamPostion.Z = tempV4.Z + CamTarget.Z;
- break;
- case Keys.Down:
- CamPostion.Subtract(CamTarget);
- tempV4 = Vector3.Transform(CamPostion, Matrix.RotationQuaternion(
- Quaternion.RotationAxis(new Vector3(device.Transform.View.M11
- , device.Transform.View.M21, device.Transform.View.M31), angleY)));
- CamPostion.X = tempV4.X + CamTarget.X;
- CamPostion.Y = tempV4.Y + CamTarget.Y;
- CamPostion.Z = tempV4.Z + CamTarget.Z;
- break;
- case Keys.Add:
- CamPostion.Subtract(CamTarget);
- CamPostion.Scale(0.95f);
- CamPostion.Add(CamTarget);
- break;
- case Keys.Subtract:
- CamPostion.Subtract(CamTarget);
- CamPostion.Scale(1.05f);
- CamPostion.Add(CamTarget);
- break;
- }
- Matrix viewMatrix = Matrix.LookAtLH(CamPostion, CamTarget, new Vector3(0, 1, 0));
- device.Transform.View = viewMatrix;
- }
- protected override void OnMouseDown(MouseEventArgs e)
- {
- if (e.Button == MouseButtons.Left)
- {
- mouseLastX = e.X;
- mouseLastY = e.Y;
- isRotateByMouse = true;
-
- //计算摄像机投影窗口上对应的点击位置坐标
- Vector3 SVector = new Vector3();
- SVector.X = 2 * (float)e.X / this.ClientSize.Width - 1;
- SVector.Y = -2 * (float)e.Y / this.ClientSize.Height + 1;
- SVector.Z = 1.0f / (float)Math.Tan(Math.PI / 8);
- //视图矩阵
- Matrix viewMatrix = device.Transform.View;
- viewMatrix.Invert();//计算视图矩阵的逆矩阵
- Vector4 VectorOP = Vector3.Transform(SVector, viewMatrix);
- //射线位置
- Vector3 rayPos = new Vector3(VectorOP.X, VectorOP.Y, VectorOP.Z);
- //射线方向
- Vector3 rayDir = Vector3.Subtract(rayPos,CamPostion);
- //设置绘制射线的顶点坐标
- rayVertices = new CustomVertex.PositionColored[2];
- rayVertices[0].Position = rayPos;
- rayVertices[0].Color = Color.Red.ToArgb();
- rayVertices[1].Position = rayDir;
- rayVertices[1].Color = Color.Red.ToArgb();
- }
- else if (e.Button == MouseButtons.Middle)
- {
- mouseLastX = e.X;
- mouseLastY = e.Y;
- isMoveByMouse=true;
- }
- }
-
- protected override void OnMouseUp(MouseEventArgs e)
- {
- isRotateByMouse = false;
- isMoveByMouse = false;
- }
- protected override void OnMouseMove(MouseEventArgs e)
- {
- if (isRotateByMouse)
- {
- Matrix currentView = device.Transform.View;//当前摄像机的视图矩阵
- float tempAngleY = 2 * (float)(e.X - mouseLastX) / this.Width;
- CamPostion.Subtract(CamTarget);
- Vector4 tempV4 = Vector3.Transform(CamPostion, Matrix.RotationQuaternion(
- Quaternion.RotationAxis(new Vector3(currentView.M12, currentView.M22, currentView.M32), tempAngleY)));
- CamPostion.X = tempV4.X;
- CamPostion.Y = tempV4.Y;
- CamPostion.Z = tempV4.Z;
- float tempAngleX = 4 * (float)(e.Y - mouseLastY) / this.Height;
- tempV4 = Vector3.Transform(CamPostion, Matrix.RotationQuaternion(
- Quaternion.RotationAxis(new Vector3(currentView.M11, currentView.M21, currentView.M31), tempAngleX)));
- CamPostion.X = tempV4.X + CamTarget.X;
- CamPostion.Y = tempV4.Y + CamTarget.Y;
- CamPostion.Z = tempV4.Z + CamTarget.Z;
- Matrix viewMatrix = Matrix.LookAtLH(CamPostion, CamTarget, new Vector3(0, 1, 0));
- device.Transform.View = viewMatrix;
- mouseLastX = e.X;
- mouseLastY = e.Y;
- }
- else if (isMoveByMouse)
- {
- Matrix currentView = device.Transform.View;//当前摄像机的视图矩阵
- float moveFactor=0.01f;
- CamTarget.X += -moveFactor * ((e.X - mouseLastX) * currentView.M11 - (e.Y - mouseLastY) * currentView.M12);
- CamTarget.Y += -moveFactor * ((e.X - mouseLastX) * currentView.M21 - (e.Y - mouseLastY) * currentView.M22);
- CamTarget.Z += -moveFactor * ((e.X - mouseLastX) * currentView.M31 - (e.Y - mouseLastY) * currentView.M32);
- CamPostion.X +=- moveFactor * ((e.X - mouseLastX) * currentView.M11 - (e.Y - mouseLastY) * currentView.M12);
- CamPostion.Y += -moveFactor * ((e.X - mouseLastX) * currentView.M21 - (e.Y - mouseLastY) * currentView.M22);
- CamPostion.Z += -moveFactor * ((e.X - mouseLastX) * currentView.M31 - (e.Y - mouseLastY) * currentView.M32);
-
- Matrix viewMatrix = Matrix.LookAtLH(CamPostion, CamTarget, new Vector3(0, 1, 0));
- device.Transform.View = viewMatrix;
- mouseLastX = e.X;
- mouseLastY = e.Y;
- }
- }
- protected override void OnMouseWheel(MouseEventArgs e)
- {
- float scaleFactor = -(float)e.Delta / 2000 + 1f;
- CamPostion.Subtract(CamTarget);
- CamPostion.Scale(scaleFactor);
- CamPostion.Add(CamTarget);
- Matrix viewMatrix = Matrix.LookAtLH(CamPostion, CamTarget, new Vector3(0, 1, 0));
- device.Transform.View = viewMatrix;
- }
- static void Main()
- {
- ClickRay clickRay = new ClickRay(); //创建窗体对象
- if (clickRay.InitializeDirect3D() == false) //检查Direct3D是否启动
- {
- MessageBox.Show("无法启动Direct3D!", "错误!");
- return;
- }
- clickRay.Show(); //如果一切都初始化成功,则显示窗体
- while (clickRay.Created) //设置一个循环用于实时更新渲染状态
- {
- clickRay.Render(); //保持device渲染,直到程序结束
- Application.DoEvents(); //处理键盘鼠标等输入事件
- }
- }
- }
- }