duheng
2024-09-23 d9b8c4b8639e5239cea8fbb88fa2ae6fa1ac706b
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
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;//管网
 
        /// <summary>
        /// 是否初始化
        /// </summary>
        public bool Initialized => _network != null;
 
        private float rotation_X = 0.0f;
        private float rotation_Y = 0.0f;
        private float rotation_Z = 0.0f;
 
        /// <summary>
        /// 初始化
        /// </summary>
        public virtual void Initial(Network network)
        {
            _network = network;
        }
 
        private void openGLControl1_OpenGLDraw(object sender, RenderEventArgs args)
        {
            SharpGL.OpenGL gl = this.openGLControl1.OpenGL;
            //清除深度缓存 
            gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT);
 
            //重置当前指定的矩阵为单位矩阵,将当前的用户坐标系的原点移到了屏幕中心
            gl.LoadIdentity();
 
            //坐标轴变换位置到(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)
        {
            OpenGL gl = openGLControl1.OpenGL;
 
            // 设置当前矩阵模式,对投影矩阵应用随后的矩阵操作
            gl.MatrixMode(OpenGL.GL_PROJECTION);
 
            // 重置当前指定的矩阵为单位矩阵,将当前的用户坐标系的原点移到了屏幕中心
            gl.LoadIdentity();
 
            // 创建透视投影变换
            gl.Perspective(30.0f, (double)Width / (double)Height, 5, 100.0);
 
            // 视点变换
            gl.LookAt(0, 5, 0, 0, 0, 0, 0, 1, 0);
 
            // 设置当前矩阵为模型视图矩阵
            gl.MatrixMode(OpenGL.GL_MODELVIEW);
        }
    }
}