lixiaojun
2025-03-10 939bbd43d708ccec94c3ee97e6a03e58bfae9bbe
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
using System;
using System.Windows.Forms;
using OpenTK;
using OpenTK.GLControl;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;
using OpenTK.Mathematics;
 
namespace Yw.WinFrmUI.Test.Core
{
    public partial class Form1 : Form
    {
        private GLControl glControl;
 
        public Form1()
        {
            InitializeComponent();
 
            // ´´½¨ GLControl
            glControl = new GLControl();
            glControl.Dock = DockStyle.Fill;
            glControl.Load += GLControl_Load;
            glControl.Paint += GLControl_Paint;
            glControl.Resize += GLControl_Resize;
 
            // ½« GLControl Ìí¼Óµ½´°Ìå
            this.Controls.Add(glControl);
        }
 
        private void GLControl_Load(object sender, EventArgs e)
        {
            // ³õʼ»¯ OpenGL ÉèÖÃ
            GL.ClearColor(Color4.CornflowerBlue);
            GL.Enable(EnableCap.DepthTest);
        }
 
        private void GLControl_Paint(object sender, PaintEventArgs e)
        {
            // äÖȾ³¡¾°
            Render();
        }
 
        private void GLControl_Resize(object sender, EventArgs e)
        {
            // µ÷ÕûÊÓ¿Ú
            glControl.MakeCurrent();
            GL.Viewport(0, 0, glControl.Width, glControl.Height);
 
            // ÉèÖÃͶӰ¾ØÕó
            Matrix4 projection = Matrix4.CreatePerspectiveFieldOfView(
                MathHelper.PiOver4, glControl.Width / (float)glControl.Height, 0.1f, 100f);
            GL.MatrixMode(MatrixMode.Projection);
            GL.LoadMatrix(ref projection);
        }
 
        private void Render()
        {
            // Çå³ýÑÕÉ«ºÍÉî¶È»º³åÇø
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
 
            // ÉèÖÃÄ£ÐÍÊÓͼ¾ØÕó
            Matrix4 modelView = Matrix4.LookAt(Vector3.UnitZ * 5, Vector3.Zero, Vector3.UnitY);
            GL.MatrixMode(MatrixMode.Modelview);
            GL.LoadMatrix(ref modelView);
 
            // »æÖƹÜÍøÄ£ÐÍ
            DrawPipeline();
 
            // ½»»»»º³åÇø
            glControl.SwapBuffers();
        }
 
        private void DrawPipeline()
        {
            // ÕâÀï»æÖÆÄãµÄÈýά¹ÜÍøÄ£ÐÍ
            GL.Begin(PrimitiveType.Lines);
 
            // Ê¾Àý£º»æÖÆÒ»¸ö¼òµ¥µÄÁ¢·½Ìå
            GL.Color3(139, 0, 0);
            GL.Vertex3(-1, -1, -1);
            GL.Vertex3(1, -1, -1);
 
            GL.Vertex3(1, -1, -1);
            GL.Vertex3(1, 1, -1);
 
            GL.Vertex3(1, 1, -1);
            GL.Vertex3(-1, 1, -1);
 
            GL.Vertex3(-1, 1, -1);
            GL.Vertex3(-1, -1, -1);
 
            GL.Vertex3(-1, -1, 1);
            GL.Vertex3(1, -1, 1);
 
            GL.Vertex3(1, -1, 1);
            GL.Vertex3(1, 1, 1);
 
            GL.Vertex3(1, 1, 1);
            GL.Vertex3(-1, 1, 1);
 
            GL.Vertex3(-1, 1, 1);
            GL.Vertex3(-1, -1, 1);
 
            GL.Vertex3(-1, -1, -1);
            GL.Vertex3(-1, -1, 1);
 
            GL.Vertex3(1, -1, -1);
            GL.Vertex3(1, -1, 1);
 
            GL.Vertex3(1, 1, -1);
            GL.Vertex3(1, 1, 1);
 
            GL.Vertex3(-1, 1, -1);
            GL.Vertex3(-1, 1, 1);
 
            GL.End();
        }
    }
}