From 9aac7e2259c1d81c23b6ca62c18e299c1dafae35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20K=C5=82os?= Date: Sun, 9 Sep 2018 01:13:28 +0200 Subject: [PATCH 1/4] test scene --- .../Res/Models/Primitives/Cube.mtl | 12 ++ .../Res/Models/Primitives/Cube.obj | 49 ++++++ .../Src/GameManagerSystem.cpp | 142 ++++++++++++++---- .../Src/GameManagerSystem.hpp | 84 ++++++----- .../Src/GameManagerWorldComponent.hpp | 1 + 5 files changed, 217 insertions(+), 71 deletions(-) create mode 100644 SandboxRendering/SandboxRendering/Res/Models/Primitives/Cube.mtl create mode 100644 SandboxRendering/SandboxRendering/Res/Models/Primitives/Cube.obj diff --git a/SandboxRendering/SandboxRendering/Res/Models/Primitives/Cube.mtl b/SandboxRendering/SandboxRendering/Res/Models/Primitives/Cube.mtl new file mode 100644 index 0000000..8ec060d --- /dev/null +++ b/SandboxRendering/SandboxRendering/Res/Models/Primitives/Cube.mtl @@ -0,0 +1,12 @@ +# Blender MTL File: 'None' +# Material Count: 1 + +newmtl Material.001 +Ns 96.078431 +Ka 1.000000 1.000000 1.000000 +Kd 0.640000 0.640000 0.640000 +Ks 0.500000 0.500000 0.500000 +Ke 0.000000 0.000000 0.000000 +Ni 1.000000 +d 1.000000 +illum 2 diff --git a/SandboxRendering/SandboxRendering/Res/Models/Primitives/Cube.obj b/SandboxRendering/SandboxRendering/Res/Models/Primitives/Cube.obj new file mode 100644 index 0000000..c3bef44 --- /dev/null +++ b/SandboxRendering/SandboxRendering/Res/Models/Primitives/Cube.obj @@ -0,0 +1,49 @@ +# Blender v2.79 (sub 0) OBJ File: '' +# www.blender.org +mtllib Cube.mtl +o Cube +v -0.500000 0.000000 0.500000 +v -0.500000 1.000000 0.500000 +v -0.500000 0.000000 -0.500000 +v -0.500000 1.000000 -0.500000 +v 0.500000 0.000000 0.500000 +v 0.500000 1.000000 0.500000 +v 0.500000 0.000000 -0.500000 +v 0.500000 1.000000 -0.500000 +vt 0.666667 0.333333 +vt 0.333333 0.333333 +vt 0.333333 0.000000 +vt 0.666667 0.000000 +vt 0.000000 0.666667 +vt 0.000000 0.333333 +vt 0.333333 0.333333 +vt 0.333333 0.666667 +vt 0.333333 0.000000 +vt 0.000000 0.333333 +vt 0.000000 0.000000 +vt 0.333333 0.666667 +vt 0.333333 0.333333 +vt 0.666667 0.333333 +vt 0.666667 0.666667 +vt 0.666667 0.333333 +vt 0.666667 0.000000 +vt 1.000000 0.000000 +vt 1.000000 0.333333 +vt 0.333333 0.666667 +vt 0.333333 1.000000 +vt 0.000000 1.000000 +vt 0.000000 0.666667 +vn -1.0000 0.0000 0.0000 +vn 0.0000 0.0000 -1.0000 +vn 1.0000 0.0000 0.0000 +vn 0.0000 0.0000 1.0000 +vn 0.0000 -1.0000 0.0000 +vn 0.0000 1.0000 0.0000 +usemtl Material.001 +s off +f 1/1/1 2/2/1 4/3/1 3/4/1 +f 3/5/2 4/6/2 8/7/2 7/8/2 +f 7/9/3 8/7/3 6/10/3 5/11/3 +f 5/12/4 6/13/4 2/14/4 1/15/4 +f 3/16/5 7/17/5 5/18/5 1/19/5 +f 8/20/6 4/21/6 2/22/6 6/23/6 diff --git a/SandboxRendering/SandboxRendering/Src/GameManagerSystem.cpp b/SandboxRendering/SandboxRendering/Src/GameManagerSystem.cpp index 1d79a39..9560896 100644 --- a/SandboxRendering/SandboxRendering/Src/GameManagerSystem.cpp +++ b/SandboxRendering/SandboxRendering/Src/GameManagerSystem.cpp @@ -34,29 +34,23 @@ void GameManagerSystem::CreateScene(Scene* scene) DeferredTaskSystem::AddWorldComponentImmediate(scene, "HDR/HDR.hdr", eResourceSource::GAME); - // gameMgrCmp->Model = CreateModel(scene, "Models/leather_shoes/Leather_Shoes.obj"); - // gameMgrCmp->Model->GetTransform().SetGlobalTranslation(Vector(500.0f, 0.0f, 0.0f)); - - // gameMgrCmp->Model = CreateModel(scene, "Models/kv-2-heavy-tank-1940/model.obj"); - // gameMgrCmp->Model->GetTransform().SetGlobalScale(Vector(5.0f, 5.0f, 5.0f)); - // gameMgrCmp->Model->GetTransform().SetGlobalTranslation(Vector(0.0f, 10.0f, 0.0f)); - - // gameMgrCmp->Model = CreateModel(scene, "Models/1972-datsun-240k-gt/model.obj"); - // gameMgrCmp->Model->GetTransform().SetGlobalScale(Vector::ONE * 20.0f); + gameMgrCmp->Model = CreateModel(scene, "Models/leather_shoes/Leather_Shoes.obj"); + // gameMgrCmp->Model->GetTransform().SetGlobalTranslation(Vector(300.0f, 0.0f, 0.0f)); + + CreateModel(scene, "Models/Primitives/Cube.obj")->GetTransform().SetGlobalScale(Vector(5000.0f, 1.0f, 5000.0f)); + CreateRandomCubes(scene, 100, Vector(0.0f, 0.0f, 0.0f), Vector(2000.0f, 1.0f, 2000.0f)); - CreatePBRShpereGrid(scene, Vector(0.0f, 0.0f, 0.0f), Color(0.0f, 0.0f, 0.0f, 1.0f)); - CreatePBRShpereGrid(scene, Vector(-300.0f, 0.0f, 0.0f), Color(0.5f, 0.5f, 0.5f, 1.0f)); - CreatePBRShpereGrid(scene, Vector(-600.0f, 0.0f, 0.0f), Color(1.0f, 1.0f, 1.0f, 1.0f)); + // CreatePBRShpereGrid(scene, Vector(0.0f, 0.0f, 0.0f), Color(0.0f, 0.0f, 0.0f, 1.0f)); + // CreatePBRShpereGrid(scene, Vector(-300.0f, 0.0f, 0.0f), Color(0.5f, 0.5f, 0.5f, 1.0f)); + // CreatePBRShpereGrid(scene, Vector(-600.0f, 0.0f, 0.0f), Color(1.0f, 1.0f, 1.0f, 1.0f)); // CreateSponza(scene); - CreateTextUI(scene); - - // CreateTranslucent(scene); + // CreateTextUI(scene); - CreatePointLights(scene, 128); + // CreatePointLights(scene, 64); - CreateParticles(scene); + // CreateParticles(scene); } void GameManagerSystem::CreateTextUI(Scene* scene) @@ -89,12 +83,9 @@ void GameManagerSystem::CreateCamera(Scene* scene) DeferredTaskSystem::AddComponentImmediate(scene, camera, 35_deg, 1.0f, 5000.f); DeferredTaskSystem::AddComponentImmediate(scene, camera, 100.0f, 0.003f, 10.0f); gameMgrCmp->PostCmp = DeferredTaskSystem::AddComponentImmediate(scene, camera); - gameMgrCmp->PostCmp->Exposure = 1.0f; - gameMgrCmp->PostCmp->DOFSize = 1.0f; - gameMgrCmp->PostCmp->DOFPoint = 300.0f; - gameMgrCmp->PostCmp->DOFRange = 200.0f; - // gameMgrCmp->PostCmp->DOFShow = 1.0f; - gameMgrCmp->PostCmp->BloomScale = 1.0f; + // PostFancy(gameMgrCmp->PostCmp); + // PostReset(gameMgrCmp->PostCmp); + PostMinimal(gameMgrCmp->PostCmp); EntityTransform& cameraTrans = camera->GetTransform(); cameraTrans.SetGlobalTranslation(Vector(-550.0f, 180.0f, 0.0f)); @@ -103,10 +94,53 @@ void GameManagerSystem::CreateCamera(Scene* scene) scene->GetWorldComponent()->SetCamera(0, scene->GetComponent(camera)); gameMgrCmp->Camera = camera; - // Entity* keyDirLight = DeferredTaskSystem::SpawnEntityImmediate(scene); - // DeferredTaskSystem::AddComponentImmediate(scene, keyDirLight, Color(1.0f, 1.0f, 1.0f), 5.0f); - // keyDirLight->GetTransform().SetGlobalRotation(Quaternion(Vector::UNIT_Y, -45_deg) * Quaternion(Vector::UNIT_X, 65_deg)); - // gameMgrCmp->GameEntities.PushBack(keyDirLight); + Entity* keyDirLight = DeferredTaskSystem::SpawnEntityImmediate(scene); + DeferredTaskSystem::AddComponentImmediate(scene, keyDirLight, Color(1.0f, 1.0f, 1.0f), 1.0f); + keyDirLight->GetTransform().SetGlobalRotation(Quaternion(Vector::UNIT_Y, -45_deg) * Quaternion(Vector::UNIT_X, 75_deg)); + gameMgrCmp->KeyDirLight = keyDirLight; + gameMgrCmp->GameEntities.PushBack(keyDirLight); +} + +void GameManagerSystem::PostFancy(PostprocessSettingsComponent* postCmp) +{ + postCmp->Exposure = 1.0f; + postCmp->DOFSize = 0.1f; + postCmp->DOFPoint = 700.0f; + postCmp->DOFRange = 800.0f; + // postCmp->DOFShow = 1.0f; + postCmp->BloomScale = 0.5f; + postCmp->AbberationScale = 0.5f; + postCmp->GrainScale = 0.1f; + postCmp->VignetteScale = 0.5f; + postCmp->MotionBlurScale = 0.5f; +} + +void GameManagerSystem::PostReset(PostprocessSettingsComponent* postCmp) +{ + postCmp->Exposure = 1.0f; + postCmp->DOFSize = 0.1f; + postCmp->DOFPoint = 500.0f; + postCmp->DOFRange = 10000.0f; + // postCmp->DOFShow = 1.0f; + postCmp->BloomScale = 0.0f; + postCmp->AbberationScale = 0.0f; + postCmp->GrainScale = 0.0f; + postCmp->VignetteScale = 0.0f; + postCmp->MotionBlurScale = 0.0f; +} + +void GameManagerSystem::PostMinimal(PostprocessSettingsComponent* postCmp) +{ + postCmp->Exposure = 1.0f; + postCmp->DOFSize = 0.1f; + postCmp->DOFPoint = 700.0f; + postCmp->DOFRange = 2000.0f; + // postCmp->DOFShow = 1.0f; + postCmp->BloomScale = 0.5f; + postCmp->AbberationScale = 0.2f; + postCmp->GrainScale = 0.1f; + postCmp->VignetteScale = 0.5f; + postCmp->MotionBlurScale = 0.5f; } void GameManagerSystem::CreatePBRShpereGrid(Scene* scene, Vector pos, Color albedo) @@ -153,7 +187,7 @@ void GameManagerSystem::CreatePBRShpereGrid(Scene* scene, Vector pos, Color albe sphereTrans.SetLocalScale(Vector(1.0f, 1.0f, 1.0f) * 20.0f); sphereTrans.SetLocalRotation(Quaternion(Vector::UNIT_Z, 90.0_deg)); // MeshRenderingComponent* meshCmp = DeferredTaskSystem::AddComponentImmediate(scene, sphere, "Models/Primitives/Sphere_HighPoly.obj", eResourceSource::GAME); - MeshRenderingComponent* meshCmp = DeferredTaskSystem::AddComponentImmediate(scene, sphere, "Models/Primitives/Sphere_LowPoly.obj", eResourceSource::GAME); + MeshRenderingComponent* meshCmp = DeferredTaskSystem::AddComponentImmediate(scene, sphere, "Models/Primitives/Sphere_HighPoly.obj", eResourceSource::GAME); size_t materialsNum = meshCmp->GetMesh()->GetSubMeshes().GetSize(); for (size_t i = 0; i < materialsNum; ++i) { @@ -174,6 +208,39 @@ void GameManagerSystem::CreatePBRShpereGrid(Scene* scene, Vector pos, Color albe } } +void GameManagerSystem::CreateRandomCubes(Scene* scene, int size, Vector pos, Vector originScatter) +{ + GameManagerWorldComponent* gameMgrCmp = scene->GetWorldComponent(); + + for (int i = 0; i < size; ++i) + { + Entity* cube = DeferredTaskSystem::SpawnEntityImmediate(scene); + MeshRenderingComponent* meshCmp = DeferredTaskSystem::AddComponentImmediate(scene, cube, "Models/Primitives/Cube.obj", eResourceSource::GAME); + meshCmp->SetMaterial(0, Material(Color::BLACK, Color::WHITE * 0.5f, 0.1f, 0.75f, 0.5f)); + + EntityTransform& sphereTrans = cube->GetTransform(); + + Vector rndRot = RandomVectorRange(-1.0f, 1.0f); + sphereTrans.SetLocalRotation( + Quaternion(Vector::UNIT_X, 180.0_deg * rndRot.X) + * Quaternion(Vector::UNIT_Y, 180.0_deg * rndRot.Y) + * Quaternion(Vector::UNIT_Z, 180.0_deg * rndRot.Z) + ); + + Vector rndPos = RandomVectorRange(-1.0f, 1.0f); + Vector offsetPos = Vector( + rndPos.X * originScatter.X, + rndPos.Y * originScatter.Y, + rndPos.Z * originScatter.Z + ); + sphereTrans.SetGlobalTranslation(pos + offsetPos + Vector::UNIT_Y * 200.0f); + + sphereTrans.SetLocalScale(Vector::ONE * 100.0f); + + gameMgrCmp->GameEntities.PushBack(cube); + } +} + void GameManagerSystem::CreateTranslucent(Scene* scene) { GameManagerWorldComponent* gameMgrCmp = scene->GetWorldComponent(); @@ -255,6 +322,8 @@ void GameManagerSystem::CreateSponzaSample(Scene* scene) void GameManagerSystem::Update(Scene* scene) { + UpdateDirLight(scene); + UpdateParticles(scene); UpdateLights(scene); @@ -268,6 +337,15 @@ void GameManagerSystem::Update(Scene* scene) // UpdatePostProcess(scene); } +void GameManagerSystem::UpdateDirLight(Poly::Scene * scene) +{ + float time = (float)(scene->GetWorldComponent()->GetGameplayTime()); + float anim = SmoothStep(-0.5f, 0.5f, Sin(0.2_rad * time)); + Angle animDeg = Lerp(60_deg, 85_deg, anim); + GameManagerWorldComponent* gameMgrCmp = scene->GetWorldComponent(); + gameMgrCmp->KeyDirLight->GetTransform().SetGlobalRotation(Quaternion(Vector::UNIT_Y, -45_deg) * Quaternion(Vector::UNIT_X, animDeg)); +} + void GameManagerSystem::UpdatePostProcess(Scene* scene) { float time = (float)(scene->GetWorldComponent()->GetGameplayTime()); @@ -293,7 +371,7 @@ void GameManagerSystem::CreatePointLights(Scene* scene, int quota) { Vector position = Vector(RandomRange(-1.0f, 1.0f)*1000.0f, RandomRange(0.0f, 800.0f), RandomRange(-1.0f, 1.0f)*500.0f); float rangeRnd = pow(RandomRange(0.0f, 1.0f), 8.0f); - float range = 100.0f + rangeRnd * 2000.0f; + float range = 100.0f + rangeRnd * 100.0f; Entity* lightEntity = CreatePointLight(scene, position, range); gameMgrCmp->LightsStartPositions.PushBack(position); @@ -424,10 +502,10 @@ void GameManagerSystem::CreateParticles(Scene* scene) GameManagerWorldComponent* gameMgrCmp = scene->GetWorldComponent(); Vector particlesPosition = Vector(0.0f, 0.0f, 0.0f); // gameMgrCmp->particleDefault = SpawnEmitterDefault(scene, particlesPosition); - gameMgrCmp->particleWorldSpace = SpawnEmitterWorldSpace(scene, particlesPosition); - gameMgrCmp->particleLocalSpace = SpawnEmitterLocalSpace(scene, particlesPosition); + // gameMgrCmp->particleWorldSpace = SpawnEmitterWorldSpace(scene, particlesPosition); + // gameMgrCmp->particleLocalSpace = SpawnEmitterLocalSpace(scene, particlesPosition); gameMgrCmp->particleAmbient = SpawnEmitterAmbient(scene, particlesPosition); - gameMgrCmp->particleAmbientWind = SpawnEmitterAmbientWind(scene, particlesPosition); + // gameMgrCmp->particleAmbientWind = SpawnEmitterAmbientWind(scene, particlesPosition); // SpawnHeartSystem(scene); } diff --git a/SandboxRendering/SandboxRendering/Src/GameManagerSystem.hpp b/SandboxRendering/SandboxRendering/Src/GameManagerSystem.hpp index aa2800e..42c93cf 100644 --- a/SandboxRendering/SandboxRendering/Src/GameManagerSystem.hpp +++ b/SandboxRendering/SandboxRendering/Src/GameManagerSystem.hpp @@ -9,48 +9,54 @@ using namespace Poly; namespace GameManagerSystem { - void CreateScene(Scene* world); + void CreateScene(Scene* scene); - Entity* CreateModel(Scene* world, String path); + Entity* CreateModel(Scene* scene, String path); - void CreateTextUI(Scene* world); - void CreateCamera(Scene* world); - void CreatePBRShpereGrid(Scene* world, Vector pos, Color albedo); - void CreateTranslucent(Scene* world); - void CreateSponza(Scene* world); - void CreateSponzaSample(Scene* world); + void CreateTextUI(Scene* scene); + void CreateCamera(Scene* scene); + void CreateRandomCubes(Scene* scene, int size, Vector pos, Vector originScatter); + void CreatePBRShpereGrid(Scene* scene, Vector pos, Color albedo); + void CreateTranslucent(Scene* scene); + void CreateSponza(Scene* scene); + void CreateSponzaSample(Scene* scene); + + void PostFancy(PostprocessSettingsComponent* postCmp); + void PostReset(PostprocessSettingsComponent* postCmp); + void PostMinimal(PostprocessSettingsComponent* postCmp); - void CreatePointLights(Scene* world, int quota); - Entity* CreatePointLight(Scene* world, Vector& position, float Range); - void CreateSpotLight(Scene* world, float Range); - void CreateParticles(Scene* world); - void SpawnSpritesSheets(Scene* world); - void SpawnShaderball(Scene* world); - - void SpawnSpritesheet11(Scene* world, Vector pos); - void SpawnSpritesheet22(Scene* world, Vector pos); - void SpawnSpritesheet44(Scene* world, Vector pos); - void SpawnSpritesheet42(Scene* world, Vector pos); - void SpawnSpritesheet41(Scene* world, Vector pos); - void SpawnSpritesheet44Random(Scene* world, Vector pos); - void SpawnSpritesheetGandalf(Scene* world, Vector pos); - - ParticleComponent* SpawnEmitterDefault(Scene* world, Vector pos); - ParticleComponent* SpawnEmitterWorldSpace(Scene* world, Vector pos); - ParticleComponent* SpawnEmitterLocalSpace(Scene* world, Vector pos); - ParticleComponent* SpawnEmitterAmbient(Scene* world, Vector pos); - ParticleComponent* SpawnEmitterAmbientWind(Scene* world, Vector pos); - ParticleComponent* SpawnEmitterHeart(Scene* world, Vector pos); - ParticleComponent* SpawnEmitterHeartImpact(Scene* world, Vector pos); - ParticleComponent* SpawnEmitterHeartImpact2(Scene* world, Vector pos); - void SpawnHeartSystem(Scene* world); - - void Update(Scene* world); - void UpdateParticles(Scene* world); - void UpdateLights(Scene* world); - void UpdateModel(Scene* world); - void UpdatePostProcess(Scene* world); + void CreatePointLights(Scene* scene, int quota); + Entity* CreatePointLight(Scene* scene, Vector& position, float Range); + void CreateSpotLight(Scene* scene, float Range); + void CreateParticles(Scene* scene); + void SpawnSpritesSheets(Scene* scene); + void SpawnShaderball(Scene* scene); + + void SpawnSpritesheet11(Scene* scene, Vector pos); + void SpawnSpritesheet22(Scene* scene, Vector pos); + void SpawnSpritesheet44(Scene* scene, Vector pos); + void SpawnSpritesheet42(Scene* scene, Vector pos); + void SpawnSpritesheet41(Scene* scene, Vector pos); + void SpawnSpritesheet44Random(Scene* scene, Vector pos); + void SpawnSpritesheetGandalf(Scene* scene, Vector pos); + + ParticleComponent* SpawnEmitterDefault(Scene* scene, Vector pos); + ParticleComponent* SpawnEmitterWorldSpace(Scene* scene, Vector pos); + ParticleComponent* SpawnEmitterLocalSpace(Scene* scene, Vector pos); + ParticleComponent* SpawnEmitterAmbient(Scene* scene, Vector pos); + ParticleComponent* SpawnEmitterAmbientWind(Scene* scene, Vector pos); + ParticleComponent* SpawnEmitterHeart(Scene* scene, Vector pos); + ParticleComponent* SpawnEmitterHeartImpact(Scene* scene, Vector pos); + ParticleComponent* SpawnEmitterHeartImpact2(Scene* scene, Vector pos); + void SpawnHeartSystem(Scene* scene); + + void Update(Scene* scene); + void UpdateDirLight(Poly::Scene * scene); + void UpdateParticles(Scene* scene); + void UpdateLights(Scene* scene); + void UpdateModel(Scene* scene); + void UpdatePostProcess(Scene* scene); - void Deinit(Scene* world); + void Deinit(Scene* scene); } diff --git a/SandboxRendering/SandboxRendering/Src/GameManagerWorldComponent.hpp b/SandboxRendering/SandboxRendering/Src/GameManagerWorldComponent.hpp index 33c47f7..b98f1b1 100644 --- a/SandboxRendering/SandboxRendering/Src/GameManagerWorldComponent.hpp +++ b/SandboxRendering/SandboxRendering/Src/GameManagerWorldComponent.hpp @@ -18,6 +18,7 @@ class GameManagerWorldComponent : public ComponentBase } public: SafePtr Camera; + SafePtr KeyDirLight; PostprocessSettingsComponent* PostCmp; SafePtr Model; bool IsDrawingDebugMeshes = true; From 643dcf32b41cbbf086774d57e0b390e96be0d3af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20K=C5=82os?= Date: Sun, 9 Sep 2018 04:03:17 +0200 Subject: [PATCH 2/4] calculated shadow bounds AABox containing camera frustum --- .../Src/GameManagerSystem.cpp | 93 +++++++++++++++++-- .../Src/GameManagerWorldComponent.hpp | 1 + 2 files changed, 85 insertions(+), 9 deletions(-) diff --git a/SandboxRendering/SandboxRendering/Src/GameManagerSystem.cpp b/SandboxRendering/SandboxRendering/Src/GameManagerSystem.cpp index 9560896..eb2a919 100644 --- a/SandboxRendering/SandboxRendering/Src/GameManagerSystem.cpp +++ b/SandboxRendering/SandboxRendering/Src/GameManagerSystem.cpp @@ -80,18 +80,19 @@ void GameManagerSystem::CreateCamera(Scene* scene) GameManagerWorldComponent* gameMgrCmp = scene->GetWorldComponent(); Entity* camera = DeferredTaskSystem::SpawnEntityImmediate(scene); - DeferredTaskSystem::AddComponentImmediate(scene, camera, 35_deg, 1.0f, 5000.f); + CameraComponent* cameraCmp = DeferredTaskSystem::AddComponentImmediate(scene, camera, 35_deg, 1.0f, 5000.f); + cameraCmp->SetRenderingMode(eRenderingModeType::IMMEDIATE_DEBUG); + DeferredTaskSystem::AddComponentImmediate(scene, camera, 100.0f, 0.003f, 10.0f); gameMgrCmp->PostCmp = DeferredTaskSystem::AddComponentImmediate(scene, camera); // PostFancy(gameMgrCmp->PostCmp); // PostReset(gameMgrCmp->PostCmp); PostMinimal(gameMgrCmp->PostCmp); - - EntityTransform& cameraTrans = camera->GetTransform(); - cameraTrans.SetGlobalTranslation(Vector(-550.0f, 180.0f, 0.0f)); - cameraTrans.SetGlobalRotation(Quaternion(Vector::UNIT_Y, -90.0_deg) * Quaternion(Vector::UNIT_X, -10.0_deg)); + + camera->GetTransform().SetGlobalTranslation(Vector(-550.0f, 180.0f, 0.0f)); + camera->GetTransform().SetGlobalRotation(Quaternion(Vector::UNIT_Y, -90.0_deg) * Quaternion(Vector::UNIT_X, -10.0_deg)); // cameraTrans.SetGlobalRotation(Quaternion(Matrix(cameraTrans.GetGlobalTranslation(), Vector(0.0f, 0.0f, 0.0f)))); - scene->GetWorldComponent()->SetCamera(0, scene->GetComponent(camera)); + scene->GetWorldComponent()->SetCamera(0, cameraCmp); gameMgrCmp->Camera = camera; Entity* keyDirLight = DeferredTaskSystem::SpawnEntityImmediate(scene); @@ -99,6 +100,17 @@ void GameManagerSystem::CreateCamera(Scene* scene) keyDirLight->GetTransform().SetGlobalRotation(Quaternion(Vector::UNIT_Y, -45_deg) * Quaternion(Vector::UNIT_X, 75_deg)); gameMgrCmp->KeyDirLight = keyDirLight; gameMgrCmp->GameEntities.PushBack(keyDirLight); + + Entity* cameraStatic = DeferredTaskSystem::SpawnEntityImmediate(scene); + CameraComponent* cameraStaticCmp = DeferredTaskSystem::AddComponentImmediate(scene, cameraStatic, 35_deg, 1.0f, 100.f); + //cameraStatic->GetTransform().SetGlobalTranslation(Vector(-550.0f, 180.0f, 0.0f)); + //cameraStatic->GetTransform().SetGlobalRotation(Quaternion(Vector::UNIT_Y, -90.0_deg) * Quaternion(Vector::UNIT_X, -10.0_deg)); + gameMgrCmp->CameraStaticCmp = cameraStaticCmp; + gameMgrCmp->GameEntities.PushBack(cameraStatic); + + // ViewportWorldComponent* viewportCmp = scene->GetWorldComponent(); + // viewportCmp->AddViewport(AARect(Vector2f(0.0f, 0.0f), Vector2f(1.0f, 1.0f))); + // scene->GetWorldComponent()->SetCamera(1, cameraStaticCmp); } void GameManagerSystem::PostFancy(PostprocessSettingsComponent* postCmp) @@ -329,11 +341,74 @@ void GameManagerSystem::Update(Scene* scene) UpdateLights(scene); // UpdateModel(scene); + + // Vector offset = Vector(800.0f, 0.0f, 0.0f); + // Vector offset = Vector(0.0f, 0.0f, 0.0f); + // DebugDrawSystem::DrawLine(scene, offset, offset + Vector::UNIT_Y * 1000.0f, Color::RED); + // DebugDrawSystem::DrawBox(scene, offset + Vector(-100.0f, 0.0f, -100.0f), offset + Vector(100.0f, 200.0f, 100.0f), Color::RED); + + GameManagerWorldComponent* gameMgrCmp = scene->GetWorldComponent(); + Optional frustum = gameMgrCmp->CameraStaticCmp->GetCameraFrustum(); + EntityTransform& cameraStaticTrans = gameMgrCmp->CameraStaticCmp->GetOwner()->GetTransform(); + + gameMgrCmp->CameraStaticCmp->UpdateProjection(); - Vector offset = Vector(800.0f, 0.0f, 0.0f); - DebugDrawSystem::DrawLine(scene, offset, offset + Vector::UNIT_Y * 1000.0f, Color::RED); - DebugDrawSystem::DrawBox(scene, offset + Vector(-100.0f, 0.0f, -100.0f), offset + Vector(100.0f, 200.0f, 100.0f), Color::RED); + if (frustum.HasValue()) + { + DebugDrawSystem::DrawFrustum(scene, frustum.Value(), + cameraStaticTrans.GetGlobalTranslation() + ); + } + Dynarray cornersInNDC = Dynarray { + Vector(-1.0f, -1.0f, 1.0f), + Vector(-1.0f, 1.0f, 1.0f), + Vector(-1.0f, -1.0f, -1.0f), + Vector(-1.0f, 1.0f, -1.0f), + Vector( 1.0f, -1.0f, 1.0f), + Vector( 1.0f, 1.0f, 1.0f), + Vector( 1.0f, -1.0f, -1.0f), + Vector( 1.0f, 1.0f, -1.0f) + }; + + Matrix ClipFromView; + ClipFromView.SetPerspective( 35_deg, 1280.0f / 720.0f, 1.0f, 100.0f ); // 35_deg, 1.0f, 100.f + Matrix ViewFromWorld = cameraStaticTrans.GetWorldFromModel().GetInversed(); + Matrix ClipFromWorld = ClipFromView * ViewFromWorld; + Matrix WorldFromClip = ClipFromWorld.GetInversed(); + + Dynarray cornersInWorld; + for (Vector clip : cornersInNDC) + { + Vector world = WorldFromClip * clip; + world.X /= world.W; + world.Y /= world.W; + world.Z /= world.W; + cornersInWorld.PushBack(world); + // DebugDrawSystem::DrawLine(scene, Vector::UNIT_Y * 100.0f + Vector::UNIT_X * 100.0f, world, Color::BLUE); + } + + Matrix FirLightFromWorld = gameMgrCmp->KeyDirLight->GetTransform().GetWorldFromModel().GetInversed(); + Dynarray cornersInDirLight; + for (Vector world : cornersInWorld) + { + Vector dirLight = FirLightFromWorld * world; + cornersInDirLight.PushBack(dirLight); + DebugDrawSystem::DrawLine(scene, world, dirLight, Color::GREEN); + } + + const float maxFloat = std::numeric_limits::max(); + Vector min(maxFloat, maxFloat, maxFloat); + Vector max(-maxFloat, -maxFloat, -maxFloat); + + for (Vector dirLight : cornersInDirLight) + { + min = Vector::Min(min, dirLight); + max = Vector::Max(max, dirLight); + } + + AABox dirLightAABB(min, max - min); + DebugDrawSystem::DrawBox(scene, dirLightAABB, Color::RED); // UpdatePostProcess(scene); } diff --git a/SandboxRendering/SandboxRendering/Src/GameManagerWorldComponent.hpp b/SandboxRendering/SandboxRendering/Src/GameManagerWorldComponent.hpp index b98f1b1..47c92a8 100644 --- a/SandboxRendering/SandboxRendering/Src/GameManagerWorldComponent.hpp +++ b/SandboxRendering/SandboxRendering/Src/GameManagerWorldComponent.hpp @@ -18,6 +18,7 @@ class GameManagerWorldComponent : public ComponentBase } public: SafePtr Camera; + CameraComponent* CameraStaticCmp; SafePtr KeyDirLight; PostprocessSettingsComponent* PostCmp; SafePtr Model; From 473b4a381e41b11ebac7a70c0b4cd44ef88bce96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20K=C5=82os?= Date: Sun, 9 Sep 2018 04:03:50 +0200 Subject: [PATCH 3/4] comment --- SandboxRendering/SandboxRendering/Src/GameManagerSystem.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/SandboxRendering/SandboxRendering/Src/GameManagerSystem.cpp b/SandboxRendering/SandboxRendering/Src/GameManagerSystem.cpp index eb2a919..e8e282d 100644 --- a/SandboxRendering/SandboxRendering/Src/GameManagerSystem.cpp +++ b/SandboxRendering/SandboxRendering/Src/GameManagerSystem.cpp @@ -347,6 +347,8 @@ void GameManagerSystem::Update(Scene* scene) // DebugDrawSystem::DrawLine(scene, offset, offset + Vector::UNIT_Y * 1000.0f, Color::RED); // DebugDrawSystem::DrawBox(scene, offset + Vector(-100.0f, 0.0f, -100.0f), offset + Vector(100.0f, 200.0f, 100.0f), Color::RED); + // following this answer: https://gamedev.stackexchange.com/questions/73851/how-do-i-fit-the-camera-frustum-inside-directional-light-space + GameManagerWorldComponent* gameMgrCmp = scene->GetWorldComponent(); Optional frustum = gameMgrCmp->CameraStaticCmp->GetCameraFrustum(); EntityTransform& cameraStaticTrans = gameMgrCmp->CameraStaticCmp->GetOwner()->GetTransform(); @@ -409,6 +411,7 @@ void GameManagerSystem::Update(Scene* scene) AABox dirLightAABB(min, max - min); DebugDrawSystem::DrawBox(scene, dirLightAABB, Color::RED); + // UpdatePostProcess(scene); } From ca781e410b46675c9b436e9f9350cbbeeefb04a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20K=C5=82os?= Date: Tue, 11 Sep 2018 01:04:44 +0200 Subject: [PATCH 4/4] shadows pt 2 test scene --- .../Src/GameManagerSystem.cpp | 91 +++---------------- 1 file changed, 13 insertions(+), 78 deletions(-) diff --git a/SandboxRendering/SandboxRendering/Src/GameManagerSystem.cpp b/SandboxRendering/SandboxRendering/Src/GameManagerSystem.cpp index e8e282d..8b2bec9 100644 --- a/SandboxRendering/SandboxRendering/Src/GameManagerSystem.cpp +++ b/SandboxRendering/SandboxRendering/Src/GameManagerSystem.cpp @@ -1,5 +1,7 @@ #include "GameManagerSystem.hpp" +#include // std::min + #include "GameManagerWorldComponent.hpp" #include @@ -34,11 +36,11 @@ void GameManagerSystem::CreateScene(Scene* scene) DeferredTaskSystem::AddWorldComponentImmediate(scene, "HDR/HDR.hdr", eResourceSource::GAME); - gameMgrCmp->Model = CreateModel(scene, "Models/leather_shoes/Leather_Shoes.obj"); + // gameMgrCmp->Model = CreateModel(scene, "Models/leather_shoes/Leather_Shoes.obj"); // gameMgrCmp->Model->GetTransform().SetGlobalTranslation(Vector(300.0f, 0.0f, 0.0f)); - CreateModel(scene, "Models/Primitives/Cube.obj")->GetTransform().SetGlobalScale(Vector(5000.0f, 1.0f, 5000.0f)); - CreateRandomCubes(scene, 100, Vector(0.0f, 0.0f, 0.0f), Vector(2000.0f, 1.0f, 2000.0f)); + CreateModel(scene, "Models/Primitives/Cube.obj")->GetTransform().SetGlobalScale(Vector(4000.0f, 1.0f, 4000.0f)); + CreateRandomCubes(scene, 100, Vector(0.0f, 0.0f, 0.0f), Vector(2000.0f, 100.0f, 2000.0f)); // CreatePBRShpereGrid(scene, Vector(0.0f, 0.0f, 0.0f), Color(0.0f, 0.0f, 0.0f, 1.0f)); // CreatePBRShpereGrid(scene, Vector(-300.0f, 0.0f, 0.0f), Color(0.5f, 0.5f, 0.5f, 1.0f)); @@ -230,10 +232,10 @@ void GameManagerSystem::CreateRandomCubes(Scene* scene, int size, Vector pos, Ve MeshRenderingComponent* meshCmp = DeferredTaskSystem::AddComponentImmediate(scene, cube, "Models/Primitives/Cube.obj", eResourceSource::GAME); meshCmp->SetMaterial(0, Material(Color::BLACK, Color::WHITE * 0.5f, 0.1f, 0.75f, 0.5f)); - EntityTransform& sphereTrans = cube->GetTransform(); + EntityTransform& cubeTrans = cube->GetTransform(); Vector rndRot = RandomVectorRange(-1.0f, 1.0f); - sphereTrans.SetLocalRotation( + cubeTrans.SetLocalRotation( Quaternion(Vector::UNIT_X, 180.0_deg * rndRot.X) * Quaternion(Vector::UNIT_Y, 180.0_deg * rndRot.Y) * Quaternion(Vector::UNIT_Z, 180.0_deg * rndRot.Z) @@ -245,9 +247,9 @@ void GameManagerSystem::CreateRandomCubes(Scene* scene, int size, Vector pos, Ve rndPos.Y * originScatter.Y, rndPos.Z * originScatter.Z ); - sphereTrans.SetGlobalTranslation(pos + offsetPos + Vector::UNIT_Y * 200.0f); + cubeTrans.SetGlobalTranslation(pos + offsetPos + Vector::UNIT_Y * 200.0f); - sphereTrans.SetLocalScale(Vector::ONE * 100.0f); + cubeTrans.SetLocalScale(Vector::ONE * 100.0f); gameMgrCmp->GameEntities.PushBack(cube); } @@ -334,7 +336,7 @@ void GameManagerSystem::CreateSponzaSample(Scene* scene) void GameManagerSystem::Update(Scene* scene) { - UpdateDirLight(scene); + // UpdateDirLight(scene); UpdateParticles(scene); @@ -346,82 +348,15 @@ void GameManagerSystem::Update(Scene* scene) // Vector offset = Vector(0.0f, 0.0f, 0.0f); // DebugDrawSystem::DrawLine(scene, offset, offset + Vector::UNIT_Y * 1000.0f, Color::RED); // DebugDrawSystem::DrawBox(scene, offset + Vector(-100.0f, 0.0f, -100.0f), offset + Vector(100.0f, 200.0f, 100.0f), Color::RED); - - // following this answer: https://gamedev.stackexchange.com/questions/73851/how-do-i-fit-the-camera-frustum-inside-directional-light-space - - GameManagerWorldComponent* gameMgrCmp = scene->GetWorldComponent(); - Optional frustum = gameMgrCmp->CameraStaticCmp->GetCameraFrustum(); - EntityTransform& cameraStaticTrans = gameMgrCmp->CameraStaticCmp->GetOwner()->GetTransform(); - - gameMgrCmp->CameraStaticCmp->UpdateProjection(); - - if (frustum.HasValue()) - { - DebugDrawSystem::DrawFrustum(scene, frustum.Value(), - cameraStaticTrans.GetGlobalTranslation() - ); - } - - Dynarray cornersInNDC = Dynarray { - Vector(-1.0f, -1.0f, 1.0f), - Vector(-1.0f, 1.0f, 1.0f), - Vector(-1.0f, -1.0f, -1.0f), - Vector(-1.0f, 1.0f, -1.0f), - Vector( 1.0f, -1.0f, 1.0f), - Vector( 1.0f, 1.0f, 1.0f), - Vector( 1.0f, -1.0f, -1.0f), - Vector( 1.0f, 1.0f, -1.0f) - }; - - Matrix ClipFromView; - ClipFromView.SetPerspective( 35_deg, 1280.0f / 720.0f, 1.0f, 100.0f ); // 35_deg, 1.0f, 100.f - Matrix ViewFromWorld = cameraStaticTrans.GetWorldFromModel().GetInversed(); - Matrix ClipFromWorld = ClipFromView * ViewFromWorld; - Matrix WorldFromClip = ClipFromWorld.GetInversed(); - - Dynarray cornersInWorld; - for (Vector clip : cornersInNDC) - { - Vector world = WorldFromClip * clip; - world.X /= world.W; - world.Y /= world.W; - world.Z /= world.W; - cornersInWorld.PushBack(world); - // DebugDrawSystem::DrawLine(scene, Vector::UNIT_Y * 100.0f + Vector::UNIT_X * 100.0f, world, Color::BLUE); - } - - Matrix FirLightFromWorld = gameMgrCmp->KeyDirLight->GetTransform().GetWorldFromModel().GetInversed(); - Dynarray cornersInDirLight; - for (Vector world : cornersInWorld) - { - Vector dirLight = FirLightFromWorld * world; - cornersInDirLight.PushBack(dirLight); - DebugDrawSystem::DrawLine(scene, world, dirLight, Color::GREEN); - } - - const float maxFloat = std::numeric_limits::max(); - Vector min(maxFloat, maxFloat, maxFloat); - Vector max(-maxFloat, -maxFloat, -maxFloat); - - for (Vector dirLight : cornersInDirLight) - { - min = Vector::Min(min, dirLight); - max = Vector::Max(max, dirLight); - } - - AABox dirLightAABB(min, max - min); - DebugDrawSystem::DrawBox(scene, dirLightAABB, Color::RED); - - // UpdatePostProcess(scene); } void GameManagerSystem::UpdateDirLight(Poly::Scene * scene) { float time = (float)(scene->GetWorldComponent()->GetGameplayTime()); - float anim = SmoothStep(-0.5f, 0.5f, Sin(0.2_rad * time)); - Angle animDeg = Lerp(60_deg, 85_deg, anim); + float anim = SmoothStep(-0.8f, 0.8f, Sin(0.2_rad * time)); + Angle animDeg = Lerp(0_deg, 90_deg, anim); GameManagerWorldComponent* gameMgrCmp = scene->GetWorldComponent(); - gameMgrCmp->KeyDirLight->GetTransform().SetGlobalRotation(Quaternion(Vector::UNIT_Y, -45_deg) * Quaternion(Vector::UNIT_X, animDeg)); + gameMgrCmp->KeyDirLight->GetTransform().SetGlobalRotation(Quaternion(Vector::UNIT_X, animDeg)); } void GameManagerSystem::UpdatePostProcess(Scene* scene)