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 28, 2024
1 parent 1286c1a commit 00eba9b
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 3 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 (#1182, **@mkuritsu**)

### Fixed

Expand Down
1 change: 1 addition & 0 deletions engine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ set(CUBOS_ENGINE_SOURCE
"src/render/depth/plugin.cpp"
"src/render/depth/depth.cpp"
"src/render/camera/plugin.cpp"
"src/render/camera/orthographic_camera.cpp"
"src/render/camera/perspective_camera.cpp"
"src/render/camera/draws_to.cpp"
"src/render/camera/camera.cpp"
Expand Down
31 changes: 31 additions & 0 deletions engine/include/cubos/engine/render/camera/orthographic_camera.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/// @file
/// @brief Component @ref cubos::engine::OrthographicCamera
/// @ingroup render-camera-plugin

#pragma once

#include <glm/vec2.hpp>

#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 Size multiplier for orthographic camera planes.
glm::vec2 size{1.0F, 1.0F};

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

/// @brief Far clipping plane.
float zFar{1000.0F};
};
} // namespace cubos::engine
9 changes: 7 additions & 2 deletions engine/samples/render/main/assets/main.cubos
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@
}
},
"camera2": {
"cubos::engine::PerspectiveCamera": {},
"cubos::engine::OrthographicCamera": {
"size": {
"x": 0.1,
"y": 0.1
}
},
"cubos::engine::DrawsTo@render-target": {},
"cubos::engine::Position": {
"x": 3,
Expand Down Expand Up @@ -63,4 +68,4 @@
}
}
}
}
}
14 changes: 14 additions & 0 deletions engine/src/render/camera/orthographic_camera.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#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/orthographic_camera.hpp>

CUBOS_REFLECT_IMPL(cubos::engine::OrthographicCamera)

Check warning on line 7 in engine/src/render/camera/orthographic_camera.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/render/camera/orthographic_camera.cpp#L7

Added line #L7 was not covered by tests
{
return core::ecs::TypeBuilder<OrthographicCamera>("cubos::engine::OrthographicCamera")
.withField("size", &OrthographicCamera::size)
.withField("zNear", &OrthographicCamera::zNear)
.withField("zFar", &OrthographicCamera::zFar)
.build();

Check warning on line 13 in engine/src/render/camera/orthographic_camera.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/render/camera/orthographic_camera.cpp#L9-L13

Added lines #L9 - L13 were not covered by tests
}
28 changes: 27 additions & 1 deletion engine/src/render/camera/plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <cubos/engine/render/camera/camera.hpp>
#include <cubos/engine/render/camera/draws_to.hpp>
#include <cubos/engine/render/camera/orthographic_camera.hpp>
#include <cubos/engine/render/camera/perspective_camera.hpp>
#include <cubos/engine/render/camera/plugin.hpp>
#include <cubos/engine/render/target/plugin.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>();

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

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,18 @@ 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)

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 width = static_cast<float>(target.size.x) * drawsTo.viewportSize.x;
float height = static_cast<float>(target.size.y) * drawsTo.viewportSize.y;
float left = (-width / 2.0F) * ortho.size.x;
float right = (width / 2.0F) * ortho.size.x;
float bottom = (-height / 2.0F) * ortho.size.y;
float top = (height / 2.0F) * ortho.size.y;
camera.projection = glm::ortho(left, right, bottom, top, ortho.zNear, ortho.zFar);

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

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L61 - L67 were not covered by tests
}
});

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L69 was not covered by tests
}

0 comments on commit 00eba9b

Please sign in to comment.