#include "HydraPointShadowSourceSystem.h" #include "../../engine/HydraEngine.h" #include "../../camera/HydraCamera.h" #include "../HydraECS.h" #include "../components/HydraPointShadowSourceComponent.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/HydraCreateCubemapShadowFrameBuffer.hpp" #include "../../task/renderer/HydraCreateShadowFrameBuffer.hpp" #include "../../actor/HydraActorManager.h" void HydraPointShadowSourceSystem::Initialise() { } void HydraPointShadowSourceSystem::InitialiseComponent(HydraID entity_id) { HydraEngine *engine = GetEngine(); HydraECS *ecs = engine->ECS(); HydraLightComponent &light_comp = ecs->GetComponent(entity_id); HydraPointShadowSourceComponent& shadow_comp = ecs->GetComponent(entity_id); HydraID shadow_id = GetEngine()->ShadowBufferManager()->AddPointShadow(shadow_comp); assert(shadow_id != INVALID_HYDRA_ID && "Add point Shadow - out of shadows!"); light_comp.shadow_map_id = shadow_id; light_comp.shadow_caster = 1; shadow_comp.entity_id = entity_id; //shadow_comp.shadow_id = shadow_id; //_InitialiseShadowMap(entity_id); } void HydraPointShadowSourceSystem::UpdateSystem(float delta_t_seconds) { for (HydraID entity_id : Entities) { _UpdateShadowMap(entity_id); } } void HydraPointShadowSourceSystem::DestroyComponent(HydraID entity_id) { HydraEngine *engine = GetEngine(); HydraECS *ecs = engine->ECS(); HydraPointShadowSourceComponent& shadow_comp = ecs->GetComponent(entity_id); GetEngine()->ShadowBufferManager()->DeletePointShadow(shadow_comp.shadow_id); } void HydraPointShadowSourceSystem::_UpdateShadowMap(HydraID entity_id) { HydraEngine *engine = GetEngine(); HydraECS *ecs = engine->ECS(); HydraPositionComponent pos_comp = ecs->GetComponent(entity_id); HydraPointShadowSourceComponent &shad_comp = ecs->GetComponent(entity_id); float view_size = 4.0f; float view_near = 0.1f; float view_far = 100.0f; glm::vec3 camera_pos = GetEngine()->Camera()->GetPosition(); float aspect = (float)POINT_SHADOW_MAP_SIZE/(float)POINT_SHADOW_MAP_SIZE; float width = POINT_SHADOW_MAP_SIZE; float fov = glm::radians(90.0f); glm::mat4 shadow_proj = glm::perspectiveFovLH(fov, width, width, view_near, view_far); //glm::mat4 shadow_proj = glm::perspective(fov, 1.0f, view_near, view_far); glm::vec3 light_pos = pos_comp.position; //GL_TEXTURE_CUBE_MAP_POSITIVE_X - left shad_comp.light_space_view[0] = glm::lookAt(light_pos, light_pos + glm::vec3(1.0, 0.0, 0.0), glm::vec3(0.0, 1.0, 0.0)); //GL_TEXTURE_CUBE_MAP_NEGATIVE_X - right shad_comp.light_space_view[1] = glm::lookAt(light_pos, light_pos + glm::vec3(-1.0, 0.0, 0.0), glm::vec3(0.0, 1.0, 0.0)); //GL_TEXTURE_CUBE_MAP_POSITIVE_Y - Up shad_comp.light_space_view[2] = glm::lookAt(light_pos, light_pos + glm::vec3(0.0, 1.0, 0.0), glm::vec3(0.0, 0.0, -1.0)); //GL_TEXTURE_CUBE_MAP_NEGATIVE_Y - down shad_comp.light_space_view[3] = glm::lookAt(light_pos, light_pos + glm::vec3(0.0, -1.0, 0.0), glm::vec3(0.0, 0.0, 1.0)); //GL_TEXTURE_CUBE_MAP_POSITIVE_Z - back shad_comp.light_space_view[4] = glm::lookAt(light_pos, light_pos + glm::vec3(0.0, 0.0, 1.0), glm::vec3(0.0, 1.0, 0.0)); //GL_TEXTURE_CUBE_MAP_NEGATIVE_Z - forward shad_comp.light_space_view[5] = glm::lookAt(light_pos, light_pos + glm::vec3(0.0, 0.0, -1.0), glm::vec3(0.0, 1.0, 0.0)); shad_comp.light_space_proj = shadow_proj; shad_comp.far_plane = view_far; GetEngine()->ShadowBufferManager()->UpdatePointShadow(shad_comp.shadow_id, shad_comp); } void HydraPointShadowSourceSystem::_InitialiseShadowMap(HydraID entity_id) { // HydraEngine *engine = GetEngine(); // HydraECS *ecs = engine->ECS(); // HydraTextureBufferManager *tex_man = engine->TextureBufferManager(); // HydraPointShadowSourceComponent &shad_comp = ecs->GetComponent(entity_id); // HydraLightComponent &light_comp = ecs->GetComponent(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(HydraThreadAffinity::Render); // HydraCreateCubemapShadowFrameBuffer *fb_task = static_cast(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; }