lixiaojun
2025-03-17 85fc43aa549bbe24c4d867a055c0d90d21deba8b
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
using OpenTK.GLControl;
using OpenTK.Graphics.OpenGL;
using OpenTK.Mathematics;
using System.Collections.Immutable;
using System.Transactions;
using System.Windows.Forms;
 
namespace Yw.WinFrmUI.Hydro
{
    /// <summary>
    /// 
    /// </summary>
    public partial class Drawer2d : UserControl
    {
        /// <summary>
        /// 
        /// </summary>
        public Drawer2d()
        {
            InitializeComponent();
            this.glControl1.Load += glControl_Load;
            this.glControl1.Paint += glControl_Paint;
            this.glControl1.Resize += glControl_Resize;
            this.glControl1.MouseWheel += OnMouseWheel;
            this.glControl1.MouseDown += OnMouseDown;
            this.glControl1.MouseUp += OnMouseUp;
            this.glControl1.MouseMove += OnMouseMove;
        }
 
 
 
        private NetworkL3d _nw = null;
        private Ortho2dCamera _orthoHelper = null;
 
        /// <summary>
        /// 初始化管网
        /// </summary>
        public void InitialNetwork(NetworkL3d nw)
        {
            _nw = nw;
            var pts = _nw.Nodes.Select(x => new Vector3(x.Position.X, x.Position.Y, x.Position.Z)).ToList();
            _orthoHelper = new Ortho2dCamera();
            _orthoHelper.Initial(pts);
        }
 
        //加载事件
        private void glControl_Load(object sender, EventArgs e)
        {
            GL.ClearColor(Color.White); // 背景颜色
            GL.Enable(EnableCap.DepthTest);//深度测试
            GL.Enable(EnableCap.PointSmooth);//启用点平滑
            GL.Hint(HintTarget.PointSmoothHint, HintMode.Nicest);
            GL.Enable(EnableCap.LineSmooth);//启用线平滑
            GL.Hint(HintTarget.LineSmoothHint, HintMode.Nicest);
 
            GL.PointSize(5);
            GL.LineWidth(3);
 
            ResizeGL();
        }
 
        // 尺寸改变
        private void glControl_Resize(object sender, EventArgs e)
        {
            ResizeGL();
        }
 
        // 绘制
        private void glControl_Paint(object sender, PaintEventArgs e)
        {
            RenderGL();
        }
 
        //获取节点
        private void DrawNodes()
        {
            if (_nw == null)
            {
                return;
            }
            GL.PointSize(5f);
            GL.Begin(PrimitiveType.Points);
 
            foreach (NodeL3d node in _nw.Nodes)
            {
                // 根据高度设置不同颜色
                GL.Color3(Color.Red);
                GL.Vertex3(node.Position.X, node.Position.Y, node.Position.Z);
            }
 
            GL.End();
        }
 
        //绘制管段
        private void DrawLinks()
        {
            GL.LineWidth(2.5f);
            GL.Begin(PrimitiveType.Lines);
            GL.Color3(Color.Blue); // 管道颜色
 
            foreach (var link in _nw.Links)
            {
                // 绘制管道起点和终点
                GL.Vertex3(link.StartPosition.X, link.StartPosition.Y, link.StartPosition.Z);
                GL.Vertex3(link.EndPosition.X, link.EndPosition.Y, link.EndPosition.Z);
            }
 
            GL.End();
        }
 
        //绘制坐标
        private void DrawAxes()
        {
 
            GL.Begin(PrimitiveType.Lines);
 
            // X轴(红)
            GL.Color3(Color.Red);
            GL.Vertex3(0, 0, 0);
            GL.Vertex3(1000, 0, 0);
 
            // Y轴(绿)
            GL.Color3(Color.Green);
            GL.Vertex3(0, 0, 0);
            GL.Vertex3(0, 1000, 0);
 
            // Z轴(蓝)
            GL.Color3(Color.Blue);
            GL.Vertex3(0, 0, 0);
            GL.Vertex3(0, 0, 100);
 
            GL.End();
 
        }
 
        // 渲染
        private void RenderGL()
        {
            if (_nw == null)
            {
                return;
            }
            this.glControl1.MakeCurrent();
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
 
            //投影矩阵
            GL.MatrixMode(MatrixMode.Projection);
            var projectionMatrix = _orthoHelper.ProjectionMatrix;
            GL.LoadMatrix(ref projectionMatrix);
 
            //视图矩阵
            GL.MatrixMode(MatrixMode.Modelview);
            var viewMatrix = _orthoHelper.ViewMatrix;
            GL.LoadMatrix(ref viewMatrix);
 
            //模型矩阵
            GL.MatrixMode(MatrixMode.Modelview);
            var modelMatrix = _orthoHelper.ModelMatrix;
            GL.LoadMatrix(ref modelMatrix);
            // 先旋转后平移
            GL.Translate(_orthoHelper.Translation);
            GL.Rotate(_orthoHelper.Rotation.X, Vector3.UnitX);
            GL.Rotate(_orthoHelper.Rotation.Y, Vector3.UnitY);
            GL.Rotate(_orthoHelper.Rotation.Z, Vector3.UnitZ);
 
 
            DrawLinks();
            DrawNodes();
 
 
            DrawAxes();
 
            this.glControl1.SwapBuffers();
        }
 
        // 调整大小
        private void ResizeGL()
        {
            this.glControl1.MakeCurrent();
            _orthoHelper.UpdateViewport(this.glControl1.Width, this.glControl1.Height);
            _orthoHelper.UpdateModelMatrix();
            _orthoHelper.UpdateViewMatrix();
            _orthoHelper.UpdateProjectionMatrix();
            this.glControl1.Invalidate();
        }
 
 
 
        #region 鼠标交互
 
 
        //鼠标滚轮
        private void OnMouseWheel(object sender, MouseEventArgs e)
        {
            _orthoHelper?.HandleMouseWheel(this.glControl1, e);
        }
 
        // 鼠标按下
        private void OnMouseDown(object sender, MouseEventArgs e)
        {
            _orthoHelper?.HandleMouseDown(this.glControl1, e);
        }
 
        //鼠标弹起
        private void OnMouseUp(object sender, MouseEventArgs e)
        {
            _orthoHelper?.HandleMouseUp(this.glControl1, e);
        }
 
        //鼠标移动
        private void OnMouseMove(object sender, MouseEventArgs e)
        {
            _orthoHelper?.HandleMouseMove(this.glControl1, e);
        }
 
        #endregion
 
 
    }
}