-
Notifications
You must be signed in to change notification settings - Fork 1
/
Main.cs
515 lines (419 loc) · 21.4 KB
/
Main.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
using OpenTK.Windowing.GraphicsLibraryFramework;
using OpenTK.Windowing.Common;
using OpenTK.Windowing.Desktop;
using OpenTK.Graphics.OpenGL4;
using OpenTK.Mathematics;
using System.Text.Json;
using System.IO;
using System;
using ImGuiNET;
using static Engine.UserInterface.GUI;
using static Engine.Importer.Import;
using static Engine.RenderEngine.Rendering;
using static Engine.SettingUP.Setup;
using Engine.MathLib;
using System.Text.Json.Serialization;
using System.Runtime.InteropServices;
using System.Collections.Generic;
using Engine.SettingUP;
using Object = Engine.SettingUP.Setup.Object;
using System.Linq;
namespace Engine
{
class Main : GameWindow
{
// Create a window and assign values
public Main(int width, int height, string title)
: base(GameWindowSettings.Default, new NativeWindowSettings()
{
Title = title,
Size = new Vector2i(width, height),
WindowBorder = WindowBorder.Resizable,
StartVisible = false,
StartFocused = true,
WindowState = WindowState.Normal,
API = ContextAPI.OpenGL,
Profile = ContextProfile.Core,
APIVersion = new Version(3, 3)
})
{
// Center the window
CenterWindow();
WindowHeight = Size.Y;
WindowWidth = Size.X;
CameraHeight = Size.Y;
CameraWidth = Size.X;
}
public static Texture[] PBRmaps = new Texture[5];
public static Texture PointLightTexture;
public static Material M_Default = new Material(new Vector3(1), 0.5f, 0, 1, new int[] { 0, 0, 0, 0, 0 });
public static Material M_Misc = new Material(new Vector3(1), 1, 1, 1, new int[] { 1, 1, 0, 1, 1 });
public static bool wireframeonoff = false;
// Window bools
public static bool showSettings = true;
public static bool CloseWindow = false;
public static bool isMainHovered;
bool fullScreen = false;
bool vsynconoff = true;
// Camera settings
public static float WindowWidth;
public static float WindowHeight;
public static float CameraWidth;
public static float CameraHeight;
Vector3 front = new Vector3(0.0f, 0.0f, -1.0f);
public static Vector3 up = Vector3.UnitY;
public static Vector3 position = new Vector3(0.0f, 4.0f, 4.0f);
float sensitivity = 0.25f;
public static float Pitch = -90f;
public static float Yaw;
int FOV = 75;
int speed = 12;
ImGuiController UIController;
// Runs when the window is resizeds
protected override void OnResize(ResizeEventArgs e)
{
WindowWidth = e.Width;
WindowHeight = e.Height;
GL.DeleteFramebuffer(FBO);
GenFBO(CameraWidth, WindowHeight);
UIController.WindowResized((int)WindowWidth, (int)WindowHeight);
GL.Viewport(0, 0, e.Width, e.Height);
base.OnResize(e);
}
// Runs after Run();
unsafe protected override void OnLoad()
{
Console.WriteLine(AppDomain.CurrentDomain.BaseDirectory);
GL.Enable(EnableCap.DepthTest);
GL.Enable(EnableCap.CullFace);
GL.Enable(EnableCap.PolygonOffsetFill);
GL.CullFace(CullFaceMode.Front);
GL.ClearColor(new Color4(0.5f, 0.5f, 0.5f, 1f));
GL.LineWidth(2f);
GL.PointSize(5f);
LoadIcons();
Icon = LoadedIcon;
UIController = new ImGuiController((int)WindowWidth, (int)WindowHeight);
LoadTheme();
VSync = VSyncMode.On;
IsVisible = true;
LoadDefaultMaps();
PBRmaps[0] = Texture.LoadFromFile(AppDomain.CurrentDomain.BaseDirectory + "Resources/3D_Models/statue/DefaultMaterial_albedo.jpg", TextureUnit.Texture0);
PBRmaps[1] = Texture.LoadFromFile(AppDomain.CurrentDomain.BaseDirectory + "Resources/3D_Models/statue/DefaultMaterial_roughness.jpg", TextureUnit.Texture0);
PBRmaps[2] = Texture.LoadFromFile(AppDomain.CurrentDomain.BaseDirectory + "Resources/3D_Models/statue/DefaultMaterial_roughness.jpg", TextureUnit.Texture0);
PBRmaps[3] = Texture.LoadFromFile(AppDomain.CurrentDomain.BaseDirectory + "Resources/3D_Models/statue/DefaultMaterial_normal.jpg", TextureUnit.Texture0);
PBRmaps[4] = Texture.LoadFromFile(AppDomain.CurrentDomain.BaseDirectory + "Resources/3D_Models/statue/DefaultMaterial_AO.jpg", TextureUnit.Texture0);
PointLightTexture = Texture.LoadFromFile(AppDomain.CurrentDomain.BaseDirectory + "Engine/Engine_Resources/Images/PointLightTexture.png", TextureUnit.Texture0);
// First model, placeholder at 0 in array so array isnt empty
LoadModel(AppDomain.CurrentDomain.BaseDirectory + "Engine/Engine_Resources/Primitives/PointLightMesh.fbx");
AddObjectToArray("Scene Root", M_Default, new Vector3(1), new Vector3(0), new Vector3(0), importedVertexData, importindices);
//////////////////////////////////////////////////////////
LoadModel(AppDomain.CurrentDomain.BaseDirectory + "Resources/3D_Models/statue/model.dae");
AddObjectToArray(importname, M_Misc, new Vector3(3), new Vector3(0, 4, 0), new Vector3(0), importedVertexData, importindices);
LoadModel(AppDomain.CurrentDomain.BaseDirectory + "Engine/Engine_Resources/Primitives/Plane.fbx");
AddObjectToArray("Floor", M_Default, new Vector3(10), importedLocation, new Vector3(-90, 0, 0), importedVertexData, importindices);
LoadModel(AppDomain.CurrentDomain.BaseDirectory + "Engine/Engine_Resources/Primitives/Plane.fbx", true);
AddLightToArray(5, 0, "Point Light", new Vector3(1), new Vector3(4, 5, 3), new Vector3(0f), importedVertPosData, importindices);
ConstructObjects();
ConstructLights();
PBRShader.SetFloat("NoiseAmount", NoiseAmount);
GenFBO(CameraWidth, CameraHeight);
GenScreenRect();
SetUpCubeMap();
GLFW.MaximizeWindow(WindowPtr);
base.OnLoad();
}
Matrix4 projection;
static bool UIon = true;
// Render loop
protected override void OnRenderFrame(FrameEventArgs args)
{
if (UIon == true) GL.BindFramebuffer(FramebufferTarget.Framebuffer, FBO);
else GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
GL.ClearColor(new Color4(BG_Color.X, BG_Color.Y, BG_Color.Z, 1f));
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
GL.Enable(EnableCap.DepthTest);
if (isMainHovered == true | IsMouseButtonDown(MouseButton.Right) | IsKeyDown(Keys.LeftAlt)) GeneralInput(args);
if (IsMouseButtonDown(MouseButton.Right) | IsKeyDown(Keys.LeftAlt)) MouseInput();
if (IsMouseButtonReleased(MouseButton.Right) | IsKeyReleased(Keys.LeftAlt)) CursorState = CursorState.Normal;
if (UIon == true) projection = Matrix4.CreatePerspectiveFieldOfView(MathHelper.DegreesToRadians(FOV), Math.Abs(CameraWidth / CameraHeight), 0.1f, 100.0f);
else projection = Matrix4.CreatePerspectiveFieldOfView(MathHelper.DegreesToRadians(FOV), Math.Abs(WindowWidth / WindowHeight), 0.1f, 100.0f);
Matrix4 view = Matrix4.LookAt(position, position + front, up);
DrawObjects(projection, view, wireframeonoff);
GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Fill);
GL.ActiveTexture(TextureUnit.Texture0);
GL.BindTexture(TextureTarget.TextureCubeMap, cubeMapTexture);
if (showCubeMap == true) DrawCubeMapCube(projection, view, position);
// Draw all lights
if (UIon == true)
{
DrawLights(projection, view);
GL.Disable(EnableCap.DepthTest);
fboShader.Use();
GL.ActiveTexture(TextureUnit.Texture0);
GL.BindTexture(TextureTarget.Texture2D, framebufferTexture);
GL.BindVertexArray(rectVAO);
GL.DrawArrays(PrimitiveType.Triangles, 0, 6);
GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
// UI
UIController.Update(this, (float)args.Time);
ImGui.DockSpaceOverViewport();
WindowOnOffs();
if (showSettings)
{
// Settings
ImGui.Begin("Settings");
ImGui.Dummy(new System.Numerics.Vector2(0f, spacing));
if (ImGui.TreeNode("Rendering"))
{
ImGui.Dummy(new System.Numerics.Vector2(0f, spacing));
if (ImGui.Checkbox("V-Sync", ref vsynconoff))
{
if (vsynconoff == false) VSync = VSyncMode.Off;
if (vsynconoff == true) VSync = VSyncMode.On;
}
ImGui.Checkbox("Wireframe", ref wireframeonoff);
ImGui.TreePop();
}
ImGui.Dummy(new System.Numerics.Vector2(0f, spacing));
ImGui.Separator();
ImGui.Dummy(new System.Numerics.Vector2(0f, spacing));
if (ImGui.TreeNode("Camera"))
{
ImGui.Dummy(new System.Numerics.Vector2(0f, spacing));
ImGui.SliderInt("Field of View", ref FOV, 10, 179);
ImGui.SliderFloat("Sensitivity", ref sensitivity, 0.1f, 2.0f, "%.1f");
ImGui.SliderInt("Speed", ref speed, 1, 30);
ImGui.TreePop();
}
ImGui.Dummy(new System.Numerics.Vector2(0f, spacing));
ImGui.Separator();
ImGui.Dummy(new System.Numerics.Vector2(0f, spacing));
if (ImGui.TreeNode("Post Processing"))
{
ImGui.Dummy(new System.Numerics.Vector2(0f, spacing));
if (ImGui.Checkbox("Chromatic Abberation", ref ChromaticAbberationOnOff))
{
fboShader.SetBool("ChromaticAbberationOnOff", ChromaticAbberationOnOff);
}
if (ChromaticAbberationOnOff == true)
{
if (ImGui.SliderFloat("##Chromatic Abberation Offset", ref ChromaticAbberationOffset, 0, 0.05f, "%.3f"))
{
fboShader.SetFloat("ChromaticAbberationOffset", ChromaticAbberationOffset);
}
}
ImGui.Dummy(new System.Numerics.Vector2(0f, spacing));
ImGui.Separator();
ImGui.Dummy(new System.Numerics.Vector2(0f, spacing));
ImGui.Text("Noise Amount"); ImGui.SameLine(); UserInterface.GUI.HelpMarker("Values around 0.5 reduce banding \nHigh values causes visible noise");
ImGui.Dummy(new System.Numerics.Vector2(0f, spacing));
if (ImGui.SliderFloat("##NA", ref NoiseAmount, 0.01f, 10.0f, "%.2f")) PBRShader.SetFloat("NoiseAmount", NoiseAmount);
ImGui.TreePop();
}
ImGui.Dummy(new System.Numerics.Vector2(0f, spacing));
ImGui.Separator();
ImGui.Dummy(new System.Numerics.Vector2(0f, spacing));
if (ImGui.TreeNode("Lightning"))
{
ImGui.Dummy(new System.Numerics.Vector2(0f, spacing));
ImGui.Text("Sun Direction");
ImGui.SliderFloat3("##SunDirection", ref LightDirection, -1, 1);
ImGui.Dummy(new System.Numerics.Vector2(0f, spacing));
ImGui.Text("Sun Color");
ImGui.ColorEdit3("##SunColor", ref LightColor);
ImGui.Dummy(new System.Numerics.Vector2(0f, spacing));
ImGui.Separator();
ImGui.Dummy(new System.Numerics.Vector2(0f, spacing));
ImGui.Text("Background Color");
if (ImGui.ColorEdit3("Background Color", ref BG_Color, ImGuiColorEditFlags.NoLabel))
{
GL.ClearColor(new Color4(BG_Color.X, BG_Color.Y, BG_Color.Z, 1f));
}
ImGui.Text("Show Cubemap");
ImGui.Dummy(new System.Numerics.Vector2(0f, spacing));
ImGui.Checkbox("##Show Cubemap", ref showCubeMap);
ImGui.TreePop();
}
ImGui.Dummy(new System.Numerics.Vector2(0f, spacing));
ImGui.Separator();
ImGui.Dummy(new System.Numerics.Vector2(0f, spacing));
if (ImGui.TreeNode("Editor"))
{
ImGui.Dummy(new System.Numerics.Vector2(0f, spacing));
if (ImGui.SliderFloat("Font Size", ref fontSize, 0.1f, 2.0f, "%.2f"))
{
ImGui.GetIO().FontGlobalScale = fontSize;
ImGui.TreePop();
}
ImGui.SliderFloat("Spacing", ref spacing, 1f, 10f, "%.1f");
}
ImGui.Dummy(new System.Numerics.Vector2(0f, spacing));
ImGui.Separator();
ImGui.Dummy(new System.Numerics.Vector2(0f, spacing));
ImGui.End();
}
LoadGameWindow(ref CameraWidth, ref CameraHeight);
UIController.Render();
ImGuiController.CheckGLError("End of frame");
}
// Close editor
if (IsKeyDown(Keys.Escape) | CloseWindow == true) Close();
Context.SwapBuffers();
base.OnRenderFrame(args);
}
public static System.Numerics.Vector3 Vec3toSN(Vector3 v)
{
return new System.Numerics.Vector3(v.X, v.Y, v.Z);
}
public static System.Numerics.Vector2 Vec2toSN(Vector2 v)
{
return new System.Numerics.Vector2(v.X, v.Y);
}
public static Vector3 SNtoVec3(System.Numerics.Vector3 v)
{
return new Vector3(v.X, v.Y, v.Z);
}
public static Vector2 SNtoVec2(System.Numerics.Vector2 v)
{
return new Vector2(v.X, v.Y);
}
public static ConvertedVertexData[] ConvertVertexData(VertexData[] v)
{
ConvertedVertexData[] convertArray = new ConvertedVertexData[v.Length];
for (int i = 0; i < convertArray.Length; i++)
{
convertArray[i] = new ConvertedVertexData(Vec3toSN(v[i].Position), Vec2toSN(v[i].texCoord), Vec3toSN(v[i].Normals), Vec3toSN(v[i].Tangents), Vec3toSN(v[i].BiTangents));
}
return convertArray;
}
public static VertexData[] RevertVertexData(ConvertedVertexData[] v)
{
VertexData[] convertArray = new VertexData[v.Length];
for (int i = 0; i < convertArray.Length; i++)
{
convertArray[i] = new VertexData(SNtoVec3(v[i].Position), SNtoVec2(v[i].texCoord), SNtoVec3(v[i].Normals), SNtoVec3(v[i].Tangents), SNtoVec3(v[i].BiTangents));
}
return convertArray;
}
public static void SaveFile(string filename)
{
ConvertedObject[] convertedObject = new ConvertedObject[Objects.Count];
for (int i = 0; i < convertedObject.Length; i++)
{
convertedObject[i] = new ConvertedObject(
Objects[i].Name,
Objects[i].ID,
new ConvertedMaterial(Vec3toSN(
Objects[i].Material.albedo),
Objects[i].Material.roughness,
Objects[i].Material.metallic,
Objects[i].Material.ao,
Objects[i].Material.Maps),
ConvertVertexData(Objects[i].VertData), Objects[i].Indices,
Vec3toSN(Objects[i].Location),
Vec3toSN(Objects[i].Rotation),
Vec3toSN(Objects[i].Scale));
}
var _options = new JsonSerializerOptions()
{
WriteIndented = true,
IncludeFields = true,
//DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
};
string jsonString = JsonSerializer.Serialize(convertedObject, _options);
File.WriteAllText(filename, jsonString);
Console.WriteLine("Saved file to " + filename);
}
public static void LoadSave(string filename)
{
string loadedjson = File.ReadAllText(filename);
ConvertedObject[] loadedSave = JsonSerializer.Deserialize<ConvertedObject[]>(loadedjson, new JsonSerializerOptions() { IncludeFields = true });
Object[] revertedObject = new Object[loadedSave.Length];
for (int i = 0; i < revertedObject.Length; i++)
{
revertedObject[i] = new Object(
loadedSave[i].Name,
loadedSave[i].ID,
new Material(SNtoVec3(
loadedSave[i].Material.albedo),
loadedSave[i].Material.roughness,
loadedSave[i].Material.metallic,
loadedSave[i].Material.ao,
loadedSave[i].Material.Maps),
RevertVertexData(loadedSave[i].VertData), loadedSave[i].Indices,
SNtoVec3(loadedSave[i].Location),
SNtoVec3(loadedSave[i].Rotation),
SNtoVec3(loadedSave[i].Scale));
}
List<Object> newObjectsArray = revertedObject.ToList();
while (VAO.Count < newObjectsArray.Count)
{
VAO.Add(0);
}
Objects = newObjectsArray;
Console.WriteLine(VAO.Count);
Console.WriteLine(Objects.Count);
ConstructObjects();
Console.WriteLine("Loaded save from " + filename);
}
private void MouseInput()
{
CursorState = CursorState.Grabbed;
if (Pitch > 360) Pitch = 0;
if (Pitch < -360) Pitch = 0;
Pitch += MouseState.Delta.X * sensitivity;
Yaw -= MouseState.Delta.Y * sensitivity;
front.X = (float)Math.Cos(MathHelper.DegreesToRadians(Math.Clamp(Yaw, -89, 89))) * (float)Math.Cos(MathHelper.DegreesToRadians(Pitch));
front.Y = (float)Math.Sin(MathHelper.DegreesToRadians(Math.Clamp(Yaw, -89, 89)));
front.Z = (float)Math.Cos(MathHelper.DegreesToRadians(Math.Clamp(Yaw, -89, 89))) * (float)Math.Sin(MathHelper.DegreesToRadians(Pitch));
front = Vector3.Normalize(front);
}
// Keyboard input
private void GeneralInput(FrameEventArgs args)
{
// Delete
if (IsKeyPressed(Keys.Delete))
{
if (selectedObject > 0)
{
Objects.RemoveAt(selectedObject);
selectedObject -= 1;
}
}
if (IsKeyPressed(Keys.K)) UIon = Functions.ToggleBool(UIon);
if (IsKeyDown(Keys.F)) position = Objects[selectedObject].Location;
// X and Z movement
if (IsKeyDown(Keys.W)) position += front * speed * (float)args.Time;
if (IsKeyDown(Keys.S)) position -= front * speed * (float)args.Time;
if (IsKeyDown(Keys.A)) position -= Vector3.Normalize(Vector3.Cross(front, up)) * speed * (float)args.Time;
if (IsKeyDown(Keys.D)) position += Vector3.Normalize(Vector3.Cross(front, up)) * speed * (float)args.Time;
// Y movement
if (IsKeyDown(Keys.Q)) position -= up * speed * (float)args.Time;
if (IsKeyDown(Keys.E)) position += up * speed * (float)args.Time;
}
protected override void OnKeyDown(KeyboardKeyEventArgs e)
{
if (isMainHovered)
{
if (IsKeyPressed(Keys.D1)) showStatistics = Functions.ToggleBool(showStatistics);
if (IsKeyPressed(Keys.D2)) showObjectProperties = Functions.ToggleBool(showObjectProperties);
if (IsKeyPressed(Keys.D3)) showLightProperties = Functions.ToggleBool(showLightProperties);
if (IsKeyPressed(Keys.D4)) showOutliner = Functions.ToggleBool(showOutliner);
if (IsKeyPressed(Keys.D5)) showSettings = Functions.ToggleBool(showSettings);
}
if (IsKeyPressed(Keys.F11)) fullScreen = Functions.ToggleBool(fullScreen);
base.OnKeyDown(e);
}
protected override void OnMouseWheel(MouseWheelEventArgs e)
{
base.OnMouseWheel(e);
UIController.MouseScroll(e.Offset);
}
protected override void OnTextInput(TextInputEventArgs e)
{
base.OnTextInput(e);
UIController.PressChar((char)e.Unicode);
}
}
}