Skip to content

Commit 3b0470c

Browse files
authored
Merge pull request #178 from orange-cpp/feature/imrovements
Feature/imrovements
2 parents 29b7ac6 + 8562c5d commit 3b0470c

7 files changed

Lines changed: 53 additions & 40 deletions

File tree

include/omath/linear_algebra/mat.hpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -677,11 +677,13 @@ namespace omath
677677
{0.f, 1.f / fov_half_tan, 0.f, 0.f},
678678
{0.f, 0.f, far / (far - near), -(near * far) / (far - near)},
679679
{0.f, 0.f, 1.f, 0.f}};
680-
else
680+
else if constexpr (DepthRange == NDCDepthRange::NEGATIVE_ONE_TO_ONE)
681681
return {{1.f / (aspect_ratio * fov_half_tan), 0.f, 0.f, 0.f},
682682
{0.f, 1.f / fov_half_tan, 0.f, 0.f},
683683
{0.f, 0.f, (far + near) / (far - near), -(2.f * near * far) / (far - near)},
684684
{0.f, 0.f, 1.f, 0.f}};
685+
else
686+
std::unreachable();
685687
}
686688

687689
template<class Type = float, MatStoreType St = MatStoreType::ROW_MAJOR,
@@ -697,11 +699,13 @@ namespace omath
697699
{0.f, 1.f / fov_half_tan, 0.f, 0.f},
698700
{0.f, 0.f, -far / (far - near), -(near * far) / (far - near)},
699701
{0.f, 0.f, -1.f, 0.f}};
700-
else
702+
else if constexpr (DepthRange == NDCDepthRange::NEGATIVE_ONE_TO_ONE)
701703
return {{1.f / (aspect_ratio * fov_half_tan), 0.f, 0.f, 0.f},
702704
{0.f, 1.f / fov_half_tan, 0.f, 0.f},
703705
{0.f, 0.f, -(far + near) / (far - near), -(2.f * near * far) / (far - near)},
704706
{0.f, 0.f, -1.f, 0.f}};
707+
else
708+
std::unreachable();
705709
}
706710
template<class Type = float, MatStoreType St = MatStoreType::ROW_MAJOR,
707711
NDCDepthRange DepthRange = NDCDepthRange::NEGATIVE_ONE_TO_ONE>
@@ -717,14 +721,16 @@ namespace omath
717721
{ 0.f, 0.f, static_cast<Type>(1) / (far - near), -near / (far - near) },
718722
{ 0.f, 0.f, 0.f, 1.f }
719723
};
720-
else
724+
else if constexpr (DepthRange == NDCDepthRange::NEGATIVE_ONE_TO_ONE)
721725
return
722726
{
723727
{ static_cast<Type>(2) / (right - left), 0.f, 0.f, -(right + left) / (right - left)},
724728
{ 0.f, static_cast<Type>(2) / (top - bottom), 0.f, -(top + bottom) / (top - bottom)},
725729
{ 0.f, 0.f, static_cast<Type>(2) / (far - near), -(far + near) / (far - near) },
726730
{ 0.f, 0.f, 0.f, 1.f }
727731
};
732+
else
733+
std::unreachable();
728734
}
729735
template<class Type = float, MatStoreType St = MatStoreType::ROW_MAJOR,
730736
NDCDepthRange DepthRange = NDCDepthRange::NEGATIVE_ONE_TO_ONE>
@@ -740,14 +746,16 @@ namespace omath
740746
{ 0.f, 0.f, -static_cast<Type>(1) / (far - near), -near / (far - near) },
741747
{ 0.f, 0.f, 0.f, 1.f }
742748
};
743-
else
749+
else if constexpr (DepthRange == NDCDepthRange::NEGATIVE_ONE_TO_ONE)
744750
return
745751
{
746752
{ static_cast<Type>(2) / (right - left), 0.f, 0.f, -(right + left) / (right - left)},
747753
{ 0.f, static_cast<Type>(2) / (top - bottom), 0.f, -(top + bottom) / (top - bottom)},
748754
{ 0.f, 0.f, -static_cast<Type>(2) / (far - near), -(far + near) / (far - near) },
749755
{ 0.f, 0.f, 0.f, 1.f }
750756
};
757+
else
758+
std::unreachable();
751759
}
752760
template<class T = float, MatStoreType St = MatStoreType::COLUMN_MAJOR>
753761
Mat<4, 4, T, St> mat_look_at_left_handed(const Vector3<T>& eye, const Vector3<T>& center, const Vector3<T>& up)

source/engines/cry_engine/formulas.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ namespace omath::cry_engine
4141
return mat_perspective_left_handed<float, MatStoreType::ROW_MAJOR, NDCDepthRange::ZERO_TO_ONE>(
4242
field_of_view, aspect_ratio, near, far);
4343

44-
return mat_perspective_left_handed(field_of_view, aspect_ratio, near, far);
44+
if (ndc_depth_range == NDCDepthRange::NEGATIVE_ONE_TO_ONE)
45+
return mat_perspective_left_handed<float, MatStoreType::ROW_MAJOR, NDCDepthRange::NEGATIVE_ONE_TO_ONE>(
46+
field_of_view, aspect_ratio, near, far);
47+
std::unreachable();
4548
}
4649
} // namespace omath::unity_engine

source/engines/frostbite_engine/formulas.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ namespace omath::frostbite_engine
4141
return mat_perspective_left_handed<float, MatStoreType::ROW_MAJOR, NDCDepthRange::ZERO_TO_ONE>(
4242
field_of_view, aspect_ratio, near, far);
4343

44-
return mat_perspective_left_handed(field_of_view, aspect_ratio, near, far);
44+
if (ndc_depth_range == NDCDepthRange::NEGATIVE_ONE_TO_ONE)
45+
return mat_perspective_left_handed<float, MatStoreType::ROW_MAJOR, NDCDepthRange::NEGATIVE_ONE_TO_ONE>(
46+
field_of_view, aspect_ratio, near, far);
47+
48+
std::unreachable();
4549
}
4650
} // namespace omath::unity_engine

source/engines/iw_engine/formulas.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,13 @@ namespace omath::iw_engine
5050
{0, 0, far / (far - near), -(near * far) / (far - near)},
5151
{0, 0, 1, 0},
5252
};
53-
54-
return {
55-
{1.f / (aspect_ratio * fov_half_tan), 0, 0, 0},
56-
{0, 1.f / (fov_half_tan), 0, 0},
57-
{0, 0, (far + near) / (far - near), -(2.f * far * near) / (far - near)},
58-
{0, 0, 1, 0},
59-
};
53+
if (ndc_depth_range == NDCDepthRange::NEGATIVE_ONE_TO_ONE)
54+
return {
55+
{1.f / (aspect_ratio * fov_half_tan), 0, 0, 0},
56+
{0, 1.f / (fov_half_tan), 0, 0},
57+
{0, 0, (far + near) / (far - near), -(2.f * far * near) / (far - near)},
58+
{0, 0, 1, 0},
59+
};
60+
std::unreachable();
6061
};
6162
} // namespace omath::iw_engine

source/engines/opengl_engine/formulas.cpp

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ namespace omath::opengl_engine
88

99
Vector3<float> forward_vector(const ViewAngles& angles) noexcept
1010
{
11-
const auto vec
12-
= rotation_matrix(angles) * mat_column_from_vector<float, MatStoreType::COLUMN_MAJOR>(k_abs_forward);
11+
const auto vec =
12+
rotation_matrix(angles) * mat_column_from_vector<float, MatStoreType::COLUMN_MAJOR>(k_abs_forward);
1313

1414
return {vec.at(0, 0), vec.at(1, 0), vec.at(2, 0)};
1515
}
1616
Vector3<float> right_vector(const ViewAngles& angles) noexcept
1717
{
18-
const auto vec
19-
= rotation_matrix(angles) * mat_column_from_vector<float, MatStoreType::COLUMN_MAJOR>(k_abs_right);
18+
const auto vec =
19+
rotation_matrix(angles) * mat_column_from_vector<float, MatStoreType::COLUMN_MAJOR>(k_abs_right);
2020

2121
return {vec.at(0, 0), vec.at(1, 0), vec.at(2, 0)};
2222
}
@@ -28,7 +28,7 @@ namespace omath::opengl_engine
2828
}
2929
Mat4X4 calc_view_matrix(const ViewAngles& angles, const Vector3<float>& cam_origin) noexcept
3030
{
31-
return mat_look_at_right_handed(cam_origin, cam_origin+forward_vector(angles), up_vector(angles));
31+
return mat_look_at_right_handed(cam_origin, cam_origin + forward_vector(angles), up_vector(angles));
3232
}
3333
Mat4X4 rotation_matrix(const ViewAngles& angles) noexcept
3434
{
@@ -39,21 +39,14 @@ namespace omath::opengl_engine
3939
Mat4X4 calc_perspective_projection_matrix(const float field_of_view, const float aspect_ratio, const float near,
4040
const float far, const NDCDepthRange ndc_depth_range) noexcept
4141
{
42-
const float fov_half_tan = std::tan(angles::degrees_to_radians(field_of_view) / 2.f);
42+
if (ndc_depth_range == NDCDepthRange::NEGATIVE_ONE_TO_ONE)
43+
return mat_perspective_right_handed<float, MatStoreType::COLUMN_MAJOR, NDCDepthRange::NEGATIVE_ONE_TO_ONE>(
44+
field_of_view, aspect_ratio, near, far);
4345

4446
if (ndc_depth_range == NDCDepthRange::ZERO_TO_ONE)
45-
return {
46-
{1.f / (aspect_ratio * fov_half_tan), 0, 0, 0},
47-
{0, 1.f / (fov_half_tan), 0, 0},
48-
{0, 0, -far / (far - near), -(near * far) / (far - near)},
49-
{0, 0, -1, 0},
50-
};
47+
return mat_perspective_right_handed<float, MatStoreType::COLUMN_MAJOR, NDCDepthRange::ZERO_TO_ONE>(
48+
field_of_view, aspect_ratio, near, far);
5149

52-
return {
53-
{1.f / (aspect_ratio * fov_half_tan), 0, 0, 0},
54-
{0, 1.f / (fov_half_tan), 0, 0},
55-
{0, 0, -(far + near) / (far - near), -(2.f * far * near) / (far - near)},
56-
{0, 0, -1, 0},
57-
};
50+
std::unreachable();
5851
}
5952
} // namespace omath::opengl_engine

source/engines/source_engine/formulas.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,13 @@ namespace omath::source_engine
5050
{0, 0, far / (far - near), -(near * far) / (far - near)},
5151
{0, 0, 1, 0},
5252
};
53-
54-
return {
55-
{1.f / (aspect_ratio * fov_half_tan), 0, 0, 0},
56-
{0, 1.f / (fov_half_tan), 0, 0},
57-
{0, 0, (far + near) / (far - near), -(2.f * far * near) / (far - near)},
58-
{0, 0, 1, 0},
59-
};
53+
if (ndc_depth_range == NDCDepthRange::NEGATIVE_ONE_TO_ONE)
54+
return {
55+
{1.f / (aspect_ratio * fov_half_tan), 0, 0, 0},
56+
{0, 1.f / (fov_half_tan), 0, 0},
57+
{0, 0, (far + near) / (far - near), -(2.f * far * near) / (far - near)},
58+
{0, 0, 1, 0},
59+
};
60+
std::unreachable();
6061
}
6162
} // namespace omath::source_engine

source/engines/unity_engine/formulas.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@ namespace omath::unity_engine
4040
if (ndc_depth_range == NDCDepthRange::ZERO_TO_ONE)
4141
return omath::mat_perspective_right_handed<float, MatStoreType::ROW_MAJOR, NDCDepthRange::ZERO_TO_ONE>(
4242
field_of_view, aspect_ratio, near, far);
43-
44-
return omath::mat_perspective_right_handed(field_of_view, aspect_ratio, near, far);
43+
if (ndc_depth_range == NDCDepthRange::NEGATIVE_ONE_TO_ONE)
44+
return omath::mat_perspective_right_handed<float, MatStoreType::ROW_MAJOR,
45+
NDCDepthRange::NEGATIVE_ONE_TO_ONE>(field_of_view, aspect_ratio,
46+
near, far);
47+
std::unreachable();
4548
}
4649
} // namespace omath::unity_engine

0 commit comments

Comments
 (0)