117 lines
4.4 KiB
C++
117 lines
4.4 KiB
C++
#include "HydraSpotShadowSourceSystem.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/renderer/HydraCreateCascadeShadowFrameBuffer.hpp"
|
|
#include "../../task/renderer/HydraCreateShadowFrameBuffer.hpp"
|
|
#include "../../actor/HydraActorManager.h"
|
|
|
|
void HydraSpotShadowSourceSystem::Initialise()
|
|
{
|
|
|
|
}
|
|
|
|
void HydraSpotShadowSourceSystem::InitialiseComponent(HydraID entity_id)
|
|
{
|
|
HydraEngine *engine = GetEngine();
|
|
HydraECS *ecs = engine->ECS();
|
|
HydraLightComponent &light_comp = ecs->GetComponent<HydraLightComponent>(entity_id);
|
|
|
|
|
|
HydraSpotShadowSourceComponent& shadow_comp = ecs->GetComponent<HydraSpotShadowSourceComponent>(entity_id);
|
|
|
|
HydraID shadow_id = GetEngine()->ShadowBufferManager()->AddSpotShadow(shadow_comp);
|
|
assert(shadow_id != INVALID_HYDRA_ID && "Add Spot Shadow - out of shadows!");
|
|
|
|
light_comp.shadow_map_id = shadow_id;
|
|
light_comp.shadow_caster = 1;
|
|
shadow_comp.shadow_id = shadow_id;
|
|
_InitialiseShadowMap(entity_id);
|
|
|
|
}
|
|
|
|
void HydraSpotShadowSourceSystem::UpdateSystem(float delta_t_seconds)
|
|
{
|
|
HydraEngine *engine = GetEngine();
|
|
HydraECS *ecs = engine->ECS();
|
|
|
|
for (HydraID entity_id : Entities)
|
|
{
|
|
_UpdateShadowMap(entity_id);
|
|
}
|
|
|
|
}
|
|
|
|
void HydraSpotShadowSourceSystem::_UpdateShadowMap(HydraID entity_id)
|
|
{
|
|
HydraEngine *engine = GetEngine();
|
|
HydraECS *ecs = engine->ECS();
|
|
HydraPositionComponent pos_comp = ecs->GetComponent<HydraPositionComponent>(entity_id);
|
|
HydraSpotShadowSourceComponent &shad_comp = ecs->GetComponent<HydraSpotShadowSourceComponent>(entity_id);
|
|
HydraLightComponent light_comp = ecs->GetComponent<HydraLightComponent>(entity_id);
|
|
|
|
float view_size = 50.0f;
|
|
float view_near = 0.3f;
|
|
float view_far = 50.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 light_pos = pos_comp.position;
|
|
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);
|
|
|
|
float fovRad = glm::radians(55.0f);
|
|
glm::mat4 proj = glm::perspectiveFovRH<float>(fovRad, view_size, view_size, view_near, view_far);
|
|
|
|
// Save data to component
|
|
shad_comp.far_plane = view_far;
|
|
shad_comp.light_space_proj = proj;
|
|
shad_comp.light_space_view = view;
|
|
GetEngine()->ShadowBufferManager()->UpdateSpotShadow(shad_comp.shadow_id, shad_comp);
|
|
}
|
|
|
|
|
|
void HydraSpotShadowSourceSystem::_InitialiseShadowMap(HydraID entity_id)
|
|
{
|
|
HydraEngine *engine = GetEngine();
|
|
HydraECS *ecs = engine->ECS();
|
|
HydraTextureBufferManager *tex_man = engine->TextureBufferManager();
|
|
HydraSpotShadowSourceComponent &shad_comp = ecs->GetComponent<HydraSpotShadowSourceComponent>(entity_id);
|
|
HydraLightComponent &light_comp = ecs->GetComponent<HydraLightComponent>(entity_id);
|
|
|
|
shad_comp.entity_id = entity_id;
|
|
|
|
HydraTaskScheduler *task_scheduler = GetEngine()->TaskScheduler();
|
|
|
|
uint32_t depth_texture_id = 0;
|
|
HydraID create_framebuffer_task_id = task_scheduler->CreateTask<HydraCreateShadowFrameBuffer>(HydraThreadAffinity::Render);
|
|
HydraCreateShadowFrameBuffer *fb_task = static_cast<HydraCreateShadowFrameBuffer *>(task_scheduler->GetTask(create_framebuffer_task_id));
|
|
fb_task->Initialise(&shad_comp.shadow_texture_id, &depth_texture_id, &shad_comp.frame_buffer_id);
|
|
|
|
task_scheduler->WaitForTask(create_framebuffer_task_id);
|
|
shad_comp.shadow_texture_id = depth_texture_id;
|
|
}
|
|
|