-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
284 lines (252 loc) · 9 KB
/
Program.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#nullable disable
using System.Numerics;
using ImGuiNET;
using Silk.NET.Input;
using Silk.NET.Maths;
using Silk.NET.OpenGL;
using Silk.NET.Windowing;
internal class Program
{
private static readonly IWindow window = CreateWindow();
private static readonly KeyState keyState = new();
private static readonly MouseState mouseState = new();
private static GL Gl;
private static readonly Camera camera = new(new(1600, 1600), new(0, 0, 5));
private static ImGuiController imGuiController;
private static VertexArrayObject<Vertex, uint> modelVao;
private static VertexArrayObject<Vertex, uint> lightVao;
private static BufferObject<Vertex> boxVbo;
private static ShaderProgram modelShader;
private static ShaderProgram lightShader;
private static Texture diffuseMap;
private static Texture specularMap;
private static bool polygonModeToggle = false;
private static readonly DirectionalLight directionalLight =
new(
new(-0.2f, -1.0f, -0.3f),
new Vector3(1.0f) * 0.05f,
new Vector3(1.0f) * 0.4f,
new Vector3(1.0f) * 0.5f
);
private static readonly PointLight[] pointLights =
{
new(
new(0.7f, 0.2f, 2.0f),
new Vector3(1.0f) * 0.05f,
new Vector3(1.0f) * 0.8f,
new Vector3(1.0f),
1.0f,
0.09f,
0.032f
),
new(
new(2.3f, -3.3f, -4.0f),
new Vector3(1.0f) * 0.05f,
new Vector3(1.0f) * 0.8f,
new Vector3(1.0f),
1.0f,
0.09f,
0.032f
),
new(
new(-4.0f, 2.0f, -12.0f),
new Vector3(1.0f) * 0.05f,
new Vector3(1.0f) * 0.8f,
new Vector3(1.0f),
1.0f,
0.09f,
0.032f
),
new(
new(0.0f, 0.0f, -3.0f),
new Vector3(1.0f) * 0.05f,
new Vector3(1.0f) * 0.8f,
new Vector3(1.0f),
1.0f,
0.09f,
0.032f
)
};
private static readonly Transform[] modelTransforms =
{
new() { Translation = new(0.0f, 0.0f, 0.0f) },
new() { Translation = new(2.0f, 5.0f, -15.0f) },
new() { Translation = new(-1.5f, -2.2f, -2.5f) },
new() { Translation = new(-3.8f, -2.0f, -12.3f) },
new() { Translation = new(2.4f, -0.4f, -3.5f) },
new() { Translation = new(-1.7f, 3.0f, -7.5f) },
new() { Translation = new(1.3f, -2.0f, -2.5f) },
new() { Translation = new(1.5f, 2.0f, -2.5f) },
new() { Translation = new(1.5f, 0.2f, -1.5f) },
new() { Translation = new(-1.3f, 1.0f, -1.5f) }
};
private static IWindow CreateWindow()
{
var options = WindowOptions.Default;
options.Size = new Vector2D<int>(1600, 1600);
options.Title = "Hello OpenGL with Silk.NET";
options.PreferredDepthBufferBits = 24;
options.Samples = 8;
options.VSync = false;
var window = Window.Create(options);
window.Load += OnLoad;
window.Update += OnUpdate;
window.Render += OnRender;
window.Closing += OnClose;
window.Resize += OnResize;
window.FocusChanged += OnFocusChanged;
window.StateChanged += OnStateChanged;
return window;
}
private static void OnStateChanged(WindowState state) => mouseState.ResetMouse();
private static void OnFocusChanged(bool obj) => mouseState.ResetMouse();
private static unsafe void OnLoad()
{
var input = window.CreateInput();
foreach (var keyboard in input.Keyboards)
{
keyboard.KeyDown += (_, key, _) =>
{
keyState.KeyDown(key);
switch (key)
{
case Key.Escape:
window.Close();
break;
case Key.Space:
case Key.Enter:
Gl.PolygonMode(
MaterialFace.FrontAndBack,
polygonModeToggle ? PolygonMode.Fill : PolygonMode.Line
);
polygonModeToggle = !polygonModeToggle;
break;
case Key.GraveAccent:
mouseState.ResetMouse();
foreach (var mouse in input.Mice)
{
mouse.Cursor.CursorMode =
mouse.Cursor.CursorMode == CursorMode.Raw
? CursorMode.Normal
: CursorMode.Raw;
}
break;
}
};
keyboard.KeyUp += (_, key, _) => keyState.KeyUp(key);
}
foreach (var mouse in input.Mice)
{
mouse.MouseMove += (mouse, position) =>
{
if (mouse.Cursor.CursorMode == CursorMode.Raw)
{
mouseState.MouseMove(position);
}
};
mouse.Scroll += (_, delta) =>
mouseState.MouseScroll(delta.Y > 0 ? ScrollDirection.Up : ScrollDirection.Down);
}
Gl = GL.GetApi(window);
diffuseMap = Texture.FromFile(Gl, "container.png");
specularMap = Texture.FromFile(Gl, "container_specular.png");
boxVbo = new BufferObject<Vertex>(Gl, Vertex.Cube, BufferTargetARB.ArrayBuffer);
modelVao = new VertexArrayObject<Vertex, uint>(Gl, boxVbo);
modelVao.VertexAttributePointer(0, 3, VertexAttribPointerType.Float);
modelVao.VertexAttributePointer(
1,
3,
VertexAttribPointerType.Float,
1,
sizeof(VertexPosition)
);
modelVao.VertexAttributePointer(
2,
2,
VertexAttribPointerType.Float,
1,
sizeof(VertexPosition) + sizeof(VertexNormal)
);
lightVao = new VertexArrayObject<Vertex, uint>(Gl, boxVbo);
lightVao.VertexAttributePointer(0, 3, VertexAttribPointerType.Float);
modelShader = ShaderProgram.FromFiles(Gl, "model.vs", "model.fs");
modelShader.Set("directionalLight", directionalLight);
modelShader.Set("pointLights", pointLights);
modelShader.Set(
"material",
new TexturedMaterial(
TextureUnit.Texture0,
TextureUnit.Texture1,
TextureUnit.Texture2,
32f
)
);
lightShader = ShaderProgram.FromFiles(Gl, "light.vs", "light.fs");
Gl.Enable(EnableCap.DepthTest);
imGuiController = new(Gl, window, input);
}
private static void OnUpdate(double deltaTime)
{
camera.Update(deltaTime, keyState, mouseState);
modelShader.Set("view", camera.ViewMatrix);
modelShader.Set("projection", camera.ProjectionMatrix);
modelShader.Set("viewPos", camera.Position);
lightShader.Set("view", camera.ViewMatrix);
lightShader.Set("projection", camera.ProjectionMatrix);
for (int i = 0; i < modelTransforms.Length; i++)
{
float angle = 20.0f * i;
modelTransforms[i].Rotation = Quaternion<float>.CreateFromAxisAngle(
Vector3D.Normalize<float>(new(1.0f, 0.3f, 0.5f)),
angle.ToRadians()
);
}
}
private static unsafe void OnRender(double deltaTime)
{
Gl.ClearColor(0.1f, 0.1f, 0.1f, 1.0f);
Gl.Clear((uint)(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit));
modelVao.Bind();
modelShader.Use();
diffuseMap.Use(TextureUnit.Texture0);
specularMap.Use(TextureUnit.Texture1);
foreach (var transform in modelTransforms)
{
modelShader.Set("model", transform.Matrix);
Gl.DrawArrays(PrimitiveType.Triangles, 0, (uint)Vertex.Cube.Length);
}
lightVao.Bind();
lightShader.Use();
foreach (var light in pointLights)
{
var scale = Matrix4X4.CreateScale(0.2f);
var translate = Matrix4X4.CreateTranslation(light.Position.ToVectorD());
lightShader.Set("model", scale * translate);
lightShader.Set("lightColor", light.Diffuse);
Gl.DrawArrays(PrimitiveType.Triangles, 0, (uint)Vertex.Cube.Length);
}
imGuiController.NewFrame(deltaTime);
ImGui.ShowDemoWindow();
imGuiController.Render();
}
private static void OnClose()
{
modelVao.Dispose();
lightVao.Dispose();
boxVbo.Dispose();
modelShader.Dispose();
lightShader.Dispose();
diffuseMap.Dispose();
specularMap.Dispose();
imGuiController.Dispose();
}
private static void OnResize(Vector2D<int> size)
{
Gl.Viewport(size);
camera.WindowSize = size;
}
private static void Main()
{
window.Run();
}
}