camera modes for viewports

This commit is contained in:
2020-01-29 20:12:49 +07:00
parent d92f2c1969
commit b8f53c4582
4 changed files with 20 additions and 21 deletions

View File

@@ -25,19 +25,21 @@ namespace
camera.zfar(),
camera.znear() + math::default_precision<f32>());
const v2f target_size = camera.target()
? camera.target()->size().cast_to<f32>()
: window.real_size().cast_to<f32>();
const v2f virtual_size = window.virtual_size().cast_to<f32>();
const v2u target_size = camera.target()
? camera.target()->size()
: window.real_size();
if ( math::is_near_zero(math::length_squared(target_size), 0.f) ||
math::is_near_zero(math::length_squared(virtual_size), 0.f) )
const v2f virtual_size = window.virtual_size().cast_to<f32>();
const v2f viewport_size = target_size.cast_to<f32>() * camera.viewport().size;
if ( math::is_near_zero(math::length_squared(virtual_size), 0.f) ||
math::is_near_zero(math::length_squared(viewport_size), 0.f) )
{
return camera.projection();
}
const f32 target_aspect = target_size.x / target_size.y;
const f32 virtual_aspect = virtual_size.x / virtual_size.y;
const f32 viewport_aspect = viewport_size.x / viewport_size.y;
switch ( camera.mode() ) {
case camera::modes::manual:
@@ -49,21 +51,21 @@ namespace
ortho_zfar);
case camera::modes::flexible:
return math::make_orthographic_lh_matrix4(
target_size,
viewport_size,
ortho_znear,
ortho_zfar);
case camera::modes::fixed_fit:
return math::make_orthographic_lh_matrix4(
target_aspect < virtual_aspect
? v2f(virtual_size.x, virtual_size.y * (virtual_aspect / target_aspect))
: v2f(virtual_size.x * (target_aspect / virtual_aspect), virtual_size.y),
viewport_aspect < virtual_aspect
? v2f(virtual_size.x, virtual_size.y * (virtual_aspect / viewport_aspect))
: v2f(virtual_size.x * (viewport_aspect / virtual_aspect), virtual_size.y),
ortho_znear,
ortho_zfar);
case camera::modes::fixed_crop:
return math::make_orthographic_lh_matrix4(
virtual_aspect < target_aspect
? v2f(virtual_size.x, virtual_size.y * (virtual_aspect / target_aspect))
: v2f(virtual_size.x * (target_aspect / virtual_aspect), virtual_size.y),
virtual_aspect < viewport_aspect
? v2f(virtual_size.x, virtual_size.y * (virtual_aspect / viewport_aspect))
: v2f(virtual_size.x * (viewport_aspect / virtual_aspect), virtual_size.y),
ortho_znear,
ortho_zfar);
default:

View File

@@ -56,12 +56,11 @@ namespace e2d
~internal_state() noexcept = default;
void process_render(const ecs::const_entity& cam_e, ecs::registry& owner) {
if ( !cam_e.valid() || !ecs::exists_all<actor, camera>()(cam_e) ) {
if ( !cam_e.valid() || !cam_e.exists_component<camera>() ) {
return;
}
drawer_.with(
cam_e.get_component<camera>(),
cam_e.get_component<actor>().node(),
[&owner](drawer::context& ctx){
for_all_scenes(ctx, owner);
});

View File

@@ -42,7 +42,6 @@ namespace e2d::render_system_impl
drawer::context::context(
const camera& cam,
const const_node_iptr& cam_n,
engine& engine,
render& render,
window& window,

View File

@@ -33,7 +33,6 @@ namespace e2d::render_system_impl
public:
context(
const camera& cam,
const const_node_iptr& cam_n,
engine& engine,
render& render,
window& window,
@@ -66,7 +65,7 @@ namespace e2d::render_system_impl
drawer(engine& e, debug& d, render& r, window& w);
template < typename F >
void with(const camera& cam, const const_node_iptr& cam_n, F&& f);
void with(const camera& cam, F&& f);
private:
engine& engine_;
render& render_;
@@ -78,8 +77,8 @@ namespace e2d::render_system_impl
namespace e2d::render_system_impl
{
template < typename F >
void drawer::with(const camera& cam, const const_node_iptr& cam_n, F&& f) {
context ctx{cam, cam_n, engine_, render_, window_, batcher_};
void drawer::with(const camera& cam, F&& f) {
context ctx{cam, engine_, render_, window_, batcher_};
std::forward<F>(f)(ctx);
ctx.flush();
}