forked from opentk/GLControl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForm1.cs
130 lines (104 loc) · 4.28 KB
/
Form1.cs
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
using System;
using System.Windows.Forms;
using OpenTK.Graphics.OpenGL;
using OpenTK.Mathematics;
namespace OpenTK.WinForms.TestForm
{
public partial class Form1 : Form
{
private Timer _timer = null!;
private float _angle = 0.0f;
public Form1()
{
InitializeComponent();
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Close();
}
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show(this,
"This demonstrates a simple use of the new OpenTK 4.x GLControl.",
"GLControl Test Form",
MessageBoxButtons.OK);
}
private void glControl_Load(object? sender, EventArgs e)
{
// Make sure that when the GLControl is resized or needs to be painted,
// we update our projection matrix or re-render its contents, respectively.
glControl.Resize += glControl_Resize;
glControl.Paint += glControl_Paint;
// Redraw the screen every 1/20 of a second.
_timer = new Timer();
_timer.Tick += (sender, e) =>
{
_angle += 0.5f;
Render();
};
_timer.Interval = 50; // 1000 ms per sec / 50 ms per frame = 20 FPS
_timer.Start();
// Ensure that the viewport and projection matrix are set correctly initially.
glControl_Resize(glControl, EventArgs.Empty);
}
private void glControl_Resize(object? sender, EventArgs e)
{
glControl.MakeCurrent();
if (glControl.ClientSize.Height == 0)
glControl.ClientSize = new System.Drawing.Size(glControl.ClientSize.Width, 1);
GL.Viewport(0, 0, glControl.ClientSize.Width, glControl.ClientSize.Height);
float aspect_ratio = Math.Max(glControl.ClientSize.Width, 1) / (float)Math.Max(glControl.ClientSize.Height, 1);
Matrix4 perpective = Matrix4.CreatePerspectiveFieldOfView(MathHelper.PiOver4, aspect_ratio, 1, 64);
GL.MatrixMode(MatrixMode.Projection);
GL.LoadMatrix(ref perpective);
}
private void glControl_Paint(object sender, PaintEventArgs e)
{
Render();
}
private void Render()
{
glControl.MakeCurrent();
GL.ClearColor(Color4.MidnightBlue);
GL.Enable(EnableCap.DepthTest);
Matrix4 lookat = Matrix4.LookAt(0, 5, 5, 0, 0, 0, 0, 1, 0);
GL.MatrixMode(MatrixMode.Modelview);
GL.LoadMatrix(ref lookat);
GL.Rotate(_angle, 0.0f, 1.0f, 0.0f);
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
GL.Begin(BeginMode.Quads);
GL.Color4(Color4.Silver);
GL.Vertex3(-1.0f, -1.0f, -1.0f);
GL.Vertex3(-1.0f, 1.0f, -1.0f);
GL.Vertex3(1.0f, 1.0f, -1.0f);
GL.Vertex3(1.0f, -1.0f, -1.0f);
GL.Color4(Color4.Honeydew);
GL.Vertex3(-1.0f, -1.0f, -1.0f);
GL.Vertex3(1.0f, -1.0f, -1.0f);
GL.Vertex3(1.0f, -1.0f, 1.0f);
GL.Vertex3(-1.0f, -1.0f, 1.0f);
GL.Color4(Color4.Moccasin);
GL.Vertex3(-1.0f, -1.0f, -1.0f);
GL.Vertex3(-1.0f, -1.0f, 1.0f);
GL.Vertex3(-1.0f, 1.0f, 1.0f);
GL.Vertex3(-1.0f, 1.0f, -1.0f);
GL.Color4(Color4.IndianRed);
GL.Vertex3(-1.0f, -1.0f, 1.0f);
GL.Vertex3(1.0f, -1.0f, 1.0f);
GL.Vertex3(1.0f, 1.0f, 1.0f);
GL.Vertex3(-1.0f, 1.0f, 1.0f);
GL.Color4(Color4.PaleVioletRed);
GL.Vertex3(-1.0f, 1.0f, -1.0f);
GL.Vertex3(-1.0f, 1.0f, 1.0f);
GL.Vertex3(1.0f, 1.0f, 1.0f);
GL.Vertex3(1.0f, 1.0f, -1.0f);
GL.Color4(Color4.ForestGreen);
GL.Vertex3(1.0f, -1.0f, -1.0f);
GL.Vertex3(1.0f, 1.0f, -1.0f);
GL.Vertex3(1.0f, 1.0f, 1.0f);
GL.Vertex3(1.0f, -1.0f, 1.0f);
GL.End();
glControl.SwapBuffers();
}
}
}