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