-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcamera.cpp
More file actions
134 lines (111 loc) · 2.48 KB
/
camera.cpp
File metadata and controls
134 lines (111 loc) · 2.48 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
#include "camera.h"
#include <QtMath>
#include <QtGlobal>
SphericalCamera::SphericalCamera():center(0.0,0.0,0.0),distance(10.0f),
fov(75.0f),Camera(),nearPlane(0.01f),farPlane(300.0f),
pitch(0.0f),yaw(0.0f),maxDistance(200.0f),minDistance(0.5f)
{
}
QMatrix4x4 SphericalCamera::getView()
{
QMatrix4x4 view;
view.lookAt(center+QVector3D(cos(pitch)*sin(yaw),sin(pitch),cos(pitch)*cos(yaw))*distance,center,QVector3D(0.0,1.0,0.0));
return view;
}
QMatrix4x4 SphericalCamera::getProjection()
{
QMatrix4x4 proj;
proj.perspective(fov,width/height,nearPlane,farPlane);
return proj;
}
void Camera::setWidth(float w)
{
width=w;
}
void Camera::setHeight(float h)
{
height=h;
}
void SphericalCamera::setFov(float fov)
{
this->fov=fov;
}
void SphericalCamera::setYaw(float angle)
{
yaw=angle-qFloor(angle/qDegreesToRadians(360.0f))*qDegreesToRadians(360.0f);
}
void SphericalCamera::setPitch(float angle)
{
pitch=qMax(qMin(angle,
qDegreesToRadians(80.0f)),
-qDegreesToRadians(80.0f));
}
void SphericalCamera::move(QVector2D delta)
{
setPitch(pitch+delta.y()*qDegreesToRadians(180.0f));
setYaw(yaw+delta.x()*qDegreesToRadians(180.0f));
}
void SphericalCamera::changeDistance(float change)
{
setDistance(distance+qLn(qAbs(change)+1.0)*(change>0.0?1.0:-1.0));
}
void SphericalCamera::setCenter(QVector3D c)
{
center=c;
}
void SphericalCamera::setDistance(float d)
{
distance=qMin(maxDistance,qMax(minDistance,d));
}
float SphericalCamera::getDistance() const
{
return distance;
}
float SphericalCamera::getMinDistance() const
{
return minDistance;
}
float SphericalCamera::getMaxDistance() const
{
return maxDistance;
}
float SphericalCamera::getDistanceNormalized() const
{
return distance-minDistance/
(maxDistance-minDistance);
}
OrthoCamera::OrthoCamera():zoom(1.0f),nearPlane(0.01f),farPlane(1000.0f)
{
}
void OrthoCamera::setDirection(QVector3D value)
{
direction=value;
}
void OrthoCamera::setPosition(QVector3D value)
{
position=value;
}
void OrthoCamera::setUp(QVector3D value)
{
up=value;
}
void OrthoCamera::translate(QVector3D vec)
{
position+=vec;
}
void OrthoCamera::setZoom(float value)
{
zoom=value;
}
QMatrix4x4 OrthoCamera::getView()
{
QMatrix4x4 view;
view.lookAt(position,position+direction,up);
return view;
}
QMatrix4x4 OrthoCamera::getProjection()
{
QMatrix4x4 proj;
proj.ortho(-zoom,zoom,-zoom,zoom,nearPlane,farPlane);
return proj;
}