lixiaojun
2024-09-24 5610ef481946ff15d856f65e08b3f25aeaf3e269
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
using 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;
 
namespace Yw.WinFrmUI.HydroL3d
{
    public partial class NetworkPanel : DevExpress.XtraEditors.XtraUserControl
    {
        public NetworkPanel()
        {
            InitializeComponent();
        }
 
        protected Network _network = null;//管网
        protected BoundingBox3d _bounndingBox = null;//包围盒
        protected Point3d _center = null;//中心
 
        /// <summary>
        /// 是否初始化
        /// </summary>
        public bool Initialized => _network != null;
 
 
        /// <summary>
        /// 初始化
        /// </summary>
        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);
 
            //重置当前指定的矩阵为单位矩阵,将当前的用户坐标系的原点移到了屏幕中心
            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();   //强制刷新
 
        }
 
        private void openGLControl1_OpenGLInitialized(object sender, EventArgs e)
        {
            OpenGL gl = openGLControl1.OpenGL;
            gl.ClearColor(0, 0, 0, 0);
        }
 
 
        private void openGLControl1_Resize(object sender, EventArgs e)
        {
            if (_network == null)
            {
                return;
            }
            OpenGL gl = openGLControl1.OpenGL;
 
            // 设置当前矩阵模式,对投影矩阵应用随后的矩阵操作
            gl.MatrixMode(OpenGL.GL_PROJECTION);
 
            // 重置当前指定的矩阵为单位矩阵,将当前的用户坐标系的原点移到了屏幕中心
            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.MatrixMode(OpenGL.GL_MODELVIEW);
 
        }
    }
}