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 6, 2024
1 parent 622f012 commit b2b2d03
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 5 deletions.
2 changes: 2 additions & 0 deletions engine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,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
8 changes: 5 additions & 3 deletions engine/src/render/g_buffer_rasterizer/plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#include <cubos/core/reflection/external/uuid.hpp>

#include <cubos/engine/assets/plugin.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/plugin.hpp>
Expand Down Expand Up @@ -145,7 +147,7 @@ void cubos::engine::gBufferRasterizerPlugin(Cubos& cubos)
.with<Camera>()
.related<DrawsTo>()
.call([](State& state, const Window& window, const RenderMeshPool& pool, const RenderPalette& palette,
Assets& assets, Query<const LocalToWorld&, Camera&, const DrawsTo&> cameras,
Assets& assets, Query<const LocalToWorld&, Camera&, const Active&, const DrawsTo&> cameras,
Query<Entity, GBufferRasterizer&, GBuffer&, RenderDepth&, RenderPicker&> targets,
Query<Entity, const LocalToWorld&, const RenderMesh&, const RenderVoxelGrid&> meshes) {
auto& rd = window->renderDevice();
Expand Down Expand Up @@ -233,10 +235,10 @@ void cubos::engine::gBufferRasterizerPlugin(Cubos& cubos)
}

// Find the active cameras for this target.
for (auto [cameraLocalToWorld, camera, drawsTo] : cameras.pin(1, ent))
for (auto [cameraLocalToWorld, camera, active, drawsTo] : cameras.pin(1, ent))
{
// Skip inactive cameras.
if (!camera.active)
if (!active.active)
{
continue;
}
Expand Down
33 changes: 33 additions & 0 deletions engine/src/render/lights/plugin.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include <cubos/engine/render/active/active.hpp>
#include <cubos/engine/render/active/plugin.hpp>
#include <cubos/engine/render/lights/directional.hpp>
#include <cubos/engine/render/lights/environment.hpp>
#include <cubos/engine/render/lights/plugin.hpp>
Expand All @@ -7,8 +9,39 @@
void cubos::engine::lightsPlugin(Cubos& cubos)
{
cubos.resource<RenderEnvironment>();
cubos.depends(activePlugin);

cubos.component<DirectionalLight>();
cubos.component<PointLight>();
cubos.component<SpotLight>();

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

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

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

0 comments on commit b2b2d03

Please sign in to comment.