lixiaojun
2025-03-25 f8173dd270a14277b70504818ab7d7e7a2453f38
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
using OpenTK.Graphics.OpenGL;
using OpenTK.Mathematics;
 
namespace Yw.WinFrmUI.Test.Core
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
            this.glControl1.Load += GlControl1_Load;
            this.glControl1.Paint += GlControl1_Paint;
            this.glControl1.Resize += GlControl1_Resize;
        }
 
        private Matrix4 _projectionMatrix;
        private Matrix4 _viewMatrix;
        private Matrix4 _modelMatrix;
        private float _translationX, _translationY;
        private float _rotationX, _rotationY;
 
        private void GlControl1_Load(object sender, EventArgs e)
        {
            InitializeGLState();
        }
 
        private void GlControl1_Resize(object sender, EventArgs e)
        {
            UpdateViewPort();
            UpdateModelMatrix();
            UpdateViewMatrix();
            UpdateProjectionMatrix();
        }
 
        private void GlControl1_Paint(object sender, PaintEventArgs e)
        {
            this.glControl1.MakeCurrent();
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit | ClearBufferMask.StencilBufferBit);
 
            // 旋转立方体
            _rotationX += 0.001f;
            //_rotationY += 0.01f;
 
            //投影矩阵
            GL.MatrixMode(MatrixMode.Projection);
            GL.LoadMatrix(ref _projectionMatrix);
 
            //视图矩阵
            GL.MatrixMode(MatrixMode.Modelview);
            var viewMatrix = _viewMatrix;
            GL.LoadMatrix(ref viewMatrix);
            GL.MultMatrix(ref _modelMatrix);
 
 
            //GL.Rotate(_rotationY, Vector3.UnitY); // 绕Y轴旋转45度
            //GL.Rotate(_rotationX, Vector3.UnitX); // 绕X轴旋转30度
 
            // 绘制立方体
            DrawCube(100);
            Draw3DAxes(100);
 
            this.glControl1.SwapBuffers();
            UpdateViewPort();
            UpdateModelMatrix();
            UpdateViewMatrix();
            UpdateProjectionMatrix();
 
            this.glControl1.Invalidate();
        }
 
 
 
 
        //初始化GL状态
        private void InitializeGLState()
        {
            GL.ClearColor(Color.Transparent);
            GL.Enable(EnableCap.DepthTest);
        }
 
        //设置ViewPort
        private void UpdateViewPort()
        {
            GL.Viewport(0, 0, Width, Height);
        }
 
        //更新投影矩阵
        private void UpdateProjectionMatrix()
        {
            var width = this.glControl1.Width;
            var height = this.glControl1.Height;
            _projectionMatrix = Matrix4.CreateOrthographic(width, height, 0.1f, 1000f);
        }
 
        //更新视图矩阵
        private void UpdateViewMatrix()
        {
            _viewMatrix = Matrix4.LookAt(
                new Vector3(0, 0, 150), // 摄像机位置
                Vector3.Zero,           // 观察目标
                Vector3.UnitY           // 上方向
            );
        }
 
        //更新模型矩阵
        private void UpdateModelMatrix()
        {
            _modelMatrix = Matrix4.CreateScale(2f) * Matrix4.CreateRotationX(_rotationX)
                * Matrix4.CreateRotationY(_rotationY);
 
        }
 
 
 
 
        void Draw3DAxes(float length)
        {
            GL.LineWidth(3);
            GL.Begin(PrimitiveType.Lines);
 
            // X轴 (红)
            GL.Color3(1, 0, 0);
            GL.Vertex3(0, 0, 0); GL.Vertex3(length, 0, 0);
 
            // Y轴 (绿)
            GL.Color3(0, 1, 0);
            GL.Vertex3(0, 0, 0); GL.Vertex3(0, length, 0);
 
            // Z轴 (蓝)
            GL.Color3(0, 0, 1);
            GL.Vertex3(0, 0, 0); GL.Vertex3(0, 0, length);
 
            GL.End();
        }
 
        void DrawCube(float size)
        {
            float half = size / 2;
 
            GL.Begin(PrimitiveType.Quads);
 
            // 前面 (Z+ 方向,红色)
            GL.Color3(1.0, 0.0, 0.0);
            GL.Vertex3(-half, -half, half);
            GL.Vertex3(half, -half, half);
            GL.Vertex3(half, half, half);
            GL.Vertex3(-half, half, half);
 
            // 后面 (Z- 方向,绿色)
            GL.Color3(0.0, 1.0, 0.0);
            GL.Vertex3(-half, -half, -half);
            GL.Vertex3(-half, half, -half);
            GL.Vertex3(half, half, -half);
            GL.Vertex3(half, -half, -half);
 
            // 左面 (X- 方向,蓝色)
            GL.Color3(0.0, 0.0, 1.0);
            GL.Vertex3(-half, -half, -half);
            GL.Vertex3(-half, -half, half);
            GL.Vertex3(-half, half, half);
            GL.Vertex3(-half, half, -half);
 
            // 右面 (X+ 方向,黄色)
            GL.Color3(1.0, 1.0, 0.0);
            GL.Vertex3(half, -half, half);
            GL.Vertex3(half, -half, -half);
            GL.Vertex3(half, half, -half);
            GL.Vertex3(half, half, half);
 
            // 顶面 (Y+ 方向,品红)
            GL.Color3(1.0, 0.0, 1.0);
            GL.Vertex3(-half, half, half);
            GL.Vertex3(half, half, half);
            GL.Vertex3(half, half, -half);
            GL.Vertex3(-half, half, -half);
 
            // 底面 (Y- 方向,青色)
            GL.Color3(0.0, 1.0, 1.0);
            GL.Vertex3(-half, -half, -half);
            GL.Vertex3(half, -half, -half);
            GL.Vertex3(half, -half, half);
            GL.Vertex3(-half, -half, half);
 
            GL.End();
        }
 
 
    }
}