This repository was archived by the owner on Jan 21, 2022. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathcamera.cpp
More file actions
140 lines (117 loc) · 2.62 KB
/
Copy pathcamera.cpp
File metadata and controls
140 lines (117 loc) · 2.62 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#define _USE_MATH_DEFINES
#include "camera.h"
#include <math.h>
Camera::Camera(CameraPerspective perspective)
: viewMatrix_(),
trackedObject_(nullptr),
resetPosition_()
,
minY_(1),
orientation_(0),
position_(),
target_(),
up_(),
right_(),
initialUp_(),
resetOrientation_(0)
{
if (perspective == Default)
{
up_.setY(1);
right_.setX(1);
target_.setZ(-1);
}
else if (perspective == BirdsEye)
{
up_.setX(1);
right_.setZ(1);
target_.setY(-1);
}
initialUp_ = QVector4D(up_, 1);
UpdateViewMatrix();
}
void Camera::Translate(float x, float y, float z)
{
Translate(QVector3D(x, y, z));
}
void Camera::Translate(QVector3D translation)
{
SetPosition(position_ + translation);
}
void Camera::SetPosition(float x, float y, float z)
{
SetPosition(QVector3D(x, y, z));
}
void Camera::SetPosition(QVector3D position)
{
if (position.y() >= minY_)
{
position_ = position;
UpdateViewMatrix();
}
}
void Camera::SetToObjectPosition(GLObject* object)
{
SetPosition(object->GetPosition());
}
void Camera::UpdateRight()
{
right_ = -QVector3D::crossProduct(up_, target_).normalized();
}
void Camera::UpdateUp()
{
double cosp = cos(orientation_);
double sinp = sin(orientation_);
// TODO: Find init ctor for QMatrix3x3 (maybe use << ?)
QMatrix4x4 rotY(cosp, 0, sinp, 0, 0, 1, 0, 0, -sinp, 0, cosp, 0, 0, 0, 0, 1);
// Don't accumulate the rotations on "up_", small errors will increase over time
// Always use the initial up_ vector and rotate it to the current absolute orientation
up_ = (rotY * initialUp_).toVector3D();
}
void Camera::SetOrientation(float orientation)
{
orientation = fmod(orientation, 2 * M_PI);
orientation_ = orientation;
UpdateUp();
UpdateRight();
UpdateViewMatrix();
}
void Camera::RotateAroundYAxis(float rotationRad)
{
SetOrientation(orientation_ + rotationRad);
}
void Camera::ResetPosition()
{
SetPosition(resetPosition_);
}
void Camera::ResetOrientation()
{
SetOrientation(resetOrientation_);
}
void Camera::ResetAll()
{
trackedObject_ = nullptr;
ResetPosition();
ResetOrientation();
}
void Camera::SetUp(QVector3D up)
{
up_ = up;
UpdateViewMatrix();
}
void Camera::UpdateViewMatrix()
{
viewMatrix_.setToIdentity();
viewMatrix_.lookAt(position_, position_ + target_, up_);
}
/*
void Camera::SetTarget(float x, float y, float z)
{
SetTarget(QVector3D(x, y, z));
}
void Camera::SetTarget(QVector3D target)
{
this->target = target;
UpdateViewMatrix();
}
*/