Spot started

This commit is contained in:
Confideo-IOM
2026-08-04 22:40:48 +01:00
parent f51b435a9d
commit 6d7d7571b5
48 changed files with 1086 additions and 1222 deletions
@@ -0,0 +1,16 @@
#ifndef HYDRASPOTSHADOWCOMPONENT
#define HYDRASPOTSHADOWCOMPONENT
#include "../../engine/HydraEngine.h"
struct HydraSpotShadowSourceComponent
{
uint32_t entity_id;
uint32_t shadow_texture_id;
uint32_t frame_buffer_id;
float far_plane;
glm::mat4 light_space_proj;
glm::mat4 light_space_view;
};
#endif /* HYDRASPOTSHADOWCOMPONENT */
@@ -50,12 +50,8 @@ void HydraDirectionalShadowSourceSystem::InitialiseComponent(HydraID entity_id)
light_comp.shadow_caster = 1;
}
switch (light_comp.light_type)
{
case (int)HYDRA_LIGHT_TYPE::HYDRA_LIGHT_DIRECTIONAL:
_InitialiseDirectionalLight(entity_id);
break;
}
_InitialiseShadowMap(entity_id);
}
@@ -68,12 +64,8 @@ void HydraDirectionalShadowSourceSystem::UpdateSystem(float delta_t_seconds)
{
HydraLightComponent &light_comp = ecs->GetComponent<HydraLightComponent>(entity_id);
switch (light_comp.light_type)
{
case (int)HYDRA_LIGHT_TYPE::HYDRA_LIGHT_DIRECTIONAL:
_UpdateDirectionalLight(entity_id);
break;
}
_UpdateShadowMap(entity_id);
HydraDirectionalShadowSourceComponent shad_comp = ecs->GetComponent<HydraDirectionalShadowSourceComponent>(entity_id);
m_shadows[light_comp.shadow_map_id] = shad_comp;
@@ -81,19 +73,16 @@ void HydraDirectionalShadowSourceSystem::UpdateSystem(float delta_t_seconds)
_WriteToGPU();
}
void HydraDirectionalShadowSourceSystem::_UpdateDirectionalLight(HydraID entity_id)
void HydraDirectionalShadowSourceSystem::_UpdateShadowMap(HydraID entity_id)
{
HydraEngine *engine = GetEngine();
HydraECS *ecs = engine->ECS();
HydraPositionComponent pos_comp = ecs->GetComponent<HydraPositionComponent>(entity_id);
HydraDirectionalShadowSourceComponent &shad_comp = ecs->GetComponent<HydraDirectionalShadowSourceComponent>(entity_id);
float view_size = 20.0f;
float view_size = 4.0f;
float view_near = 0.1f;
float view_far = 20.0f;
float PI = 3.1415927;
float TWO_PI = 6.2831854;
float view_far = 4.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)));
@@ -102,7 +91,7 @@ void HydraDirectionalShadowSourceSystem::_UpdateDirectionalLight(HydraID entity_
for (uint32_t i = 0; i < 6; i++)
{
glm::vec3 light_pos = camera_pos - (light_dir * 2.0f);
glm::vec3 light_pos = camera_pos;//- (light_dir * 2.0f);
glm::vec3 target_pos = light_pos + light_dir;
glm::vec3 up_vector = glm::vec3(0.0f, 1.0f, 0.0f);
@@ -121,20 +110,19 @@ void HydraDirectionalShadowSourceSystem::_UpdateDirectionalLight(HydraID entity_
shad_comp.light_space_view[i] = view;
// Scale bounds exponentially for the next cascade tier
view_size *= 3.5f;
view_far *= 3.5f;
view_size *= 3.0f;
view_far *= 3.0f;
}
}
void HydraDirectionalShadowSourceSystem::_InitialiseDirectionalLight(HydraID entity_id)
void HydraDirectionalShadowSourceSystem::_InitialiseShadowMap(HydraID entity_id)
{
HydraEngine *engine = GetEngine();
HydraECS *ecs = engine->ECS();
HydraTextureBufferManager *tex_man = engine->TextureBufferManager();
HydraDirectionalShadowSourceComponent &shad_comp = ecs->GetComponent<HydraDirectionalShadowSourceComponent>(entity_id);
HydraLightComponent &light_comp = ecs->GetComponent<HydraLightComponent>(entity_id);
shad_comp.entity_id = entity_id;
shad_comp.cascade_count = 6;
@@ -16,8 +16,8 @@ class HydraDirectionalShadowSourceSystem : public HydraSystem
void InitialiseComponent(HydraID entity_id) override;
void UpdateSystem(float delta_t_seconds) override;
private:
void _UpdateDirectionalLight(HydraID entity_id);
void _InitialiseDirectionalLight(HydraID entity_id);
void _UpdateShadowMap(HydraID entity_id);
void _InitialiseShadowMap(HydraID entity_id);
void _WriteToGPU();
HydraID m_shadow_buffer_id = INVALID_HYDRA_ID;
std::queue<HydraID> m_available_shadows;
@@ -0,0 +1,147 @@
#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 "../../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()
{
HydraTaskScheduler *task_scheduler = GetEngine()->TaskScheduler();
for(uint32_t i = 0; i < MAX_SHADOWS; i++)
{
m_available_shadows.push(i);
}
m_shadows.resize(MAX_SHADOWS);
HydraID create_gpu_buffer_task_id = task_scheduler->CreateTask<HydraCreateGPUBufferTask>(HydraThreadAffinity::Render);
HydraCreateGPUBufferTask *task = static_cast<HydraCreateGPUBufferTask *>(task_scheduler->GetTask(create_gpu_buffer_task_id));
task->Intialise("ShadowBuffer", 1, MAX_SHADOWS * sizeof(HydraSpotShadowSourceComponent), &m_shadow_buffer_id, HYDRA_BUFFER_TYPE::HYDRA_SSBO_BUFFER);
task_scheduler->WaitForTask(create_gpu_buffer_task_id);
}
void HydraSpotShadowSourceSystem::InitialiseComponent(HydraID entity_id)
{
HydraEngine *engine = GetEngine();
HydraECS *ecs = engine->ECS();
HydraLightComponent &light_comp = ecs->GetComponent<HydraLightComponent>(entity_id);
{
std::lock_guard<std::mutex> lock(m_mutex);
assert(m_available_shadows.size() > 0 && "Run out of shadows");
uint32_t shad_id = m_available_shadows.front();
m_available_shadows.pop();
light_comp.shadow_map_id = shad_id;
light_comp.shadow_caster = 1;
}
_InitialiseShadowMap(entity_id);
}
void HydraSpotShadowSourceSystem::UpdateSystem(float delta_t_seconds)
{
HydraEngine *engine = GetEngine();
HydraECS *ecs = engine->ECS();
for (HydraID entity_id : Entities)
{
HydraLightComponent &light_comp = ecs->GetComponent<HydraLightComponent>(entity_id);
_UpdateShadowMap(entity_id);
HydraSpotShadowSourceComponent shad_comp = ecs->GetComponent<HydraSpotShadowSourceComponent>(entity_id);
m_shadows[light_comp.shadow_map_id] = shad_comp;
}
_WriteToGPU();
}
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);
float view_size = 50.0f;
float view_near = 0.1f;
float view_far = 50.0f;
float PI = 3.1415927;
float TWO_PI = 6.2831854;
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 = 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);
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 = view_far;
shad_comp.light_space_proj = proj;
shad_comp.light_space_view = view;
}
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;
}
void HydraSpotShadowSourceSystem::_WriteToGPU()
{
HydraEngine *engine = GetEngine();
HydraECS *ecs = engine->ECS();
void *buffer = static_cast<void *>(m_shadows.data());
HydraTaskScheduler *task_scheduler = GetEngine()->TaskScheduler();
HydraID update_buffer_task_id = task_scheduler->CreateTask<HydraUpdateWholeGPUBufferTask>(HydraThreadAffinity::Render);
HydraUpdateWholeGPUBufferTask *task = static_cast<HydraUpdateWholeGPUBufferTask *>(task_scheduler->GetTask(update_buffer_task_id));
task->Intialise(m_shadow_buffer_id, buffer);
task_scheduler->WaitForTask(update_buffer_task_id);
}
@@ -0,0 +1,31 @@
#ifndef HYDRASPOTSHADOWSOURCESYSTEM
#define HYDRASPOTSHADOWSOURCESYSTEM
#include "../../engine/HydraObject.h"
#include "../HydraSystem.h"
#include "../components/HydraSpotShadowSourceComponent.h"
#include <GLMIncludes.h>
#include <queue>
#include <vector>
#include <mutex>
class HydraSpotShadowSourceSystem : public HydraSystem
{
public:
virtual void Initialise() override;
protected:
void InitialiseComponent(HydraID entity_id) override;
void UpdateSystem(float delta_t_seconds) override;
private:
void _UpdateShadowMap(HydraID entity_id);
void _InitialiseShadowMap(HydraID entity_id);
void _WriteToGPU();
HydraID m_shadow_buffer_id = INVALID_HYDRA_ID;
std::queue<HydraID> m_available_shadows;
std::vector<HydraSpotShadowSourceComponent> m_shadows;
std::mutex m_mutex;
};
#endif /* HYDRASPOTSHADOWSOURCESYSTEM */