lixiaojun
2024-09-23 4e38ec6e6bdbbe8d8f2921b7a7ea6377cb77d213
WinFrmUI/Yw.WinFrmUI.Hydro.L3d.Core/02-panel/NetworkPanel.cs
@@ -22,15 +22,14 @@
        }
        protected Network _network = null;//管网
        protected BoundingBox3d _bounndingBox = null;//包围盒
        protected Point3d _center = null;//中心
        /// <summary>
        /// 是否初始化
        /// </summary>
        public bool Initialized => _network != null;
        private float rotation_X = 0.0f;
        private float rotation_Y = 0.0f;
        private float rotation_Z = 0.0f;
        /// <summary>
        /// 初始化
@@ -38,10 +37,16 @@
        public virtual void Initial(Network network)
        {
            _network = network;
            _bounndingBox = _network.GetBoundingBox();
            _center = _bounndingBox.GetCenter();
        }
        private void openGLControl1_OpenGLDraw(object sender, RenderEventArgs args)
        {
            if (_network == null)
            {
                return;
            }
            SharpGL.OpenGL gl = this.openGLControl1.OpenGL;
            //清除深度缓存 
            gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT);
@@ -49,59 +54,72 @@
            //重置当前指定的矩阵为单位矩阵,将当前的用户坐标系的原点移到了屏幕中心
            gl.LoadIdentity();
            //坐标轴变换位置到(0.0f, 0.0f, -5.0f),这样我们的坐标轴就相当于往屏幕内走5个单位
            gl.Translate(0.0f, 0.0f, -5.0f);
            gl.Translate(-_center.X, -_center.Y, -_bounndingBox.Max.Z);
            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 点到线
            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(-2.0f, 0.0f, 0.0f);//左顶点
            gl.Vertex(2.0f, 2.0f, 0.0f);//右顶点
                gl.Vertex(startPosition.X, startPosition.Y, startPosition.Z);//左顶点
                gl.Vertex(endPosition.X, endPosition.Y, endPosition.Z);//右顶点
            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
            }
            //坐标轴变换位置到(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();   //强制刷新
@@ -116,6 +134,10 @@
        private void openGLControl1_Resize(object sender, EventArgs e)
        {
            if (_network == null)
            {
                return;
            }
            OpenGL gl = openGLControl1.OpenGL;
            // 设置当前矩阵模式,对投影矩阵应用随后的矩阵操作
@@ -125,13 +147,16 @@
            gl.LoadIdentity();
            // 创建透视投影变换
            gl.Perspective(30.0f, (double)Width / (double)Height, 5, 100.0);
            //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(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.MatrixMode(OpenGL.GL_MODELVIEW);
        }
    }
}