Skip to content

Commit

Permalink
feat(engine): implement active component for cameras and light
Browse files Browse the repository at this point in the history
  • Loading branch information
RodrigoVintem committed Nov 10, 2024
1 parent 69c606d commit ff7aef1
Show file tree
Hide file tree
Showing 16 changed files with 246 additions and 141 deletions.
2 changes: 2 additions & 0 deletions engine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ set(CUBOS_ENGINE_SOURCE
"src/render/shadow_atlas_rasterizer/shadow_atlas_rasterizer.cpp"
"src/render/cascaded_shadow_maps/plugin.cpp"
"src/render/cascaded_shadow_maps_rasterizer/plugin.cpp"
"src/render/active/plugin.cpp"
"src/render/active/active.cpp"

"src/tools/settings_inspector/plugin.cpp"
"src/tools/selection/plugin.cpp"
Expand Down
20 changes: 20 additions & 0 deletions engine/include/cubos/engine/render/active/active.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/// @file
/// @brief Component @ref cubos::engine::Active
/// @ingroup render-active-plugin
#pragma once

#include <cubos/core/reflection/reflect.hpp>

namespace cubos::engine
{
/// @brief Component which stores the active state for an entity.
/// @note Added automatically once a specific camera or light is added.
/// @ingroup render-active-plugin
struct Active
{
CUBOS_REFLECT;

/// @brief Whether the entity is active.
bool active = true;
};
} // namespace cubos::engine
10 changes: 10 additions & 0 deletions engine/include/cubos/engine/render/active/plugin.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#pragma once

#include <cubos/engine/api.hpp>
#include <cubos/engine/prelude.hpp>
#include <cubos/engine/render/active/active.hpp>

namespace cubos::engine
{
CUBOS_ENGINE_API void activePlugin(Cubos& cubos);
}
1 change: 0 additions & 1 deletion engine/include/cubos/engine/render/camera/camera.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ namespace cubos::engine
CUBOS_REFLECT;

/// @brief Whether the camera is drawing to a target.
bool active{true};

/// @brief Projection matrix of the camera.
glm::mat4 projection{};
Expand Down
9 changes: 9 additions & 0 deletions engine/src/render/active/active.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include <cubos/core/ecs/reflection.hpp>
#include <cubos/core/reflection/external/primitives.hpp>

#include <cubos/engine/render/active/active.hpp>

CUBOS_REFLECT_IMPL(cubos::engine::Active)
{
return core::ecs::TypeBuilder<Active>("cubos::engine::Active").withField("active", &Active::active).build();
}
7 changes: 7 additions & 0 deletions engine/src/render/active/plugin.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include <cubos/engine/render/active/active.hpp>
#include <cubos/engine/render/active/plugin.hpp>

void cubos::engine::activePlugin(Cubos& cubos)
{
cubos.component<Active>();
}
1 change: 0 additions & 1 deletion engine/src/render/camera/camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
CUBOS_REFLECT_IMPL(cubos::engine::Camera)
{
return core::ecs::TypeBuilder<Camera>("cubos::engine::Camera")
.withField("active", &Camera::active)
.withField("projection", &Camera::projection)
.withField("zNear", &Camera::zNear)
.withField("zFar", &Camera::zFar)
Expand Down
13 changes: 13 additions & 0 deletions engine/src/render/camera/plugin.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include <glm/gtc/matrix_transform.hpp>

#include <cubos/engine/render/active/active.hpp>
#include <cubos/engine/render/active/plugin.hpp>
#include <cubos/engine/render/camera/camera.hpp>
#include <cubos/engine/render/camera/draws_to.hpp>
#include <cubos/engine/render/camera/orthographic.hpp>
Expand All @@ -13,13 +15,24 @@ using namespace cubos::engine;
void cubos::engine::cameraPlugin(Cubos& cubos)
{
cubos.depends(renderTargetPlugin);
cubos.depends(activePlugin);

cubos.component<Camera>();
cubos.component<PerspectiveCamera>();
cubos.component<OrthographicCamera>();

cubos.relation<DrawsTo>();

cubos.observer("add active component on add Camera")
.onAdd<Camera>()
.without<Active>()
.call([](Commands cmds, Query<Entity> query) {
for (auto [ent] : query)
{
cmds.add(ent, Active{});
}
});

cubos.observer("add Camera on add PerspectiveCamera")
.onAdd<PerspectiveCamera>()
.without<Camera>()
Expand Down
12 changes: 7 additions & 5 deletions engine/src/render/cascaded_shadow_maps/plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <cubos/core/io/window.hpp>
#include <cubos/core/reflection/external/primitives.hpp>

#include <cubos/engine/render/active/plugin.hpp>
#include <cubos/engine/render/camera/camera.hpp>
#include <cubos/engine/render/camera/plugin.hpp>
#include <cubos/engine/render/cascaded_shadow_maps/plugin.hpp>
Expand All @@ -26,7 +27,8 @@ void cubos::engine::cascadedShadowMapsPlugin(Cubos& cubos)

cubos.system("create cascaded shadow maps")
.tagged(createCascadedShadowMapsTag)
.call([](const Window& window, Query<DirectionalShadowCaster&> query, Query<Entity, const Camera&> cameras) {
.call([](const Window& window, Query<DirectionalShadowCaster&> query,
Query<Entity, const Camera&, const Active&> cameras) {
for (auto [caster] : query)
{
// Remove shadow maps for cameras that no longer exist
Expand All @@ -38,9 +40,9 @@ void cubos::engine::cascadedShadowMapsPlugin(Cubos& cubos)
removedCameras.push_back(cameraEntity);
}
}
for (auto [entity, camera] : cameras)
for (auto [entity, camera, active] : cameras)
{
if (!camera.active && caster.shadowMaps.contains(entity))
if (!active.active && caster.shadowMaps.contains(entity))
{
removedCameras.push_back(entity);
}
Expand All @@ -53,9 +55,9 @@ void cubos::engine::cascadedShadowMapsPlugin(Cubos& cubos)
caster.updateShadowMaps(window->renderDevice());

// Create shadow maps for new cameras
for (auto [entity, camera] : cameras)
for (auto [entity, camera, active] : cameras)
{
if (camera.active && !caster.shadowMaps.contains(entity))
if (active.active && !caster.shadowMaps.contains(entity))
{
caster.shadowMaps[entity] = std::make_shared<DirectionalShadowCaster::ShadowMap>();
caster.shadowMaps[entity]->resize(window->renderDevice(), caster.size,
Expand Down
Loading

0 comments on commit ff7aef1

Please sign in to comment.