-
Notifications
You must be signed in to change notification settings - Fork 0
/
Projection.cpp
36 lines (31 loc) · 912 Bytes
/
Projection.cpp
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
30
31
32
33
34
35
36
#include <cmath>
#include "Projection.h"
Projection::Projection(Real fov, Real znear, Real zfar, unsigned width, unsigned height)
: m_fov(fov)
, m_znear(znear)
, m_zfar(zfar)
, m_width(width)
, m_height(height)
{ }
Matrix4 Projection::GetProjectionMatrix() const
{
Real aspect = (Real)m_width / (Real)m_height;
Real scalex = 1 / tan(m_fov * DEG_TO_RAD * 0.5f);
Real scaley = scalex * aspect;
Real zClip1 = -((m_zfar + m_znear) / (m_zfar - m_znear));
Real zClip2 = -((2 * m_znear * m_zfar) / (m_zfar - m_znear));
return {{{
{ scalex, 0.0, 0.0, 0.0 },
{ 0.0, scaley, 0.0, 0.0 },
{ 0.0, 0.0, zClip1, zClip2 },
{ 0.0, 0.0, -1.0, 0.0 }
}}};
}
Real Projection::ToScreenX(Real x) const
{
return ((x + 1.0f) / 2.0f) * m_width;
}
Real Projection::ToScreenY(Real y) const
{
return (1.0f - ((y + 1.0f) / 2.0f)) * m_height;
}