-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rendering): add generic camera component to hold the projection …
…matrix of a specific camera
- Loading branch information
Showing
15 changed files
with
140 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/// @file | ||
/// @brief Component @ref cubos::engine::Camera | ||
/// @ingroup render-camera-plugin | ||
|
||
#pragma once | ||
|
||
#include <glm/mat4x4.hpp> | ||
|
||
#include <cubos/core/reflection/reflect.hpp> | ||
|
||
#include <cubos/engine/api.hpp> | ||
|
||
namespace cubos::engine | ||
{ | ||
/// @brief Generic component to hold the projection matrix of a specific camera (either perspective or orthogonal). | ||
/// @note Added automatically once a specific camera is added (e.g @ref cubos::engine::PerspectiveCamera). | ||
/// @ingroup render-camera-plugin | ||
struct CUBOS_ENGINE_API Camera | ||
{ | ||
CUBOS_REFLECT; | ||
|
||
/// @brief Whether the camera is drawing to a target. | ||
bool active{true}; | ||
|
||
/// @brief Projection matrix of the camera. | ||
glm::mat4 projection{}; | ||
}; | ||
} // namespace cubos::engine |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#include <cubos/core/ecs/reflection.hpp> | ||
#include <cubos/core/reflection/external/glm.hpp> | ||
#include <cubos/core/reflection/external/primitives.hpp> | ||
|
||
#include <cubos/engine/render/camera/camera.hpp> | ||
|
||
CUBOS_REFLECT_IMPL(cubos::engine::Camera) | ||
{ | ||
return core::ecs::TypeBuilder<Camera>("cubos::engine::Camera") | ||
.withField("active", &Camera::active) | ||
.withField("projection", &Camera::projection) | ||
.build(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,44 @@ | ||
#include <glm/gtc/matrix_transform.hpp> | ||
|
||
#include <cubos/engine/render/camera/camera.hpp> | ||
#include <cubos/engine/render/camera/draws_to.hpp> | ||
#include <cubos/engine/render/camera/perspective_camera.hpp> | ||
#include <cubos/engine/render/camera/plugin.hpp> | ||
#include <cubos/engine/render/target/plugin.hpp> | ||
#include <cubos/engine/render/target/target.hpp> | ||
|
||
using namespace cubos::engine; | ||
|
||
void cubos::engine::cameraPlugin(Cubos& cubos) | ||
{ | ||
cubos.depends(renderTargetPlugin); | ||
|
||
cubos.component<PerspectiveCamera>(); | ||
cubos.component<Camera>(); | ||
|
||
cubos.relation<DrawsTo>(); | ||
|
||
cubos.observer("add Camera on add PerspectiveCamera") | ||
.onAdd<PerspectiveCamera>() | ||
.without<Camera>() | ||
.call([](Commands cmds, Query<Entity> query) { | ||
for (auto [ent] : query) | ||
{ | ||
cmds.add(ent, Camera{}); | ||
} | ||
}); | ||
|
||
cubos.system("update Camera projection by PerspectiveCamera") | ||
.call([](Query<Camera&, const PerspectiveCamera&, const DrawsTo&, const RenderTarget&> query) { | ||
for (auto [camera, perspective, drawsTo, target] : query) | ||
{ | ||
float aspect = (static_cast<float>(target.size.x) * drawsTo.viewportSize.x) / | ||
(static_cast<float>(target.size.y) * drawsTo.viewportSize.y); | ||
if (aspect > 0) | ||
{ | ||
camera.projection = | ||
glm::perspective(glm::radians(perspective.fovY), aspect, perspective.zNear, perspective.zFar); | ||
} | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.