-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcamera.cpp
144 lines (130 loc) · 3.46 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
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
135
136
137
138
139
140
141
142
143
144
#include "camera.h"
#include "terrain.h"
#include "gardenwidget.h"
#include <QtCore/qmath.h>
#include <GL/glu.h>
const float MOVE_STEP = 16 * 2;
const float LOOK_RANGE = 45 * M_PI / 180;
const float BORDER = 16;
Camera::Camera(QObject *parent) :
QObject(parent)
{
eyeX = eyeZ = 0;
theta = phi = 0;
select_terrain = false;
}
void Camera::init(Terrain *t) {
terrain = t;
eyeY = terrain->snowed_height(eyeX + WIDTH / 2, eyeZ + LENGTH / 2) + HEIGHT / 24;
}
void Camera::toggle_select_terrain() {
if (!select_terrain)
terrain->highlight(eyeX + WIDTH/2, eyeY, eyeZ + LENGTH/2, theta, phi);
else
terrain->disable_highlight();
select_terrain = !select_terrain;
}
void Camera::check_border() {
if (eyeX < -WIDTH/2 + BORDER)
eyeX = -WIDTH/2 + BORDER;
if (eyeZ < -LENGTH/2 + BORDER)
eyeZ = -LENGTH/2 + BORDER;
if (eyeX > WIDTH/2 - BORDER)
eyeX = WIDTH/2 - BORDER;
if (eyeZ > LENGTH/2 - BORDER)
eyeZ = LENGTH/2 - BORDER;
if (eyeY > HEIGHT - BORDER)
eyeY = HEIGHT - BORDER;
if (eyeY < BORDER)
eyeY = BORDER;
}
float Camera::getPhi() const
{
return phi;
}
void Camera::setPhi(float value)
{
phi = value;
}
float Camera::getTheta() const
{
return theta;
}
void Camera::setTheta(float value)
{
theta = value;
}
void Camera::move(Direction dir) {
switch (dir) {
case LEFT:
eyeX -= MOVE_STEP * qCos(theta);
eyeZ += MOVE_STEP * qSin(theta);
break;
case RIGHT:
eyeX += MOVE_STEP * qCos(theta);
eyeZ -= MOVE_STEP * qSin(theta);
break;
case FORWARD:
eyeX -= MOVE_STEP * qSin(theta);
eyeZ -= MOVE_STEP * qCos(theta);
break;
case BACKWARD:
eyeX += MOVE_STEP * qSin(theta);
eyeZ += MOVE_STEP * qCos(theta);
break;
//上下移动不受地形高度限制
case UP:
eyeY += MOVE_STEP;
check_border();
if (select_terrain)
terrain->highlight(eyeX + WIDTH/2, eyeY, eyeZ + LENGTH/2, theta, phi);
return;
case DOWN:
eyeY -= MOVE_STEP;
check_border();
if (select_terrain)
terrain->highlight(eyeX + WIDTH/2, eyeY, eyeZ + LENGTH/2, theta, phi);
return;
default:
break;
}
check_border();
eyeY = terrain->snowed_height(eyeX + WIDTH / 2, eyeZ + LENGTH / 2) + HEIGHT / 24;
if (select_terrain)
terrain->highlight(eyeX + WIDTH/2, eyeY, eyeZ + LENGTH/2, theta, phi);
}
void Camera::look(Direction dir, float rate) {
switch (dir) {
case LEFT:
theta += rate * LOOK_RANGE;
break;
case RIGHT:
theta -= rate * LOOK_RANGE;
break;
case UP:
phi -= rate * LOOK_RANGE;
if (phi > 89.999 * M_PI / 180)
phi = 89.999 * M_PI / 180;
if (phi < -89.999 * M_PI / 180)
phi = -89.999 * M_PI / 180;
break;
case DOWN:
phi += rate * LOOK_RANGE;
if (phi > 89.999 * M_PI / 180)
phi = 89.999 * M_PI / 180;
if (phi < -89.999 * M_PI / 180)
phi = -89.999 * M_PI / 180;
break;
default:
break;
}
if (select_terrain)
terrain->highlight(eyeX + WIDTH/2, eyeY, eyeZ + LENGTH/2, theta, phi);
}
void Camera::setCamera() {
//左转theta为正
//俯视phi为正
gluLookAt(eyeX, eyeY, eyeZ,
eyeX - qCos(phi) * qSin(theta), eyeY - qSin(phi), eyeZ - qCos(phi) * qCos(theta),
0, 1, 0);
}