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;//中心
///
/// 是否初始化
///
public bool Initialized => _network != null;
///
/// 初始化
///
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);
}
}
}