Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: cbed115ed9478d45c1db29afd0209733230ca3cd (plain)
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
/* 
   client/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 "math/mathlib.h"
#include "math/matrix4f.h"
#include "core/core.h"
#include "client/client.h"
#include "client/camera.h"
#include "render/render.h"
#include "sys/sys.h"

using math::degrees360f;
using math::degrees180f;

using namespace render;

namespace client
{

namespace camera 
{

// gameworld coordinates of the camera target
math::Vector3f target;
math::Vector3f eye;
math::Axis axis;

const float MIN_DELTA = 10e-10;

// current camera mode
Mode mode;

// private variables

// current and target yaw angle in XZ plane, positive is looking left
float yaw_current;
float yaw_target;
// movement direction in free mode 
float target_direction;

// current and target pitch angle in XY, positive is looking up
float pitch_current;
float pitch_target;
// movement direction in free mode 
float target_pitch;

float distance;

// default pitch in mode::Track
const float pitch_track = -15.0f;
const float pitch_overview = -75.0f;

void set_mode(Mode newmode);

void init()
{
	yaw_current = 0;
	yaw_target = 0; 

	pitch_current = pitch_track * 2; 
	pitch_target = pitch_track; 

	target_pitch = 0.0f;
	target_direction = 0.0f;

	distance = 0.4f;

	set_mode(Track);
	
}

void shutdown() 
{
}

void set_mode(Mode newmode) {

	yaw_target = 0;
	yaw_current = yaw_target;
	pitch_target = pitch_track;
	pitch_current = pitch_target;

	target_direction = 0.0f;
	target_pitch = 0.0f;
	distance = 0.4f;

	axis.clear();

	switch(newmode) {
	case Track:
		// switch camera to Track mode
		mode = Track;
		if (core::localcontrol()) {
			if (core::localcontrol()->state())
				axis.assign(core::localcontrol()->state()->axis());
			else
				axis.assign(core::localcontrol()->axis());
		}
		break;

	case Free:
		// switch camera to Free mode
		mode = Free;
		pitch_target = 2.0 * pitch_track;
		pitch_current = pitch_target;
		break;

	case Cockpit:
		mode = Cockpit;
		break;

	case Overview:
		// switch camera to Overview mode
		mode = Overview;

	default:
		break;
	}

}

void next_mode()
{
	
 	if (!core::localcontrol()) {
		set_mode(Overview);
		return;
	}

	switch(mode) {
	case Free:
		// switch camera to Track mode
		set_mode(Track);
		con_print << "camera mode: track" << std::endl;
		break;

	case Track:
		// switch camera to Cockpit mode
		set_mode(Cockpit);
		con_print << "camera mode: cockpit" << std::endl;
		break;
	
	case Cockpit:
		// switch camera to Free mode
		set_mode(Free);
		con_print << "camera mode: free" << std::endl;
		break;
	
	default:
		break;
	}
}

void draw(float seconds) 
{	
	math::Matrix4f matrix;
	math::Axis target_axis;
	float d = 0;

	if (!core::localcontrol()) {

		if (mode != Overview) {
			set_mode(Overview);
		}
		
		target.clear();
		axis.clear();
		pitch_current = pitch_overview;
		axis.change_pitch(pitch_current);

		distance = 20.0f;

	} else {
		if (mode == Overview)
			set_mode(Track);

		if (core::localcontrol()->state()) {
			target.assign(core::localcontrol()->state()->location());
			target_axis.assign(core::localcontrol()->state()->axis());
		} else {
			target.assign(core::localcontrol()->location());
			target_axis.assign(core::localcontrol()->axis());
		}
		
		if (mode == Track) {
			float cosangle;
			float angle;
			float side;
			float u;
			const float cam_speed = seconds;

			math::Vector3f n;
			math::Vector3f p;

			// camera axis: pitch

			// project target_axis.up() into the plane with axis->left() normal
			n = axis.left();
			p = target_axis.up();
			u = p[0]*n[0] + p[1]*n[1] + p[2]*n[2] / (-n[0]*n[0] - n[1]*n[1] - n[2] * n[2]);
			p = target_axis.up() + u * n;
	
			side = axis.forward().x * p.x + 
				axis.forward().y * p.y +
				axis.forward().z * p.z;

			if ((fabs(side) - MIN_DELTA > 0)) {
				
				cosangle = math::dotproduct(p, axis.up());
				if (fabs(cosangle) + MIN_DELTA < 1 ) {
					angle = acos(cosangle) * 180.0f / M_PI;
					angle = math::sgnf(side) * angle * cam_speed;
					axis.change_pitch(-angle);
				}
			}

			// camera axis: direction

			// project target_axis.forward() into the plane with axis.up() normal
			n = axis.up();
			p = target_axis.forward();
			u = p[0]*n[0] + p[1]*n[1] + p[2]*n[2] / (-n[0]*n[0] - n[1]*n[1] - n[2] * n[2]);
			p = target_axis.forward() + u * n;

			side = axis.left().x * p.x + 
				axis.left().y * p.y +
				axis.left().z * p.z;

			if ((fabs(side) - MIN_DELTA > 0)) {
				
				cosangle = math::dotproduct(p, axis.forward());
				if (fabs(cosangle) + MIN_DELTA < 1 ) {
					angle = acos(cosangle) * 180.0f / M_PI;
					angle = math::sgnf(side) * angle * cam_speed;
					axis.change_direction(angle);
				}
			}

			// camera axis: roll

			// project target_axis.up() into the plane with axis.forward() normal
			n = axis.forward();
			p = target_axis.up();
			u = p[0]*n[0] + p[1]*n[1] + p[2]*n[2] / (-n[0]*n[0] - n[1]*n[1] - n[2] * n[2]);
			p = target_axis.up() + u * n;

			side = axis.left().x * p.x + 
				axis.left().y * p.y +
				axis.left().z * p.z;

			if ((fabs(side) - MIN_DELTA > 0)) {
				
				cosangle = math::dotproduct(p, axis.up());
				if (fabs(cosangle) + MIN_DELTA < 1 ) {
					angle = acos(cosangle) * 180.0f / M_PI;
					angle = math::sgnf(side) * angle * cam_speed;
					axis.change_roll(angle);
				}
			}

			distance = 0.0f;

			if (core::localcontrol()->model()) {
 				target -= (core::localcontrol()->model()->maxbbox().x + 0.1f) * axis.forward();
				target += (core::localcontrol()->model()->maxbbox().z + 0.1f ) * axis.up();
			}

		} else if (mode == Free) {
			axis.assign(target_axis);

			yaw_target =  yaw_current - 90 * target_direction;
			pitch_target = pitch_current - 90 * target_pitch;

			if (core::localcontrol()->model()) {
				distance = core::localcontrol()->model()->radius();
			} else {
				distance = 1.0f;
			}

			// adjust direction
			d = degrees180f(yaw_current - yaw_target);
			yaw_current = degrees360f( yaw_current -  d * seconds);
			axis.change_direction(yaw_current);

			// adjust pitch 
			d = degrees180f(pitch_current - pitch_target);
			pitch_current = degrees360f(pitch_current -  d * seconds);
			axis.change_pitch(pitch_current);
		
		} else if (mode == Cockpit) {
			axis.assign(target_axis);

			if (core::localcontrol()->state() && core::localcontrol()->model())
				target += (core::localcontrol()->model()->maxbbox().x+0.05) *
					 core::localcontrol()->state()->axis().forward();
			
			distance = 0.0f;
		}
	}

	// map world coordinates to opengl coordinates
	gl::rotate(90.0f, 0, 1.0, 0);
	gl::rotate(-90.0f, 1.0f , 0, 0);

	// assign transformation matrix
	matrix.assign(axis);

	// apply the transpose of the axis transformation (the axis is orhtonormal)
	gl::multmatrix(matrix.transpose());

	// match the camera with the current target
	gl::translate(-1.0f * target);
	
	// apply camera offset
	gl::translate((1.0 + distance) * axis.forward());

	// calculate eye position
	eye = target - ((1.0f+distance) * axis.forward());
	eye.assign(target);

}

void set_direction(float direction)
{
	target_direction = direction;
	math::clamp(target_direction, -1.0f, 1.0f);
}

void set_pitch(float pitch)
{
	target_pitch = pitch;
	math::clamp(target_pitch, -1.0f, 1.0f);
}

void reset()
{
	set_mode(mode);
}

} // namespace camera

} // namespace client