#include "HydraDirectionalShadowSourceSystem.h" #include "../../engine/HydraEngine.h" #include "../../camera/HydraCamera.h" #include "../HydraECS.h" #include "../components/HydraDirectionalShadowSourceComponent.h" #include "../components/HydraPositionComponent.h" #include "../components/HydraLightComponent.h" #include "../../buffer/HydraShadowBuffer.h" #include "../../buffer/HydraTextureBufferManager.h" #include "../../buffer/HydraShadowBufferManager.h" #include "../../scheduler/HydraTaskScheduler.h" #include "../../task/buffer/HydraCreateGPUBufferTask.hpp" #include "../../task/buffer/HydraUpdateWholeGPUBufferTask.hpp" #include "../../task/buffer/HydraUpdatePartialGPUBufferTask.hpp" #include "../../task/buffer/HydraCreateUnBoundTextureTask.hpp" #include "../../task/buffer/HydraDeleteTextureTask.hpp" #include "../../task/renderer/HydraCreateCascadeShadowFrameBuffer.hpp" #include "../../task/renderer/HydraCreateShadowFrameBuffer.hpp" #include "../../actor/HydraActorManager.h" #include "../../actor/HydraTestCubeActor.h" void HydraDirectionalShadowSourceSystem::Initialise() { } void HydraDirectionalShadowSourceSystem::InitialiseComponent(HydraID entity_id) { HydraEngine *engine = GetEngine(); HydraECS *ecs = engine->ECS(); HydraLightComponent &light_comp = ecs->GetComponent(entity_id); HydraDirectionalShadowSourceComponent& shadow_comp = ecs->GetComponent(entity_id); //light_comp.shadow_map_id = shadow_id; light_comp.shadow_caster = 1; //shadow_comp.shadow_id = shadow_id; shadow_comp.entity_id = entity_id; HydraID shadow_id = GetEngine()->ShadowBufferManager()->AddDirectionalShadow(shadow_comp); assert(shadow_id != INVALID_HYDRA_ID && "Add directional Shadow - out of shadows!"); //_InitialiseShadowMap(entity_id); } void HydraDirectionalShadowSourceSystem::DestroyComponent(HydraID entity_id) { HydraEngine *engine = GetEngine(); HydraECS *ecs = engine->ECS(); HydraDirectionalShadowSourceComponent& shadow_comp = ecs->GetComponent(entity_id); GetEngine()->ShadowBufferManager()->DeleteDirectionalShadow(shadow_comp.shadow_id); } void HydraDirectionalShadowSourceSystem::UpdateSystem(float delta_t_seconds) { for (HydraID entity_id : Entities) { _UpdateShadowMap(entity_id); } } void HydraDirectionalShadowSourceSystem::_UpdateShadowMap(HydraID entity_id) { HydraEngine *engine = GetEngine(); HydraECS *ecs = engine->ECS(); HydraPositionComponent pos_comp = ecs->GetComponent(entity_id); HydraDirectionalShadowSourceComponent &shad_comp = ecs->GetComponent(entity_id); float view_size = 4.0f; float shadow_distance = 4.0f; float max_shadow_range = 100000.0f; 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))); glm::vec3 camera_pos = GetEngine()->Camera()->GetPosition(); 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); for (uint32_t i = 0; i < 6; i++) { float view_near = 0.1f;//max_shadow_range - shadow_distance; float view_far = 2000.0f;// max_shadow_range + shadow_distance; glm::mat4 proj = glm::ortho(-view_size, view_size, -view_size, view_size, view_near, view_far); // Save data to component shad_comp.far_plane[i] = shadow_distance; shad_comp.near_plane[i] = 0; shad_comp.light_space_proj[i] = proj; shad_comp.light_space_view[i] = view; // Scale bounds exponentially for the next cascade tier view_size *= 3.0f; shadow_distance *= 3.0f; } // 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); GetEngine()->ShadowBufferManager()->UpdateDirectionalShadow(shad_comp.shadow_id, shad_comp); }