-
Notifications
You must be signed in to change notification settings - Fork 2
/
camera.cpp
77 lines (63 loc) · 1.59 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
#include "camera.h"
#include <math.h>
#include <qgl.h>
Camera::Camera() :
_position(0, 0, 0),
_direction(0, 0, -1),
_pitch(0),
_yaw(0),
_roll(0) {
}
Camera::~Camera() {
}
void Camera::apply() {
glRotatef(-_pitch , 1.0, 0.0, 0.0);
glRotatef(-_yaw , 0.0, 1.0, 0.0);
glRotatef(-_roll , 0.0, 0.0, 1.0);
glTranslatef(-_position.x, -_position.y, -_position.z);
}
void Camera::move(const Vector3D &direction) {
_position += direction;
}
void Camera::rotate(float pitch, float yaw, float roll, bool offset) {
if(offset) {
_pitch += pitch;
_yaw += yaw;
_roll += roll;
}
else {
_pitch = pitch;
_yaw = yaw;
_roll = roll;
}
float pitch_rad = M_PI * _pitch / 180;
float yaw_rad = M_PI * _yaw / 180;
//@FIXME: this calculation is wrong
// We should do matrix multiplying to transform local direction to world coord
_direction.x = sin(yaw_rad) * pitch_rad;
_direction.y = sin(pitch_rad);
_direction.z = cos(yaw_rad) * pitch_rad;
}
void Camera::pitch(float value) {
rotate(value, _yaw, _roll, false);
}
void Camera::yaw(float value) {
rotate(_pitch, value, _roll, false);
}
void Camera::roll(float value) {
rotate(_pitch, _yaw, value, false);
}
void Camera::move_forwards(float distance) {
Vector3D direction;
direction.x = _direction.x * distance;
direction.y = _direction.y * distance;
direction.z = _direction.z * distance;
_position += direction;
}
void Camera::move_right(float distance) {
Vector3D direction;
direction.x = - _direction.x * distance;
direction.y = 0.0;
direction.z = _direction.z * distance;
_position += direction;
}