clipping mode config for projection matrices

This commit is contained in:
2020-01-28 10:30:31 +07:00
parent 221ac93951
commit d7ab730d2a
10 changed files with 226 additions and 213 deletions

View File

@@ -180,3 +180,24 @@
#ifndef E2D_WINDOW_MODE #ifndef E2D_WINDOW_MODE
# error E2D_WINDOW_MODE not detected # error E2D_WINDOW_MODE not detected
#endif #endif
//
// E2D_CLIPPING_MODE
//
#define E2D_CLIPPING_MODE_ZO 1
#define E2D_CLIPPING_MODE_NO 2
#ifndef E2D_CLIPPING_MODE
# if defined(E2D_RENDER_MODE) && E2D_RENDER_MODE == E2D_RENDER_MODE_NONE
# define E2D_CLIPPING_MODE E2D_CLIPPING_MODE_ZO
# elif defined(E2D_RENDER_MODE) && E2D_RENDER_MODE == E2D_RENDER_MODE_OPENGL
# define E2D_CLIPPING_MODE E2D_CLIPPING_MODE_NO
# elif defined(E2D_RENDER_MODE) && E2D_RENDER_MODE == E2D_RENDER_MODE_OPENGLES
# define E2D_CLIPPING_MODE E2D_CLIPPING_MODE_NO
# endif
#endif
#ifndef E2D_CLIPPING_MODE
# error E2D_CLIPPING_MODE not detected
#endif

View File

@@ -148,7 +148,7 @@ namespace e2d::cameras
const v2u target_size = camera.target() const v2u target_size = camera.target()
? camera.target()->size() ? camera.target()->size()
: window.real_size(); : window.real_size();
return math::make_orthographic_lh_no_matrix4( return math::make_orthographic_lh_matrix4(
target_size.cast_to<f32>(), target_size.cast_to<f32>(),
camera.znear(), camera.znear(),
math::max(camera.zfar(), camera.znear() + math::default_precision<f32>())); math::max(camera.zfar(), camera.znear() + math::default_precision<f32>()));

View File

@@ -521,9 +521,11 @@ namespace e2d::math
} }
// //
// make_orthographic_lh_zo_matrix4 // make_orthographic_lh_matrix4
// //
namespace impl
{
template < typename T > template < typename T >
std::enable_if_t<std::is_floating_point_v<T>, mat4<T>> std::enable_if_t<std::is_floating_point_v<T>, mat4<T>>
make_orthographic_lh_zo_matrix4(T width, T height, T znear, T zfar) noexcept { make_orthographic_lh_zo_matrix4(T width, T height, T znear, T zfar) noexcept {
@@ -541,16 +543,6 @@ namespace e2d::math
T(0), T(0), tz, T(1)}; T(0), T(0), tz, T(1)};
} }
template < typename T >
std::enable_if_t<std::is_floating_point_v<T>, mat4<T>>
make_orthographic_lh_zo_matrix4(const vec2<T>& size, T znear, T zfar) noexcept {
return make_orthographic_lh_zo_matrix4(size.x, size.y, znear, zfar);
}
//
// make_orthographic_lh_no_matrix4
//
template < typename T > template < typename T >
std::enable_if_t<std::is_floating_point_v<T>, mat4<T>> std::enable_if_t<std::is_floating_point_v<T>, mat4<T>>
make_orthographic_lh_no_matrix4(T width, T height, T znear, T zfar) noexcept { make_orthographic_lh_no_matrix4(T width, T height, T znear, T zfar) noexcept {
@@ -567,17 +559,32 @@ namespace e2d::math
T(0), T(0), sz, T(0), T(0), T(0), sz, T(0),
T(0), T(0), tz, T(1)}; T(0), T(0), tz, T(1)};
} }
}
template < typename T > template < typename T >
std::enable_if_t<std::is_floating_point_v<T>, mat4<T>> std::enable_if_t<std::is_floating_point_v<T>, mat4<T>>
make_orthographic_lh_no_matrix4(const vec2<T>& size, T znear, T zfar) noexcept { make_orthographic_lh_matrix4(T width, T height, T znear, T zfar) noexcept {
return make_orthographic_lh_no_matrix4(size.x, size.y, znear, zfar); #if defined(E2D_CLIPPING_MODE) && E2D_CLIPPING_MODE == E2D_CLIPPING_MODE_ZO
return impl::make_orthographic_lh_zo_matrix4(width, height, znear, zfar);
#elif defined(E2D_CLIPPING_MODE) && E2D_CLIPPING_MODE == E2D_CLIPPING_MODE_NO
return impl::make_orthographic_lh_no_matrix4(width, height, znear, zfar);
#else
# error "E2D_CLIPPING_MODE not detected"
#endif
}
template < typename T >
std::enable_if_t<std::is_floating_point_v<T>, mat4<T>>
make_orthographic_lh_matrix4(const vec2<T>& size, T znear, T zfar) noexcept {
return make_orthographic_lh_matrix4(size.x, size.y, znear, zfar);
} }
// //
// make_orthographic_rh_zo_matrix4 // make_orthographic_rh_matrix4
// //
namespace impl
{
template < typename T > template < typename T >
std::enable_if_t<std::is_floating_point_v<T>, mat4<T>> std::enable_if_t<std::is_floating_point_v<T>, mat4<T>>
make_orthographic_rh_zo_matrix4(T width, T height, T znear, T zfar) noexcept { make_orthographic_rh_zo_matrix4(T width, T height, T znear, T zfar) noexcept {
@@ -595,16 +602,6 @@ namespace e2d::math
T(0), T(0), tz, T(1)}; T(0), T(0), tz, T(1)};
} }
template < typename T >
std::enable_if_t<std::is_floating_point_v<T>, mat4<T>>
make_orthographic_rh_zo_matrix4(const vec2<T>& size, T znear, T zfar) noexcept {
return make_orthographic_rh_zo_matrix4(size.x, size.y, znear, zfar);
}
//
// make_orthographic_rh_no_matrix4
//
template < typename T > template < typename T >
std::enable_if_t<std::is_floating_point_v<T>, mat4<T>> std::enable_if_t<std::is_floating_point_v<T>, mat4<T>>
make_orthographic_rh_no_matrix4(T width, T height, T znear, T zfar) noexcept { make_orthographic_rh_no_matrix4(T width, T height, T znear, T zfar) noexcept {
@@ -621,17 +618,32 @@ namespace e2d::math
T(0), T(0), sz, T(0), T(0), T(0), sz, T(0),
T(0), T(0), tz, T(1)}; T(0), T(0), tz, T(1)};
} }
}
template < typename T > template < typename T >
std::enable_if_t<std::is_floating_point_v<T>, mat4<T>> std::enable_if_t<std::is_floating_point_v<T>, mat4<T>>
make_orthographic_rh_no_matrix4(const vec2<T>& size, T znear, T zfar) noexcept { make_orthographic_rh_matrix4(T width, T height, T znear, T zfar) noexcept {
return make_orthographic_rh_no_matrix4(size.x, size.y, znear, zfar); #if defined(E2D_CLIPPING_MODE) && E2D_CLIPPING_MODE == E2D_CLIPPING_MODE_ZO
return impl::make_orthographic_rh_zo_matrix4(width, height, znear, zfar);
#elif defined(E2D_CLIPPING_MODE) && E2D_CLIPPING_MODE == E2D_CLIPPING_MODE_NO
return impl::make_orthographic_rh_no_matrix4(width, height, znear, zfar);
#else
# error "E2D_CLIPPING_MODE not detected"
#endif
}
template < typename T >
std::enable_if_t<std::is_floating_point_v<T>, mat4<T>>
make_orthographic_rh_matrix4(const vec2<T>& size, T znear, T zfar) noexcept {
return make_orthographic_rh_matrix4(size.x, size.y, znear, zfar);
} }
// //
// make_perspective_lh_zo_matrix4 // make_perspective_lh_matrix4
// //
namespace impl
{
template < typename T, typename AngleTag > template < typename T, typename AngleTag >
std::enable_if_t<std::is_floating_point_v<T>, mat4<T>> std::enable_if_t<std::is_floating_point_v<T>, mat4<T>>
make_perspective_lh_zo_matrix4(const unit<T, AngleTag>& fov, T aspect, T znear, T zfar) noexcept { make_perspective_lh_zo_matrix4(const unit<T, AngleTag>& fov, T aspect, T znear, T zfar) noexcept {
@@ -649,10 +661,6 @@ namespace e2d::math
T(0), T(0), tz, T(0)}; T(0), T(0), tz, T(0)};
} }
//
// make_perspective_lh_no_matrix4
//
template < typename T, typename AngleTag > template < typename T, typename AngleTag >
std::enable_if_t<std::is_floating_point_v<T>, mat4<T>> std::enable_if_t<std::is_floating_point_v<T>, mat4<T>>
make_perspective_lh_no_matrix4(const unit<T, AngleTag>& fov, T aspect, T znear, T zfar) noexcept { make_perspective_lh_no_matrix4(const unit<T, AngleTag>& fov, T aspect, T znear, T zfar) noexcept {
@@ -669,11 +677,26 @@ namespace e2d::math
T(0), T(0), sz, T(1), T(0), T(0), sz, T(1),
T(0), T(0), tz, T(0)}; T(0), T(0), tz, T(0)};
} }
}
template < typename T, typename AngleTag >
std::enable_if_t<std::is_floating_point_v<T>, mat4<T>>
make_perspective_lh_matrix4(const unit<T, AngleTag>& fov, T aspect, T znear, T zfar) noexcept {
#if defined(E2D_CLIPPING_MODE) && E2D_CLIPPING_MODE == E2D_CLIPPING_MODE_ZO
return impl::make_perspective_lh_zo_matrix4(fov, aspect, znear, zfar);
#elif defined(E2D_CLIPPING_MODE) && E2D_CLIPPING_MODE == E2D_CLIPPING_MODE_NO
return impl::make_perspective_lh_no_matrix4(fov, aspect, znear, zfar);
#else
# error "E2D_CLIPPING_MODE not detected"
#endif
}
// //
// make_perspective_rh_zo_matrix4 // make_perspective_rh_matrix4
// //
namespace impl
{
template < typename T, typename AngleTag > template < typename T, typename AngleTag >
std::enable_if_t<std::is_floating_point_v<T>, mat4<T>> std::enable_if_t<std::is_floating_point_v<T>, mat4<T>>
make_perspective_rh_zo_matrix4(const unit<T, AngleTag>& fov, T aspect, T znear, T zfar) noexcept { make_perspective_rh_zo_matrix4(const unit<T, AngleTag>& fov, T aspect, T znear, T zfar) noexcept {
@@ -691,10 +714,6 @@ namespace e2d::math
T(0), T(0), tz, T(0)}; T(0), T(0), tz, T(0)};
} }
//
// make_perspective_rh_no_matrix4
//
template < typename T, typename AngleTag > template < typename T, typename AngleTag >
std::enable_if_t<std::is_floating_point_v<T>, mat4<T>> std::enable_if_t<std::is_floating_point_v<T>, mat4<T>>
make_perspective_rh_no_matrix4(const unit<T, AngleTag>& fov, T aspect, T znear, T zfar) noexcept { make_perspective_rh_no_matrix4(const unit<T, AngleTag>& fov, T aspect, T znear, T zfar) noexcept {
@@ -711,6 +730,19 @@ namespace e2d::math
T(0), T(0), sz, T(-1), T(0), T(0), sz, T(-1),
T(0), T(0), tz, T(0)}; T(0), T(0), tz, T(0)};
} }
}
template < typename T, typename AngleTag >
std::enable_if_t<std::is_floating_point_v<T>, mat4<T>>
make_perspective_rh_matrix4(const unit<T, AngleTag>& fov, T aspect, T znear, T zfar) noexcept {
#if defined(E2D_CLIPPING_MODE) && E2D_CLIPPING_MODE == E2D_CLIPPING_MODE_ZO
return impl::make_perspective_rh_zo_matrix4(fov, aspect, znear, zfar);
#elif defined(E2D_CLIPPING_MODE) && E2D_CLIPPING_MODE == E2D_CLIPPING_MODE_NO
return impl::make_perspective_rh_no_matrix4(fov, aspect, znear, zfar);
#else
# error "E2D_CLIPPING_MODE not detected"
#endif
}
// //
// approximately // approximately

View File

@@ -53,50 +53,26 @@ function m4f.make_look_at_rh(eye, at, up) end
---@overload fun(width: number, height: number, znear: number, zfar: number): m4f ---@overload fun(width: number, height: number, znear: number, zfar: number): m4f
---@overload fun(size: v2f, znear: number, zfar: number): m4f ---@overload fun(size: v2f, znear: number, zfar: number): m4f
---@return m4f ---@return m4f
function m4f.make_orthographic_lh_zo(...) end function m4f.make_orthographic_lh(...) end
---@overload fun(width: number, height: number, znear: number, zfar: number): m4f ---@overload fun(width: number, height: number, znear: number, zfar: number): m4f
---@overload fun(size: v2f, znear: number, zfar: number): m4f ---@overload fun(size: v2f, znear: number, zfar: number): m4f
---@return m4f ---@return m4f
function m4f.make_orthographic_lh_no(...) end function m4f.make_orthographic_rh(...) end
---@overload fun(width: number, height: number, znear: number, zfar: number): m4f
---@overload fun(size: v2f, znear: number, zfar: number): m4f
---@return m4f
function m4f.make_orthographic_rh_zo(...) end
---@overload fun(width: number, height: number, znear: number, zfar: number): m4f
---@overload fun(size: v2f, znear: number, zfar: number): m4f
---@return m4f
function m4f.make_orthographic_rh_no(...) end
---@param angle number ---@param angle number
---@param aspect number ---@param aspect number
---@param znear number ---@param znear number
---@param zfar number ---@param zfar number
---@return m4f ---@return m4f
function m4f.make_perspective_lh_zo(angle, aspect, znear, zfar) end function m4f.make_perspective_lh(angle, aspect, znear, zfar) end
---@param angle number ---@param angle number
---@param aspect number ---@param aspect number
---@param znear number ---@param znear number
---@param zfar number ---@param zfar number
---@return m4f ---@return m4f
function m4f.make_perspective_lh_no(angle, aspect, znear, zfar) end function m4f.make_perspective_rh(angle, aspect, znear, zfar) end
---@param angle number
---@param aspect number
---@param znear number
---@param zfar number
---@return m4f
function m4f.make_perspective_rh_zo(angle, aspect, znear, zfar) end
---@param angle number
---@param aspect number
---@param znear number
---@param zfar number
---@return m4f
function m4f.make_perspective_rh_no(angle, aspect, znear, zfar) end
---@param l m4f ---@param l m4f
---@param r m4f ---@param r m4f

View File

@@ -184,7 +184,7 @@ namespace
void frame_render() final { void frame_render() final {
const auto framebuffer_size = the<window>().real_size().cast_to<f32>(); const auto framebuffer_size = the<window>().real_size().cast_to<f32>();
const auto projection = math::make_orthographic_lh_no_matrix4( const auto projection = math::make_orthographic_lh_matrix4(
framebuffer_size, 0.f, 1.f); framebuffer_size, 0.f, 1.f);
material_.properties() material_.properties()

View File

@@ -235,7 +235,7 @@ namespace
void frame_render() final { void frame_render() final {
const auto framebuffer_size = the<window>().real_size().cast_to<f32>(); const auto framebuffer_size = the<window>().real_size().cast_to<f32>();
const auto projection = math::make_perspective_lh_no_matrix4( const auto projection = math::make_perspective_lh_matrix4(
make_deg(45.f), make_deg(45.f),
framebuffer_size.x / framebuffer_size.y, framebuffer_size.x / framebuffer_size.y,
0.1f, 0.1f,

View File

@@ -205,7 +205,7 @@ namespace
void frame_render() final { void frame_render() final {
const auto framebuffer_size = the<window>().real_size().cast_to<f32>(); const auto framebuffer_size = the<window>().real_size().cast_to<f32>();
const auto projection = math::make_perspective_lh_no_matrix4( const auto projection = math::make_perspective_lh_matrix4(
make_deg(45.f), make_deg(45.f),
framebuffer_size.x / framebuffer_size.y, framebuffer_size.x / framebuffer_size.y,
0.1f, 0.1f,

View File

@@ -129,7 +129,7 @@ namespace e2d
const v2f framebuffer_size = display_size * v2f(draw_data->FramebufferScale); const v2f framebuffer_size = display_size * v2f(draw_data->FramebufferScale);
const m4f projection = const m4f projection =
math::make_orthographic_lh_no_matrix4(display_size, 0.f, 1.f) * math::make_orthographic_lh_matrix4(display_size, 0.f, 1.f) *
math::make_translation_matrix4(-1.f, 1.f); math::make_translation_matrix4(-1.f, 1.f);
for ( int i = 0; i < draw_data->CmdListsCount; ++i ) { for ( int i = 0; i < draw_data->CmdListsCount; ++i ) {

View File

@@ -66,36 +66,20 @@ namespace
"make_look_at_rh", sol::resolve< "make_look_at_rh", sol::resolve<
mat4<T>(const vec3<T>&,const vec3<T>&,const vec3<T>&)>(&math::make_look_at_rh_matrix4), mat4<T>(const vec3<T>&,const vec3<T>&,const vec3<T>&)>(&math::make_look_at_rh_matrix4),
"make_orthographic_lh_zo", sol::overload( "make_orthographic_lh", sol::overload(
sol::resolve<mat4<T>(T,T,T,T)>(&math::make_orthographic_lh_zo_matrix4), sol::resolve<mat4<T>(T,T,T,T)>(&math::make_orthographic_lh_matrix4),
sol::resolve<mat4<T>(const vec2<T>&,T,T)>(&math::make_orthographic_lh_zo_matrix4)), sol::resolve<mat4<T>(const vec2<T>&,T,T)>(&math::make_orthographic_lh_matrix4)),
"make_orthographic_lh_no", sol::overload( "make_orthographic_rh", sol::overload(
sol::resolve<mat4<T>(T,T,T,T)>(&math::make_orthographic_lh_no_matrix4), sol::resolve<mat4<T>(T,T,T,T)>(&math::make_orthographic_rh_matrix4),
sol::resolve<mat4<T>(const vec2<T>&,T,T)>(&math::make_orthographic_lh_no_matrix4)), sol::resolve<mat4<T>(const vec2<T>&,T,T)>(&math::make_orthographic_rh_matrix4)),
"make_orthographic_rh_zo", sol::overload( "make_perspective_lh", [](T angle, T aspect, T znear, T zfar) -> mat4<T> {
sol::resolve<mat4<T>(T,T,T,T)>(&math::make_orthographic_rh_zo_matrix4), return math::make_perspective_lh_matrix4(make_rad(angle), aspect, znear, zfar);
sol::resolve<mat4<T>(const vec2<T>&,T,T)>(&math::make_orthographic_rh_zo_matrix4)),
"make_orthographic_rh_no", sol::overload(
sol::resolve<mat4<T>(T,T,T,T)>(&math::make_orthographic_rh_no_matrix4),
sol::resolve<mat4<T>(const vec2<T>&,T,T)>(&math::make_orthographic_rh_no_matrix4)),
"make_perspective_lh_zo", [](T angle, T aspect, T znear, T zfar) -> mat4<T> {
return math::make_perspective_lh_zo_matrix4(make_rad(angle), aspect, znear, zfar);
}, },
"make_perspective_lh_no", [](T angle, T aspect, T znear, T zfar) -> mat4<T> { "make_perspective_rh", [](T angle, T aspect, T znear, T zfar) -> mat4<T> {
return math::make_perspective_lh_no_matrix4(make_rad(angle), aspect, znear, zfar); return math::make_perspective_rh_matrix4(make_rad(angle), aspect, znear, zfar);
},
"make_perspective_rh_zo", [](T angle, T aspect, T znear, T zfar) -> mat4<T> {
return math::make_perspective_rh_zo_matrix4(make_rad(angle), aspect, znear, zfar);
},
"make_perspective_rh_no", [](T angle, T aspect, T znear, T zfar) -> mat4<T> {
return math::make_perspective_rh_no_matrix4(make_rad(angle), aspect, znear, zfar);
}, },
"approximately", [](const mat4<T>& l, const mat4<T>& r) -> bool { "approximately", [](const mat4<T>& l, const mat4<T>& r) -> bool {

View File

@@ -35,11 +35,11 @@ TEST_CASE("mat4") {
REQUIRE(math::make_rotation_matrix4(make_rad(1.f),1.f,1.f,1.f) == math::make_rotation_matrix4(make_rad(1.f), v3f::unit())); REQUIRE(math::make_rotation_matrix4(make_rad(1.f),1.f,1.f,1.f) == math::make_rotation_matrix4(make_rad(1.f), v3f::unit()));
REQUIRE(math::make_rotation_matrix4(make_rad(1.f),v3f(1.f,1.f,1.f)) == math::make_rotation_matrix4(make_rad(1.f), v3f::unit())); REQUIRE(math::make_rotation_matrix4(make_rad(1.f),v3f(1.f,1.f,1.f)) == math::make_rotation_matrix4(make_rad(1.f), v3f::unit()));
REQUIRE(math::make_orthographic_lh_no_matrix4(640.f, 480.f, 0.f, 1.f) == math::make_orthographic_lh_no_matrix4(v2f(640,480), 0.f, 1.f)); REQUIRE(math::make_orthographic_lh_matrix4(640.f, 480.f, 0.f, 1.f) == math::make_orthographic_lh_matrix4(v2f(640,480), 0.f, 1.f));
REQUIRE(math::make_orthographic_lh_no_matrix4(640.0, 480.0, 0.0, 1.0) == math::make_orthographic_lh_no_matrix4(v2d(640,480), 0.0, 1.0)); REQUIRE(math::make_orthographic_lh_matrix4(640.0, 480.0, 0.0, 1.0) == math::make_orthographic_lh_matrix4(v2d(640,480), 0.0, 1.0));
REQUIRE(math::make_orthographic_lh_zo_matrix4(640.f, 480.f, 0.f, 1.f) == math::make_orthographic_lh_zo_matrix4(v2f(640,480), 0.f, 1.f)); REQUIRE(math::make_orthographic_lh_matrix4(640.f, 480.f, 0.f, 1.f) == math::make_orthographic_lh_matrix4(v2f(640,480), 0.f, 1.f));
REQUIRE(math::make_orthographic_lh_zo_matrix4(640.0, 480.0, 0.0, 1.0) == math::make_orthographic_lh_zo_matrix4(v2d(640,480), 0.0, 1.0)); REQUIRE(math::make_orthographic_lh_matrix4(640.0, 480.0, 0.0, 1.0) == math::make_orthographic_lh_matrix4(v2d(640,480), 0.0, 1.0));
} }
{ {
REQUIRE(m4f(m4f::identity()) == m4f::identity()); REQUIRE(m4f(m4f::identity()) == m4f::identity());