-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCamera.cpp
More file actions
274 lines (224 loc) · 5.1 KB
/
Copy pathCamera.cpp
File metadata and controls
274 lines (224 loc) · 5.1 KB
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
#include "commons.h"
#include "Camera.h"
#include <fstream>
#include "utils.h"
#include "OGLUtils.h"
using namespace cml;
using namespace std;
using namespace utils;
using namespace game_objects;
namespace control
{
#define D2R(deg) deg*3.1415926535f/180.0f
CCamera::CCamera(): CEntity()
{
position = vector3f();
direction = vector3f(0.0f,0.0f,-1.0f);
up = vector3f(0.0f,1.0f,0.0f);
normAll = 0;
posScale=1.0f;
tmp_angle_x=tmp_angle_y=tmp_angle_z=0.0f;
}
CCamera::~CCamera()
{
}
GLvoid CCamera::transformView()
{
glLoadIdentity();
vector3f target = position+direction;
gluLookAt(position[0],position[1],position[2],target[0],target[1],target[2],up[0],up[1],up[2]);
normAll++;
if (normAll>1000)
{
normAll=0;
normalize(direction);
normalize(up);
}
}
GLvoid CCamera::move(vector3f vec)
{
position+=vec;
}
vector3f CCamera::rotateAroundArbitrary(const vector3f &v, vector3f &axis, GLfloat angle)
{
vector3f w;
GLfloat c, s, t;
normalize(axis);
//calculate parameters of the rotation matrix
c = cos(angle);
s = sin(angle);
t = 1 - c;
//multiply v with rotation matrix
w[0] = (t * axis[0] * axis[0] + c) * v[0]
+ (t * axis[0] * axis[1] + s * axis[2]) * v[1]
+ (t * axis[0] * axis[2] - s * axis[1]) * v[2];
w[1] = (t * axis[0] * axis[1] - s * axis[2]) * v[0]
+ (t * axis[1] * axis[1] + c) * v[1]
+ (t * axis[1] * axis[2] + s * axis[0]) * v[2];
w[2] = (t * axis[0] * axis[2] + s * axis[1]) * v[0]
+ (t * axis[1] * axis[2] - s * axis[0]) * v[1]
+ (t * axis[2] * axis[2] + c) * v[2];
w.normalize();
w = w * length(v);
return w;
}
GLvoid CCamera::rotateX(GLfloat a)
{
tmp_angle_x+=a;
a=D2R(a);
vector3f xaxis = cross(up,direction);
direction=rotateAroundArbitrary(direction,xaxis,-a);
up=rotateAroundArbitrary(up,xaxis,-a);
}
GLvoid CCamera::rotateY(GLfloat a)
{
tmp_angle_y+=a;
a=D2R(a);
matrix33f_r roty
(
cos(a),0.0f,-sin(a),
0.0f,1.0f,0.0f,
sin(a),0.0f,cos(a)
);
direction=roty*direction;
up=roty*up;
}
GLvoid CCamera::rotateZ(GLfloat a)
{
tmp_angle_z+=a;
/*a=D2R(a);
matrix33f_r rotz
(
cos(a),sin(a),0.0f,
-sin(a),cos(a),0.0f,
0.0f,0.0f,1.0f
);
direction=rotz*direction;
up=rotz*up;*/
}
GLvoid CCamera::strafeLeft(GLfloat a)
{
position+=cross(up,direction)*a;
}
GLvoid CCamera::moveForward(GLfloat a)
{
position+=direction*a;
}
/*vector3f CCamera::getPosition()
{
return position;
}*/
vector3f CCamera::getDirection()
{
return direction;
}
vector3f CCamera::getUpVector()
{
return up;
}
vector3f CCamera::getTargetPoint()
{
return position+direction;
}
GLvoid CCamera::setPosScale(GLfloat posScale)
{
this->posScale=posScale;
}
/*GLvoid CCamera::setPosition(vector3f position)
{
this->position=position*posScale;
}*/
GLfloat CCamera::getRotateX()
{
return tmp_angle_x;
}
GLfloat CCamera::getRotateY()
{
return tmp_angle_y;
}
GLfloat CCamera::getRotateZ()
{
return tmp_angle_z;
}
GLvoid CCamera::rotateAroundTargetOnY(cml::vector3f target, GLfloat angle)
{
vector3f oldPos = position;
this->setPosition(target);
this->rotateY(angle);
this->setPosition(-normalize(direction)*oldPos.length());
}
GLvoid CCamera::rotateAroundTargetOnX(cml::vector3f target, GLfloat angle)
{
vector3f oldPos = position;
this->setPosition(target);
this->rotateX(angle);
this->setPosition(-normalize(direction)*oldPos.length());
}
GLvoid CCamera::pushPosition()
{
tempPosition=position;
}
GLvoid CCamera::popPosition()
{
position=tempPosition;
}
GLvoid CCamera::setFollowPath(string pathFile)
{
// read contents of a file
ifstream file;
file.open(pathFile.c_str());
string line = "";
std::vector<vector3f> inPoints;
while (file.good())
{
file >> line;
GLfloat x,y;
sscanf(line.c_str(),"%f,%f",&x,&y);
inPoints.push_back(vector3f(x,y,0.0f));
}
file.close();
// aproximate path
path.clear();
path=sGeometryUtils::generateBSplines(inPoints,inPoints.size()*300,2);
pathPos=path.begin();
}
GLvoid CCamera::followPath(CDeltaTime *deltaTime)
{
vector3f dir = vector3f((*pathPos)[0],0.0f,(*pathPos)[2])-vector3f(position[0],0.0f,position[2]);
GLfloat speed = 0.05f*deltaTime->getDelta();
if (dir.length()<speed)
{
position=*pathPos;
pathPos++;
if (pathPos==path.end())
{
path.clear();
return;
}
direction = normalize(*pathPos-position);
}
else
{
dir=dir.normalize();
position+=dir*speed;
}
}
GLvoid CCamera::update(CDeltaTime *deltaTime)
{
if (path.size()>0)
{
if (pathPos==path.begin())
{
// calc direction
position = *pathPos;
pathPos++;
direction = normalize(*pathPos-position);
}
followPath(deltaTime);
}
}
std::vector<vector3f> *CCamera::getPathPoints()
{
return &path;
}
};