-
Notifications
You must be signed in to change notification settings - Fork 1
/
camera.cpp
97 lines (90 loc) · 3.23 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
87
88
89
90
91
92
93
94
95
96
97
#include "camera.h"
#include <QKeyEvent>
#include <QOpenGLWidget>
Camera::Camera() {
}
void Camera::move(float x, float y, float z) {
QVector3D right = QVector3D::crossProduct(m_front, m_up).normalized();
m_pos += m_front * z;
m_pos += right * x;
m_pos += m_up *y;
}
void Camera::look(float yaw, float pitch, float roll) {
m_yaw += yaw;
while (m_yaw >= 360)
m_yaw -= 360;
while (m_yaw < 0)
m_yaw += 360;
m_pitch += pitch;
if (m_pitch > 89)
m_pitch = 89;
if (m_pitch < -89)
m_pitch = -89;
QVector3D _front{ 0,0,-1 };
QMatrix4x4 _mat;
_mat.setToIdentity();
_mat.rotate(m_pitch, 1, 0, 0);
_mat.rotate(m_yaw, 0, 1, 0);
m_front = _front * _mat;
}
void Camera::update() {
auto _move = m_move.normalized() * m_moveSpeed;
move(_move.x(), _move.y(), _move.z());
view.setToIdentity();
view.lookAt(m_pos, m_pos + m_front, m_up);
}
bool Camera::eventFilter(QObject *obj, QEvent *ev) {
// auto _widget = qobject_cast<QOpenGLWidget *>(obj);
auto widget = qobject_cast<QOpenGLWidget *>(obj);
if (widget) {
if (ev->type() == QEvent::KeyPress) {
auto event = static_cast<QKeyEvent*>(ev);
if (event->key() == Qt::Key_Escape) {
widget->setMouseTracking(false);
widget->setCursor(Qt::ArrowCursor);
} else if (event->key() == Qt::Key_W) {
m_move.setZ(1);
} else if (event->key() == Qt::Key_S) {
m_move.setZ(-1);
} else if (event->key() == Qt::Key_D) {
m_move.setX(1);
} else if (event->key() == Qt::Key_A) {
m_move.setX(-1);
} else if (event->key() == Qt::Key_Up) {
m_move.setY(1);
} else if (event->key() == Qt::Key_Down) {
m_move.setY(-1);
}
} else if (ev->type() == QEvent::KeyRelease) {
auto event = static_cast<QKeyEvent*>(ev);
if (event->key() == Qt::Key_W) {
m_move.setZ(0);
} else if (event->key() == Qt::Key_S) {
m_move.setZ(0);
} else if (event->key() == Qt::Key_D) {
m_move.setX(0);
} else if (event->key() == Qt::Key_A) {
m_move.setX(0);
} else if (event->key() == Qt::Key_Up) {
m_move.setY(0);
} else if (event->key() == Qt::Key_Down) {
m_move.setY(0);
}
} else if (ev->type() == QEvent::MouseButtonPress) {
auto lastPos = widget->mapToGlobal(widget->rect().center());
QCursor::setPos(lastPos);
widget->setMouseTracking(true);
widget->setCursor(Qt::BlankCursor);
} else if (ev->type() == QEvent::MouseMove) {
auto event = static_cast<QMouseEvent*>(ev);
auto lastPos = widget->mapToGlobal(widget->rect().center());
QCursor::setPos(lastPos);
auto move = event->globalPos() - lastPos;
look(move.x() * m_lookSpeed, move.y() * m_lookSpeed, 0);
} else if (ev->type() == QEvent::Leave) {
widget->setMouseTracking(false);
widget->setCursor(Qt::ArrowCursor);
}
}
return false;
}