math::make_perspective_XX

This commit is contained in:
2018-10-16 01:59:19 +07:00
parent 952f468269
commit 20551c736f
3 changed files with 76 additions and 7 deletions

View File

@@ -458,12 +458,12 @@ namespace e2d { namespace math
}
//
// make_orthogonal_matrix
// make_orthogonal_lh_matrix4
//
template < typename T >
std::enable_if_t<std::is_floating_point<T>::value, mat4<T>>
make_orthogonal_matrix4(T width, T height, T znear, T zfar) noexcept {
make_orthogonal_lh_matrix4(T width, T height, T znear, T zfar) noexcept {
E2D_ASSERT(!math::is_near_zero(width, T(0)));
E2D_ASSERT(!math::is_near_zero(height, T(0)));
E2D_ASSERT(!math::approximately(znear, zfar, T(0)));
@@ -480,8 +480,77 @@ namespace e2d { namespace math
template < typename T >
std::enable_if_t<std::is_floating_point<T>::value, mat4<T>>
make_orthogonal_matrix4(const vec2<T>& size, T znear, T zfar) {
return make_orthogonal_matrix4(size.x, size.y, znear, zfar);
make_orthogonal_lh_matrix4(const vec2<T>& size, T znear, T zfar) {
return make_orthogonal_lh_matrix4(size.x, size.y, znear, zfar);
}
//
// make_orthogonal_rh_matrix4
//
template < typename T >
std::enable_if_t<std::is_floating_point<T>::value, mat4<T>>
make_orthogonal_rh_matrix4(T width, T height, T znear, T zfar) noexcept {
E2D_ASSERT(!math::is_near_zero(width, T(0)));
E2D_ASSERT(!math::is_near_zero(height, T(0)));
E2D_ASSERT(!math::approximately(znear, zfar, T(0)));
const T sx = T(2) / width;
const T sy = T(2) / height;
const T sz = T(1) / (znear - zfar);
const T tz = znear * sz;
return {
sx, T(0), T(0), T(0),
T(0), sy, T(0), T(0),
T(0), T(0), sz, T(0),
T(0), T(0), tz, T(1)};
}
template < typename T >
std::enable_if_t<std::is_floating_point<T>::value, mat4<T>>
make_orthogonal_rh_matrix4(const vec2<T>& size, T znear, T zfar) {
return make_orthogonal_rh_matrix4(size.x, size.y, znear, zfar);
}
//
// make_perspective_lh_matrix4
//
template < typename T, typename AngleTag >
std::enable_if_t<std::is_floating_point<T>::value, mat4<T>>
make_perspective_lh_matrix4(const unit<T, AngleTag>& fov, T aspect, T znear, T zfar) noexcept {
E2D_ASSERT(!math::is_near_zero(aspect, T(0)));
E2D_ASSERT(!math::approximately(znear, zfar, T(0)));
E2D_ASSERT(!math::approximately(to_rad(fov), make_rad<T>(0), T(0)));
const T sy = T(1) / math::tan(fov * T(0.5));
const T sx = sy / aspect;
const T sz = zfar / (zfar - znear);
const T tz = -znear * zfar / (zfar - znear);
return {
sx, T(0), T(0), T(0),
T(0), sy, T(0), T(0),
T(0), T(0), sz, T(1),
T(0), T(0), tz, T(0)};
}
//
// make_perspective_rh_matrix4
//
template < typename T, typename AngleTag >
std::enable_if_t<std::is_floating_point<T>::value, mat4<T>>
make_perspective_rh_matrix4(const unit<T, AngleTag>& fov, T aspect, T znear, T zfar) noexcept {
E2D_ASSERT(!math::is_near_zero(aspect, T(0)));
E2D_ASSERT(!math::approximately(znear, zfar, T(0)));
E2D_ASSERT(!math::approximately(to_rad(fov), make_rad<T>(0), T(0)));
const T sy = T(1) / math::tan(fov * T(0.5));
const T sx = sy / aspect;
const T sz = zfar / (znear - zfar);
const T tz = znear * zfar / (znear - zfar);
return {
sx, T(0), T(0), T(0),
T(0), sy, T(0), T(0),
T(0), T(0), sz, T(-1),
T(0), T(0), tz, T(0)};
}
//

View File

@@ -169,7 +169,7 @@ int e2d_main() {
const auto begin_game_time = time::now_ms();
const auto framebuffer_size = the<window>().real_size().cast_to<f32>();
const auto projection = math::make_orthogonal_matrix4(framebuffer_size.x, framebuffer_size.y, 0.f, 1.f);
const auto projection = math::make_orthogonal_lh_matrix4(framebuffer_size, 0.f, 1.f);
const keyboard& k = the<input>().keyboard();
while ( !the<window>().should_close() && !k.is_key_just_released(keyboard_key::escape) ) {

View File

@@ -35,8 +35,8 @@ 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),v2f(1.f,1.f),1.f) == math::make_rotation_matrix4(make_rad(1.f), v3f::unit()));
REQUIRE(math::make_orthogonal_matrix4(640.f, 480.f, 0.f, 1.f) == math::make_orthogonal_matrix4(v2f(640,480), 0.f, 1.f));
REQUIRE(math::make_orthogonal_matrix4(640.0, 480.0, 0.0, 1.0) == math::make_orthogonal_matrix4(v2d(640,480), 0.0, 1.0));
REQUIRE(math::make_orthogonal_lh_matrix4(640.f, 480.f, 0.f, 1.f) == math::make_orthogonal_lh_matrix4(v2f(640,480), 0.f, 1.f));
REQUIRE(math::make_orthogonal_lh_matrix4(640.0, 480.0, 0.0, 1.0) == math::make_orthogonal_lh_matrix4(v2d(640,480), 0.0, 1.0));
}
{
REQUIRE(m4f(m4f::identity()) == m4f::identity());