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 11, 2024
1 parent ff7aef1 commit a236f4e
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 25 deletions.
8 changes: 6 additions & 2 deletions engine/src/render/cascaded_shadow_maps/plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,14 @@ void cubos::engine::cascadedShadowMapsPlugin(Cubos& cubos)

cubos.system("create cascaded shadow maps")
.tagged(createCascadedShadowMapsTag)
.call([](const Window& window, Query<DirectionalShadowCaster&> query,
.call([](const Window& window, Query<DirectionalShadowCaster&, Active&> query,
Query<Entity, const Camera&, const Active&> cameras) {
for (auto [caster] : query)
for (auto [caster, active] : query)
{
if (!active.active)
{
continue;
}
// Remove shadow maps for cameras that no longer exist
std::vector<Entity> removedCameras;
for (auto& [cameraEntity, shadowMap] : caster.shadowMaps)
Expand Down
12 changes: 8 additions & 4 deletions engine/src/render/cascaded_shadow_maps_rasterizer/plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,20 +134,24 @@ void cubos::engine::cascadedShadowMapsRasterizerPlugin(Cubos& cubos)
.after(createGBufferTag)
.call(
[](State& state, const Window& window, const RenderMeshPool& pool,
Query<DirectionalShadowCaster&, const DirectionalLight&, const LocalToWorld&> lights,
Query<DirectionalShadowCaster&, const DirectionalLight&, const LocalToWorld&, const Active&> lights,
Query<Entity, const LocalToWorld&, const Camera&, const Active&, const DrawsTo&, const GBuffer&> cameras,
Query<Entity, const LocalToWorld&, const RenderMesh&, const RenderVoxelGrid&> meshes) {
auto& rd = window->renderDevice();

for (auto [cameraEntity, cameraLocalToWorld, camera, active, drawsTo, gBuffer] : cameras)
for (auto [cameraEntity, cameraLocalToWorld, camera, CameraActive, drawsTo, gBuffer] : cameras)
{
if (!active.active)
if (!CameraActive.active)
{
continue;
}

for (auto [caster, light, localToWorld] : lights)
for (auto [caster, light, localToWorld, lightActive] : lights)
{
if (!lightActive.active)
{
continue;
}
auto& shadowMap = caster.shadowMaps.at(cameraEntity);
// Check if we need to recreate the framebuffer.
if (shadowMap->previousCascades != shadowMap->cascades)
Expand Down
31 changes: 23 additions & 8 deletions engine/src/render/deferred_shading/plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,20 +207,20 @@ void cubos::engine::deferredShadingPlugin(Cubos& cubos)
.tagged(deferredShadingTag)
.call([](State& state, const Window& window, const RenderEnvironment& environment,
const ShadowAtlas& shadowAtlas,
Query<const LocalToWorld&, const DirectionalLight&, Opt<const DirectionalShadowCaster&>>
Query<const LocalToWorld&, const DirectionalLight&, const Active&, Opt<const DirectionalShadowCaster&>>
directionalLights,
Query<const LocalToWorld&, const PointLight&> pointLights,
Query<const LocalToWorld&, const SpotLight&, Opt<const SpotShadowCaster&>> spotLights,
Query<const LocalToWorld&, const PointLight&, const Active&> pointLights,
Query<const LocalToWorld&, const SpotLight&, const Active&, Opt<const SpotShadowCaster&>> spotLights,
Query<Entity, const HDR&, const GBuffer&, const SSAO&, DeferredShading&> targets,
Query<Entity, const LocalToWorld&, const Camera&, const Active&, const DrawsTo&> cameras) {
auto& rd = window->renderDevice();

for (auto [targetEnt, hdr, gBuffer, ssao, deferredShading] : targets)
{
// Find the cameras that draw to the GBuffer.
for (auto [cameraEntity, localToWorld, camera, active, drawsTo] : cameras.pin(1, targetEnt))
for (auto [cameraEntity, localToWorld, camera, cameraActive, drawsTo] : cameras.pin(1, targetEnt))
{
if (!active.active)
if (!cameraActive.active)
{
continue;
}
Expand Down Expand Up @@ -257,8 +257,13 @@ void cubos::engine::deferredShadingPlugin(Cubos& cubos)

int directionalLightIndex = 0;
Texture2DArray directionalShadowMap = nullptr;
for (auto [lightLocalToWorld, light, caster] : directionalLights)
for (auto [lightLocalToWorld, light, lightActive, caster] : directionalLights)
{
if (!lightActive.active)
{
continue;
}

auto& perLight = perScene.directionalLights[perScene.numDirectionalLights++];
perLight.direction = glm::normalize(lightLocalToWorld.mat * glm::vec4(0.0F, 0.0F, 1.0F, 0.0F));
perLight.color = glm::vec4(light.color, 1.0F);
Expand Down Expand Up @@ -338,17 +343,27 @@ void cubos::engine::deferredShadingPlugin(Cubos& cubos)
directionalLightIndex++;
}

for (auto [lightLocalToWorld, light] : pointLights)
for (auto [lightLocalToWorld, light, lightActive] : pointLights)
{
if (!lightActive.active)
{
continue;
}

auto& perLight = perScene.pointLights[perScene.numPointLights++];
perLight.position = lightLocalToWorld.mat * glm::vec4(0.0F, 0.0F, 0.0F, 1.0F);
perLight.color = glm::vec4(light.color, 1.0F);
perLight.intensity = light.intensity;
perLight.range = light.range;
}

for (auto [lightLocalToWorld, light, caster] : spotLights)
for (auto [lightLocalToWorld, light, lightActive, caster] : spotLights)
{
if (!lightActive.active)
{
continue;
}

auto& perLight = perScene.spotLights[perScene.numSpotLights++];
perLight.position = lightLocalToWorld.mat * glm::vec4(0.0F, 0.0F, 0.0F, 1.0F);
perLight.direction = glm::normalize(lightLocalToWorld.mat * glm::vec4(0.0F, 0.0F, 1.0F, 0.0F));
Expand Down
6 changes: 4 additions & 2 deletions engine/src/render/shadow_atlas/plugin.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,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/shadow_atlas/plugin.hpp>
#include <cubos/engine/render/shadow_atlas/shadow_atlas.hpp>
#include <cubos/engine/render/shadows/plugin.hpp>
Expand Down Expand Up @@ -38,16 +39,17 @@ void cubos::engine::shadowAtlasPlugin(Cubos& cubos)

cubos.system("reserve space for shadow casters")
.tagged(reserveShadowCastersTag)
.call([](ShadowAtlas& atlas, Query<SpotShadowCaster&> casters) {
.call([](ShadowAtlas& atlas, Query<SpotShadowCaster&, const Active&> casters) {
atlas.slots.clear();
atlas.slotsMap.clear();
atlas.slots.push_back(
std::make_shared<ShadowAtlas::Slot>(glm::vec2(1.0F, 1.0F), glm::vec2(0.0F, 0.0F), -1));

int id = 1;

for (auto [caster] : casters)
for (auto [caster, active] : casters)
{

bool foundSlot = false;
caster.baseSettings.id = id++;
for (const auto& slot : atlas.slots)
Expand Down
10 changes: 8 additions & 2 deletions engine/src/render/shadow_atlas_rasterizer/plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <cubos/core/reflection/external/primitives.hpp>

#include <cubos/engine/assets/plugin.hpp>
#include <cubos/engine/render/active/plugin.hpp>
#include <cubos/engine/render/lights/plugin.hpp>
#include <cubos/engine/render/lights/spot.hpp>
#include <cubos/engine/render/mesh/mesh.hpp>
Expand Down Expand Up @@ -124,7 +125,7 @@ void cubos::engine::shadowAtlasRasterizerPlugin(Cubos& cubos)
.tagged(drawToShadowAtlasTag)
.call([](State& state, const Window& window, const RenderMeshPool& pool, ShadowAtlas& atlas,
ShadowAtlasRasterizer& rasterizer,
Query<const SpotShadowCaster&, const SpotLight&, const LocalToWorld&> lights,
Query<const SpotShadowCaster&, const SpotLight&, const LocalToWorld&, const Active&> lights,
Query<Entity, const LocalToWorld&, const RenderMesh&, const RenderVoxelGrid&> meshes) {
auto& rd = window->renderDevice();

Expand Down Expand Up @@ -159,8 +160,13 @@ void cubos::engine::shadowAtlasRasterizerPlugin(Cubos& cubos)
atlas.cleared = true;
}

for (auto [caster, light, localToWorld] : lights)
for (auto [caster, light, localToWorld, active] : lights)
{
if (!active.active)
{
continue;
}

// Get light viewport
auto slot = atlas.slotsMap.at(caster.baseSettings.id);

Expand Down
7 changes: 0 additions & 7 deletions engine/src/render/ssao/plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,6 @@
#include <cubos/engine/render/camera/plugin.hpp>
#include <cubos/engine/render/g_buffer/g_buffer.hpp>
#include <cubos/engine/render/g_buffer/plugin.hpp>
#include <cubos/engine/render/hdr/hdr.hpp>
#include <cubos/engine/render/hdr/plugin.hpp>
#include <cubos/engine/render/lights/directional.hpp>
#include <cubos/engine/render/lights/environment.hpp>
#include <cubos/engine/render/lights/plugin.hpp>
#include <cubos/engine/render/lights/point.hpp>
#include <cubos/engine/render/lights/spot.hpp>
#include <cubos/engine/render/shader/plugin.hpp>
#include <cubos/engine/render/ssao/plugin.hpp>
#include <cubos/engine/render/ssao/ssao.hpp>
Expand Down

0 comments on commit a236f4e

Please sign in to comment.