-
Notifications
You must be signed in to change notification settings - Fork 0
/
Camera.cpp
119 lines (98 loc) · 2.35 KB
/
Camera.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include "SolarSystem.h"
#include "Camera.h"
Camera::Camera()
: m_Viewport(0)
, m_Position(0)
, m_Rotation()
, m_ProjectionMatrix(1)
, m_ViewMatrix(1)
, m_ViewDirty(false)
{}
Camera::Camera( int screenWidth, int screenHeight )
: m_Viewport( 0, 0, screenWidth, screenHeight )
, m_Position(0)
, m_Rotation()
, m_ProjectionMatrix(1)
, m_ViewMatrix(1)
, m_ViewDirty( false )
{}
void Camera::SetViewport( int x, int y, int width, int height ) {
m_Viewport = glm::vec4( x, y, width, height );
glViewport(x, y, width, height);
}
glm::vec4 Camera::GetViewport() const
{
return m_Viewport;
}
void Camera::SetProjectionRH( float fov, float aspectRatio, float zNear, float zFar )
{
m_ProjectionMatrix = glm::perspective( glm::radians(fov), aspectRatio, zNear, zFar );
}
void Camera::ApplyViewMatrix()
{
UpdateViewMatrix();
}
void Camera::SetPosition( const glm::vec3& pos )
{
m_Position = pos;
m_ViewDirty = true;
}
glm::vec3 Camera::GetPosition() const
{
return m_Position;
}
void Camera::Translate( const glm::vec3& delta, bool local /* = true */ )
{
if ( local )
{
m_Position += m_Rotation * delta;
}
else
{
m_Position += delta;
}
m_ViewDirty = true;
}
void Camera::SetRotation( const glm::quat& rot )
{
m_Rotation = rot;
m_ViewDirty = true;
}
glm::quat Camera::GetRotation() const
{
return m_Rotation;
}
void Camera::SetEulerAngles( const glm::vec3& eulerAngles )
{
m_Rotation = glm::quat(glm::radians(eulerAngles));
}
glm::vec3 Camera::GetEulerAngles() const
{
return glm::degrees(glm::eulerAngles( m_Rotation ));
}
void Camera::Rotate( const glm::quat& rot )
{
m_Rotation = m_Rotation * rot;
m_ViewDirty = true;
}
glm::mat4 Camera::GetProjectionMatrix()
{
return m_ProjectionMatrix;
}
glm::mat4 Camera::GetViewMatrix()
{
UpdateViewMatrix();
return m_ViewMatrix;
}
void Camera::UpdateViewMatrix()
{
if ( m_ViewDirty )
{
glm::mat4 translate = glm::translate(-m_Position);
// Since we know the rotation matrix is orthonormalized, we can simply
// transpose the rotation matrix instead of inversing.
glm::mat4 rotate = glm::transpose(glm::toMat4(m_Rotation));
m_ViewMatrix = rotate * translate;
m_ViewDirty = false;
}
}