| | |
| | | using DevExpress.Mvvm.Native; |
| | | using DevExpress.XtraCharts.GLGraphics; |
| | | using DevExpress.XtraScheduler.Outlook.Interop; |
| | | using SharpGL; |
| | | |
| | | namespace Yw.WinFrmUI.HydroL3d |
| | |
| | | public NetworkPanelDebug() |
| | | { |
| | | InitializeComponent(); |
| | | this.openGLControl1.MouseWheel += OpenGLControl1_MouseWheel; |
| | | } |
| | | |
| | | |
| | | |
| | | private Network _network = null;//管网 |
| | |
| | | public virtual void Initial(Network network) |
| | | { |
| | | _network = network; |
| | | _paras = new NetworkParas(); |
| | | _paras.BoundingBox = _network.GetBoundingBox(); |
| | | _paras.Ceneter = _paras.BoundingBox.GetCenter(); |
| | | _paras.Rotation = new Point3d(); |
| | | _paras.Translation = new Point3d(); |
| | | _paras.Perspective = new Perspective3d() |
| | | { |
| | | Fovy = 60f, |
| | | Aspect = this.openGLControl1.Width / (float)this.openGLControl1.Height, |
| | | Near = 5, |
| | | Far = 100 |
| | | }; |
| | | _paras.LookAt = new LookAt3d() |
| | | { |
| | | Eye = new Point3d(), |
| | | Center = new Point3d(), |
| | | Up = new Point3d() |
| | | }; |
| | | _paras = _network.GetParas(); |
| | | this.propertyGridControl1.SelectedObject = _paras; |
| | | } |
| | | |
| | | //gl 初始化 |
| | | private void openGLControl1_OpenGLInitialized(object sender, EventArgs e) |
| | | { |
| | | OpenGL gl = openGLControl1.OpenGL; |
| | | gl.ClearColor(0, 0, 0, 0); |
| | | } |
| | | |
| | | //gl 绘制 |
| | | private void openGLControl1_OpenGLDraw(object sender, RenderEventArgs args) |
| | | { |
| | | if (!Initialized) |
| | |
| | | gl.LoadIdentity(); |
| | | |
| | | // 创建透视投影变换 |
| | | gl.Perspective(45f, openGLControl1.Width / openGLControl1.Height, 15696f, 38395f); |
| | | |
| | | gl.Perspective(_paras.Perspective.Fovy, _paras.Perspective.Aspect, _paras.Perspective.Near, _paras.Perspective.Far); |
| | | |
| | | // 设置当前矩阵为模型视图矩阵 |
| | | gl.MatrixMode(OpenGL.GL_MODELVIEW); |
| | |
| | | //重置当前指定的矩阵为单位矩阵,将当前的用户坐标系的原点移到了屏幕中心 |
| | | gl.LoadIdentity(); |
| | | |
| | | // gl.Translate(-_paras.Ceneter.X, -_paras.Ceneter.Y, -_paras.Ceneter.Z); |
| | | //gl.Translate(0, 0, -5f); |
| | | gl.Translate(_paras.Translation.X, _paras.Translation.Y, _paras.Translation.Z); |
| | | |
| | | //gl.Rotate(_paras.Rotation.X, 1, 0, 0); |
| | | // gl.Rotate(_paras.Rotation.Y, 0, 1, 0); |
| | | //gl.Rotate(_paras.Rotation.Z, 0, 0, 1); |
| | | gl.Rotate(_paras.Rotation.X, 1, 0, 0); |
| | | gl.Rotate(_paras.Rotation.Y, 0, 1, 0); |
| | | gl.Rotate(_paras.Rotation.Z, 0, 0, 1); |
| | | |
| | | gl.Color(1.0f, 1.0f, 1.0f); |
| | | foreach (var pipe in _network.Pipes) |
| | |
| | | var startPosition = pipe.StartPosition; |
| | | var endPosition = pipe.EndPosition; |
| | | gl.Begin(OpenGL.GL_LINES); |
| | | |
| | | gl.Vertex(startPosition.X, startPosition.Y, startPosition.Z);//左顶点 |
| | | gl.Vertex(endPosition.X, endPosition.Y, endPosition.Z);//右顶点 |
| | | gl.End(); |
| | | } |
| | | |
| | | gl.Color(1.0f, 1.0f, 1.0f); |
| | | gl.Begin(OpenGL.GL_LINES); |
| | | gl.LookAt(_paras.LookAt.Eye.X, _paras.LookAt.Eye.Y, _paras.LookAt.Eye.Z, |
| | | _paras.LookAt.Center.X, _paras.LookAt.Center.Y, _paras.LookAt.Center.Z, |
| | | _paras.LookAt.Up.X, _paras.LookAt.Up.Y, _paras.LookAt.Up.Z); |
| | | |
| | | gl.Vertex(_paras.BoundingBox.Min.X, _paras.BoundingBox.Min.Y, _paras.BoundingBox.Min.Z);//左顶点 |
| | | gl.Vertex(_paras.BoundingBox.Max.X, _paras.BoundingBox.Max.Y, _paras.BoundingBox.Max.Z);//右顶点 |
| | | gl.End(); |
| | | |
| | | #region 点到线 |
| | | gl.Begin(OpenGL.GL_LINES); |
| | | gl.Color(1.0f, 1.0f, 1.0f); |
| | | gl.Vertex(-2.0f, 0.0f, 0.0f);//左顶点 |
| | | gl.Vertex(2.0f, 2.0f, 0.0f);//右顶点 |
| | | gl.End(); |
| | | #endregion |
| | | |
| | | var minX = _paras.BoundingBox.Min.X; |
| | | var maxX = _paras.BoundingBox.Max.X; |
| | | var minY = _paras.BoundingBox.Min.Y; |
| | | var maxY = _paras.BoundingBox.Max.Y; |
| | | var minZ = _paras.BoundingBox.Min.Z; |
| | | var maxZ = _paras.BoundingBox.Max.Z; |
| | | |
| | | float distance = Math.Max(maxX - minX, Math.Max(maxY - minY, maxZ - minZ)) * 1.5f; |
| | | |
| | | var centerX = (minX + maxX) / 2f; |
| | | var centerY = (minY + maxY) / 2f; |
| | | var centerZ = (minZ + maxZ) / 2f; |
| | | |
| | | var cameraX = centerX + distance; |
| | | var cameraY = centerY + distance; |
| | | var cameraZ = centerZ + distance; |
| | | |
| | | //gl.LookAt(cameraX, cameraY, cameraZ, centerX, centerY, centerZ, 0, 1, 0); |
| | | gl.LookAt(-27484f, -12576f, 8833f, 23675f, 23079f, -7200f, 0f, 0f, 1f); |
| | | gl.Flush(); //强制刷新 |
| | | |
| | | } |
| | | |
| | | private void openGLControl1_OpenGLInitialized(object sender, EventArgs e) |
| | | private void openGLControl1_MouseDown(object sender, MouseEventArgs e) |
| | | { |
| | | OpenGL gl = openGLControl1.OpenGL; |
| | | gl.ClearColor(0, 0, 0, 0); |
| | | HasMouseRightDown(e); |
| | | } |
| | | |
| | | private void openGLControl1_MouseMove(object sender, MouseEventArgs e) |
| | | { |
| | | var hasMouseRightDownMove = MouseRightDownMove(e); |
| | | if (hasMouseRightDownMove) |
| | | { |
| | | this.Invalidate(); |
| | | } |
| | | } |
| | | |
| | | private void openGLControl1_MouseUp(object sender, MouseEventArgs e) |
| | | { |
| | | var hasMouseRightUp = HasMouseRightUp(e); |
| | | if (hasMouseRightUp) |
| | | { |
| | | this.Invalidate(); |
| | | } |
| | | } |
| | | |
| | | private void openGLControl1_MouseHover(object sender, EventArgs e) |
| | | { |
| | | |
| | | } |
| | | |
| | | private void openGLControl1_MouseDoubleClick(object sender, MouseEventArgs e) |
| | | { |
| | | if (!Initialized) |
| | | { |
| | | return; |
| | | } |
| | | if (e.Button == MouseButtons.Left) |
| | | { |
| | | _paras = _network.GetParas(this.openGLControl1); |
| | | this.openGLControl1.Invalidate(); |
| | | this.propertyGridControl1.SelectedObject = _paras; |
| | | this.propertyGridControl1.UpdateRows(); |
| | | } |
| | | } |
| | | |
| | | private void openGLControl1_MouseClick(object sender, MouseEventArgs e) |
| | | { |
| | | |
| | | } |
| | | |
| | | private void OpenGLControl1_MouseWheel(object sender, MouseEventArgs e) |
| | | { |
| | | if (e.Delta > 0) |
| | | { |
| | | _paras.Perspective.Fovy += 1f; |
| | | } |
| | | else |
| | | { |
| | | _paras.Perspective.Fovy -= 1f; |
| | | } |
| | | this.openGLControl1.Invalidate(); |
| | | this.propertyGridControl1.UpdateRows(); |
| | | } |
| | | |
| | | //Resize |
| | | private void openGLControl1_Resized(object sender, EventArgs e) |
| | | { |
| | | if (!Initialized) |
| | | { |
| | | return; |
| | | } |
| | | // _paras.Perspective.Aspect = this.openGLControl1.Width / (float)this.openGLControl1.Height; |
| | | //Reset(); |
| | | _paras.Perspective.Aspect = (float)this.openGLControl1.Width / (float)this.openGLControl1.Height; |
| | | this.propertyGridControl1.UpdateRows(); |
| | | } |
| | | |
| | | //重置 |
| | | private void Reset() |
| | | #region 鼠标右键按下拖动 |
| | | |
| | | /// <summary> |
| | | /// 当鼠标右键按下时允许拖动 |
| | | /// </summary> |
| | | [Browsable(true)] |
| | | [Description("当鼠标右键按下时允许拖动")] |
| | | [DefaultValue(true)] |
| | | public bool AllowMoveWhenMouseRightDown |
| | | { |
| | | if (!Initialized) |
| | | { |
| | | return; |
| | | } |
| | | OpenGL gl = openGLControl1.OpenGL; |
| | | |
| | | // 设置当前矩阵模式,对投影矩阵应用随后的矩阵操作 |
| | | gl.MatrixMode(OpenGL.GL_PROJECTION); |
| | | |
| | | // 重置当前指定的矩阵为单位矩阵,将当前的用户坐标系的原点移到了屏幕中心 |
| | | gl.LoadIdentity(); |
| | | |
| | | // 创建透视投影变换 |
| | | gl.Perspective(_paras.Perspective.Fovy, _paras.Perspective.Aspect, _paras.Perspective.Near, _paras.Perspective.Far); |
| | | |
| | | // 视点变换 |
| | | // gl.LookAt(_paras.LookAt.Eye.X, _paras.LookAt.Eye.Y, _paras.LookAt.Eye.Z, _paras.LookAt.Center.X, _paras.LookAt.Center.Y, _paras.LookAt.Center.Z, _paras.LookAt.Up.X, _paras.LookAt.Up.Y, _paras.LookAt.Up.Z); |
| | | |
| | | // 设置当前矩阵为模型视图矩阵 |
| | | gl.MatrixMode(OpenGL.GL_MODELVIEW); |
| | | get => _allowMoveWhenMouseRightDown; |
| | | set => _allowMoveWhenMouseRightDown = value; |
| | | } |
| | | private bool _allowMoveWhenMouseRightDown = true; |
| | | |
| | | private void propertyGridControl1_CellValueChanged(object sender, DevExpress.XtraVerticalGrid.Events.CellValueChangedEventArgs e) |
| | | protected bool _hasMouseRightDown = false;//鼠标右键是否按下 |
| | | protected Point _mouseRightDownMovePoint;//鼠标右键按下移动点 |
| | | |
| | | /// <summary> |
| | | /// 判断鼠标右键是否按下 |
| | | /// </summary> |
| | | protected virtual bool HasMouseRightDown(MouseEventArgs e) |
| | | { |
| | | if (!Initialized) |
| | | if (e.Button == MouseButtons.Right) |
| | | { |
| | | return; |
| | | _hasMouseRightDown = true; |
| | | _mouseRightDownMovePoint = e.Location; |
| | | return true; |
| | | } |
| | | //Reset(); |
| | | return false; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 鼠标右键按下移动 |
| | | /// </summary> |
| | | protected virtual bool MouseRightDownMove(MouseEventArgs e) |
| | | { |
| | | if (_hasMouseRightDown) |
| | | { |
| | | if (this.AllowMoveWhenMouseRightDown) |
| | | { |
| | | if (this.Initialized) |
| | | { |
| | | var pt = new PointF(e.X - _mouseRightDownMovePoint.X, e.Y - _mouseRightDownMovePoint.Y); |
| | | _paras.Translation.X += pt.X; |
| | | _paras.Translation.Y -= pt.Y; |
| | | _mouseRightDownMovePoint = e.Location; |
| | | return true; |
| | | } |
| | | } |
| | | } |
| | | return false; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 判断鼠标左键是否弹起 |
| | | /// </summary> |
| | | protected virtual bool HasMouseRightUp(MouseEventArgs e) |
| | | { |
| | | if (_hasMouseRightDown) |
| | | { |
| | | _hasMouseRightDown = false; |
| | | return true; |
| | | } |
| | | return false; |
| | | } |
| | | |
| | | #endregion |
| | | |
| | | } |
| | | } |