-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathForm1.cs
252 lines (192 loc) · 8.24 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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
using System;
using System.Windows.Forms;
using OpenTK.Graphics.OpenGL4;
using OpenTK.Mathematics;
namespace OpenTK.GLControl.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 static readonly Vector3[] VertexData = new Vector3[]
{
new Vector3(-1.0f, -1.0f, -1.0f),
new Vector3(-1.0f, 1.0f, -1.0f),
new Vector3(1.0f, 1.0f, -1.0f),
new Vector3(1.0f, -1.0f, -1.0f),
new Vector3(-1.0f, -1.0f, -1.0f),
new Vector3(1.0f, -1.0f, -1.0f),
new Vector3(1.0f, -1.0f, 1.0f),
new Vector3(-1.0f, -1.0f, 1.0f),
new Vector3(-1.0f, -1.0f, -1.0f),
new Vector3(-1.0f, -1.0f, 1.0f),
new Vector3(-1.0f, 1.0f, 1.0f),
new Vector3(-1.0f, 1.0f, -1.0f),
new Vector3(-1.0f, -1.0f, 1.0f),
new Vector3(1.0f, -1.0f, 1.0f),
new Vector3(1.0f, 1.0f, 1.0f),
new Vector3(-1.0f, 1.0f, 1.0f),
new Vector3(-1.0f, 1.0f, -1.0f),
new Vector3(-1.0f, 1.0f, 1.0f),
new Vector3(1.0f, 1.0f, 1.0f),
new Vector3(1.0f, 1.0f, -1.0f),
new Vector3(1.0f, -1.0f, -1.0f),
new Vector3(1.0f, 1.0f, -1.0f),
new Vector3(1.0f, 1.0f, 1.0f),
new Vector3(1.0f, -1.0f, 1.0f),
};
private static readonly int[] IndexData = new int[]
{
0, 1, 2, 2, 3, 0,
4, 5, 6, 6, 7, 4,
8, 9, 10, 10, 11, 8,
12, 13, 14, 14, 15, 12,
16, 17, 18, 18, 19, 16,
20, 21, 22, 22, 23, 20,
};
private static readonly Color4[] ColorData = new Color4[]
{
Color4.Silver, Color4.Silver, Color4.Silver, Color4.Silver,
Color4.Honeydew, Color4.Honeydew, Color4.Honeydew, Color4.Honeydew,
Color4.Moccasin, Color4.Moccasin, Color4.Moccasin, Color4.Moccasin,
Color4.IndianRed, Color4.IndianRed, Color4.IndianRed, Color4.IndianRed,
Color4.PaleVioletRed, Color4.PaleVioletRed, Color4.PaleVioletRed, Color4.PaleVioletRed,
Color4.ForestGreen, Color4.ForestGreen, Color4.ForestGreen, Color4.ForestGreen,
};
private const string VertexShaderSource = @"#version 330 core
layout(location = 0) in vec3 aPos;
layout(location = 1) in vec4 aColor;
out vec4 fColor;
uniform mat4 MVP;
void main()
{
gl_Position = vec4(aPos, 1) * MVP;
fColor = aColor;
}
";
private const string FragmentShaderSource = @"#version 330 core
in vec4 fColor;
out vec4 oColor;
void main()
{
oColor = fColor;
}
";
private int CubeShader;
private int VAO;
private int EBO;
private int PositionBuffer;
private int ColorBuffer;
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) =>
{
const float DELTA_TIME = 1 / 50f;
_angle += 180f * DELTA_TIME;
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);
CubeShader = CompileProgram(VertexShaderSource, FragmentShaderSource);
VAO = GL.GenVertexArray();
GL.BindVertexArray(VAO);
EBO = GL.GenBuffer();
GL.BindBuffer(BufferTarget.ElementArrayBuffer, EBO);
GL.BufferData(BufferTarget.ElementArrayBuffer, IndexData.Length * sizeof(int), IndexData, BufferUsageHint.StaticDraw);
PositionBuffer = GL.GenBuffer();
GL.BindBuffer(BufferTarget.ArrayBuffer, PositionBuffer);
GL.BufferData(BufferTarget.ArrayBuffer, VertexData.Length * sizeof(float) * 3, VertexData, BufferUsageHint.StaticDraw);
GL.EnableVertexAttribArray(0);
GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, sizeof(float) * 3, 0);
ColorBuffer = GL.GenBuffer();
GL.BindBuffer(BufferTarget.ArrayBuffer, ColorBuffer);
GL.BufferData(BufferTarget.ArrayBuffer, ColorData.Length * sizeof(float) * 4, ColorData, BufferUsageHint.StaticDraw);
GL.EnableVertexAttribArray(1);
GL.VertexAttribPointer(1, 4, VertexAttribPointerType.Float, false, sizeof(float) * 4, 0);
}
private int CompileProgram(string vertexShader, string fragmentShader)
{
int program = GL.CreateProgram();
int vert = CompileShader(ShaderType.VertexShader, vertexShader);
int frag = CompileShader(ShaderType.FragmentShader, fragmentShader);
GL.AttachShader(program, vert);
GL.AttachShader(program, frag);
GL.LinkProgram(program);
GL.GetProgram(program, GetProgramParameterName.LinkStatus, out int success);
if (success == 0)
{
string log = GL.GetProgramInfoLog(program);
throw new Exception($"Could not link program: {log}");
}
GL.DetachShader(program, vert);
GL.DetachShader(program, frag);
GL.DeleteShader(vert);
GL.DeleteShader(frag);
return program;
static int CompileShader(ShaderType type, string source)
{
int shader = GL.CreateShader(type);
GL.ShaderSource(shader, source);
GL.CompileShader(shader);
GL.GetShader(shader, ShaderParameter.CompileStatus, out int status);
if (status == 0)
{
string log = GL.GetShaderInfoLog(shader);
throw new Exception($"Failed to compile {type}: {log}");
}
return shader;
}
}
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);
projection = Matrix4.CreatePerspectiveFieldOfView(MathHelper.PiOver4, aspect_ratio, 1, 64);
}
private void glControl_Paint(object sender, PaintEventArgs e)
{
Render();
}
Matrix4 projection;
private void Render()
{
glControl.MakeCurrent();
GL.ClearColor(Color4.MidnightBlue);
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
GL.Enable(EnableCap.DepthTest);
Matrix4 lookat = Matrix4.LookAt(0, 5, 5, 0, 0, 0, 0, 1, 0);
Matrix4 model = Matrix4.CreateFromAxisAngle(new Vector3(0.0f, 1.0f, 0.0f), MathHelper.DegreesToRadians(_angle));
Matrix4 mvp = model * lookat * projection;
GL.UseProgram(CubeShader);
GL.UniformMatrix4(GL.GetUniformLocation(CubeShader, "MVP"), true, ref mvp);
GL.DrawElements(BeginMode.Triangles, IndexData.Length, DrawElementsType.UnsignedInt, 0);
glControl.SwapBuffers();
}
}
}