From 69d59266b98eae179c10f8362df5a95069339004 Mon Sep 17 00:00:00 2001 From: Antoine Pilote Date: Sun, 28 Jul 2024 21:05:02 -0400 Subject: [PATCH] Improved shadow mapping and refactored quakemap builder scene structure --- Nuake/src/Rendering/SceneRenderer.cpp | 34 ++++++++++++-- Nuake/src/Resource/FGD/FGDFile.h | 10 ++-- Nuake/src/Scene/Components/LightComponent.h | 6 +-- Nuake/src/Scene/Systems/QuakeMapBuilder.cpp | 52 ++++++++++++++------- 4 files changed, 74 insertions(+), 28 deletions(-) diff --git a/Nuake/src/Rendering/SceneRenderer.cpp b/Nuake/src/Rendering/SceneRenderer.cpp index ec92d108..1c002af6 100644 --- a/Nuake/src/Rendering/SceneRenderer.cpp +++ b/Nuake/src/Rendering/SceneRenderer.cpp @@ -350,8 +350,8 @@ namespace Nuake Shader* shader = ShaderManager::GetShader("Resources/Shaders/shadowMap.shader"); shader->Bind(); - RenderCommand::Enable(RendererEnum::FACE_CULL); - glCullFace(GL_BACK); + RenderCommand::Disable(RendererEnum::FACE_CULL); + glCullFace(GL_FRONT); auto meshView = scene.m_Registry.view(); auto quakeView = scene.m_Registry.view(); @@ -693,7 +693,20 @@ namespace Nuake Ref env = scene.GetEnvironment(); + struct LightDistance + { + TransformComponent transform; + LightComponent light; + float distance; + }; + + std::vector lightDistances; + auto view = scene.m_Registry.view(); + + lightDistances.reserve(view.size_hint()); + + const Vector3 camPosition = scene.GetCurrentCamera()->Translation; for (auto l : view) { auto [transform, light, parent] = view.get(l); @@ -707,7 +720,22 @@ namespace Nuake light.Direction = transform.GetGlobalRotation() * Vector3(0, 0, 1); } - Renderer::RegisterDeferredLight(transform, light); + Vector3 lightPosition = transform.GetGlobalPosition(); + float distanceFromCam = glm::length(camPosition - lightPosition); + + lightDistances.push_back({transform, light, distanceFromCam}); + } + + std::sort(lightDistances.begin(), lightDistances.end(), + [](const LightDistance& a, const LightDistance& b) + { + return a.distance < b.distance; + } + ); + + for (const auto& l : lightDistances) + { + Renderer::RegisterDeferredLight(l.transform, l.light); } mGBuffer->GetTexture(GL_DEPTH_ATTACHMENT)->Bind(5); diff --git a/Nuake/src/Resource/FGD/FGDFile.h b/Nuake/src/Resource/FGD/FGDFile.h index 0024931a..a604dc93 100644 --- a/Nuake/src/Resource/FGD/FGDFile.h +++ b/Nuake/src/Resource/FGD/FGDFile.h @@ -69,7 +69,7 @@ namespace Nuake { { for (auto& b : BrushEntities) { - if (name == b.Name) + if (String::ToLower(name) == String::ToLower(b.Name)) return b; } @@ -80,8 +80,10 @@ namespace Nuake { { for (auto& p : PointEntities) { - if (name == p.Name) + if (String::ToLower(name) == String::ToLower(p.Name)) + { return p; + } } assert(false && "Point entity not found!"); @@ -91,13 +93,13 @@ namespace Nuake { { for (auto& b : BrushEntities) { - if (b.Name == className) + if (String::ToLower(b.Name) == String::ToLower(className)) return EntityType::Brush; } for (auto& p : PointEntities) { - if (p.Name == className) + if (String::ToLower(p.Name) == String::ToLower(className)) return EntityType::Point; } diff --git a/Nuake/src/Scene/Components/LightComponent.h b/Nuake/src/Scene/Components/LightComponent.h index 876894a2..dbfdd312 100644 --- a/Nuake/src/Scene/Components/LightComponent.h +++ b/Nuake/src/Scene/Components/LightComponent.h @@ -52,8 +52,8 @@ namespace Nuake glm::mat4 inverseViewProjection = glm::inverse(viewProjection); // TODO: Automate this - const float nearClip = 0.0001f; - const float farClip = 800.0f; + const float nearClip = 0.01f; + const float farClip = 500.0f; const float clipRange = farClip - nearClip; const float mCascadeNearPlaneOffset = -100.0f; @@ -73,7 +73,7 @@ namespace Nuake mCascadeSplits[i] = (d - nearClip) / clipRange; } - mCascadeSplits[0] = 0.01f; + //mCascadeSplits[0] = 0.01f; //mCascadeSplits[1] = 0.45f; //mCascadeSplits[2] = 1.0f; diff --git a/Nuake/src/Scene/Systems/QuakeMapBuilder.cpp b/Nuake/src/Scene/Systems/QuakeMapBuilder.cpp index 496703ae..bbd738ad 100644 --- a/Nuake/src/Scene/Systems/QuakeMapBuilder.cpp +++ b/Nuake/src/Scene/Systems/QuakeMapBuilder.cpp @@ -403,21 +403,17 @@ namespace Nuake { geo_generator_run(); DefaultMaterial = MaterialManager::Get()->GetMaterial("default"); + Entity worldspawnEntity = { (entt::entity)-1, m_Scene }; + std::map pointEntities = std::map(); for (uint32_t e = 0; e < (uint32_t)entity_count; ++e) { entity* entity_inst = &entities[e]; entity_geometry* entity_geo_inst = &entity_geo[e]; - Entity newEntity = m_Scene->CreateEntity("Brush " + std::to_string(e)); - - if (entity_inst->spawn_type == entity_spawn_type::EST_GROUP) - newEntity.GetComponent().Name = "Group " + std::to_string(e); - bool isTrigger = false; bool isFunc = false; bool isPos = false; - ent.AddChild(newEntity); - + std::string target = ""; std::string targetname = ""; FGDBrushEntity fgdBrush; @@ -425,6 +421,7 @@ namespace Nuake { bool isEntity = false; bool isWorldSpawn = false; bool isPointEntity = false; + Vector3 brushLocalPosition = { 0, 0, 0 }; for (int i = 0; i < entity_inst->property_count; i++) { property* prop = &(entity_inst->properties)[i]; @@ -439,7 +436,7 @@ namespace Nuake { float z = String::ToFloat(splits[0]); Vector3 position = Vector3(x, y, z) * ScaleFactor * (1.0f / 64.0f); - newEntity.GetComponent().SetLocalPosition(position); + brushLocalPosition = position; } if (key == "classname") @@ -478,19 +475,37 @@ namespace Nuake { if (key == "target") target = value; } + + if (!worldspawnEntity.IsValid()) + { + worldspawnEntity = m_Scene->CreateEntity("WorldSpawn"); + ent.AddChild(worldspawnEntity); + } + if (!isWorldSpawn) { if (isPointEntity) { - Entity newPEntity = Engine::GetCurrentScene()->CreateEntity("New prefab Entity"); - newEntity.AddChild(newPEntity); - auto& prefabComponent = newPEntity.AddComponent(); + if (pointEntities.find(pointEntity.Name) == pointEntities.end()) + { + Entity pointEntityParent = Engine::GetCurrentScene()->CreateEntity(pointEntity.Name); + pointEntities[pointEntity.Name] = pointEntityParent; + ent.AddChild(pointEntityParent); + } + + Entity newPrefab = Engine::GetCurrentScene()->CreateEntity(pointEntity.Name); + + pointEntities[pointEntity.Name].AddChild(newPrefab); + + auto& prefabComponent = newPrefab.AddComponent(); prefabComponent.SetPrefab(Prefab::New(pointEntity.Prefab)); + newPrefab.GetComponent().SetLocalPosition(brushLocalPosition); + for (auto& e : prefabComponent.PrefabInstance->Entities) { if (!e.GetComponent().HasParent) { - newPEntity.AddChild(e); + newPrefab.AddChild(e); } } } @@ -500,10 +515,10 @@ namespace Nuake { brush* brush_inst = &entity_inst->brushes[b]; brush_geometry* brush_geo_inst = &entity_geo_inst->brushes[b]; - if (isEntity) - CreateFuncBrush(brush_inst, brush_geo_inst, m_Scene, newEntity, target, targetname, fgdBrush); - else - CreateBrush(brush_inst, brush_geo_inst, m_Scene, newEntity, target, targetname); + //if (isEntity) + // CreateFuncBrush(brush_inst, brush_geo_inst, m_Scene, newEntity, target, targetname, fgdBrush); + //else + // CreateBrush(brush_inst, brush_geo_inst, m_Scene, newEntity, target, targetname); } } else @@ -511,10 +526,11 @@ namespace Nuake { std::map> m_Materials; std::map, std::vector> m_StaticWorld; - Entity brushEntity = m_Scene->CreateEntity("WorldSpawn"); - newEntity.AddChild(brushEntity); + Entity brushEntity = m_Scene->CreateEntity("Brush " + std::to_string(e)); + worldspawnEntity.AddChild(brushEntity); auto& transformComponent = brushEntity.GetComponent(); + transformComponent.SetLocalPosition(brushLocalPosition); auto& bsp = brushEntity.AddComponent(); quakeMapC.m_Brushes.push_back(brushEntity);