1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
Eigen::Matrix4f get_projection_matrix(float eye_fov, float aspect_ratio,
float zNear, float zFar)
{
// Students will implement this function
Eigen::Matrix4f projection = Eigen::Matrix4f::Identity();
// Implement this function [Implemented]
// Create the projection matrix for the given parameters.
// Then return it.
float n = zNear, f = zFar;
Eigen::Matrix4f M_persp2ortho{{n, 0, 0, 0},
{0, n, 0, 0},
{0, 0, n + f, -n * f},
{0, 0, 1, 0}};
auto a = static_cast<float>(eye_fov * MY_PI / 180. / 2.);
float t = abs(n) * tan(a), b = -t, l = b / aspect_ratio, r = -l;
auto M_ortho = Eigen::Matrix4f{{2 / (r - l), 0, 0, 0},
{0, 2 / (t - b), 0, 0},
{0, 0, 2 / (n - f), 0},
{0, 0, 0, 1}} *
Eigen::Matrix4f{{1, 0, 0, -(r + l) / 2},
{0, 1, 0, -(t + b) / 2},
{0, 0, 1, -(n + f) / 2},
{0, 0, 0, 1}};
projection = M_ortho * M_persp2ortho * projection;
return projection;
}
|