Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/world_entities/camera.cc @ 10461

Last change on this file since 10461 was 10461, checked in by patrick, 17 years ago

small change in camera.

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