-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCamera.cpp
executable file
·87 lines (75 loc) · 2.45 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
#include "Camera.h"
#include <glm/gtx/transform.hpp>
void EulerCamera::Move()
{
glm::vec3 forward(glm::sin(Yaw), 0.f, -glm::cos(Yaw));
glm::vec3 right (glm::cos(Yaw), 0.f, glm::sin(Yaw));
if (MoveForward) Position += MoveSpeed * forward;
if (MoveBackward) Position -= MoveSpeed * forward;
if (StrafeLeft) Position -= MoveSpeed * right;
if (StrafeRight) Position += MoveSpeed * right;
}
void EulerCamera::Turn(float x, float y)
{
Yaw += x * TurnSpeed;
Pitch -= y * TurnSpeed;
/*if (Pitch > glm::half_pi<float>())
Pitch = glm::half_pi<float>();
else if (Pitch < -glm::half_pi<float>())
Pitch = -glm::half_pi<float>();*/
}
glm::mat4 EulerCamera::GetMatrix() const
{
glm::mat4 translate(glm::translate(-Position));
glm::mat4 yaw (glm::rotate(-Yaw, glm::vec3(0.f, -1.f, 0.f)));
glm::mat4 pitch(glm::rotate(-Pitch, glm::vec3(1.f, 0.f, 0.f)));
return pitch * yaw * translate;
}
void QuaternionCamera::Move()
{
glm::vec3 forward(
glm::mat3_cast(glm::inverse(Orientation)) *
glm::vec3(0.f, 0.f, -1.f));
glm::vec3 right(
glm::mat3_cast(glm::inverse(Orientation)) *
glm::vec3(1.f, 0.f, 0.f));
if (MoveForward) Position += MoveSpeed * forward;
if (MoveBackward) Position -= MoveSpeed * forward;
if (StrafeLeft) Position -= MoveSpeed * right;
if (StrafeRight) Position += MoveSpeed * right;
}
void QuaternionCamera::Turn(float x, float y)
{
Orientation =
glm::angleAxis(x * TurnSpeed, glm::vec3(0.f, 1.f, 0.f)) * Orientation;
Orientation =
glm::angleAxis(y * TurnSpeed, glm::vec3(1.f, 0.f, 0.f)) * Orientation;
}
glm::mat4 QuaternionCamera::GetMatrix() const
{
glm::mat4 translate(glm::translate(-Position));
glm::mat4 rotate(glm::mat4_cast(Orientation));
return rotate * translate;
}
void FlyCam::Move()
{
glm::vec3 right = glm::normalize(glm::cross(Forward, Up));
if (MoveForward) Position += MoveSpeed * Forward;
if (MoveBackward) Position -= MoveSpeed * Forward;
if (StrafeLeft) Position -= MoveSpeed * right;
if (StrafeRight) Position += MoveSpeed * right;
}
void FlyCam::Turn(float x, float y)
{
EulerCamera::Turn(x, y);
this->Forward = glm::normalize(glm::vec3(
cos(glm::radians(Yaw)) * cos(glm::radians(Pitch)),
sin(glm::radians(Pitch)),
sin(glm::radians(Yaw)) * cos(glm::radians(Pitch))));
glm::vec3 right = glm::normalize(glm::cross(Forward, glm::vec3(0, 1.0f, 0)));
this->Up = glm::normalize(glm::cross(right, Forward));
}
glm::mat4 FlyCam::GetMatrix() const
{
return glm::lookAt(Position, Position + Forward, Up);
}