-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrackBallCamera.cpp
More file actions
84 lines (73 loc) · 1.98 KB
/
TrackBallCamera.cpp
File metadata and controls
84 lines (73 loc) · 1.98 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
#include <QDebug>
#include "TrackBallCamera.h"
TrackBallCamera::TrackBallCamera() : Camera(NULL)
{
_xAngle = 0;
_yAngle = 0;
_up = 0;
_right = 0;
_distance = 20;
setType("TrackBall");
}
TrackBallCamera::~TrackBallCamera()
{
}
void TrackBallCamera::update(float elapsed)
{
Camera::update(elapsed);
}
void TrackBallCamera::updateMatrix()
{
QVector3D up(0, 1, 0);
Node::updateMatrix();
QMatrix4x4 directionMatrix;
directionMatrix.rotate(_yAngle, 0, 1, 0);
directionMatrix.rotate(_xAngle, 1, 0, 0);
QVector3D direction(0, 0, 1);
direction = directionMatrix * direction;
direction *= _distance;
QVector3D right = QVector3D::crossProduct(direction, up).normalized();
QVector3D realUp = QVector3D::crossProduct(direction, right).normalized();
QVector3D newTarget = _target + right * _right + realUp * _up;
_local.lookAt(direction + newTarget, newTarget, up);
}
void TrackBallCamera::setTarget(const QVector3D &target)
{
_target = target;
}
const QVector3D &TrackBallCamera::target() const
{
return _target;
}
void TrackBallCamera::mousePress(const QPoint &pos, Qt::KeyboardModifiers modifiers)
{
if (modifiers & Qt::ControlModifier) {
_lastTranslatePos = pos;
} else {
_lastRotatePos = pos;
}
}
void TrackBallCamera::mouseMove(const QPoint &pos, Qt::KeyboardModifiers modifiers)
{
if (modifiers & Qt::ControlModifier) {
_up += (pos.y() - _lastTranslatePos.y()) * (-0.08);
_right += (pos.x() - _lastTranslatePos.x()) * 0.08;
_lastTranslatePos = pos;
} else {
float diffX = pos.x() - _lastRotatePos.x();
float diffY = pos.y() - _lastRotatePos.y();
_lastRotatePos = pos;
_yAngle -= diffX;
_xAngle -= diffY;
if (_xAngle > 89)
_xAngle = 89;
else if (_xAngle < -89)
_xAngle = -89;
}
}
void TrackBallCamera::wheel(float delta)
{
_distance -= delta / 5;
if (_distance < 0)
_distance = 0;
}