-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCamera.cpp
More file actions
65 lines (48 loc) · 1.62 KB
/
Camera.cpp
File metadata and controls
65 lines (48 loc) · 1.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
#include "Camera.h"
#include <QtMath>
Camera::Camera(Transform transform)
: cameraTransform(transform)
{
}
Camera::Camera(QVector3D position, QQuaternion rotation, QVector3D scale)
: cameraTransform(position, rotation, scale)
{
}
Transform& Camera::transform()
{
return cameraTransform;
}
const Transform& Camera::transform() const
{
return cameraTransform;
}
void Camera::setTransform(Transform transform)
{
cameraTransform = transform;
}
QMatrix4x4 Camera::perspective(float fieldOfView, float aspectRatio, float nearPlane, float farPlane)
{
float coeff1 = 1 / std::tan(qDegreesToRadians(fieldOfView / 2));
float coeff2 = aspectRatio * coeff1;
float coeff3 = (farPlane + nearPlane) / (farPlane - nearPlane);
float coeff4 = 2 * farPlane * nearPlane / (nearPlane - farPlane);
return QMatrix4x4(
0, coeff1, 0, 0,
0, 0, coeff2, 0,
coeff3, 0, 0, coeff4,
1, 0, 0, 0);
}
QMatrix4x4 Camera::orthographic(float right, float left, float top, float bottom, float nearPlane, float farPlane)
{
float coeff1 = 2 / (right - left);
float coeff2 = (right + left) / (left - right);
float coeff3 = 2 / (top - bottom);
float coeff4 = (top + bottom) / (bottom - top);
float coeff5 = 2 / (farPlane - nearPlane);
float coeff6 = (farPlane + nearPlane) / (nearPlane - farPlane);
return QMatrix4x4(
0, coeff1, 0, coeff2,
0, 0, coeff3, coeff4,
coeff5, 0, 0, coeff6,
0, 0, 0, 1);
}