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

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

using namespace render;

namespace client
{

namespace camera 
{

// public variables

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

// target yaw, angle in XZ plane, positive is looking left
float yaw_target;
// target pitch, angle in XZ plane, positive is looking left
float pitch_target;
// distance from the camera to the target
float distance;
// current camera mode
Mode mode;

// private variables

// current yaw, angle in XZ plane, positive is looking left
float yaw_current;
// current pitch, angle in XY, positive is looking up
float pitch_current;

// default pitch in mode::Overview
float pitch_overview;


// current x offset
float x_offset;
// current z offset
float z_offset;

// default pitch in mode::Track
const float pitch_track = 15.0f;
// default rotate offset increase/decrease
float rotate_offset_inc;
// default translate offset increase/decrease
const float translate_offset_inc = 0.1;

void init()
{
	rotate_offset_inc = 5.0f; 

	yaw_current = 0;
	yaw_target = 0; 

	pitch_current = pitch_track * 2; 
	pitch_target = pitch_track; 

	distance = 0.4f;

	mode = Track;
}

void shutdown() 
{
}

void set_mode(Mode newmode) {
	switch(newmode) {
	case Track:
		// switch camera to Track mode
		mode = Track;
		yaw_target = core::localcontrol()->direction();
		yaw_current = yaw_target;
		pitch_target = pitch_track;
		pitch_current = pitch_target;
		distance = 0.4f;
		break;
	case Free:
		// switch camera to Free mode
		mode = Free;
		yaw_target = core::localcontrol()->direction();
		yaw_current = yaw_target;
		pitch_target = pitch_track;
		pitch_current = pitch_target;
		distance = 0.4f;
		break;
	case Overview:
		// switch camera to Overview mode
		mode = Overview;
		x_offset = 0;
		z_offset = 0;
		distance = 20.0f;
	default:
		break;
	}

}

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

	switch(mode) {
	case Free:
		// switch camera to Track mode
		set_mode(Track);
		break;
	case Track:
		// switch camera to Free mode
		set_mode(Free);
		break;
	default:
		break;
	}
}

void draw(float elapsed) 
{	
	if (!core::localcontrol()) {
		// switch the camera to Overview of the player is not controling anything
		if (mode != Overview) {
			set_mode(Overview);

			camera::target = math::Vector3f(0,0,0);
		}
	} else {
		if (mode == Overview)
			set_mode(Track);

		camera::target = core::localcontrol()->location();
	}
	
	if (mode == Track) {
		yaw_target = core::localcontrol()->direction();
	}

	if ((mode == Free) || (mode == Track)) {
		// adjust yaw
		float d = degrees180f(yaw_current - yaw_target);
		yaw_current = degrees360f( yaw_current - d * elapsed) ;
	
		// adjust pitch target
		d = degrees180f(pitch_current - pitch_target);
		pitch_current = degrees360f( pitch_current - d *elapsed);
	}

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

	float x,y,z;

	// map camera coordinates to opengl coordinates
	switch (mode) {
	case Free:
		// calculate the position of the eye
		x = -distance;
		y = 0;
		z = distance/2;

		eye.x = x * cos(-pitch_current / 180.0f * M_PI) - z * sin(-pitch_current / 180.0f * M_PI);
		eye.y = y;
		eye.z = x * sin(-pitch_current / 180.0f * M_PI) + z * cos(-pitch_current / 180.0f * M_PI);

		x = eye.x;
		y = eye.y;
		z = eye.z;

		eye.x = x * cos(yaw_current / 180.0f * M_PI) - y * sin(yaw_current / 180.0f * M_PI);
		eye.y = x * sin(yaw_current / 180.0f * M_PI) + y * cos(yaw_current / 180.0f * M_PI);
		eye.z = z;

		eye = eye + target;

		// draw the camera transformation
		gl::translate(1.0f+distance, 0.0f, -distance/2);
		gl::rotate(-pitch_current, 0, 1.0f, 0 );
		gl::rotate(-yaw_current, 0, 0, 1.0f);
		gl::translate(-1*target);
		break;

	case Track:
		// calculate the position of the eye
		x = -distance;
		y = 0;
		z = distance;

		eye.x = z * sin(pitch_current / 180.0f * M_PI) + x * cos(pitch_current / 180.0f * M_PI);
		eye.y = y;
		eye.z = z * sin(pitch_current / 180.0f * M_PI) - x * cos(pitch_current / 180.0f * M_PI);

		x = eye.x;
		y = eye.y;
		z = eye.z;

		eye.x = x * cos(yaw_current / 180.0f * M_PI) - y * sin(yaw_current / 180.0f * M_PI);
		eye.y = x * sin(yaw_current / 180.0f * M_PI) + y * cos(yaw_current / 180.0f * M_PI);
		eye.z = z;

		eye = eye + target;

		// draw the camera transformation
		gl::translate(1.0f+distance, 0.0f, -distance);
		gl::rotate(-pitch_current, 0, 1.0f, 0);
		gl::rotate(-yaw_current, 0, 0, 1.0f);
		gl::translate(-1*target);
		break;

	case Overview:

		eye = target;
		eye.z = eye.z + distance-1;

		gl::rotate(-75.0f, 0.0f, 1.0f, 0.0f);
		gl::translate(0.0f, 0.0f, -distance);
		//gl::translate(x_offset, 0.0f, z_offset);
		gl::translate(-1*target);
		break;
	default:
		break;
	}
}

void key_right()
{
	if (mode == Free) {
		yaw_target = degrees360f(  yaw_target + rotate_offset_inc);
	} else if (mode == Overview) {
		z_offset += translate_offset_inc;
	}
}

void key_left()
{
	if (mode == Free) {
		yaw_target = degrees360f(  yaw_target - rotate_offset_inc);
	} else if (mode == Overview) {
		z_offset -= translate_offset_inc;
	}
}

void key_up()
{
	if (mode == Free) {
		pitch_target = pitch_target + rotate_offset_inc;
		if (pitch_target > 90.0f) pitch_target = 90.0f;
	} else if (mode == Overview) {
		x_offset += translate_offset_inc;
	}
}

void key_down()
{
	if (mode == Free) {
		pitch_target = pitch_target - rotate_offset_inc;
		if (pitch_target < -90.0f) pitch_target = -90.0f;
	} else if (mode == Overview) {
		x_offset -= translate_offset_inc;
	}
}

} // namespace camera

} // namespace client