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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
|
/*
render/camera.cc
This file is part of the Osirion project and is distributed under
the terms and conditions of the GNU General Public License version 2
*/
#include <cmath>
#include "render/camera.h"
#include "render/gl.h"
#include "render/state.h"
#include "core/entity.h"
#include "core/range.h"
#include "math/functions.h"
#include "sys/sys.h"
namespace render
{
const float MIN_DELTA = 10e-10;
const float COS_PI_4 = sqrt(2.0f) * 0.5f;
Camera::Camera(const Mode mode)
{
_mode = mode;
_distance = 1.0f;
_multiplier = 1.0f;
_target_entity = 0;
_freelook_direction = 0.0f;
_freelook_pitch = 0.0f;
_movement_direction = 0.0f;
_movement_pitch = 0.0f;
}
Camera::~Camera()
{
}
void Camera::set_mode(const Mode mode)
{
_mode = mode;
reset();
}
void Camera::set_multiplier(const float multiplier)
{
_multiplier = multiplier;
}
void Camera::set_freelook_direction(const float angle)
{
_freelook_direction = angle;
}
void Camera::set_freelook_pitch(const float angle)
{
_freelook_pitch = angle;
}
void Camera::set_movement_direction(const float speed)
{
_movement_direction = speed;
math::clamp(_movement_direction, -1.0f, 1.0f);
}
void Camera::set_movement_pitch(const float speed)
{
_movement_pitch = speed;
math::clamp(_movement_pitch, -1.0f, 1.0f);
}
void Camera::cycle_mode_next()
{
switch (mode()) {
case Free:
set_mode(Track);
break;
case Track:
set_mode(Cockpit);
break;
case Cockpit:
set_mode(Free);
break;
default:
break;
}
}
void Camera::cycle_mode_previous()
{
switch (mode()) {
case Cockpit:
set_mode(Track);
break;
case Free:
set_mode(Cockpit);
break;
case Track:
set_mode(Free);
break;
default:
break;
}
}
void Camera::reset()
{
if (target())
{
_target_location.assign(target()->location());
_target_axis.assign(target()->axis());
_distance = 0.0f;
}
else
{
_location.clear();
_target_axis.clear();
_distance = 0.0f;
}
_axis.assign(_target_axis);
if (mode() == Free)
{
_target_axis.clear();
}
_freelook_direction = 0.0f;
_freelook_pitch = 0.0f;
_movement_direction = 0.0f;
_movement_pitch = 0.0f;
}
void Camera::set_target(const core::Entity *entity)
{
_target_entity = entity;
}
void Camera::frame(const float elapsed)
{
const float ROTATESPEED = 25.0f * elapsed;
switch(mode())
{
case Track:
{
math::Axis desired_axis;
// 3rd person view
if (target())
{
_target_location.assign(target()->location());
if (target()->model())
{
const float modelscale = target()->radius() / target()->model()->radius();
_target_location += target()->axis().up() * target()->model()->box().max().z() * modelscale;
}
else
{
_target_location += target()->axis().up() * target()->radius();
}
desired_axis.assign(target()->axis());
_distance = target()->radius() * _multiplier * 2.0f;
}
else
{
_target_location.assign(0.0f, 0.0f, 1.0f);
_distance = _multiplier * 2.0f;
}
// FIXME Bad solution below
math::Vector3f n (math::crossproduct(_target_axis.forward(), desired_axis.forward()));
float l = n.length();
float d = math::dotproduct(_target_axis.forward(), desired_axis.forward());
float a = (d > 0.0f ? 1.0f - d : 1.0f);
if ((a > MIN_DELTA) && (l > MIN_DELTA))
{
n.normalize();
_target_axis.rotate(n, -ROTATESPEED * a);
}
n.assign (math::crossproduct(_target_axis.up(), desired_axis.up()));
l = n.length();
d = math::dotproduct(_target_axis.up(), desired_axis.up());
a = (d > 0.0f ? 1.0f - d : 1.0f);
if ((a > MIN_DELTA) && (l > MIN_DELTA))
{
n.normalize();
_target_axis.rotate(n, -ROTATESPEED * a);
}
_axis.assign(_target_axis);
_axis.change_direction(_freelook_direction);
_axis.change_pitch(_freelook_pitch);
break;
}
case Cockpit:
{
// 1st person view
if (target())
{
_target_location.assign(target()->location());
_target_axis.assign(target()->axis());
_distance = 0.0f;
}
else
{
_target_location.clear();
_target_axis.clear();
_distance = 0.0f;
}
_axis.assign(_target_axis);
_axis.change_direction(_freelook_direction);
_axis.change_pitch(_freelook_pitch);
break;
}
case Free:
{
// look at self
if (target())
{
_target_location.assign(target()->location());
_axis.assign(target()->axis());
_distance = target()->radius() * _multiplier * 2.0f;
}
else
{
_target_location.clear();
_axis.clear();
_distance = _multiplier * 2.0f;
}
_target_axis.rotate(math::Vector3f(0.0f, 0.0f, 1.0f), -M_PI * _movement_direction * elapsed);
_target_axis.change_pitch(180.0f * _movement_pitch * elapsed);
_axis.assign(_axis * _target_axis);
_axis.change_direction(_freelook_direction);
_axis.change_pitch(_freelook_pitch);
break;
}
case Overview:
{
if (target())
{
_target_location.assign(target()->location());
_target_axis.assign(target()->axis());
_distance = 2.0f * target()->radius() * _multiplier;
_target_axis.change_direction(180.0f);
// default pitch angle
_target_axis.change_pitch(-5.0f);
}
else
{
_target_location.clear();
_target_axis.clear();
_distance = 2.0f * _multiplier;
}
_axis.assign(_target_axis);
break;
}
}
_distance += FRUSTUMFRONT / WORLDSCALE;
_location.assign(_target_location - _axis.forward() * _distance);
}
void Camera::draw()
{
// Change to the projection matrix and set our viewing volume large enough for the skysphere
gl::matrixmode(GL_PROJECTION);
gl::loadidentity();
gl::frustum(-FRUSTUMSIZE, FRUSTUMSIZE, -FRUSTUMSIZE / State::aspect(), FRUSTUMSIZE / State::aspect(), FRUSTUMFRONT, FARPLANE);
gl::matrixmode(GL_MODELVIEW);
gl::loadidentity();
// map world coordinates to opengl coordinates
gl::rotate(90.0f, 0.0f, 1.0f, 0.0f);
gl::rotate(-90.0f, 1.0f , 0.0f, 0.0f);
// apply the transpose of the axis transformation (the axis is orhtonormal)
math::Matrix4f matrix(_axis);
gl::multmatrix(matrix.transpose());
// apply world scale
gl::scale(WORLDSCALE, WORLDSCALE, WORLDSCALE);
// apply camera eye translation
gl::translate(-1.0f * _location);
}
void Camera::draw(const float center_x, const float center_y)
{
// Change to the projection matrix and set our viewing volume large enough for the skysphere
gl::matrixmode(GL_PROJECTION);
gl::loadidentity();
// move projection center to (cx, cy)
// note: the factor 2.0f probably has to be 1.0f/frustum_size
gl::translate(2.0f*(-State::width() * 0.5f + center_x) / State::width() , 2.0f * (State::height() * 0.5f - center_y) / State::height(), 0.0f);
gl::frustum(-FRUSTUMSIZE, FRUSTUMSIZE, -FRUSTUMSIZE / State::aspect(), FRUSTUMSIZE / State::aspect(), FRUSTUMFRONT, 1023.0f);
gl::matrixmode(GL_MODELVIEW);
gl::loadidentity();
// map world coordinates to opengl coordinates
gl::rotate(90.0f, 0.0f, 1.0f, 0.0f);
gl::rotate(-90.0f, 1.0f , 0.0f, 0.0f);
// apply the transpose of the axis transformation (the axis is orhtonormal)
math::Matrix4f matrix(_axis);
gl::multmatrix(matrix.transpose());
// apply camera eye translation
gl::translate(-1.0f * _location);
}
void Camera::ortho()
{
// switch to orthographic projection
gl::matrixmode(GL_PROJECTION);
gl::loadidentity();
glOrtho(0, State::width(), State::height(), 0, -16.0f, 16.0f);
gl::matrixmode(GL_MODELVIEW);
gl::loadidentity();
}
} // namespace render
|