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
using OpenTK.GLControl;
using OpenTK.Graphics.OpenGL;
using OpenTK.Mathematics;
using System.Windows.Forms;
using Yw.WinFrmUI.Hydro;
using Timer = System.Windows.Forms.Timer;
 
namespace Yw.WinFrmUI.Hydro
{
    public partial class SimpleDrawer : UserControl
    {
        public SimpleDrawer()
        {
            InitializeComponent();
            this.glControl1.Load += glControl_Load;
            this.glControl1.Paint += glControl_Paint;
            this.glControl1.Resize += glControl_Resize;
            this.glControl1.MouseDown += glControl_MouseDown;
            this.glControl1.MouseMove += glControl_MouseMove;
            this.glControl1.MouseWheel += glControl_MouseWheel;
            this.glControl1.MouseUp += GlControl1_MouseUp;
        }
 
        private Timer _timer = null;
        private NetworkL3d _nw = null;
        private Ortho2dViewController _orthoHelper = null;
 
 
        /// <summary>
        /// 初始化管网
        /// </summary>
        public void InitialNetwork(NetworkL3d nw)
        {
            _nw = nw;
            _orthoHelper = new Ortho2dViewController();
            var nodes = _nw.Nodes.Select(x => x.Position).ToList();
            _orthoHelper.Initial(nodes);
        }
 
        //加载事件
        private void glControl_Load(object sender, EventArgs e)
        {
            GL.ClearColor(Color.Transparent); // 深青色背景
            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.DepthFunc(DepthFunction.Less); // 确保深度比较正确
            //_timer = new Timer();
            //_timer.Interval = 16;
            //_timer.Tick += (s, e) => this.glControl1.Invalidate();
            //_timer.Start();
 
 
            GL.PointSize(5);
            GL.LineWidth(3);
 
 
 
            _orthoHelper.FitToBounds(this.glControl1.Width, this.glControl1.Height);
        }
 
        // 尺寸改变
        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 DrawGrid()
        {
            GL.Color3(Color.Gray);
            GL.Begin(PrimitiveType.Lines);
 
            for (int x = -10; x <= 10; x++)
            {
                GL.Vertex3(x, -10, -0.1f);
                GL.Vertex3(x, 10, -0.1f);
            }
 
            for (int y = -10; y <= 10; y++)
            {
                GL.Vertex3(-10, y, -0.1f);
                GL.Vertex3(10, y, -0.1f);
            }
 
            GL.End();
        }
 
        //更新视口
        private void UpdateViewport()
        {
            if (_nw == null)
            {
                return;
            }
            this.glControl1.MakeCurrent();
            if (this.glControl1.ClientSize.Width == 0)
            {
                this.glControl1.ClientSize = new Size(1, this.glControl1.ClientSize.Height);
            }
            if (this.glControl1.ClientSize.Height == 0)
            {
                this.glControl1.ClientSize = new Size(this.glControl1.ClientSize.Width, 1);
            }
 
            GL.Viewport(0, 0, this.glControl1.Width, this.glControl1.Height);
            _orthoHelper.UpdateProjection(this.glControl1.Width, this.glControl1.Height);
        }
 
        // 渲染
        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);
            var modelMatrix = _orthoHelper.ModelMatrix;
            GL.MultMatrix(ref modelMatrix);
 
            DrawGrid();
            DrawLinks();
            DrawNodes();
 
            this.glControl1.SwapBuffers();
        }
 
        // 调整大小
        private void ResizeGL()
        {
            UpdateViewport();
        }
 
        #region 鼠标交互
 
        //鼠标按下
        private void glControl_MouseDown(object sender, MouseEventArgs e)
        {
            _orthoHelper.HandleMouseDown(e);
        }
 
 
        //鼠标移动
        private void glControl_MouseMove(object sender, MouseEventArgs e)
        {
            _orthoHelper.HandleMouseMove(e, this.glControl1);
        }
 
 
        //滚动缩放
        private void glControl_MouseWheel(object sender, MouseEventArgs e)
        {
            _orthoHelper.HandleMouseWheel(e);
        }
 
        private void GlControl1_MouseUp(object sender, MouseEventArgs e)
        {
            _orthoHelper.HandleMouseUp(e);
        }
 
        #endregion
 
    }
}