-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCamera.cpp
108 lines (98 loc) · 2.89 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
98
99
100
101
102
103
104
105
106
107
108
#include "Camera.h"
Camera::Camera() {
position.z = -90.f;
position.y = 100.f;
position.x = position.lx = position.angleVertical = position.ly = position.angle = position.deltaAngle = position.deltaAngleVertical = position.deltaMove = 0;
position.lz = -10.f;
speed = 17.f;
rotationSpeed = 0.05f;
}
Camera::~Camera() {
}
void Camera::computePos() {
position.x += position.deltaMove * position.lx * 0.1f;
if (position.y + position.deltaMove * position.ly * 0.1f > 0.f)
position.y += position.deltaMove * position.ly * 0.1f;
position.z += position.deltaMove * position.lz * 0.1f;
}
void Camera::computeDir() {
position.angle += position.deltaAngle;
position.lx = sin(position.angle);
position.lz = -cos(position.angle);
}
void Camera::computeDirVertical() {
if (position.deltaAngleVertical >= 0) {
if (position.angleVertical < 3.14f / 2.f) {
position.angleVertical += position.deltaAngleVertical;
position.ly = 2 * sin(position.angleVertical);
}
else {
position.ly = 5.f;
}
}
else {
if (position.angleVertical > -3.14f / 2.f) {
position.angleVertical += position.deltaAngleVertical;
position.ly = 2 * sin(position.angleVertical);
}
else {
position.ly = -5.f;
}
}
}
void Camera::refresh() {
computePos();
computeDir();
computeDirVertical();
gluLookAt(position.x, position.y, position.z,
position.x + position.lx, position.y + position.ly, position.z + position.lz,
0.0f, 1.0f, 0.0f);
}
void Camera::update(Action a, int key) {
if (a == NORMAL_DOWN) {
switch (key) {
case 'w':
position.deltaMove = speed;
break;
case 's':
position.deltaMove = -speed;
break;
}
}
if (a == NORMAL_UP) {
switch (key) {
case 'w':
case 's':
position.deltaMove = 0;
break;
}
}
if (a == ARROW_DOWN) {
switch (key) {
case GLUT_KEY_LEFT:
position.deltaAngle = -rotationSpeed;
break;
case GLUT_KEY_RIGHT:
position.deltaAngle = rotationSpeed;
break;
case GLUT_KEY_UP:
position.deltaAngleVertical = rotationSpeed;
break;
case GLUT_KEY_DOWN:
position.deltaAngleVertical = -rotationSpeed;
break;
}
}
if (a == ARROW_UP) {
switch (key) {
case GLUT_KEY_LEFT:
case GLUT_KEY_RIGHT:
position.deltaAngle = 0.0f;
break;
case GLUT_KEY_UP:
case GLUT_KEY_DOWN:
position.deltaAngleVertical = 0.0f;
break;
}
}
}