IceShard
1
A personal game engine project, with development focused on 2D/2.5D games.
Toggle main menu visibility
Loading...
Searching...
No Matches
math
public
ice
math
projection.hxx
Go to the documentation of this file.
1
3
4
#pragma once
5
#include <
ice/math.hxx
>
6
7
namespace
ice::math
8
{
9
10
inline
auto
perspective_fovx
(
11
rad
field_of_view_horizontal,
12
f32
aspect_ratio,
13
f32
near_plane,
14
f32
far_plane
15
)
noexcept
->
mat<4, 4, f32>
;
16
17
inline
auto
perspective_fovy
(
18
rad
field_of_view_vertical,
19
f32
aspect_ratio,
20
f32
near_plane,
21
f32
far_plane
22
)
noexcept
->
mat<4, 4, f32>
;
23
24
constexpr
auto
orthographic
(
25
f32
left,
f32
right,
26
f32
bottom,
f32
top,
27
f32
near_plane,
f32
far_plane
28
)
noexcept
->
mat<4, 4, f32>
;
29
30
namespace
math_detail
31
{
32
33
constexpr
auto
perspective_rhs_lrbt
(
34
f32
left,
35
f32
right,
36
f32
bottom,
37
f32
top,
38
f32
near_plane,
39
f32
far_plane
40
)
noexcept
->
mat<4, 4, f32>
41
{
42
mat<4, 4, f32>
result{ };
43
44
result.
v
[0][0] = 2.f * near_plane / (right - left);
45
result.
v
[1][1] = 2.f * near_plane / (bottom - top);
46
result.
v
[2][0] = (right + left) / (right - left);
47
result.
v
[2][1] = (bottom + top) / (bottom - top);
48
result.
v
[2][2] = far_plane / (near_plane - far_plane);
49
result.
v
[2][3] = -1.f;
50
result.
v
[3][2] = (near_plane * far_plane) / (near_plane - far_plane);
51
52
// #NOTE: The below values should be used when we want to produce clip.z between -1.f .. 1.f
53
//result.v[2][2] = (plane_far + plane_near) / (plane_near - plane_far);
54
//result.v[2][3] = -1.f;
55
//result.v[3][2] = 2 * (plane_far * plane_near) / (plane_near - plane_far);
56
return
result;
57
}
58
59
}
// namespace detail
60
61
inline
auto
perspective_fovx
(
62
rad
field_of_view_horizontal,
63
f32
aspect_ratio,
64
f32
near_plane,
65
f32
far_plane
66
)
noexcept
->
mat<4, 4, f32>
67
{
68
f32
const
tan_half_fovx =
ice::math::tan
(field_of_view_horizontal * 0.5f);
69
70
f32
right = tan_half_fovx * near_plane;
71
f32
left = -right;
72
f32
top = right / aspect_ratio;
73
f32
bottom = -top;
74
75
return
math_detail::perspective_rhs_lrbt
(left, right, bottom, top, near_plane, far_plane);
76
}
77
78
inline
auto
perspective_fovy
(
79
rad
field_of_view_vertical,
80
f32
aspect_ratio,
81
f32
near_plane,
82
f32
far_plane
83
)
noexcept
->
mat<4, 4, f32>
84
{
85
f32
const
tan_half_fovy =
ice::math::tan
(field_of_view_vertical * 0.5f);
86
87
f32
top = tan_half_fovy * near_plane;
88
f32
bottom = -top;
89
f32
right = top * aspect_ratio;
90
f32
left = -right;
91
92
return
math_detail::perspective_rhs_lrbt
(left, right, bottom, top, near_plane, far_plane);
93
}
94
95
constexpr
auto
orthographic
(
96
vec<2, f32>
left_right,
97
vec<2, f32>
bottom_top,
98
vec<2, f32>
near_far
99
)
noexcept
->
mat<4, 4, f32>
100
{
101
return
orthographic
(
102
left_right.v[0][0],
103
left_right.v[0][1],
104
bottom_top.v[0][0],
105
bottom_top.v[0][1],
106
near_far.v[0][0],
107
near_far.v[0][1]
108
);
109
}
110
111
constexpr
auto
orthographic
(
112
f32
left,
f32
right,
113
f32
bottom,
f32
top,
114
f32
near_plane,
f32
far_plane
115
)
noexcept
->
mat<4, 4, f32>
116
{
117
mat<4, 4, f32>
result{ };
118
#if 1
119
result.
v
[0][0] = 2.f / (right - left);
120
result.
v
[1][1] = 2.f / (bottom - top);
121
result.
v
[2][2] = 1.f / (near_plane - far_plane);
122
result.
v
[3][0] = -(right + left) / (right - left);
123
result.
v
[3][1] = -(bottom + top) / (bottom - top);
124
result.
v
[3][2] = near_plane / (near_plane - far_plane);
125
result.
v
[3][3] = 1.f;
126
#else
127
result.
v
[0][0] = 2.f / (right - left);
128
result.
v
[1][1] = 2.f / (top - bottom);
129
result.
v
[2][2] = 1.f / (near_plane - far_plane);
130
result.
v
[3][0] = -(right + left) / (right - left);
131
result.
v
[3][1] = -(top + bottom) / (top - bottom);
132
result.
v
[3][2] = near_plane / (near_plane - far_plane);
133
result.
v
[3][3] = 1.f;
134
#endif
135
return
result;
136
}
137
138
}
// namespace ice::math
math.hxx
ice::math::math_detail
Definition
projection.hxx:31
ice::math::math_detail::perspective_rhs_lrbt
constexpr auto perspective_rhs_lrbt(f32 left, f32 right, f32 bottom, f32 top, f32 near_plane, f32 far_plane) noexcept -> mat< 4, 4, f32 >
Definition
projection.hxx:33
ice::math
Definition
algorithm.hxx:8
ice::math::rad
rad32 rad
Definition
angles.hxx:170
ice::math::orthographic
constexpr auto orthographic(f32 left, f32 right, f32 bottom, f32 top, f32 near_plane, f32 far_plane) noexcept -> mat< 4, 4, f32 >
Definition
projection.hxx:111
ice::math::vec
mat< Size, 1, T > vec
Definition
vector.hxx:172
ice::math::tan
constexpr auto tan(rad radians) noexcept -> f32
Definition
common.hxx:203
ice::math::perspective_fovy
auto perspective_fovy(rad field_of_view_vertical, f32 aspect_ratio, f32 near_plane, f32 far_plane) noexcept -> mat< 4, 4, f32 >
Definition
projection.hxx:78
ice::math::perspective_fovx
auto perspective_fovx(rad field_of_view_horizontal, f32 aspect_ratio, f32 near_plane, f32 far_plane) noexcept -> mat< 4, 4, f32 >
Definition
projection.hxx:61
ice::f32
float f32
Definition
types.hxx:16
ice::math::mat
Definition
matrix.hxx:12
ice::math::mat::v
T v[count_columns][count_rows]
Definition
matrix.hxx:17
Generated by
1.18.0