NoJobEngine 2.0.7
Loading...
Searching...
No Matches
main.cpp
Go to the documentation of this file.
16#include "Engine/Scene/Scene.h"
19
22
23
24
25#include "Editor/EditorCamera.h"
26#include "Editor/EditorLayer.h"
29
30#include <GLFW/glfw3.h>
31
32#include <cmath>
33#include <cstdint>
34#include <exception>
35#include <iostream>
36#include <iterator>
37#include <memory>
38#include <string>
39#include <vector>
40
41
42int main()
43{
44 try
45 {
46 // Created first so it is destroyed last during exception unwinding.
47 // The worker must be joined before its static std::thread is destroyed.
48 struct ScriptBuildWorkerExitGuard final
49 {
50 ~ScriptBuildWorkerExitGuard() noexcept
51 {
53 }
54 } scriptBuildWorkerExitGuard;
55
56 NoJob::Window window({ "NoJobEngine", 1600, 900 });
58
59 std::cerr << "[Startup] Creating PBR scene shader...\n";
60
61 auto shader = NoJob::Shader::Create(
64
65 std::cerr << "[Startup] PBR scene shader OK.\n";
66
67 auto cubeMesh = NoJob::Mesh::CreateCube();
68
69 auto cubeMaterial =
70 std::make_shared<NoJob::Material>(
71 shader,
72 glm::vec4(
73 0.95f,
74 0.35f,
75 0.15f,
76 1.0f));
77
78 auto checkerTexture =
80
81 cubeMaterial->SetTexture(checkerTexture);
82
83 NoJob::Scene editorScene;
84
85 std::unique_ptr<NoJob::Scene>
86 runtimeScene;
87
88 NoJob::Scene* activeScene =
89 &editorScene;
90
91 bool isPlaying = false;
92 bool isPaused = false;
93
94 std::filesystem::path currentScenePath =
96 / "Assets/Scenes/CurrentScene.nojobscene";
97
98
99 // ------------------------------------------------------------
100 // Default cube
101 // ------------------------------------------------------------
102
103 NoJob::Entity cube =
104 editorScene.CreateEntity("Cube");
105
107 cubeMesh);
108
109 cube.AddComponent<
111 cubeMaterial);
112
113 cube.GetComponent<
114 NoJob::TransformComponent>().Position =
115 { 0.0f, 2.0f, 0.0f };
116
117 cube.AddComponent<
119
120 cube.AddComponent<
122
123
124 // ------------------------------------------------------------
125 // Ground
126 // ------------------------------------------------------------
127
128 auto groundMaterial =
129 std::make_shared<NoJob::Material>(
130 shader,
131 glm::vec4(
132 0.28f,
133 0.32f,
134 0.38f,
135 1.0f));
136
137 groundMaterial->SetTexture(
138 checkerTexture);
139
140 NoJob::Entity ground =
141 editorScene.CreateEntity("Ground");
142
143 ground.AddComponent<
145 cubeMesh);
146
147 ground.AddComponent<
149 groundMaterial);
150
151 auto& groundTransform =
152 ground.GetComponent<
154
155 groundTransform.Position =
156 { 0.0f, -1.5f, 0.0f };
157
158 groundTransform.Scale =
159 { 6.0f, 0.5f, 6.0f };
160
161 NoJob::RigidbodyComponent groundBody;
162
163 groundBody.Type =
165
166 groundBody.UseGravity = false;
167
168 ground.AddComponent<
170 groundBody);
171
172 ground.AddComponent<
174
175
176 // ------------------------------------------------------------
177 // Main camera
178 // ------------------------------------------------------------
179
180 NoJob::Entity mainCamera =
181 editorScene.CreateEntity(
182 "Main Camera");
183
184 auto& cameraTransform =
185 mainCamera.GetComponent<
187
188 cameraTransform.Position =
189 { 0.0f, 2.5f, 7.0f };
190
191 cameraTransform.Rotation =
192 {
193 glm::radians(-10.0f),
194 0.0f,
195 0.0f
196 };
197
198 mainCamera.AddComponent<
200
201
202 // ------------------------------------------------------------
203 // Lights
204 // ------------------------------------------------------------
205
206 NoJob::Entity sun =
207 editorScene.CreateEntity("Sun");
208
209 auto& sunTransform =
210 sun.GetComponent<
212
213 sunTransform.Rotation =
214 {
215 glm::radians(-50.0f),
216 glm::radians(-30.0f),
217 0.0f
218 };
219
221 sunLight;
222
223 sunLight.Intensity = 1.1f;
224
225 sun.AddComponent<
227 sunLight);
228
229 NoJob::Entity fillLight =
230 editorScene.CreateEntity(
231 "Point Light");
232
233 fillLight.GetComponent<
234 NoJob::TransformComponent>().Position =
235 { 2.5f, 2.5f, 2.0f };
236
238 pointLight;
239
240 pointLight.Intensity = 0.75f;
241 pointLight.Range = 8.0f;
242
243 fillLight.AddComponent<
245 pointLight);
246
247
248 // ------------------------------------------------------------
249 // Physics
250 // ------------------------------------------------------------
251
252 NoJob::PhysicsSystem physics;
253
254 // Adapter between AI perception and Jolt Physics.
255 // PhysicsSystem owns the physical world.
256 NoJob::JoltSpatialQuery spatialQuery(physics);
257
258 // ------------------------------------------------------------
259 // Main framebuffer
260 // ------------------------------------------------------------
261
263 framebufferSpecification;
264
265 framebufferSpecification.Width = 1280;
266 framebufferSpecification.Height = 720;
267
268 framebufferSpecification.HDR = true;
269 framebufferSpecification.Exposure = 0.72f;
270
271 framebufferSpecification.Bloom = true;
272 framebufferSpecification.BloomThreshold =
273 1.35f;
274
275 framebufferSpecification.BloomStrength =
276 0.08f;
277
278 framebufferSpecification.ScreenSpaceAO =
279 true;
280
281 framebufferSpecification.AOIntensity =
282 0.18f;
283
284 framebufferSpecification.FXAA = true;
285
286 auto framebuffer =
288 framebufferSpecification);
289
290
291 // ------------------------------------------------------------
292 // Camera preview framebuffer
293 // ------------------------------------------------------------
294
296 cameraPreviewSpecification;
297
298 cameraPreviewSpecification.Width = 320;
299 cameraPreviewSpecification.Height = 180;
300
301 cameraPreviewSpecification.HDR = true;
302
303 cameraPreviewSpecification.Exposure =
304 framebufferSpecification.Exposure;
305
306 cameraPreviewSpecification.Bloom =
307 framebufferSpecification.Bloom;
308
309 cameraPreviewSpecification.BloomThreshold =
310 framebufferSpecification.BloomThreshold;
311
312 cameraPreviewSpecification.BloomStrength =
313 framebufferSpecification.BloomStrength;
314
315 cameraPreviewSpecification.ScreenSpaceAO =
316 framebufferSpecification.ScreenSpaceAO;
317
318 cameraPreviewSpecification.AOIntensity =
319 framebufferSpecification.AOIntensity;
320
321 cameraPreviewSpecification.FXAA =
322 framebufferSpecification.FXAA;
323
324 auto cameraPreviewFramebuffer =
326 cameraPreviewSpecification);
327
328
329 // ------------------------------------------------------------
330 // Editor
331 // ------------------------------------------------------------
332
333 NoJob::EditorLayer editor;
334
335 editor.Init(
336 window.GetNativeWindow(),
337 &editorScene);
338
339 editor.SetGraphicsSettings(
340 framebufferSpecification);
341
342 editor.SetSelectedEntity(cube);
343
345 cubeMesh,
346 cubeMaterial);
347
348 editor.SetViewportTexture(
349 framebuffer->
350 GetColorAttachmentRendererID());
351
353 cameraPreviewFramebuffer->
354 GetColorAttachmentRendererID());
355
356
357 NoJob::EditorCamera editorCamera;
358
359
360 // ------------------------------------------------------------
361 // Navigation
362 // ------------------------------------------------------------
363
365 navigationSystem;
366
368 navMeshDebugRenderer;
369 // ------------------------------------------------------------
370
371
372 // ------------------------------------------------------------
373 // Editor-only grid
374 // ------------------------------------------------------------
375
376 std::vector<float>
377 gridVertices;
378
379 std::vector<std::uint32_t>
380 gridIndices;
381
382 constexpr int gridHalfSize = 10;
383 constexpr float lineHalfWidth = 0.012f;
384
385 std::uint32_t baseIndex = 0;
386
387 auto addGridLine =
388 [&](float x0,
389 float z0,
390 float x1,
391 float z1)
392 {
393 const float dx = x1 - x0;
394 const float dz = z1 - z0;
395
396 const float length =
397 std::sqrt(
398 dx * dx +
399 dz * dz);
400
401 const float px =
402 -dz / length *
403 lineHalfWidth;
404
405 const float pz =
406 dx / length *
407 lineHalfWidth;
408
409 const float y = -1.0f;
410
411 const float quad[] =
412 {
413 x0 + px, y, z0 + pz,
414 x0 - px, y, z0 - pz,
415 x1 - px, y, z1 - pz,
416 x1 + px, y, z1 + pz
417 };
418
419 gridVertices.insert(
420 gridVertices.end(),
421 std::begin(quad),
422 std::end(quad));
423
424 const std::uint32_t local[] =
425 {
426 baseIndex + 0,
427 baseIndex + 1,
428 baseIndex + 2,
429
430 baseIndex + 2,
431 baseIndex + 3,
432 baseIndex + 0
433 };
434
435 gridIndices.insert(
436 gridIndices.end(),
437 std::begin(local),
438 std::end(local));
439
440 baseIndex += 4;
441 };
442
443
444 for (int i = -gridHalfSize;
445 i <= gridHalfSize;
446 ++i)
447 {
448 addGridLine(
449 static_cast<float>(i),
450 -static_cast<float>(
451 gridHalfSize),
452 static_cast<float>(i),
453 static_cast<float>(
454 gridHalfSize));
455
456 addGridLine(
457 -static_cast<float>(
458 gridHalfSize),
459 static_cast<float>(i),
460 static_cast<float>(
461 gridHalfSize),
462 static_cast<float>(i));
463 }
464
465
466 auto gridVB =
468 gridVertices.data(),
469 static_cast<std::uint32_t>(
470 gridVertices.size()
471 * sizeof(float)));
472
473 auto gridIB =
475 gridIndices.data(),
476 static_cast<std::uint32_t>(
477 gridIndices.size()));
478
479 auto gridVA =
481
482 gridVA->SetVertexBuffer(gridVB);
483 gridVA->SetIndexBuffer(gridIB);
484
485
486 // ------------------------------------------------------------
487 // Main loop
488 // ------------------------------------------------------------
489
490 double lastTime = glfwGetTime();
491
492 while (!window.ShouldClose())
493 {
494 const double currentTime =
495 glfwGetTime();
496
497 const float deltaTime =
498 static_cast<float>(
499 currentTime -
500 lastTime);
501
502 lastTime = currentTime;
503
504 window.PollEvents();
505
506
507 // --------------------------------------------------------
508 // Runtime update
509 // --------------------------------------------------------
510
511 if (isPlaying &&
512 !isPaused &&
513 runtimeScene)
514 {
515 runtimeScene->OnUpdate(
516 deltaTime);
517
518 physics.Update(
519 deltaTime);
520 }
521
522
523 // --------------------------------------------------------
524 // Viewport
525 // --------------------------------------------------------
526
527 const std::uint32_t viewportWidth =
528 editor.GetViewportWidth();
529
530 const std::uint32_t viewportHeight =
531 editor.GetViewportHeight();
532
533 const auto& currentSpec =
534 framebuffer->GetSpecification();
535
536 if (viewportWidth !=
537 currentSpec.Width ||
538 viewportHeight !=
539 currentSpec.Height)
540 {
541 framebuffer->Resize(
542 viewportWidth,
543 viewportHeight);
544
545 editor.SetViewportTexture(
546 framebuffer->
547 GetColorAttachmentRendererID());
548 }
549
550
551 editorCamera.SetViewportSize(
552 static_cast<float>(
553 viewportWidth),
554 static_cast<float>(
555 viewportHeight));
556
557 if (!isPlaying)
558 {
559 editorCamera.OnUpdate(
560 window.GetNativeWindow(),
561 deltaTime,
562 editor.IsViewportHovered());
563 }
564
565
566 // --------------------------------------------------------
567 // Graphics settings
568 // --------------------------------------------------------
569
570 const auto& graphicsSettings =
571 editor.GetGraphicsSettings();
572
573 framebuffer->
574 SetPostProcessSettings(
575 graphicsSettings);
576
577 cameraPreviewFramebuffer->
578 SetPostProcessSettings(
579 graphicsSettings);
580
583 graphicsSettings);
584
585
586 // --------------------------------------------------------
587 // Main scene render
588 // --------------------------------------------------------
589
590 framebuffer->Bind();
591
593 0.055f,
594 0.065f,
595 0.085f,
596 1.0f);
597
599
600
601 glm::mat4 renderView =
602 editorCamera.GetViewMatrix();
603
604 glm::mat4 renderProjection =
605 editorCamera.GetProjectionMatrix();
606
607 glm::mat4 viewProjection =
608 renderProjection *
609 renderView;
610
611 glm::vec3 renderCameraPosition =
612 editorCamera.GetPosition();
613
614
615 // During Play the primary Scene camera owns the game view.
616 if (isPlaying && activeScene)
617 {
618 for (NoJob::Entity entity :
619 activeScene->GetEntities())
620 {
621 if (!entity.HasComponent<
623 {
624 continue;
625 }
626
627 const auto& camera =
628 entity.GetComponent<
630
631 if (!camera.Primary)
632 continue;
633
634 const glm::mat4 cameraWorld =
635 activeScene->
636 GetWorldTransform(
637 entity);
638
639 renderView =
640 glm::inverse(
641 cameraWorld);
642
643 const float aspect =
644 viewportHeight > 0
645 ? static_cast<float>(
646 viewportWidth)
647 /
648 static_cast<float>(
649 viewportHeight)
650 : 1.0f;
651
652 renderProjection =
653 camera.GetProjection(
654 aspect);
655
656 viewProjection =
657 renderProjection *
658 renderView;
659
660 renderCameraPosition =
661 glm::vec3(
662 cameraWorld[3]);
663
664 break;
665 }
666 }
667
668
669 // --------------------------------------------------------
670 // Editor grid
671 // --------------------------------------------------------
672
673 if (!isPlaying)
674 {
675 shader->Bind();
676
677 shader->SetFloat3(
678 "u_AmbientColor",
679 {
680 1.0f,
681 1.0f,
682 1.0f
683 });
684
685 shader->SetInt(
686 "u_HasDirectionalLight",
687 0);
688
689 shader->SetInt(
690 "u_PointLightCount",
691 0);
692
693 shader->SetInt(
694 "u_SpotLightCount",
695 0);
696
697 shader->SetFloat3(
698 "u_ViewPosition",
699 renderCameraPosition);
700
702 gridVA,
703 shader,
704 glm::mat4(1.0f),
705 viewProjection,
706 glm::vec4(
707 0.28f,
708 0.30f,
709 0.34f,
710 1.0f));
711 }
712
713
715 *activeScene,
716 viewProjection,
717 renderCameraPosition);
718
719
720 // --------------------------------------------------------
721 // Navigation debug rendering
722 // --------------------------------------------------------
723
724 if (!isPlaying &&
725 editor.IsNavMeshVisible() &&
726 navigationSystem.HasNavMesh())
727 {
728 navMeshDebugRenderer.Draw(
729 viewProjection);
730 }
731
732
733 framebuffer->Unbind();
734
735
736 // --------------------------------------------------------
737 // Selected camera preview
738 // --------------------------------------------------------
739
740 if (!isPlaying)
741 {
742 const NoJob::Entity selected =
743 editor.GetSelectedEntity();
744
745 if (selected &&
746 selected.HasComponent<
748 {
749 const auto& previewCamera =
750 selected.GetComponent<
752
753 const glm::mat4 cameraWorld =
754 editorScene.
755 GetWorldTransform(
756 selected);
757
758 const glm::mat4 previewView =
759 glm::inverse(
760 cameraWorld);
761
762 const glm::mat4
763 previewProjection =
764 previewCamera.
765 GetProjection(
766 320.0f /
767 180.0f);
768
769 cameraPreviewFramebuffer->
770 Bind();
771
774 0.055f,
775 0.065f,
776 0.085f,
777 1.0f);
778
780 Clear();
781
782 // Secondary camera preview reuses
783 // the shadow maps generated by the
784 // main scene render this frame.
786 editorScene,
787 previewProjection *
788 previewView,
789 glm::vec3(
790 cameraWorld[3]),
791 false);
792
793 cameraPreviewFramebuffer->
794 Unbind();
795 }
796 }
797
798
799 // --------------------------------------------------------
800 // Backbuffer / Editor UI
801 // --------------------------------------------------------
802
804 0,
805 0,
806 1600,
807 900);
808
810 0.035f,
811 0.038f,
812 0.045f,
813 1.0f);
814
816
817
819 renderView,
820 renderProjection);
821
822 editor.BeginFrame();
823 editor.Draw();
824
825
826 // --------------------------------------------------------
827 // Navigation requests
828 // --------------------------------------------------------
829
830 if (!isPlaying &&
831 editor.
832 ConsumeBakeNavMeshRequest())
833 {
834 if (navigationSystem.Bake(
835 editorScene))
836 {
837 navMeshDebugRenderer.Build(
838 navigationSystem.
839 GetNavMesh());
840
841 std::cout
842 << "[Navigation] NavMesh baked: "
843 << navigationSystem.
844 GetNavMesh().
845 GetPolygonCount()
846 << " polygons.\n";
847 }
848 else
849 {
850 navMeshDebugRenderer.Clear();
851
852 std::cout
853 << "[Navigation] "
854 "NavMesh bake produced "
855 "no walkable polygons.\n";
856 }
857 }
858
859
860 if (!isPlaying &&
861 editor.
862 ConsumeClearNavMeshRequest())
863 {
864 navigationSystem.Clear();
865 navMeshDebugRenderer.Clear();
866
867 std::cout
868 << "[Navigation] "
869 "NavMesh cleared.\n";
870 }
871
872
873 // --------------------------------------------------------
874 // Play
875 // --------------------------------------------------------
876
877 if (editor.ConsumePlayRequest() &&
878 !isPlaying)
879 {
880 std::cerr << "[Play] Copying editor scene...\n";
881 runtimeScene =
882 editorScene.Copy();
883 std::cerr << "[Play] Scene copy complete.\n";
884
885 // The runtime scene does not own navigation.
886 // It uses the NavMesh already baked by the
887 // editor NavigationSystem.
888 runtimeScene->
889 SetNavigationSystem(
890 &navigationSystem);
891
892 std::cerr << "[Play] Starting runtime scripts...\n";
893 runtimeScene->OnRuntimeStart();
894 std::cerr << "[Play] Runtime scripts started.\n";
895
896 std::cerr << "[Play] Starting physics...\n";
897 physics.Start(*runtimeScene);
898 std::cerr << "[Play] Physics started.\n";
899
900 // Connect AI perception to the active physics world.
901 runtimeScene->SetSpatialQuery(&spatialQuery);
902
903 activeScene = runtimeScene.get();
904
905 isPlaying = true;
906 isPaused = false;
907
908 editor.SetScene(
909 activeScene);
910
911 editor.SetRuntimeState(
912 true,
913 false);
914 std::cerr << "[Play] Play mode initialized.\n";
915 }
916
917
918 // --------------------------------------------------------
919 // Pause
920 // --------------------------------------------------------
921
922 if (editor.ConsumePauseRequest() &&
923 isPlaying)
924 {
925 isPaused = !isPaused;
926
927 editor.SetRuntimeState(
928 true,
929 isPaused);
930 }
931
932
933 // --------------------------------------------------------
934 // Stop
935 // --------------------------------------------------------
936
937 if (editor.ConsumeStopRequest() &&
938 isPlaying)
939 {
940 // Disconnect the spatial query before stopping physics.
941 runtimeScene->SetSpatialQuery(nullptr);
942
943 runtimeScene->OnRuntimeStop();
944
945 physics.Stop();
946
947 runtimeScene.reset();
948
949 activeScene =
950 &editorScene;
951
952 isPlaying = false;
953 isPaused = false;
954
955 editor.SetScene(
956 activeScene);
957
958 editor.SetRuntimeState(
959 false,
960 false);
961 }
962
963
964 // --------------------------------------------------------
965 // Save Scene
966 // --------------------------------------------------------
967
968 if (!isPlaying &&
969 editor.
970 ConsumeSaveSceneRequest())
971 {
972 std::filesystem::
973 create_directories(
974 currentScenePath.
975 parent_path());
976
978 editorScene,
979 currentScenePath))
980 {
981 std::cout
982 << "[Scene] Saved "
983 << currentScenePath.string()
984 << "\n";
985 }
986 }
987
988
989 // --------------------------------------------------------
990 // Save Scene As
991 // --------------------------------------------------------
992
993 if (!isPlaying &&
994 editor.
995 ConsumeSaveSceneAsRequest())
996 {
997 const auto selected =
1000 GetProjectRoot()
1001 /
1002 "Assets/Scenes");
1003
1004 if (!selected.empty())
1005 {
1006 std::filesystem::
1007 create_directories(
1008 selected.parent_path());
1009
1011 editorScene,
1012 selected))
1013 {
1014 currentScenePath =
1015 selected;
1016
1017 std::cout
1018 << "[Scene] Saved As "
1019 << currentScenePath.
1020 string()
1021 << "\n";
1022 }
1023 }
1024 }
1025
1026
1027 // --------------------------------------------------------
1028 // Load Scene
1029 // --------------------------------------------------------
1030
1031 if (!isPlaying &&
1032 editor.
1033 ConsumeLoadSceneRequest())
1034 {
1035 const auto selected =
1038 GetProjectRoot()
1039 /
1040 "Assets/Scenes");
1041
1042 if (!selected.empty() &&
1044 editorScene,
1045 selected,
1046 cubeMesh,
1047 cubeMaterial))
1048 {
1049 currentScenePath =
1050 selected;
1051
1052 activeScene =
1053 &editorScene;
1054
1055 editor.SetScene(
1056 activeScene);
1057
1058 // The baked NavMesh belonged to
1059 // the previous scene.
1060 navigationSystem.Clear();
1061 navMeshDebugRenderer.Clear();
1062
1063 std::cout
1064 << "[Scene] Opened "
1065 << currentScenePath.string()
1066 << "\n";
1067 }
1068 }
1069
1070
1071 // --------------------------------------------------------
1072 // Graphics validation scene
1073 // --------------------------------------------------------
1074
1075 if (!isPlaying &&
1076 editor.
1077 ConsumeGraphicsTestSceneRequest())
1078 {
1079 // This replaces the scene, therefore
1080 // any previously baked NavMesh is stale.
1081 navigationSystem.Clear();
1082 navMeshDebugRenderer.Clear();
1083
1084 for (auto e :
1085 editorScene.GetEntities())
1086 {
1087 editorScene.DestroyEntity(e);
1088 }
1089
1090
1091 auto makeCube =
1092 [&](const char* name,
1093 glm::vec3 pos,
1094 glm::vec3 scale,
1095 glm::vec4 color,
1096 float metal,
1097 float rough,
1098 float emissive)
1099 {
1100 auto mat =
1101 std::make_shared<
1103 shader,
1104 color);
1105
1106 mat->Metallic() = metal;
1107 mat->Roughness() = rough;
1108
1109 mat->AmbientOcclusion() =
1110 1.0f;
1111
1112 mat->EmissiveColor() =
1113 glm::vec3(color);
1114
1115 mat->EmissiveStrength() =
1116 emissive;
1117
1118 auto e =
1119 editorScene.
1120 CreateEntity(name);
1121
1122 e.AddComponent<
1124 cubeMesh);
1125
1126 e.AddComponent<
1127 NoJob::
1128 MeshRendererComponent>(
1129 mat);
1130
1131 auto& tr =
1132 e.GetComponent<
1133 NoJob::
1134 TransformComponent>();
1135
1136 tr.Position = pos;
1137 tr.Scale = scale;
1138
1139 return e;
1140 };
1141
1142
1143 makeCube(
1144 "PBR Dielectric",
1145 { -4.5f, 0.0f, 0.0f },
1146 { 1, 1, 1 },
1147 {
1148 0.72f,
1149 0.18f,
1150 0.08f,
1151 1
1152 },
1153 0.0f,
1154 0.35f,
1155 0.0f);
1156
1157 makeCube(
1158 "PBR Metal",
1159 { -1.5f, 0.0f, 0.0f },
1160 { 1, 1, 1 },
1161 {
1162 0.75f,
1163 0.72f,
1164 0.62f,
1165 1
1166 },
1167 1.0f,
1168 0.16f,
1169 0.0f);
1170
1171 makeCube(
1172 "Rough Surface",
1173 { 1.5f, 0.0f, 0.0f },
1174 { 1, 1, 1 },
1175 {
1176 0.12f,
1177 0.35f,
1178 0.75f,
1179 1
1180 },
1181 0.25f,
1182 0.92f,
1183 0.0f);
1184
1185 makeCube(
1186 "HDR Emissive",
1187 { 4.5f, 0.0f, 0.0f },
1188 { 1, 1, 1 },
1189 {
1190 0.15f,
1191 0.8f,
1192 0.32f,
1193 1
1194 },
1195 0.0f,
1196 0.4f,
1197 8.0f);
1198
1199 makeCube(
1200 "Shadow Receiver",
1201 { 0.0f, -1.6f, 0.0f },
1202 { 7.0f, 0.25f, 3.0f },
1203 {
1204 0.18f,
1205 0.20f,
1206 0.23f,
1207 1
1208 },
1209 0.0f,
1210 0.75f,
1211 0.0f);
1212
1213
1214 auto cam =
1215 editorScene.CreateEntity(
1216 "Graphics Test Camera");
1217
1218 auto& ct =
1219 cam.GetComponent<
1220 NoJob::
1221 TransformComponent>();
1222
1223 ct.Position =
1224 {
1225 0.0f,
1226 3.5f,
1227 11.5f
1228 };
1229
1230 ct.Rotation =
1231 {
1232 glm::radians(-14.0f),
1233 0.0f,
1234 0.0f
1235 };
1236
1237 cam.AddComponent<
1239
1240
1241 auto sunE =
1242 editorScene.CreateEntity(
1243 "Directional Shadow Test");
1244
1245 sunE.GetComponent<
1246 NoJob::
1247 TransformComponent>().
1248 Rotation =
1249 {
1250 glm::radians(-52.0f),
1251 glm::radians(-28.0f),
1252 0.0f
1253 };
1254
1255 NoJob::
1256 DirectionalLightComponent dl;
1257
1258 dl.Intensity = 1.15f;
1259 dl.CastShadows = true;
1260
1261 sunE.AddComponent<
1262 NoJob::
1263 DirectionalLightComponent>(
1264 dl);
1265
1266
1267 auto pointE =
1268 editorScene.CreateEntity(
1269 "Point Shadow Test");
1270
1271 pointE.GetComponent<
1272 NoJob::
1273 TransformComponent>().
1274 Position =
1275 {
1276 -3.0f,
1277 2.8f,
1278 2.0f
1279 };
1280
1282
1283 pl.Intensity = 1.4f;
1284 pl.Range = 7.0f;
1285 pl.CastShadows = true;
1286
1287 pointE.AddComponent<
1289 pl);
1290
1291
1292 auto spotE =
1293 editorScene.CreateEntity(
1294 "Spot Shadow Test");
1295
1296 auto& st =
1297 spotE.GetComponent<
1298 NoJob::
1299 TransformComponent>();
1300
1301 st.Position =
1302 {
1303 3.0f,
1304 4.0f,
1305 3.0f
1306 };
1307
1308 st.Rotation =
1309 {
1310 glm::radians(-55.0f),
1311 glm::radians(18.0f),
1312 0.0f
1313 };
1314
1316
1317 sl.Intensity = 4.0f;
1318 sl.Range = 12.0f;
1319 sl.CastShadows = true;
1320
1321 spotE.AddComponent<
1323 sl);
1324
1325
1326 activeScene =
1327 &editorScene;
1328
1329 editor.SetScene(
1330 activeScene);
1331
1332 const auto graphicsScenePath =
1335 /
1336 "Assets/Scenes/"
1337 "GraphicsValidation.nojobscene";
1338
1339 std::filesystem::
1340 create_directories(
1341 graphicsScenePath.
1342 parent_path());
1343
1345 editorScene,
1346 graphicsScenePath);
1347
1348 std::cout
1349 << "[Graphics] Validation "
1350 "scene generated and saved.\n";
1351 }
1352
1353
1354 editor.EndFrame();
1355
1356 window.SwapBuffers();
1357 }
1358
1359
1360 // ------------------------------------------------------------
1361 // Shutdown
1362 // ------------------------------------------------------------
1363 if (runtimeScene)
1364 {
1365 runtimeScene->SetSpatialQuery(nullptr);
1366 runtimeScene->OnRuntimeStop();
1367
1368 physics.Stop();
1369
1370 runtimeScene.reset();
1371 activeScene = &editorScene;
1372 editor.SetScene(activeScene);
1373 editor.SetRuntimeState(false, false);
1374 }
1375 editor.Shutdown();
1376
1377 navMeshDebugRenderer.Clear();
1378 navigationSystem.Clear();
1379
1380 cameraPreviewFramebuffer.reset();
1381 framebuffer.reset();
1382
1383 gridVA.reset();
1384 gridIB.reset();
1385 gridVB.reset();
1386
1387 groundMaterial.reset();
1388 cubeMaterial.reset();
1389 cubeMesh.reset();
1390 shader.reset();
1391
1394
1395 return 0;
1396 }
1397 catch (const std::exception& e)
1398 {
1399 std::cerr << "[Fatal] Unhandled std::exception: " << e.what() << '\n';
1401 return 1;
1402 }
1403 catch (...)
1404 {
1405 std::cerr << "[Fatal] Unhandled non-std exception during editor execution.\n";
1407 return 1;
1408 }
1409}
AssetManager declarations for the Asset subsystem.
Buffer declarations for the Rendering subsystem.
Components declarations for the Scenes and Native Scripting subsystem.
DefaultSceneShaders declarations for the Rendering subsystem.
EditorCamera declarations for the Editor subsystem.
Main editor interaction, rendering and debugging layer.
Framebuffer declarations for the Rendering subsystem.
JoltSpatialQuery declarations for the Physics subsystem.
Material declarations for the Rendering subsystem.
Mesh declarations for the Rendering subsystem.
NavMeshDebugRenderer declarations for the Navigation subsystem.
NavigationSystem declarations for the Navigation subsystem.
int main()
PhysicsSystem declarations for the Physics subsystem.
Project-native script build orchestration and reload notifications.
RenderCommand declarations for the Rendering subsystem.
Renderer declarations for the Rendering subsystem.
Scene declarations for the Scenes and Native Scripting subsystem.
SceneFileDialog declarations for the Scenes and Native Scripting subsystem.
SceneRenderer declarations for the Scenes and Native Scripting subsystem.
Scene serialization and deserialization.
Shader declarations for the Rendering subsystem.
Texture declarations for the Rendering subsystem.
VertexArray declarations for the Rendering subsystem.
Window declarations for the Core subsystem.
static const std::filesystem::path & GetProjectRoot()
const glm::mat4 & GetViewMatrix() const
const glm::mat4 & GetProjectionMatrix() const
const glm::vec3 & GetPosition() const
void OnUpdate(GLFWwindow *window, float deltaTime, bool viewportHovered)
void SetViewportSize(float width, float height)
void SetScene(Scene *scene)
void SetSelectedEntity(Entity entity)
void SetEditorCameraMatrices(const glm::mat4 &view, const glm::mat4 &projection)
Entity GetSelectedEntity() const
Definition EditorLayer.h:42
void SetRuntimeState(bool playing, bool paused)
bool IsViewportHovered() const
void Init(GLFWwindow *window, Scene *scene)
void SetDefaultCubeAssets(std::shared_ptr< Mesh > mesh, std::shared_ptr< Material > material)
void SetViewportTexture(std::uint32_t textureID)
bool IsNavMeshVisible() const
Definition EditorLayer.h:73
std::uint32_t GetViewportWidth() const
std::uint32_t GetViewportHeight() const
const FramebufferSpecification & GetGraphicsSettings() const
Definition EditorLayer.h:89
void SetGraphicsSettings(const FramebufferSpecification &settings)
Definition EditorLayer.h:82
void SetCameraPreviewTexture(std::uint32_t textureID)
Definition EditorLayer.h:51
T & GetComponent()
Definition Scene.h:851
T & AddComponent(Args &&... args)
Definition Scene.h:836
bool HasComponent() const
Definition Scene.h:865
static std::shared_ptr< Framebuffer > Create(const FramebufferSpecification &specification)
static std::shared_ptr< IndexBuffer > Create(const std::uint32_t *indices, std::uint32_t count)
Definition Buffer.cpp:20
float & Metallic()
Definition Material.h:49
static std::shared_ptr< Mesh > CreateCube()
Definition Mesh.cpp:167
void Build(const NavMesh &navMesh)
void Draw(const glm::mat4 &viewProjection, const glm::vec4 &color=glm::vec4(0.15f, 0.85f, 1.0f, 1.0f)) const
static void SetClearColor(float r, float g, float b, float a)
static void SetViewport(std::uint32_t x, std::uint32_t y, std::uint32_t width, std::uint32_t height)
static void Submit(const std::shared_ptr< VertexArray > &vertexArray, const std::shared_ptr< Shader > &shader, const glm::mat4 &transform=glm::mat4(1.0f), const glm::mat4 &viewProjection=glm::mat4(1.0f), const glm::vec4 &color=glm::vec4(1.0f))
Definition Renderer.cpp:28
static void Init()
Definition Renderer.cpp:8
static void Shutdown()
Definition Renderer.cpp:13
static std::filesystem::path Open(const std::filesystem::path &initialDirectory)
static std::filesystem::path SaveAs(const std::filesystem::path &initialDirectory)
std::unique_ptr< Scene > Copy() const
Definition Scene.cpp:98
std::vector< Entity > GetEntities()
Definition Scene.cpp:231
Entity CreateEntity(const std::string &name="Entity")
Definition Scene.cpp:133
void DestroyEntity(Entity entity)
Definition Scene.cpp:161
static void Render(Scene &scene, const glm::mat4 &viewProjection, const glm::vec3 &cameraPosition=glm::vec3(0.0f), bool rebuildShadowMaps=true)
static void SetGraphicsSettings(const FramebufferSpecification &settings)
static bool Load(Scene &scene, const std::filesystem::path &path, const std::shared_ptr< Mesh > &defaultMesh, const std::shared_ptr< Material > &defaultMaterial)
static bool Save(const Scene &scene, const std::filesystem::path &path)
static std::shared_ptr< Shader > Create(const std::string &vertexSource, const std::string &fragmentSource)
Definition Shader.cpp:8
static std::shared_ptr< Texture2D > CreateCheckerboard(std::uint32_t width=256, std::uint32_t height=256)
Definition Texture.cpp:6
static std::shared_ptr< VertexArray > Create()
static std::shared_ptr< VertexBuffer > Create(const float *vertices, std::uint32_t size)
Definition Buffer.cpp:8
constexpr const char * Fragment
constexpr const char * Vertex