From 12a6316ffa897b4ce4205f545b88359195b386d6 Mon Sep 17 00:00:00 2001
From: duheng <2784771470@qq.com>
Date: 星期三, 25 九月 2024 17:34:24 +0800
Subject: [PATCH] Merge branch 'master' of http://47.103.154.90:83/r/HStation/XHS.V1.0

---
 WinFrmUI/Yw.WinFrmUI.Hydro.L3d.Core/02-panel/NetworkPanel.cs |  408 ++++++++++++++++++++++++++++++++++++++++++---------------
 1 files changed, 299 insertions(+), 109 deletions(-)

diff --git a/WinFrmUI/Yw.WinFrmUI.Hydro.L3d.Core/02-panel/NetworkPanel.cs b/WinFrmUI/Yw.WinFrmUI.Hydro.L3d.Core/02-panel/NetworkPanel.cs
index f9b244a..13b48da 100644
--- a/WinFrmUI/Yw.WinFrmUI.Hydro.L3d.Core/02-panel/NetworkPanel.cs
+++ b/WinFrmUI/Yw.WinFrmUI.Hydro.L3d.Core/02-panel/NetworkPanel.cs
@@ -1,16 +1,6 @@
-锘縰sing DevExpress.XtraBars.Docking.Paint;
-using DevExpress.XtraEditors;
-using SharpGL;
-using System;
-using System.Collections.Generic;
-using System.ComponentModel;
-using System.Data;
-using System.Drawing;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
-using System.Windows.Forms;
-using System.Windows.Media;
+锘縰sing DevExpress.CodeParser;
+using DevExpress.XtraCharts.Native;
+using Yw.Untity;
 
 namespace Yw.WinFrmUI.HydroL3d
 {
@@ -19,11 +9,19 @@
         public NetworkPanel()
         {
             InitializeComponent();
+            this.openGLControl1.MouseWheel += openGLControl1_MouseWheel;
+            this.propertyGridControl1.CellValueChanged += PropertyGridControl1_CellValueChanged;
         }
 
-        protected Network _network = null;//绠$綉
-        protected BoundingBox3d _bounndingBox = null;//鍖呭洿鐩�
-        protected Point3d _center = null;//涓績
+
+
+        private Network _network = null;//绠$綉
+        private NetworkParas _paras = null;//鍙傛暟
+        private float _zoom = 1f;//缂╂斁
+        private float _rotationX = 0.0f;//x鏃嬭浆瑙掑害
+        private float _rotationY = 0.0f;//y鏃嬭浆瑙掑害
+        private float _viewPortX = 0f;//
+        private float _viewPortY = 0f;//
 
         /// <summary>
         /// 鏄惁鍒濆鍖�
@@ -37,108 +35,50 @@
         public virtual void Initial(Network network)
         {
             _network = network;
-            _bounndingBox = _network.GetBoundingBox();
-            _center = _bounndingBox.GetCenter();
+            _paras = _network.GetParas();
+            InitialParas();
+            this.propertyGridControl1.SelectedObject = _paras;
         }
 
-        private void openGLControl1_OpenGLDraw(object sender, RenderEventArgs args)
+        //鍒濆鍖栧弬鏁�
+        private void InitialParas()
         {
-            if (_network == null)
-            {
-                return;
-            }
-            SharpGL.OpenGL gl = this.openGLControl1.OpenGL;
-            //娓呴櫎娣卞害缂撳瓨 
-            gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT);
-
-            //閲嶇疆褰撳墠鎸囧畾鐨勭煩闃典负鍗曚綅鐭╅樀,灏嗗綋鍓嶇殑鐢ㄦ埛鍧愭爣绯荤殑鍘熺偣绉诲埌浜嗗睆骞曚腑蹇�
-            gl.LoadIdentity();
-
-            gl.Translate(-_center.X, -_center.Y, -_bounndingBox.Max.Z);
-
-            foreach (var pipe in _network.Pipes)
-            {
-                var startPosition = pipe.StartPosition;
-                var endPosition = pipe.EndPosition;
-                gl.Begin(OpenGL.GL_LINES);
-                gl.Color(1.0f, 1.0f, 1.0f);
-                gl.Vertex(startPosition.X, startPosition.Y, startPosition.Z);//宸﹂《鐐�
-                gl.Vertex(endPosition.X, endPosition.Y, endPosition.Z);//鍙抽《鐐�
-                gl.End();
-            }
-
-            //鍧愭爣杞村彉鎹綅缃埌(0.0f, 0.0f, -5.0f)锛岃繖鏍锋垜浠殑鍧愭爣杞村氨鐩稿綋浜庡線灞忓箷鍐呰蛋5涓崟浣�
-            //gl.Translate(0.0f, 0.0f, -5.0f);
-
-            //rotation_X += 1f;
-            //gl.Rotate(rotation_X, 1.0f, 0.0f, 0.0f);//rotationX:瑙掑害
-            //rotation_Y += 1f;
-            //gl.Rotate(rotation_Y, 0.0f, 1.0f, 0.0f);//rotationY:瑙掑害
-            //rotation_Z += 1f;
-            //gl.Rotate(rotation_Z, 0.0f, 0.0f, 1.0f);//rotationZ:瑙掑害
-
-            //#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
-            //#region 绾挎垚闈�(涓夎褰�)
-            //gl.Begin(OpenGL.GL_TRIANGLES);//绗竴涓潰
-            //gl.Color(1.0f, 0.0f, 0.0f);
-            //gl.Vertex(0.0f, 1f, 0.0f);//椤剁偣
-            //gl.Color(0.0f, 1.0f, 0.0f);
-            //gl.Vertex(-1.0f, -1.0f, 0.0f);//宸﹂《鐐� 
-            //gl.Color(0.0f, 0.0f, 1.0f);
-            //gl.Vertex(1.0f, -1.0f, 0.0f);//鍙抽《鐐�
-            //gl.End();
-            //#endregion
-            //#region 闈㈢粍鍚堟垚浣�
-            //gl.Begin(OpenGL.GL_TRIANGLES);//绗簩涓潰
-            //gl.Color(1.0f, 1.0f, 1.0f);
-            //gl.Vertex(0.0f, 0.0f, -2.0f);//绗洓涓偣
-            //gl.Color(0.0f, 1.0f, 0.0f);
-            //gl.Vertex(-1.0f, -1.0f, 0.0f);//宸﹂《鐐� 
-            //gl.Color(0.0f, 0.0f, 1.0f);
-            //gl.Vertex(1.0f, -1.0f, 0.0f);//鍙抽《鐐�
-            //gl.End();
-            //gl.Begin(OpenGL.GL_TRIANGLES);//绗笁涓潰
-            //gl.Color(1.0f, 1.0f, 1.0f);
-            //gl.Vertex(0.0f, 0.0f, -2.0f);//绗洓涓偣
-            //gl.Color(0.0f, 1.0f, 0.0f);
-            //gl.Vertex(-1.0f, -1.0f, 0.0f);//宸﹂《鐐� 
-            //gl.Color(1.0f, 0.0f, 0.0f);
-            //gl.Vertex(0.0f, 1f, 0.0f);//椤剁偣
-            //gl.End();
-            //gl.Begin(OpenGL.GL_TRIANGLES);//绗洓涓潰
-            //gl.Color(1.0f, 1.0f, 1.0f);
-            //gl.Vertex(0.0f, 0.0f, -2.0f);//绗洓涓偣
-            //gl.Color(0.0f, 0.0f, 1.0f);
-            //gl.Vertex(1.0f, -1.0f, 0.0f);//鍙抽《鐐�
-            //gl.Color(1.0f, 0.0f, 0.0f);
-            //gl.Vertex(0.0f, 1f, 0.0f);//椤剁偣
-            //gl.End();
-            //#endregion
-
-            gl.Flush();   //寮哄埗鍒锋柊
-
+            _zoom = _paras.Scale.X;
+            _rotationX = _paras.Rotation.X;
+            _rotationY = _paras.Rotation.Y;
+            _viewPortX = 0f;
+            _viewPortY = 0f;
         }
 
+        private void PropertyGridControl1_CellValueChanged(object sender, DevExpress.XtraVerticalGrid.Events.CellValueChangedEventArgs e)
+        {
+            InitialParas();
+            this.openGLControl1.Refresh();
+        }
+
+        //OpenGL 鍒濆鍖�
         private void openGLControl1_OpenGLInitialized(object sender, EventArgs e)
         {
             OpenGL gl = openGLControl1.OpenGL;
-            gl.ClearColor(0, 0, 0, 0);
+            gl.ClearColor(214 / 255f, 224 / 255f, 235 / 255f, 0);
         }
 
-
-        private void openGLControl1_Resize(object sender, EventArgs e)
+        //OpenGL 缁樺埗
+        private void openGLControl1_OpenGLDraw(object sender, RenderEventArgs args)
         {
-            if (_network == null)
+            if (!Initialized)
             {
                 return;
             }
             OpenGL gl = openGLControl1.OpenGL;
+            //鍚敤娣卞害娴嬭瘯
+            gl.Enable(OpenGL.GL_DEPTH_TEST);
+
+            //娓呴櫎娣卞害缂撳瓨 
+            gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT);
+
+            // 璁剧疆瑙嗗彛
+            gl.Viewport((int)_viewPortX, (int)_viewPortY, this.openGLControl1.Width, this.openGLControl1.Height);
 
             // 璁剧疆褰撳墠鐭╅樀妯″紡,瀵规姇褰辩煩闃靛簲鐢ㄩ殢鍚庣殑鐭╅樀鎿嶄綔
             gl.MatrixMode(OpenGL.GL_PROJECTION);
@@ -147,16 +87,266 @@
             gl.LoadIdentity();
 
             // 鍒涘缓閫忚鎶曞奖鍙樻崲
-            //gl.Perspective(30.0f, (double)Width / (double)Height, 5, 100);
-            gl.Perspective(150, (double)Width / (double)Height, 5, _bounndingBox.Max.Z);
-
-            // 瑙嗙偣鍙樻崲
-            //gl.LookAt(0, 5, 0, 0, 0, 0, 0, 1, 0);
-            gl.LookAt(_center.X, _center.Y, 0, _center.X, _center.Y, _center.Z, 0, 1, 0);
+            gl.Perspective(_paras.Perspective.Fovy, _paras.Perspective.Aspect, _paras.Perspective.Near, _paras.Perspective.Far);
 
             // 璁剧疆褰撳墠鐭╅樀涓烘ā鍨嬭鍥剧煩闃�
             gl.MatrixMode(OpenGL.GL_MODELVIEW);
 
+            //閲嶇疆褰撳墠鎸囧畾鐨勭煩闃典负鍗曚綅鐭╅樀,灏嗗綋鍓嶇殑鐢ㄦ埛鍧愭爣绯荤殑鍘熺偣绉诲埌浜嗗睆骞曚腑蹇�
+            gl.LoadIdentity();
+
+            //骞崇Щ
+            gl.Translate(_paras.Translation.X, _paras.Translation.Y, _paras.Translation.Z);
+
+
+            //灏唞杞村悜涓�
+            //gl.Rotate(90f, 1f, 0f, 0f);
+            //鏃嬭浆
+            gl.Rotate(_rotationX, 1, 0, 0);
+            gl.Rotate(_rotationY, 0, 1, 0);
+
+            //缂╂斁
+            gl.Scale(_zoom, _zoom, _zoom);
+
+            _network.Draw(gl);
+
+            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.Flush();   //寮哄埗鍒锋柊
+
         }
+
+        //Resize
+        private void openGLControl1_Resized(object sender, EventArgs e)
+        {
+            if (!Initialized)
+            {
+                return;
+            }
+            _paras.Perspective.Aspect = (float)this.openGLControl1.Width / (float)this.openGLControl1.Height;
+            _viewPortX = 0f;
+            _viewPortY = 0f;
+        }
+
+        #region 榧犳爣宸﹂敭鎸変笅鏃嬭浆
+
+        /// <summary>
+        /// 褰撻紶鏍囧乏閿寜涓嬫椂鍏佽鏃嬭浆
+        /// </summary>
+        [Browsable(true)]
+        [Description("褰撻紶鏍囧乏閿寜涓嬫椂鍏佽鏃嬭浆")]
+        [DefaultValue(true)]
+        public bool AllowRotateWhenMouseLeftDown
+        {
+            get => _allowRotateWhenMouseLeftDown;
+            set => _allowRotateWhenMouseLeftDown = value;
+        }
+        private bool _allowRotateWhenMouseLeftDown = true;
+
+        protected bool _hasMouseLeftDown = false;//榧犳爣宸﹂敭鏄惁鎸変笅
+        protected Point _mouseLeftDownRotatePoint;//榧犳爣宸﹂敭鎸変笅鏃嬭浆鐐� 
+
+        /// <summary>
+        /// 鍒ゆ柇榧犳爣宸﹂敭鏄惁鎸変笅
+        /// </summary>
+        protected virtual bool HasMouseLeftDown(MouseEventArgs e)
+        {
+            if (e.Button == MouseButtons.Left)
+            {
+                _hasMouseLeftDown = true;
+                _mouseLeftDownRotatePoint = e.Location;
+                return true;
+            }
+            return false;
+        }
+
+        /// <summary>
+        /// 榧犳爣宸﹂敭鎸変笅鏃嬭浆
+        /// </summary>
+        protected virtual bool MouseLeftDownRotate(MouseEventArgs e)
+        {
+            if (_hasMouseLeftDown)
+            {
+                if (this.AllowRotateWhenMouseLeftDown)
+                {
+                    if (this.Initialized)
+                    {
+                        int deltaX = e.X - _mouseLeftDownRotatePoint.X;
+                        int deltaY = e.Y - _mouseLeftDownRotatePoint.Y;
+
+                        _rotationX += deltaY * 0.05f;
+                        _rotationY += deltaX * 0.05f;
+
+                        _mouseLeftDownRotatePoint = e.Location;
+                        return true;
+                    }
+                }
+            }
+            return false;
+        }
+
+        /// <summary>
+        /// 鍒ゆ柇榧犳爣宸﹂敭鏄惁寮硅捣
+        /// </summary>
+        protected virtual bool HasMouseLeftUp(MouseEventArgs e)
+        {
+            if (_hasMouseLeftDown)
+            {
+                _hasMouseLeftDown = false;
+                return true;
+            }
+            return false;
+        }
+
+        #endregion
+
+        #region 榧犳爣鍙抽敭鎸変笅鎷栧姩
+
+        /// <summary>
+        /// 褰撻紶鏍囧彸閿寜涓嬫椂鍏佽鎷栧姩
+        /// </summary>
+        [Browsable(true)]
+        [Description("褰撻紶鏍囧彸閿寜涓嬫椂鍏佽鎷栧姩")]
+        [DefaultValue(true)]
+        public bool AllowMoveWhenMouseRightDown
+        {
+            get => _allowMoveWhenMouseRightDown;
+            set => _allowMoveWhenMouseRightDown = value;
+        }
+        private bool _allowMoveWhenMouseRightDown = true;
+
+        protected bool _hasMouseRightDown = false;//榧犳爣鍙抽敭鏄惁鎸変笅
+        protected Point _mouseRightDownMovePoint;//榧犳爣鍙抽敭鎸変笅绉诲姩鐐�
+
+        /// <summary>
+        /// 鍒ゆ柇榧犳爣鍙抽敭鏄惁鎸変笅
+        /// </summary>
+        protected virtual bool HasMouseRightDown(MouseEventArgs e)
+        {
+            if (e.Button == MouseButtons.Right)
+            {
+                _hasMouseRightDown = true;
+                _mouseRightDownMovePoint = e.Location;
+                return true;
+            }
+            return false;
+        }
+
+        /// <summary>
+        /// 榧犳爣鍙抽敭鎸変笅绉诲姩
+        /// </summary>
+        protected virtual bool MouseRightDownMove(MouseEventArgs e)
+        {
+            if (_hasMouseRightDown)
+            {
+                if (this.AllowMoveWhenMouseRightDown)
+                {
+                    if (this.Initialized)
+                    {
+                        int deltaX = e.X - _mouseRightDownMovePoint.X;
+                        int deltaY = e.Y - _mouseRightDownMovePoint.Y;
+
+                        _viewPortX += deltaX;
+                        _viewPortY -= deltaY;
+                        _mouseRightDownMovePoint = e.Location;
+                        return true;
+                    }
+                }
+            }
+            return false;
+        }
+
+        /// <summary>
+        /// 鍒ゆ柇榧犳爣宸﹂敭鏄惁寮硅捣
+        /// </summary>
+        protected virtual bool HasMouseRightUp(MouseEventArgs e)
+        {
+            if (_hasMouseRightDown)
+            {
+                _hasMouseRightDown = false;
+                return true;
+            }
+            return false;
+        }
+
+        #endregion
+
+        #region 榧犳爣浜嬩欢
+
+        private void openGLControl1_MouseDown(object sender, MouseEventArgs e)
+        {
+            HasMouseLeftDown(e);
+            HasMouseRightDown(e);
+        }
+
+        private void openGLControl1_MouseMove(object sender, MouseEventArgs e)
+        {
+            var list = this.openGLControl1.OpenGL.UnProject(e.X, e.Y, 0);
+            this.labelControl1.Text = DoubleListHelper.ToString(list);
+            var hasMouseLeftDownRotate = MouseLeftDownRotate(e);
+            var hasMouseRightDownMove = MouseRightDownMove(e);
+            if (hasMouseLeftDownRotate || hasMouseRightDownMove)
+            {
+                this.openGLControl1.Invalidate();
+            }
+
+        }
+
+        private void openGLControl1_MouseUp(object sender, MouseEventArgs e)
+        {
+            var hasMouseLeftUp = HasMouseLeftUp(e);
+            var hasMouseRightUp = HasMouseRightUp(e);
+            if (hasMouseLeftUp || hasMouseRightUp)
+            {
+                this.openGLControl1.Invalidate();
+            }
+        }
+
+        private void openGLControl1_MouseHover(object sender, EventArgs e)
+        {
+            if (!Initialized)
+            {
+                return;
+            }
+            var pt = this.openGLControl1.PointToClient(MousePosition);
+            var list = this.openGLControl1.OpenGL.UnProject(pt.X, pt.Y, 0);
+            var wpt = new Point3d(list[0], list[1], list[2]);
+            _network.Hover(wpt);
+        }
+
+        private void openGLControl1_MouseDoubleClick(object sender, MouseEventArgs e)
+        {
+            if (!Initialized)
+            {
+                return;
+            }
+            if (e.Button == MouseButtons.Left)
+            {
+                InitialParas();
+                this.openGLControl1.Invalidate();
+            }
+        }
+
+        private void openGLControl1_MouseClick(object sender, MouseEventArgs e)
+        {
+
+        }
+
+        private void openGLControl1_MouseWheel(object sender, MouseEventArgs e)
+        {
+            if (e.Delta > 0)
+            {
+                _zoom *= 1.1f;
+            }
+            else
+            {
+                _zoom /= 1.1f;
+            }
+            this.openGLControl1.Refresh();
+        }
+
+        #endregion
     }
 }

--
Gitblit v1.9.3