Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(rendering): Add OrthographicCamera component #1335

Merged
merged 2 commits into from
Sep 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 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**).
- Orthographic Camera component (#1182, **@mkuritsu**).

### Changed

Expand All @@ -28,6 +29,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Spot light angle mismatch between light and shadows (#1310, **@tomas7770**).
- Spot shadows cause light range cutoff (#1312, **@tomas7770**).
- Precision error in split screen size calculations (**@mkuritsu**).


## [v0.3.0] - 2024-08-02
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)

Check warning on line 12 in engine/src/render/camera/orthographic.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/render/camera/orthographic.cpp#L12

Added line #L12 was not covered by tests
{
return Type::create("cubos::engine::CameraAxis")
.with(EnumTrait{}
.withVariant<OrthographicCamera::Axis::Vertical>("Vertical")
.withVariant<OrthographicCamera::Axis::Horizontal>("Horizontal"));

Check warning on line 17 in engine/src/render/camera/orthographic.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/render/camera/orthographic.cpp#L14-L17

Added lines #L14 - L17 were not covered by tests
}

CUBOS_REFLECT_IMPL(OrthographicCamera)

Check warning on line 20 in engine/src/render/camera/orthographic.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/render/camera/orthographic.cpp#L20

Added line #L20 was not covered by tests
{
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();

Check warning on line 27 in engine/src/render/camera/orthographic.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/render/camera/orthographic.cpp#L22-L27

Added lines #L22 - L27 were not covered by tests
}
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 @@
{
cubos.depends(renderTargetPlugin);

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

Check warning on line 19 in engine/src/render/camera/plugin.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/render/camera/plugin.cpp#L18-L19

Added lines #L18 - L19 were not covered by tests

cubos.relation<DrawsTo>();

Expand All @@ -28,6 +30,16 @@
}
});

cubos.observer("add Camera on add OrthographicCamera")
.onAdd<OrthographicCamera>()
.without<Camera>()
.call([](Commands cmds, Query<Entity> query) {
for (auto [ent] : query)

Check warning on line 37 in engine/src/render/camera/plugin.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/render/camera/plugin.cpp#L33-L37

Added lines #L33 - L37 were not covered by tests
{
cmds.add(ent, Camera{});

Check warning on line 39 in engine/src/render/camera/plugin.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/render/camera/plugin.cpp#L39

Added line #L39 was not covered by tests
}
});

Check warning on line 41 in engine/src/render/camera/plugin.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/render/camera/plugin.cpp#L41

Added line #L41 was not covered by tests

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 @@
}
}
});

cubos.system("update Camera projection by OrthographicCamera")
.call([](Query<Camera&, const OrthographicCamera&, const DrawsTo&, const RenderTarget&> query) {
for (auto [camera, ortho, drawsTo, target] : query)

Check warning on line 59 in engine/src/render/camera/plugin.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/render/camera/plugin.cpp#L57-L59

Added lines #L57 - L59 were not covered by tests
{
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)

Check warning on line 63 in engine/src/render/camera/plugin.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/render/camera/plugin.cpp#L61-L63

Added lines #L61 - L63 were not covered by tests
{
camera.projection = glm::ortho(-ortho.size * aspect, ortho.size * aspect, -ortho.size, ortho.size,
ortho.zNear, ortho.zFar);

Check warning on line 66 in engine/src/render/camera/plugin.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/render/camera/plugin.cpp#L65-L66

Added lines #L65 - L66 were not covered by tests
}
else
{
camera.projection = glm::ortho(-ortho.size, ortho.size, -ortho.size / aspect, ortho.size / aspect,
ortho.zNear, ortho.zFar);

Check warning on line 71 in engine/src/render/camera/plugin.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/render/camera/plugin.cpp#L70-L71

Added lines #L70 - L71 were not covered by tests
}
}
});

Check warning on line 74 in engine/src/render/camera/plugin.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/render/camera/plugin.cpp#L74

Added line #L74 was not covered by tests
}
4 changes: 2 additions & 2 deletions engine/src/render/split_screen/plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@
// Split along the largest axis.
if (size.x > size.y)
{
splitSize = {size.x / 2, size.y};
splitSize = {(size.x + 1) / 2, size.y};

Check warning on line 38 in engine/src/render/split_screen/plugin.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/render/split_screen/plugin.cpp#L38

Added line #L38 was not covered by tests
splitOffset = {size.x / 2, 0};
}
else
{
splitSize = {size.x, size.y / 2};
splitSize = {size.x, (size.y + 1) / 2};

Check warning on line 43 in engine/src/render/split_screen/plugin.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/render/split_screen/plugin.cpp#L43

Added line #L43 was not covered by tests
splitOffset = {0, size.y / 2};
}

Expand Down
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
Loading