1 | /* |
---|
2 | orxonox - the future of 3D-vertical-scrollers |
---|
3 | |
---|
4 | Copyright (C) 2004 orx |
---|
5 | |
---|
6 | This program is free software; you can redistribute it and/or modify |
---|
7 | it under the terms of the GNU General Public License as published by |
---|
8 | the Free Software Foundation; either version 2, or (at your option) |
---|
9 | any later version. |
---|
10 | |
---|
11 | ### File Specific: |
---|
12 | main-programmer: Christian Meyer |
---|
13 | co-programmer: Benjamin Grauer |
---|
14 | */ |
---|
15 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD_ENTITY |
---|
16 | |
---|
17 | #include "camera.h" |
---|
18 | #include "key_mapper.h" |
---|
19 | #include "glincl.h" |
---|
20 | #include "util/loading/load_param.h" |
---|
21 | #include "world_entity.h" |
---|
22 | #include "vector.h" |
---|
23 | #include "targets.h" |
---|
24 | #include "track/track.h" |
---|
25 | #include "script_class.h" |
---|
26 | |
---|
27 | |
---|
28 | ObjectListDefinition(Camera); |
---|
29 | |
---|
30 | CREATE_SCRIPTABLE_CLASS(Camera, |
---|
31 | addMethod("setAbsCoor", Executor3<PNode, lua_State*,float,float,float>(&PNode::setAbsCoor)) |
---|
32 | ->addMethod("getAbsCoorX", Executor0ret<PNode, lua_State*, float>(&PNode::getAbsCoorX)) |
---|
33 | ->addMethod("getAbsCoorY", Executor0ret<PNode, lua_State*, float>(&PNode::getAbsCoorY)) |
---|
34 | ->addMethod("getAbsCoorZ", Executor0ret<PNode, lua_State*, float>(&PNode::getAbsCoorZ)) |
---|
35 | ); |
---|
36 | |
---|
37 | /** |
---|
38 | * creates a Camera |
---|
39 | */ |
---|
40 | Camera::Camera() |
---|
41 | { |
---|
42 | this->registerObject(this, Camera::_objectList); |
---|
43 | |
---|
44 | this->init(); |
---|
45 | } |
---|
46 | |
---|
47 | |
---|
48 | Camera::Camera(const TiXmlElement* root) |
---|
49 | { |
---|
50 | this->registerObject(this, Camera::_objectList); |
---|
51 | |
---|
52 | if( root != NULL) |
---|
53 | this->loadParams(root); |
---|
54 | |
---|
55 | this->init(); |
---|
56 | |
---|
57 | } |
---|
58 | |
---|
59 | |
---|
60 | /** |
---|
61 | * default destructor |
---|
62 | */ |
---|
63 | Camera::~Camera() |
---|
64 | {} |
---|
65 | |
---|
66 | void Camera::init() |
---|
67 | { |
---|
68 | //this->setName("camera"); |
---|
69 | this->target = new CameraTarget(); |
---|
70 | this->target->masta=this; |
---|
71 | this->subscribeEvent(ES_GAME, KeyMapper::PEV_VIEW0); |
---|
72 | this->subscribeEvent(ES_GAME, KeyMapper::PEV_VIEW1); |
---|
73 | this->subscribeEvent(ES_GAME, KeyMapper::PEV_VIEW2); |
---|
74 | this->subscribeEvent(ES_GAME, KeyMapper::PEV_VIEW3); |
---|
75 | this->subscribeEvent(ES_GAME, KeyMapper::PEV_VIEW4); |
---|
76 | this->subscribeEvent(ES_GAME, KeyMapper::PEV_VIEW5); |
---|
77 | |
---|
78 | //this->setFovy(90); |
---|
79 | this->setAspectRatio(1.33f); |
---|
80 | this->setClipRegion(.1, 10000); |
---|
81 | |
---|
82 | this->viewTopFovy = 60; |
---|
83 | this->viewNormalFovy = 90; |
---|
84 | this->viewFrontFovy = 120; |
---|
85 | this->viewRightFovy = 90; |
---|
86 | this->viewLeftFovy = 90; |
---|
87 | |
---|
88 | this->viewTopDistance = 70; |
---|
89 | this->viewNormalDistance = 10; |
---|
90 | this->viewFrontDistance = 4; |
---|
91 | this->viewRightDistance = 10; |
---|
92 | this->viewLeftDistance = 10; |
---|
93 | |
---|
94 | this->setViewMode(Camera::ViewNormal); |
---|
95 | |
---|
96 | this->setParentMode(PNODE_ALL); |
---|
97 | this->eventHandling = true; |
---|
98 | |
---|
99 | //add to track |
---|
100 | if(this->entityTrack) |
---|
101 | this->setParent(this->entityTrack->getTrackNode()); |
---|
102 | } |
---|
103 | |
---|
104 | /** |
---|
105 | * focuses the Camera onto a Target |
---|
106 | * @param target the new PNode the Camera should look at. |
---|
107 | */ |
---|
108 | void Camera::lookAt(PNode* target) |
---|
109 | { |
---|
110 | this->target->setParentSoft(target,0.2); |
---|
111 | } |
---|
112 | |
---|
113 | /** |
---|
114 | * @returns The PNode of the Target (from there you can get position and so on |
---|
115 | */ |
---|
116 | PNode* Camera::getTargetNode() const |
---|
117 | { |
---|
118 | return (PNode*)this->target; |
---|
119 | } |
---|
120 | |
---|
121 | void Camera::setTargetNode(PNode* target) |
---|
122 | { |
---|
123 | this->target->setParent(target); |
---|
124 | } |
---|
125 | |
---|
126 | /** |
---|
127 | * sets a new AspectRatio |
---|
128 | * @param aspectRatio the new aspect ratio to set (width / height) |
---|
129 | */ |
---|
130 | void Camera::setAspectRatio(float aspectRatio) |
---|
131 | { |
---|
132 | this->aspectRatio = aspectRatio; |
---|
133 | } |
---|
134 | |
---|
135 | /** |
---|
136 | * Sets a new clipping region |
---|
137 | * @param nearClip The near clip plane |
---|
138 | * @param farClip The far clip plane |
---|
139 | */ |
---|
140 | void Camera::setClipRegion(float nearClip, float farClip) |
---|
141 | { |
---|
142 | this->nearClip = nearClip; |
---|
143 | this->farClip = farClip; |
---|
144 | } |
---|
145 | |
---|
146 | /** |
---|
147 | * sets the new VideoMode and initializes iteration to it. |
---|
148 | * @param mode the mode to change to. |
---|
149 | */ |
---|
150 | void Camera::setViewMode(ViewMode mode) |
---|
151 | { |
---|
152 | currentMode = mode; |
---|
153 | switch (mode) |
---|
154 | { |
---|
155 | default: |
---|
156 | case Camera::ViewNormal: |
---|
157 | { |
---|
158 | this->fovy = viewNormalFovy; |
---|
159 | this->toFovy = viewNormalFovy; |
---|
160 | //this->fovy = 60; |
---|
161 | //this->toFovy = 60; |
---|
162 | this->setRelCoorSoft(-2.0/3.0 * this->viewNormalDistance, 1.0/3.0 * this->viewNormalDistance, 0); |
---|
163 | this->target->setRelCoorSoft(0,0,0); |
---|
164 | break; |
---|
165 | } |
---|
166 | case Camera::ViewBehind: |
---|
167 | break; |
---|
168 | case Camera::ViewFront: |
---|
169 | { |
---|
170 | this->fovy = viewFrontFovy; |
---|
171 | this->toFovy = viewFrontFovy; |
---|
172 | this->setRelCoorSoft(this->viewFrontDistance, 0, 0, 5); |
---|
173 | this->target->setRelCoorSoft(Vector(10,0,0), 5); |
---|
174 | break; |
---|
175 | } |
---|
176 | case Camera::ViewLeft: |
---|
177 | { |
---|
178 | this->fovy = viewLeftFovy; |
---|
179 | this->toFovy = viewLeftFovy; |
---|
180 | this->setRelCoorSoft(0, 1, -viewLeftDistance, .5); |
---|
181 | this->target->setRelCoorSoft(0,0,0); |
---|
182 | break; |
---|
183 | } |
---|
184 | case Camera::ViewRight: |
---|
185 | { |
---|
186 | this->fovy = viewRightFovy; |
---|
187 | this->toFovy = viewRightFovy; |
---|
188 | this->setRelCoorSoft(Vector(0, 1, viewRightDistance), 0.5); |
---|
189 | this->target->setRelCoorSoft(0,0,0); |
---|
190 | break; |
---|
191 | } |
---|
192 | case Camera::ViewTop: |
---|
193 | { |
---|
194 | this->fovy= viewTopFovy; |
---|
195 | this->toFovy = viewTopFovy; |
---|
196 | this->setRelCoor(Vector(-0.05, this->viewTopDistance , 0)); |
---|
197 | this->target->setRelCoor(0,0,0); |
---|
198 | } |
---|
199 | } |
---|
200 | } |
---|
201 | |
---|
202 | |
---|
203 | /** |
---|
204 | * Updates the position of the camera. |
---|
205 | * @param dt: The time that elapsed. |
---|
206 | */ |
---|
207 | void Camera::tick(float dt) |
---|
208 | { |
---|
209 | //update frustum plane |
---|
210 | this->viewVector = (this->target->getAbsCoor() - this->getAbsCoor()).getNormalized(); |
---|
211 | this->frustumPlane = Plane(this->viewVector, this->getAbsCoor() + this->viewVector * 0.1); |
---|
212 | this->upVector = this->getAbsDirV(); |
---|
213 | |
---|
214 | // iteration for fovy |
---|
215 | float tmpFovy = (this->toFovy - this->fovy); |
---|
216 | if (fabsf(tmpFovy) > 0.01) |
---|
217 | this->fovy += tmpFovy * fabsf(dt); |
---|
218 | |
---|
219 | if(this->entityTrack) |
---|
220 | this->entityTrack->tick(dt); |
---|
221 | |
---|
222 | |
---|
223 | |
---|
224 | |
---|
225 | //iterate(float dt, translate, target) |
---|
226 | target->translate(dt); |
---|
227 | } |
---|
228 | |
---|
229 | |
---|
230 | /** |
---|
231 | * initialize rendering perspective according to this camera |
---|
232 | * |
---|
233 | * This is called immediately before the rendering cycle starts, it sets all global |
---|
234 | * rendering options as well as the GL_PROJECTION matrix according to the camera. |
---|
235 | */ |
---|
236 | void Camera::apply () |
---|
237 | { |
---|
238 | // switching to Projection Matrix |
---|
239 | glMatrixMode (GL_PROJECTION); |
---|
240 | glLoadIdentity (); |
---|
241 | |
---|
242 | gluPerspective(this->fovy, |
---|
243 | this->aspectRatio, |
---|
244 | this->nearClip, |
---|
245 | this->farClip); |
---|
246 | |
---|
247 | |
---|
248 | // setting up the perspective |
---|
249 | // speed-up feature |
---|
250 | glMatrixMode (GL_MODELVIEW); |
---|
251 | glLoadIdentity(); |
---|
252 | |
---|
253 | |
---|
254 | } |
---|
255 | |
---|
256 | void Camera::project() |
---|
257 | { |
---|
258 | Vector cameraPosition = this->getAbsCoor(); |
---|
259 | Vector targetPosition = this->target->getAbsCoor(); |
---|
260 | |
---|
261 | |
---|
262 | //Setting the Camera Eye, lookAt and up Vectors |
---|
263 | gluLookAt(cameraPosition.x, cameraPosition.y, cameraPosition.z, |
---|
264 | targetPosition.x, targetPosition.y, targetPosition.z, |
---|
265 | this->upVector.x, this->upVector.y, this->upVector.z); |
---|
266 | } |
---|
267 | |
---|
268 | |
---|
269 | /** |
---|
270 | * processes an event |
---|
271 | * @param event: the event to process |
---|
272 | */ |
---|
273 | void Camera::process(const Event &event) |
---|
274 | { |
---|
275 | if (eventHandling == true) |
---|
276 | { |
---|
277 | if( event.type == KeyMapper::PEV_VIEW0) |
---|
278 | { |
---|
279 | this->setViewMode(Camera::ViewNormal); |
---|
280 | } |
---|
281 | else if( event.type == KeyMapper::PEV_VIEW1) |
---|
282 | { |
---|
283 | this->setViewMode(Camera::ViewBehind); |
---|
284 | } |
---|
285 | else if( event.type == KeyMapper::PEV_VIEW2) |
---|
286 | { |
---|
287 | this->setViewMode(Camera::ViewFront); |
---|
288 | } |
---|
289 | else if( event.type == KeyMapper::PEV_VIEW3) |
---|
290 | { |
---|
291 | this->setViewMode(Camera::ViewLeft); |
---|
292 | } |
---|
293 | else if( event.type == KeyMapper::PEV_VIEW4) |
---|
294 | { |
---|
295 | this->setViewMode(Camera::ViewRight); |
---|
296 | } |
---|
297 | else if( event.type == KeyMapper::PEV_VIEW5) |
---|
298 | { |
---|
299 | this->setViewMode(Camera::ViewTop); |
---|
300 | } |
---|
301 | } |
---|
302 | } |
---|
303 | |
---|
304 | |
---|
305 | void Camera::loadParams(const TiXmlElement* root) |
---|
306 | { |
---|
307 | // Do the PNode loading stuff |
---|
308 | WorldEntity::loadParams(root); |
---|
309 | |
---|
310 | LoadParam(root, "viewTopFovy", this, Camera, setViewTopFovy); |
---|
311 | LoadParam(root, "viewFrontFovy", this, Camera, setViewFrontFovy); |
---|
312 | LoadParam(root, "viewLeftFovy", this, Camera, setViewLeftFovy); |
---|
313 | LoadParam(root, "viewRightFovy", this, Camera, setViewRightFovy); |
---|
314 | LoadParam(root, "viewBehindFovy", this, Camera, setViewBehindFovy); |
---|
315 | LoadParam(root, "viewNormalFovy", this, Camera, setViewNormalFovy); |
---|
316 | |
---|
317 | LoadParam(root, "viewTopDistance", this, Camera, setViewTopDistance); |
---|
318 | LoadParam(root, "viewFrontDistance", this, Camera, setViewFrontDistance); |
---|
319 | LoadParam(root, "viewLeftDistance", this, Camera, setViewLeftDistance); |
---|
320 | LoadParam(root, "viewRightDistance", this, Camera, setViewRightDistance); |
---|
321 | LoadParam(root, "viewBehindDistance", this, Camera, setViewBehindDistance); |
---|
322 | LoadParam(root, "viewNormalDistance", this, Camera, setViewNormalDistance); |
---|
323 | } |
---|
324 | |
---|
325 | |
---|
326 | void Camera::setViewTopFovy(float fovy) |
---|
327 | { |
---|
328 | this->viewTopFovy = fovy; |
---|
329 | } |
---|
330 | |
---|
331 | void Camera::setViewFrontFovy(float fovy) |
---|
332 | { |
---|
333 | this->viewFrontFovy = fovy; |
---|
334 | } |
---|
335 | |
---|
336 | void Camera::setViewLeftFovy(float fovy) |
---|
337 | { |
---|
338 | this->viewLeftFovy = fovy; |
---|
339 | } |
---|
340 | |
---|
341 | void Camera::setViewRightFovy(float fovy) |
---|
342 | { |
---|
343 | this->viewRightFovy = fovy; |
---|
344 | } |
---|
345 | |
---|
346 | void Camera::setViewBehindFovy(float fovy) |
---|
347 | { |
---|
348 | this->viewBehindFovy = fovy; |
---|
349 | } |
---|
350 | |
---|
351 | void Camera::setViewNormalFovy(float fovy) |
---|
352 | { |
---|
353 | this->viewNormalFovy = fovy; |
---|
354 | } |
---|
355 | |
---|
356 | void Camera::setViewTopDistance(float Distance) |
---|
357 | { |
---|
358 | this->viewTopDistance = Distance; |
---|
359 | } |
---|
360 | |
---|
361 | void Camera::setViewFrontDistance(float Distance) |
---|
362 | { |
---|
363 | this->viewFrontDistance = Distance; |
---|
364 | } |
---|
365 | |
---|
366 | void Camera::setViewLeftDistance(float Distance) |
---|
367 | { |
---|
368 | this->viewLeftDistance = Distance; |
---|
369 | } |
---|
370 | |
---|
371 | void Camera::setViewRightDistance(float Distance) |
---|
372 | { |
---|
373 | this->viewRightDistance = Distance; |
---|
374 | } |
---|
375 | |
---|
376 | void Camera::setViewBehindDistance(float Distance) |
---|
377 | { |
---|
378 | this->viewBehindDistance = Distance; |
---|
379 | } |
---|
380 | |
---|
381 | void Camera::setViewNormalDistance(float Distance) |
---|
382 | { |
---|
383 | this->viewNormalDistance = Distance; |
---|
384 | } |
---|
385 | |
---|
386 | |
---|
387 | |
---|
388 | |
---|
389 | void Camera::glLookAt(float eyex, float eyey, float eyez, float centerx, float centery, float centerz, float upx, float upy, float upz) |
---|
390 | { |
---|
391 | //Vector* eye=new Vector(eyex, eyey, eyez); |
---|
392 | Vector* center=new Vector (centerx, centery, centerz); |
---|
393 | Vector* up=new Vector(upx, upy, upz); |
---|
394 | |
---|
395 | center->x-=eyex; |
---|
396 | center->y-=eyey; |
---|
397 | center->z-=eyez; |
---|
398 | |
---|
399 | center->normalize(); |
---|
400 | up->normalize(); |
---|
401 | Vector* s = VectorProd(center, up); |
---|
402 | Vector* u = VectorProd(s, center); |
---|
403 | GLfloat Matrix[]={s->x, s->y, s->z, 0, u->x, u->y, u->z, 0, -center->x, -center->y, -center->z, 0, 0, 0, 0, 1}; |
---|
404 | |
---|
405 | glMultMatrixf(Matrix); |
---|
406 | glTranslated(-eyex, -eyey, -eyez); |
---|
407 | delete center; |
---|
408 | delete up; |
---|
409 | delete s; |
---|
410 | delete u; |
---|
411 | |
---|
412 | } |
---|
413 | |
---|
414 | |
---|
415 | |
---|
416 | |
---|
417 | Vector* Camera::VectorProd(Vector* v1, Vector* v2) |
---|
418 | { |
---|
419 | Vector* temp= new Vector(); |
---|
420 | temp->x=v1->y * v2->z - v1->z * v2->y; |
---|
421 | temp->y=v1->z * v2->x - v1->x * v2->z; |
---|
422 | temp->z=v1->x * v2->y - v1->y * v2->x; |
---|
423 | return temp; |
---|
424 | } |
---|
425 | |
---|
426 | |
---|
427 | |
---|
428 | |
---|
429 | |
---|
430 | |
---|
431 | |
---|
432 | |
---|
433 | |
---|
434 | |
---|
435 | |
---|
436 | |
---|
437 | |
---|
438 | |
---|
439 | /////////////////// |
---|
440 | // CAMERA-TARGET // |
---|
441 | /////////////////// |
---|
442 | //REATE_FACTORY(CameraTarget); |
---|
443 | |
---|
444 | |
---|
445 | ObjectListDefinition(CameraTarget); |
---|
446 | |
---|
447 | |
---|
448 | CameraTarget::CameraTarget() |
---|
449 | { |
---|
450 | this->registerObject(this, CameraTarget::_objectList); |
---|
451 | // this->setParentMode(PNODE_MOVEMENT); |
---|
452 | this->speed=1; |
---|
453 | translateTo.x=0; |
---|
454 | translateTo.y=0; |
---|
455 | translateTo.z=0; |
---|
456 | rotateBy.x=0; |
---|
457 | rotateBy.y=0; |
---|
458 | rotateBy.z=0; |
---|
459 | target=createStick(); |
---|
460 | } |
---|
461 | |
---|
462 | |
---|
463 | void CameraTarget::detach() |
---|
464 | { |
---|
465 | masta->setParentSoft(target); |
---|
466 | masta->getTargetNode()->setParentSoft(target); |
---|
467 | } |
---|
468 | |
---|
469 | PNode* CameraTarget::createStick() |
---|
470 | { |
---|
471 | return new Targets(); |
---|
472 | } |
---|
473 | |
---|
474 | |
---|
475 | void CameraTarget::atach(PNode* object) |
---|
476 | { |
---|
477 | masta->setParentSoft(object); |
---|
478 | masta->getTargetNode()->setParentSoft(object); |
---|
479 | } |
---|
480 | |
---|
481 | |
---|
482 | |
---|
483 | |
---|
484 | Vector CameraTarget::iterate(float dt, const Vector* Target, const Vector* cam) |
---|
485 | { |
---|
486 | |
---|
487 | |
---|
488 | Vector tmpVec; |
---|
489 | tmpVec= (*Target - *cam); |
---|
490 | tmpVec.normalize(); |
---|
491 | return tmpVec; |
---|
492 | |
---|
493 | } |
---|
494 | |
---|
495 | |
---|
496 | void CameraTarget::translate(float dt) |
---|
497 | { |
---|
498 | if (fabs(translateTo.len() - (target->getAbsCoor()).len()) >= 11 ) |
---|
499 | { |
---|
500 | Vector tmpVec= iterate(dt, &translateTo, &(masta->getAbsCoor())); |
---|
501 | target->shiftCoor(speed*tmpVec.x, speed*tmpVec.y, speed*tmpVec.z); |
---|
502 | } |
---|
503 | } |
---|
504 | |
---|
505 | Vector * CameraTarget::rotate(Vector* newPos, float speed) |
---|
506 | { |
---|
507 | |
---|
508 | } |
---|
509 | |
---|
510 | void CameraTarget::jump(float x, float y, float z) |
---|
511 | { |
---|
512 | target->setAbsCoor(x,y,z); |
---|
513 | } |
---|
514 | |
---|
515 | |
---|
516 | void CameraTarget::trans(float x, float y, float z) |
---|
517 | { |
---|
518 | Vector tmpVec=Vector(x,y,z); |
---|
519 | if( this->getParent()) |
---|
520 | this->getParent()->setRelCoor(this->getParent()->getRelCoor()); |
---|
521 | translateNow(&tmpVec); |
---|
522 | } |
---|
523 | |
---|
524 | void CameraTarget::translateNow(Vector* vec) |
---|
525 | { |
---|
526 | translateTo=*vec; |
---|
527 | } |
---|
528 | |
---|
529 | void CameraTarget::changeSpeed(float speed) |
---|
530 | { |
---|
531 | if (speed!=0) |
---|
532 | this->speed=speed; |
---|
533 | return; |
---|
534 | } |
---|
535 | |
---|
536 | |
---|
537 | bool CameraTarget::isDone() |
---|
538 | { |
---|
539 | if (fabs(translateTo.len() - (target->getAbsCoor()).len()) >= 11 ) |
---|
540 | return 0; |
---|
541 | else |
---|
542 | return 1; |
---|
543 | } |
---|