Skip to content

Commit

Permalink
feat(rendering): add OrthographicCamera component
Browse files Browse the repository at this point in the history
  • Loading branch information
mkuritsu committed Sep 29, 2024
1 parent f19856a commit 4110473
Show file tree
Hide file tree
Showing 14 changed files with 117 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Generic Camera component to hold projection matrix (#1331, **@mkuritsu**).
- Initial application debugging through Tesseratos (#1303, **@RiscadoA**).
- Print stacktrace with *cpptrace* on calls to CUBOS_FAIL (#1172, **@RiscadoA**).
- OrthographicCamera component (#1335, **@mkuritsu**)

### Changed

Expand Down
3 changes: 2 additions & 1 deletion engine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@ set(CUBOS_ENGINE_SOURCE
"src/render/depth/plugin.cpp"
"src/render/depth/depth.cpp"
"src/render/camera/plugin.cpp"
"src/render/camera/perspective_camera.cpp"
"src/render/camera/orthographic.cpp"
"src/render/camera/perspective.cpp"
"src/render/camera/draws_to.cpp"
"src/render/camera/camera.cpp"
"src/render/voxels/plugin.cpp"
Expand Down
41 changes: 41 additions & 0 deletions engine/include/cubos/engine/render/camera/orthographic.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/// @file
/// @brief Component @ref cubos::engine::OrthographicCamera
/// @ingroup render-camera-plugin

#pragma once

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

#include <cubos/engine/api.hpp>

namespace cubos::engine
{
/// @brief Component which defines parameters of a orthographic camera used to render the world.
/// @note Should be used with @ref LocalToWorld.
/// @ingroup render-camera-plugin
struct CUBOS_ENGINE_API OrthographicCamera
{
CUBOS_REFLECT;

/// @brief The axis that can be fixed for this projection.
enum class Axis
{
Horizontal,
Vertical
};

/// @brief The size of the fixed axis of the projection.
float size{50.0F};

/// @brief The axis to be fixed for the projection.
Axis axis{Axis::Vertical};

/// @brief Near clipping plane.
float zNear{0.1F};

/// @brief Far clipping plane.
float zFar{1000.0F};
};
} // namespace cubos::engine

CUBOS_REFLECT_EXTERNAL_DECL(CUBOS_ENGINE_API, cubos::engine::OrthographicCamera::Axis);
2 changes: 1 addition & 1 deletion engine/samples/collisions/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#include <cubos/engine/physics/plugin.hpp>
#include <cubos/engine/physics/solver/plugin.hpp>
#include <cubos/engine/render/camera/draws_to.hpp>
#include <cubos/engine/render/camera/perspective_camera.hpp>
#include <cubos/engine/render/camera/perspective.hpp>
#include <cubos/engine/render/defaults/plugin.hpp>
#include <cubos/engine/render/defaults/target.hpp>
#include <cubos/engine/render/tone_mapping/plugin.hpp>
Expand Down
2 changes: 1 addition & 1 deletion engine/samples/complex_physics/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include <cubos/engine/physics/plugins/gravity.hpp>
#include <cubos/engine/physics/solver/plugin.hpp>
#include <cubos/engine/render/camera/draws_to.hpp>
#include <cubos/engine/render/camera/perspective_camera.hpp>
#include <cubos/engine/render/camera/perspective.hpp>
#include <cubos/engine/render/defaults/plugin.hpp>
#include <cubos/engine/render/defaults/target.hpp>
#include <cubos/engine/render/lights/directional.hpp>
Expand Down
2 changes: 1 addition & 1 deletion engine/samples/gizmos/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include <cubos/engine/gizmos/target.hpp>
#include <cubos/engine/prelude.hpp>
#include <cubos/engine/render/camera/draws_to.hpp>
#include <cubos/engine/render/camera/perspective_camera.hpp>
#include <cubos/engine/render/camera/perspective.hpp>
#include <cubos/engine/render/camera/plugin.hpp>
#include <cubos/engine/render/picker/picker.hpp>
#include <cubos/engine/render/picker/plugin.hpp>
Expand Down
2 changes: 1 addition & 1 deletion engine/samples/physics/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <cubos/engine/physics/plugins/gravity.hpp>
#include <cubos/engine/physics/solver/plugin.hpp>
#include <cubos/engine/render/camera/draws_to.hpp>
#include <cubos/engine/render/camera/perspective_camera.hpp>
#include <cubos/engine/render/camera/perspective.hpp>
#include <cubos/engine/render/defaults/plugin.hpp>
#include <cubos/engine/render/defaults/target.hpp>
#include <cubos/engine/render/lights/directional.hpp>
Expand Down
6 changes: 4 additions & 2 deletions engine/samples/render/main/assets/main.cubos
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
}
},
"camera3": {
"cubos::engine::PerspectiveCamera": {},
"cubos::engine::OrthographicCamera": {
"size": 10
},
"cubos::engine::DrawsTo@render-target": {},
"cubos::engine::Position": {
"x": -3,
Expand Down Expand Up @@ -63,4 +65,4 @@
}
}
}
}
}
4 changes: 2 additions & 2 deletions engine/samples/voxels/main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <cubos/engine/assets/plugin.hpp>
#include <cubos/engine/render/camera/draws_to.hpp>
#include <cubos/engine/render/camera/perspective_camera.hpp>
#include <cubos/engine/render/camera/perspective.hpp>
#include <cubos/engine/render/defaults/plugin.hpp>
#include <cubos/engine/render/defaults/target.hpp>
#include <cubos/engine/render/lights/directional.hpp>
Expand Down Expand Up @@ -69,4 +69,4 @@ int main(int argc, char** argv)
/// [Spawn car system]

cubos.run();
}
}
28 changes: 28 additions & 0 deletions engine/src/render/camera/orthographic.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <cubos/core/ecs/reflection.hpp>
#include <cubos/core/reflection/external/primitives.hpp>
#include <cubos/core/reflection/traits/enum.hpp>
#include <cubos/core/reflection/type.hpp>

#include <cubos/engine/render/camera/orthographic.hpp>

using cubos::core::reflection::EnumTrait;
using cubos::core::reflection::Type;
using cubos::engine::OrthographicCamera;

CUBOS_REFLECT_EXTERNAL_IMPL(OrthographicCamera::Axis)
{
return Type::create("cubos::engine::CameraAxis")
.with(EnumTrait{}
.withVariant<OrthographicCamera::Axis::Vertical>("Vertical")
.withVariant<OrthographicCamera::Axis::Horizontal>("Horizontal"));
}

CUBOS_REFLECT_IMPL(OrthographicCamera)
{
return core::ecs::TypeBuilder<OrthographicCamera>("cubos::engine::OrthographicCamera")
.withField("size", &OrthographicCamera::size)
.withField("axis", &OrthographicCamera::axis)
.withField("zNear", &OrthographicCamera::zNear)
.withField("zFar", &OrthographicCamera::zFar)
.build();
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <cubos/core/ecs/reflection.hpp>
#include <cubos/core/reflection/external/primitives.hpp>

#include <cubos/engine/render/camera/perspective_camera.hpp>
#include <cubos/engine/render/camera/perspective.hpp>

CUBOS_REFLECT_IMPL(cubos::engine::PerspectiveCamera)
{
Expand Down
35 changes: 33 additions & 2 deletions engine/src/render/camera/plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

#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/orthographic.hpp>
#include <cubos/engine/render/camera/perspective.hpp>
#include <cubos/engine/render/camera/plugin.hpp>
#include <cubos/engine/render/target/plugin.hpp>
#include <cubos/engine/render/target/target.hpp>
Expand All @@ -13,8 +14,9 @@ void cubos::engine::cameraPlugin(Cubos& cubos)
{
cubos.depends(renderTargetPlugin);

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

cubos.relation<DrawsTo>();

Expand All @@ -28,6 +30,16 @@ void cubos::engine::cameraPlugin(Cubos& cubos)
}
});

cubos.observer("add Camera on add OrthographicCamera")
.onAdd<OrthographicCamera>()
.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)
Expand All @@ -41,4 +53,23 @@ void cubos::engine::cameraPlugin(Cubos& cubos)
}
}
});

cubos.system("update Camera projection by OrthographicCamera")
.call([](Query<Camera&, const OrthographicCamera&, const DrawsTo&, const RenderTarget&> query) {
for (auto [camera, ortho, drawsTo, target] : query)
{
float aspect = (static_cast<float>(target.size.x) * drawsTo.viewportSize.x) /
(static_cast<float>(target.size.y) * drawsTo.viewportSize.y);
if (ortho.axis == OrthographicCamera::Axis::Vertical)
{
camera.projection = glm::ortho(-ortho.size * aspect, ortho.size * aspect, -ortho.size, ortho.size,
ortho.zNear, ortho.zFar);
}
else
{
camera.projection = glm::ortho(-ortho.size, ortho.size, -ortho.size / aspect, ortho.size / aspect,
ortho.zNear, ortho.zFar);
}
}
});
}
2 changes: 1 addition & 1 deletion tools/tesseratos/src/tesseratos/debug_camera/plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include <cubos/engine/input/plugin.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/perspective.hpp>
#include <cubos/engine/render/camera/plugin.hpp>
#include <cubos/engine/render/target/plugin.hpp>
#include <cubos/engine/render/target/target.hpp>
Expand Down

0 comments on commit 4110473

Please sign in to comment.