Files
HydraV3/HydraEngine/source/ecs/systems/HydraDirectionalShadowSourceSystem.cpp
T

116 lines
4.5 KiB
C++
Raw Normal View History

2026-08-04 18:32:37 +01:00
#include "HydraDirectionalShadowSourceSystem.h"
2026-07-28 00:26:04 +01:00
#include "../../engine/HydraEngine.h"
#include "../../camera/HydraCamera.h"
#include "../HydraECS.h"
2026-08-04 18:32:37 +01:00
#include "../components/HydraDirectionalShadowSourceComponent.h"
2026-07-28 00:26:04 +01:00
#include "../components/HydraPositionComponent.h"
#include "../components/HydraLightComponent.h"
#include "../../buffer/HydraShadowBuffer.h"
#include "../../buffer/HydraTextureBufferManager.h"
2026-08-06 21:11:33 +01:00
#include "../../buffer/HydraShadowBufferManager.h"
2026-07-28 00:26:04 +01:00
#include "../../scheduler/HydraTaskScheduler.h"
#include "../../task/buffer/HydraCreateGPUBufferTask.hpp"
#include "../../task/buffer/HydraUpdateWholeGPUBufferTask.hpp"
#include "../../task/buffer/HydraUpdatePartialGPUBufferTask.hpp"
#include "../../task/buffer/HydraCreateUnBoundTextureTask.hpp"
2026-08-17 21:44:00 +01:00
#include "../../task/buffer/HydraDeleteTextureTask.hpp"
2026-08-04 17:39:07 +01:00
#include "../../task/renderer/HydraCreateCascadeShadowFrameBuffer.hpp"
2026-07-28 19:02:45 +01:00
#include "../../task/renderer/HydraCreateShadowFrameBuffer.hpp"
2026-07-28 00:26:04 +01:00
#include "../../actor/HydraActorManager.h"
2026-08-17 14:12:11 +01:00
#include "../../actor/HydraTestCubeActor.h"
2026-07-28 00:26:04 +01:00
2026-08-04 18:32:37 +01:00
void HydraDirectionalShadowSourceSystem::Initialise()
2026-07-25 16:40:34 +01:00
{
2026-07-28 00:26:04 +01:00
2026-07-25 16:40:34 +01:00
}
2026-08-04 18:32:37 +01:00
void HydraDirectionalShadowSourceSystem::InitialiseComponent(HydraID entity_id)
2026-07-28 19:02:45 +01:00
{
HydraEngine *engine = GetEngine();
HydraECS *ecs = engine->ECS();
HydraLightComponent &light_comp = ecs->GetComponent<HydraLightComponent>(entity_id);
2026-08-03 22:34:52 +01:00
2026-08-06 21:11:33 +01:00
HydraDirectionalShadowSourceComponent& shadow_comp = ecs->GetComponent<HydraDirectionalShadowSourceComponent>(entity_id);
2026-08-04 22:40:48 +01:00
2026-08-18 16:23:20 +01:00
//light_comp.shadow_map_id = shadow_id;
light_comp.shadow_caster = 1;
//shadow_comp.shadow_id = shadow_id;
shadow_comp.entity_id = entity_id;
2026-08-06 21:11:33 +01:00
HydraID shadow_id = GetEngine()->ShadowBufferManager()->AddDirectionalShadow(shadow_comp);
assert(shadow_id != INVALID_HYDRA_ID && "Add directional Shadow - out of shadows!");
2026-08-18 16:23:20 +01:00
//_InitialiseShadowMap(entity_id);
2026-08-17 21:44:00 +01:00
}
2026-08-04 17:39:07 +01:00
2026-08-17 21:44:00 +01:00
void HydraDirectionalShadowSourceSystem::DestroyComponent(HydraID entity_id)
{
HydraEngine *engine = GetEngine();
HydraECS *ecs = engine->ECS();
HydraDirectionalShadowSourceComponent& shadow_comp = ecs->GetComponent<HydraDirectionalShadowSourceComponent>(entity_id);
2026-08-18 16:23:20 +01:00
GetEngine()->ShadowBufferManager()->DeleteDirectionalShadow(shadow_comp.shadow_id);
2026-07-25 16:40:34 +01:00
}
2026-07-28 00:26:04 +01:00
2026-08-04 18:32:37 +01:00
void HydraDirectionalShadowSourceSystem::UpdateSystem(float delta_t_seconds)
2026-07-28 00:26:04 +01:00
{
2026-07-28 19:02:45 +01:00
for (HydraID entity_id : Entities)
2026-07-28 00:26:04 +01:00
{
2026-08-04 22:40:48 +01:00
_UpdateShadowMap(entity_id);
2026-07-28 00:26:04 +01:00
}
}
2026-08-04 22:40:48 +01:00
void HydraDirectionalShadowSourceSystem::_UpdateShadowMap(HydraID entity_id)
2026-07-28 00:26:04 +01:00
{
2026-07-28 19:02:45 +01:00
HydraEngine *engine = GetEngine();
HydraECS *ecs = engine->ECS();
2026-07-28 00:26:04 +01:00
HydraPositionComponent pos_comp = ecs->GetComponent<HydraPositionComponent>(entity_id);
2026-08-04 18:32:37 +01:00
HydraDirectionalShadowSourceComponent &shad_comp = ecs->GetComponent<HydraDirectionalShadowSourceComponent>(entity_id);
2026-07-28 00:26:04 +01:00
2026-08-17 14:12:11 +01:00
float view_size = 4.0f;
float shadow_distance = 4.0f;
float max_shadow_range = 100000.0f;
2026-08-04 20:06:37 +01:00
glm::mat4 light_rot = glm::eulerAngleXYZ(pos_comp.orientation.x, pos_comp.orientation.y, pos_comp.orientation.z);
glm::vec3 light_dir = glm::normalize(glm::vec3(light_rot * glm::vec4(0.0f, 0.0f, -1.0f, 0.0f)));
2026-07-28 00:26:04 +01:00
2026-07-28 19:02:45 +01:00
glm::vec3 camera_pos = GetEngine()->Camera()->GetPosition();
2026-08-17 14:12:11 +01:00
glm::vec3 light_pos = (camera_pos - (light_dir * 1000.0f));
glm::vec3 target_pos = light_pos + light_dir;
glm::vec3 up_vector = glm::vec3(0.0f, 1.0f, 0.0f);
if (glm::abs(glm::dot(light_dir, up_vector)) > 0.99f)
{
up_vector = glm::vec3(1.0f, 0.0f, 0.0f); // Prevent gimbal lock
}
glm::mat4 view = glm::lookAt(light_pos, target_pos, up_vector);
2026-07-28 19:02:45 +01:00
for (uint32_t i = 0; i < 6; i++)
{
2026-08-17 14:12:11 +01:00
float view_near = 0.1f;//max_shadow_range - shadow_distance;
float view_far = 2000.0f;// max_shadow_range + shadow_distance;
2026-07-28 00:26:04 +01:00
2026-08-04 20:06:37 +01:00
glm::mat4 proj = glm::ortho(-view_size, view_size, -view_size, view_size, view_near, view_far);
// Save data to component
2026-08-17 14:12:11 +01:00
shad_comp.far_plane[i] = shadow_distance;
shad_comp.near_plane[i] = 0;
2026-08-04 20:06:37 +01:00
shad_comp.light_space_proj[i] = proj;
shad_comp.light_space_view[i] = view;
// Scale bounds exponentially for the next cascade tier
2026-08-04 22:40:48 +01:00
view_size *= 3.0f;
2026-08-17 14:12:11 +01:00
shadow_distance *= 3.0f;
2026-07-28 19:02:45 +01:00
}
2026-08-17 14:12:11 +01:00
// GetEngine()->ActorManager()->SetPosition(m_shadow_actor_id, light_pos);
// GetEngine()->ActorManager()->SetOrientation(m_shadow_actor_id, pos_comp.orientation.x, pos_comp.orientation.y, pos_comp.orientation.z);
2026-08-06 21:11:33 +01:00
GetEngine()->ShadowBufferManager()->UpdateDirectionalShadow(shad_comp.shadow_id, shad_comp);
2026-07-28 00:26:04 +01:00
}
2026-08-04 20:06:37 +01:00