Skip to content

Commit

Permalink
refactor(engine): use engine lights directly
Browse files Browse the repository at this point in the history
  • Loading branch information
RiscadoA committed Sep 25, 2023
1 parent 7f3c3a7 commit 84cc9f6
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 58 deletions.
24 changes: 15 additions & 9 deletions engine/include/cubos/engine/renderer/frame.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@

#include <vector>

#include <cubos/engine/renderer/directional_light.hpp>
#include <cubos/engine/renderer/point_light.hpp>
#include <cubos/engine/renderer/renderer.hpp>
#include <cubos/engine/renderer/spot_light.hpp>

namespace cubos::engine
{
Expand Down Expand Up @@ -44,16 +47,19 @@ namespace cubos::engine
void skyGradient(glm::vec3 bottom, glm::vec3 top);

/// @brief Adds a spot light to the frame.
/// @param transform Light transform matrix.
/// @param light Spot light to add.
void light(const core::gl::SpotLight& light);
void light(glm::mat4 transform, const SpotLight& light);

/// @brief Adds a directional light to the frame.
/// @param transform Light transform matrix.
/// @param light Directional light to add.
void light(const core::gl::DirectionalLight& light);
void light(glm::mat4 transform, const DirectionalLight& light);

/// @brief Adds a point light to the frame.
/// @param transform Light transform matrix.
/// @param light Point light to add.
void light(const core::gl::PointLight& light);
void light(glm::mat4 transform, const PointLight& light);

/// @brief Clears the frame, removing all draw calls and lights.
void clear();
Expand All @@ -73,22 +79,22 @@ namespace cubos::engine

/// @brief Gets the spot lights of the frame.
/// @return Spot lights.
const std::vector<core::gl::SpotLight>& spotLights() const;
const std::vector<std::pair<glm::mat4, SpotLight>>& spotLights() const;

/// @brief Gets the directional lights of the frame.
/// @return Directional lights.
const std::vector<core::gl::DirectionalLight>& directionalLights() const;
const std::vector<std::pair<glm::mat4, DirectionalLight>>& directionalLights() const;

/// @brief Gets the point lights of the frame.
/// @return Point lights.
const std::vector<core::gl::PointLight>& pointLights() const;
const std::vector<std::pair<glm::mat4, PointLight>>& pointLights() const;

private:
glm::vec3 mAmbientColor;
glm::vec3 mSkyGradient[2];
std::vector<DrawCmd> mDrawCmds;
std::vector<core::gl::SpotLight> mSpotLights;
std::vector<core::gl::DirectionalLight> mDirectionalLights;
std::vector<core::gl::PointLight> mPointLights;
std::vector<std::pair<glm::mat4, SpotLight>> mSpotLights;
std::vector<std::pair<glm::mat4, DirectionalLight>> mDirectionalLights;
std::vector<std::pair<glm::mat4, PointLight>> mPointLights;
};
} // namespace cubos::engine
1 change: 0 additions & 1 deletion engine/include/cubos/engine/renderer/renderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

#include <cubos/core/gl/camera.hpp>
#include <cubos/core/gl/grid.hpp>
#include <cubos/core/gl/light.hpp>
#include <cubos/core/gl/palette.hpp>
#include <cubos/core/gl/render_device.hpp>
#include <cubos/core/gl/vertex.hpp>
Expand Down
15 changes: 8 additions & 7 deletions engine/src/cubos/engine/renderer/deferred_renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtx/compatibility.hpp>
#include <glm/gtx/matrix_decompose.hpp>
#include <glm/gtx/quaternion.hpp>

#include <cubos/core/gl/debug.hpp>
Expand Down Expand Up @@ -638,7 +639,7 @@ void DeferredRenderer::onRender(const Camera& camera, const RendererFrame& frame
lightData.ambientLight = glm::vec4(frame.ambient(), 1.0F);

// Spotlights.
for (const auto& light : frame.spotLights())
for (const auto& [transform, light] : frame.spotLights())
{
if (lightData.numSpotLights >= CUBOS_DEFERRED_RENDERER_MAX_SPOT_LIGHT_COUNT)
{
Expand All @@ -648,8 +649,8 @@ void DeferredRenderer::onRender(const Camera& camera, const RendererFrame& frame
break;
}

lightData.spotLights[lightData.numSpotLights].position = glm::vec4(light.position, 1.0F);
lightData.spotLights[lightData.numSpotLights].rotation = glm::toMat4(light.rotation);
lightData.spotLights[lightData.numSpotLights].position = transform * glm::vec4(0.0F, 0.0F, 0.0F, 1.0F);
lightData.spotLights[lightData.numSpotLights].rotation = glm::toMat4(glm::quat_cast(transform));
lightData.spotLights[lightData.numSpotLights].color = glm::vec4(light.color, 1.0F);
lightData.spotLights[lightData.numSpotLights].intensity = light.intensity;
lightData.spotLights[lightData.numSpotLights].range = light.range;
Expand All @@ -658,7 +659,7 @@ void DeferredRenderer::onRender(const Camera& camera, const RendererFrame& frame
}

// Directional lights.
for (const auto& light : frame.directionalLights())
for (const auto& [transform, light] : frame.directionalLights())
{
if (lightData.numDirectionalLights >= CUBOS_DEFERRED_RENDERER_MAX_DIRECTIONAL_LIGHT_COUNT)
{
Expand All @@ -668,14 +669,14 @@ void DeferredRenderer::onRender(const Camera& camera, const RendererFrame& frame
break;
}

lightData.directionalLights[lightData.numDirectionalLights].rotation = glm::toMat4(light.rotation);
lightData.directionalLights[lightData.numDirectionalLights].rotation = glm::toMat4(glm::quat_cast(transform));
lightData.directionalLights[lightData.numDirectionalLights].color = glm::vec4(light.color, 1.0F);
lightData.directionalLights[lightData.numDirectionalLights].intensity = light.intensity;
lightData.numDirectionalLights += 1;
}

// Point lights.
for (const auto& light : frame.pointLights())
for (const auto& [transform, light] : frame.pointLights())
{
if (lightData.numPointLights >= CUBOS_DEFERRED_RENDERER_MAX_POINT_LIGHT_COUNT)
{
Expand All @@ -684,7 +685,7 @@ void DeferredRenderer::onRender(const Camera& camera, const RendererFrame& frame
CUBOS_DEFERRED_RENDERER_MAX_POINT_LIGHT_COUNT);
break;
}
lightData.pointLights[lightData.numPointLights].position = glm::vec4(light.position, 1.0F);
lightData.pointLights[lightData.numPointLights].position = transform * glm::vec4(0.0F, 0.0F, 0.0F, 1.0F);
lightData.pointLights[lightData.numPointLights].color = glm::vec4(light.color, 1.0F);
lightData.pointLights[lightData.numPointLights].intensity = light.intensity;
lightData.pointLights[lightData.numPointLights].range = light.range;
Expand Down
28 changes: 11 additions & 17 deletions engine/src/cubos/engine/renderer/frame.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
#include <utility>

#include <cubos/engine/renderer/frame.hpp>

using cubos::core::gl::DirectionalLight;
using cubos::core::gl::PointLight;
using cubos::core::gl::SpotLight;
using cubos::engine::RendererFrame;
using cubos::engine::RendererGrid;
using namespace cubos::engine;

void RendererFrame::draw(RendererGrid grid, glm::mat4 modelMat)
{
mDrawCmds.push_back(DrawCmd{std::move(grid), modelMat});
mDrawCmds.push_back(DrawCmd{grid, modelMat});
}

void RendererFrame::ambient(const glm::vec3& color)
Expand All @@ -24,19 +18,19 @@ void RendererFrame::skyGradient(glm::vec3 bottom, glm::vec3 top)
mSkyGradient[1] = top;
}

void RendererFrame::light(const SpotLight& light)
void RendererFrame::light(glm::mat4 transform, const SpotLight& light)
{
mSpotLights.push_back(light);
mSpotLights.emplace_back(transform, light);
}

void RendererFrame::light(const DirectionalLight& light)
void RendererFrame::light(glm::mat4 transform, const DirectionalLight& light)
{
mDirectionalLights.push_back(light);
mDirectionalLights.emplace_back(transform, light);
}

void RendererFrame::light(const PointLight& light)
void RendererFrame::light(glm::mat4 transform, const PointLight& light)
{
mPointLights.push_back(light);
mPointLights.emplace_back(transform, light);
}

void RendererFrame::clear()
Expand All @@ -62,17 +56,17 @@ const glm::vec3& RendererFrame::skyGradient(int i) const
return mSkyGradient[i];
}

const std::vector<SpotLight>& RendererFrame::spotLights() const
const std::vector<std::pair<glm::mat4, SpotLight>>& RendererFrame::spotLights() const
{
return mSpotLights;
}

const std::vector<DirectionalLight>& RendererFrame::directionalLights() const
const std::vector<std::pair<glm::mat4, DirectionalLight>>& RendererFrame::directionalLights() const
{
return mDirectionalLights;
}

const std::vector<PointLight>& RendererFrame::pointLights() const
const std::vector<std::pair<glm::mat4, PointLight>>& RendererFrame::pointLights() const
{
return mPointLights;
}
27 changes: 3 additions & 24 deletions engine/src/cubos/engine/renderer/plugin.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtx/matrix_decompose.hpp>

#include <cubos/core/ecs/query.hpp>
#include <cubos/core/gl/camera.hpp>

Expand Down Expand Up @@ -69,41 +66,23 @@ static void frameSpotLights(Write<RendererFrame> frame, Query<Read<SpotLight>, R
{
for (auto [entity, light, localToWorld] : query)
{
auto position = localToWorld->mat * glm::vec4(0.0F, 0.0F, 0.0F, 1.0F);
frame->light(cubos::core::gl::SpotLight{
{position.x, position.y, position.z},
glm::quat_cast(localToWorld->mat),
light->color,
light->intensity,
light->range,
light->spotAngle,
});
frame->light(localToWorld->mat, *light);
}
}

static void frameDirectionalLights(Write<RendererFrame> frame, Query<Read<DirectionalLight>, Read<LocalToWorld>> query)
{
for (auto [entity, light, localToWorld] : query)
{
frame->light(cubos::core::gl::DirectionalLight{
glm::quat_cast(localToWorld->mat),
light->color,
light->intensity,
});
frame->light(localToWorld->mat, *light);
}
}

static void framePointLights(Write<RendererFrame> frame, Query<Read<PointLight>, Read<LocalToWorld>> query)
{
for (auto [entity, light, localToWorld] : query)
{
auto position = localToWorld->mat * glm::vec4(0.0F, 0.0F, 0.0F, 1.0F);
frame->light(cubos::core::gl::PointLight{
{position.x, position.y, position.z},
light->color,
light->intensity,
light->range,
});
frame->light(localToWorld->mat, *light);
}
}

Expand Down

0 comments on commit 84cc9f6

Please sign in to comment.