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
|
|
}
|
}
|