-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgl_camera.cpp
266 lines (238 loc) · 7.51 KB
/
gl_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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#include "gl_camera.h"
#include "gl_screen.h"
#include "opengta.h"
#include "dataholder.h"
#include <iostream>
#include "log.h"
#include "blockdata.h"
using namespace OpenGTA;
float slope_height_offset(unsigned char slope_type, float dx, float dz);
namespace OpenGL {
/* implementation mainly taken from:
*
* "Talk to me like I'm a 3 year old!" Programming Lessons
* by DigiBen ([email protected])
*
*/
Camera::Camera() : eye(), center(), up(), doRotate(false),
camGravity(false), gameCamMode(false), followTarget(¢er) {
interpolateStart = 0;
interpolateEnd = 0;
}
void Camera::update_game(Uint32 ticks) {
Vector3D delta(center - *followTarget);
//INFO << delta.x << ", " << delta.y << ", " << delta.z << std::endl;
float height_dist = fabs(delta.y);
delta.y = 0;
if (camGravity) {
if (center.y - followTarget->y > 4.1) {
delta.y = 0.001f * height_dist;
}
else if (center.y - followTarget->y < 3.9) {
delta.y = -0.001f * height_dist;
}
}
//INFO << center.y << " " << followTarget->y<< " " << height_dist << std::endl;
center += -delta;
eye += -delta;
gluLookAt(eye.x, eye.y, eye.z, center.x, center.y, center.z,
up.x, up.y, up.z);
}
void Camera::setFollowMode(const Vector3D & target) {
followTarget = ⌖
//INFO << "following " << target.x << ", " << target.y << ", " << target.z << std::endl;
gameCamMode = 1;
}
void Camera::releaseFollowMode() {
followTarget = ¢er;
gameCamMode = 0;
}
void Camera::update(Uint32 ticks) {
if (gameCamMode) {
update_game(ticks);
return;
}
moveByMouse();
float x,y,z;
x = floor(eye.x);
y = floor(eye.y);
z = floor(eye.z);
int do_grav = 1;
float delta_y = 0;
if (camGravity && do_grav) {
center.y -= 0.1f * ticks/40.0f;
eye.y -= 0.1f * ticks/40.0f;
}
OpenGTA::Map & map = OpenGTA::MapHolder::Instance().get();
if (y < map.getNumBlocksAtNew(PHYSFS_uint8(x), PHYSFS_uint8(z)) && y > 0.0f) {
OpenGTA::Map::BlockInfo * block = map.getBlockAtNew(PHYSFS_uint8(x), PHYSFS_uint8(z), PHYSFS_uint8(y));
if (block->blockType() > 0 && block->blockType() <= 5) {
float bz = slope_height_offset(block->slopeType(), eye.x - x, eye.z - z);
if (block->slopeType() == 0 && (block->blockType() != 5 && block->blockType() != 6))
bz -= 1;
//INFO << int(block->blockType()) << ", " << eye.y << ", " <<
// eye.y - y << ", " << eye.y - y - bz << ", " << bz << std::endl;
float react_delta = 0.3f;
if (block->blockType() == 5 || block->blockType() == 6)
react_delta = 0.0f;
if (eye.y - y - bz < react_delta) {
//do_grav = 0;
Vector3D new_eye(eye);
new_eye.y = y + bz + react_delta;
delta_y = new_eye.y - eye.y;
center.y = center.y - eye.y + new_eye.y;
eye.y = new_eye.y;
}
}
#if 0
if (y >= 1.0f) {
block = map.getBlockAtNew(PHYSFS_uint8(x), PHYSFS_uint8(z), PHYSFS_uint8(y-1));
if (block->blockType() > 0) {
INFO << "below is " << int(block->blockType()) << std::endl;
center.y = center.y - eye.y + y + 0.3f;
eye.y = y + 0.3f;
}
}
#endif
}
y -= 1;
if (y < map.getNumBlocksAtNew(PHYSFS_uint8(x), PHYSFS_uint8(z)) && y > 0.0f) {
OpenGTA::Map::BlockInfo * block = map.getBlockAtNew(PHYSFS_uint8(x), PHYSFS_uint8(z), PHYSFS_uint8(y));
if (block->blockType() == 5 || block->blockType() == 6) {
float bz = slope_height_offset(block->slopeType(), eye.x - x, eye.z - z);
//INFO << eye.y << ", " << y << " bz " << bz << std::endl;
if (eye.y - y - bz < 0.4f) {
Vector3D new_eye(eye);
new_eye.y = y + bz + 0.4;
delta_y = new_eye.y - eye.y;
//INFO << "setting " << new_eye.y << std::endl;
center.y = center.y - eye.y + new_eye.y;
eye.y = new_eye.y;
do_grav = 0;
}
}
}
/*
if (camGravity && do_grav) {
Vector dn(0, -0.1f*do_grav, 0);
eye += dn;
center += dn;
}*/
/*
if (camGravity && do_grav) {
center.y -= 0.1f * ticks/40.0f;
eye.y -= 0.1f * ticks/40.0f;
}*/
if (interpolateStart) {
float as_one = float(interpolateStart - 1) / interpolateEnd;
Vector3D now = (1 - as_one) * interpolateFrom + as_one * interpolateTo;
center = center - eye + now;
eye = now;
interpolateStart += ticks;
if (interpolateStart > interpolateEnd)
interpolateStart = 0;
}
else {
if (speed > 0.01f || speed < -0.01f) {
Vector3D v = center - eye;
if (camGravity)
v.y = delta_y;
v = v.Normalized();
center += speed * ticks/40.0f * v;
eye += speed * ticks/40.0f * v;
//INFO << v.y << std::endl;
}
if (doRotate)
rotateAround(Vector3D(center.x, 0, center.z), 0, 0.01f, 0);
}
gluLookAt(eye.x, eye.y, eye.z, center.x, center.y, center.z,
up.x, up.y, up.z);
}
void Camera::setRotating(bool demo) {
doRotate = demo;
}
void Camera::setCamGravity(bool demo) {
camGravity = demo;
}
void Camera::setVectors(const Vector3D & e, const Vector3D & c, const Vector3D & u) {
eye = e;
center = c;
up = u;
}
void Camera::setSpeed(float new_speed) {
speed = new_speed;
}
void Camera::translateBy(const Vector3D & t) {
eye += t;
center += t;
}
void Camera::translateTo(const Vector3D & e) {
Vector3D rel = eye - e;
translateBy(rel);
}
void Camera::rotateView(float x, float y, float z) {
Vector3D v = center - eye;
if (x) {
center.z = eye.z + sin(x) * v.y + cos(x) * v.z;
center.y = eye.y + cos(x) * v.y - sin(x) * v.z;
}
if (y) {
center.z = eye.z + sin(y) * v.x + cos(y) * v.z;
center.x = eye.x + cos(y) * v.x - sin(y) * v.z;
}
if (z) {
center.x = eye.x + sin(z) * v.y + cos(z) * v.x;
center.y = eye.y + cos(z) * v.y - sin(z) * v.x;
}
}
void Camera::rotateAround(const Vector3D & lookAt, float x, float y, float z) {
Vector3D v = eye - lookAt;
if (x) {
eye.z = lookAt.z + sin(x) * v.y + cos(x) * v.z;
eye.y = lookAt.y + cos(x) * v.y - sin(x) * v.z;
}
if (y) {
eye.z = lookAt.z + sin(y) * v.x + cos(y) * v.z;
eye.x = lookAt.x + cos(y) * v.x - sin(y) * v.z;
}
if (z) {
eye.x = lookAt.x + sin(z) * v.y + cos(z) * v.x;
eye.y = lookAt.y + cos(z) * v.y - sin(z) * v.x;
}
}
void Camera::moveByMouse() {
Screen & screen = ScreenHolder::Instance();
int w, h;
w = screen.getWidth() / 2;
h = screen.getHeight() / 2;
int mx, my;
SDL_GetMouseState(&mx, &my);
SDL_WarpMouse(w, h);
if ((mx == w) && (my == h))
return;
float rot_x = (float(w) - mx) / 100;
float rot_y = (float(h) - my) / 100;
center.y += rot_y * 8;
if (center.y - eye.y > 15)
center.y = eye.y + 15;
else if (center.y - eye.y < -15)
center.y = eye.y - 15;
rotateView(0, -rot_x, 0);
}
void Camera::interpolate(const Vector3D & to, const Uint32 & start, const Uint32 & end) {
interpolateFrom = eye;
interpolateTo = to;
interpolateStart = start;
interpolateEnd = end;
}
#if 0
void QuaternionCamera::update(Uint32 ticks) {
float cur_ratio = 0;
quat step = exp(sampleLinear(ln_start, ln_end, cur_ratio));
mtx44 m = quatToRotationMatrix(step);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glMultMatrixf((float*)m.x);
}
#endif
}