ajout du composant lumière basique

This commit is contained in:
Tom Ray
2026-04-25 00:45:43 +02:00
parent 4752c24c6e
commit 9f4740e20f
15 changed files with 297 additions and 13 deletions
+58 -1
View File
@@ -9,6 +9,7 @@
#include "../../Utils/Factory/CEntityFactory.hpp"
#include "../../Utils/Factory/ComponentFactory.hpp"
#include "Core/Component/Light/CLight.hpp"
#include "glm/ext/vector_float3.hpp"
#include "glm/gtc/type_ptr.hpp"
#include "nlohmann/json_fwd.hpp"
@@ -18,6 +19,7 @@
#include <optional>
#include <unordered_map>
#include <vector>
#include "../Systems/Graphics/Data/Light/CLightsBuffer.hpp"
namespace CosmicCore {
CScene::CScene(std::string name) : CSerializable(),
@@ -29,6 +31,7 @@ namespace CosmicCore {
m_tangibleWorld.initWorld();
m_audioWorld.initWorld();
m_scriptWorld.initWorld();
m_lightsBuffer.init();
}
@@ -58,6 +61,38 @@ namespace CosmicCore {
}
void CScene::collectLights(vk::raii::CommandBuffer& cmd, uint32_t frameIndex) {
auto& registry = m_ecManager.registry;
auto view = registry.view<CLight, CTransform>();
SLightsUBOData lightsData{};
for (auto entity : view) {
if (lightsData.lightCount >= MAX_LIGHTS) break;
auto& light = registry.get<CLight>(entity);
auto& transform = registry.get<CTransform>(entity);
SLightData data{};
glm::vec3 pos = transform.getCenter();
glm::vec3 dir = transform.getOrientation() * glm::vec3(0, -1, 0);
data.position = {pos.x, pos.y, pos.z, (float)light.getType()};
data.direction = {dir.x, dir.y, dir.z, light.getRange()};
data.color = {light.getColor().x, light.getColor().y,
light.getColor().z, light.getIntensity()};
data.innerCosAngle = glm::cos(glm::radians(light.getInnerAngle()));
data.outerCosAngle = glm::cos(glm::radians(light.getOuterAngle()));
data.attenuation = light.getAttenuation();
lightsData.lights[lightsData.lightCount++] = data;
}
// upload dans le SSBO
m_lightsBuffer.update(frameIndex, lightsData);
}
void CScene::render(vk::raii::CommandBuffer& cmd, uint32_t frameIndex){
auto entities = m_ecManager.registry.view<CRenderer, CTransform>();
VulkanImpl* api = dynamic_cast<VulkanImpl*>(GraphicsAPI::getAPI().get());
@@ -65,6 +100,8 @@ namespace CosmicCore {
auto& camera = m_ecManager.registry.get<CCamera>(cam.front());
auto& transform = m_ecManager.registry.get<CTransform>(cam.front());
collectLights(cmd, frameIndex);
camera.updateUniformBuffer(frameIndex, transform);
cmd.bindDescriptorSets(
@@ -75,6 +112,12 @@ namespace CosmicCore {
{}
);
//bind lumières (set 3)
cmd.bindDescriptorSets(vk::PipelineBindPoint::eGraphics,
api->getDefaultPipelineLayout(), 3,
*m_lightsBuffer.getDescriptorSet(frameIndex), {});
for (auto& entity : entities) {
auto model = m_ecManager.registry.get<CRenderer>(entity).getModel();
auto& transform = m_ecManager.registry.get<CTransform>(entity);
@@ -147,6 +190,7 @@ namespace CosmicCore {
nlohmann::json& renderer = components[CosmicUtils::JsonKeyRegistry::key(CosmicUtils::EJsonKeys::Component_Renderer)];
nlohmann::json& speaker = components[CosmicUtils::JsonKeyRegistry::key(CosmicUtils::EJsonKeys::Component_Speaker)];
nlohmann::json& script = components[CosmicUtils::JsonKeyRegistry::key(CosmicUtils::EJsonKeys::Component_Script)];
nlohmann::json& light = components[CosmicUtils::JsonKeyRegistry::key(CosmicUtils::EJsonKeys::Component_Light)];
auto trConfig(!transform.is_null() ? std::optional<CosmicUtils::ComponentFactory::TransformConfig>({
glm::make_vec3(transform[CosmicUtils::JsonKeyRegistry::key(CosmicUtils::EJsonKeys::Transform_Position)].get<std::vector<float>>().data()),
@@ -205,6 +249,18 @@ namespace CosmicCore {
}
}
auto lightConfig(!light.is_null() ? std::optional<CosmicUtils::ComponentFactory::LightConfig>(
{
static_cast<ELightType>(light[CosmicUtils::JsonKeyRegistry::key(CosmicUtils::EJsonKeys::Light_Type)].get<unsigned int>()),
glm::make_vec3(light[CosmicUtils::JsonKeyRegistry::key(CosmicUtils::EJsonKeys::Light_Color)].get<std::vector<float>>().data()),
light[CosmicUtils::JsonKeyRegistry::key(CosmicUtils::EJsonKeys::Light_Intensity)].get<float>(),
light[CosmicUtils::JsonKeyRegistry::key(CosmicUtils::EJsonKeys::Light_Range)].get<float>(),
light[CosmicUtils::JsonKeyRegistry::key(CosmicUtils::EJsonKeys::Light_Attenuation)].get<float>(),
light[CosmicUtils::JsonKeyRegistry::key(CosmicUtils::EJsonKeys::Light_InnerAngle)].get<float>(),
light[CosmicUtils::JsonKeyRegistry::key(CosmicUtils::EJsonKeys::Light_OuterAngle)].get<float>(),
light[CosmicUtils::JsonKeyRegistry::key(CosmicUtils::EJsonKeys::Light_CastShadow)].get<bool>()
}) : std::nullopt);
CosmicUtils::CEntityFactory::EntityConfig config = {
entityObj[CosmicUtils::JsonKeyRegistry::key(CosmicUtils::EJsonKeys::Entity_Name)],
entityObj[CosmicUtils::JsonKeyRegistry::key(CosmicUtils::EJsonKeys::Entity_Description)],
@@ -214,7 +270,8 @@ namespace CosmicCore {
colliderConfig,
rigidbodyConfig,
speakerConfig,
scriptConfig
scriptConfig,
lightConfig
};
auto entityHandle = CosmicUtils::CEntityFactory::create(scene.get(), config);