Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10408 was 10403, checked in by patrick, 18 years ago

fading now works

File size: 11.8 KB
RevLine 
[4832]1/*
[2068]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:
[2080]12   main-programmer: Christian Meyer
[6424]13   co-programmer: Benjamin Grauer
[2068]14*/
[5357]15#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD_ENTITY
[2068]16
17#include "camera.h"
[7868]18#include "key_mapper.h"
[9406]19#include "glincl.h"
[10379]20#include "util/loading/load_param.h"
[10368]21#include "world_entity.h"
[10379]22#include "vector.h"
23#include "targets.h"
[10389]24#include "track/track.h"
[10388]25#include "script_class.h"
[3608]26
[10379]27
[9869]28ObjectListDefinition(Camera);
29
[10388]30CREATE_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                       );
[10379]36
[2096]37/**
[4836]38 *  creates a Camera
[2096]39*/
[4746]40Camera::Camera()
[2068]41{
[9869]42  this->registerObject(this, Camera::_objectList);
[10391]43
[10368]44  this->init();
45}
46
[10391]47
[10368]48Camera::Camera(const TiXmlElement* root)
49{
50  this->registerObject(this, Camera::_objectList);
[10391]51
52  if( root != NULL)
53    this->loadParams(root);
54
[10368]55  this->init();
[10391]56
[10368]57}
58
[10391]59
[10368]60/**
61 *  default destructor
62*/
63Camera::~Camera()
64{}
65
66void Camera::init()
67{
[10396]68  //this->setName("camera");
[3635]69  this->target = new CameraTarget();
[10379]70  this->target->masta=this;
[7868]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);
[4414]77
[10368]78  //this->setFovy(90);
79  this->setAspectRatio(1.33f);
[9235]80  this->setClipRegion(.1, 10000);
[10387]81
[10368]82  this->viewTopFovy = 60;
83  this->viewNormalFovy = 90;
84  this->viewFrontFovy = 120;
85  this->viewRightFovy = 90;
86  this->viewLeftFovy = 90;
[3641]87
[10368]88  this->viewTopDistance = 70;
89  this->viewNormalDistance = 10;
90  this->viewFrontDistance = 4;
91  this->viewRightDistance = 10;
92  this->viewLeftDistance = 10;
93
[7347]94  this->setViewMode(Camera::ViewNormal);
[4987]95
[5004]96  this->setParentMode(PNODE_ALL);
[10368]97  this->eventHandling = true;
[10391]98
[10389]99  //add to track
100  if(this->entityTrack)
101    this->setParent(this->entityTrack->getTrackNode());
[2068]102}
103
[2096]104/**
[4836]105 *  focuses the Camera onto a Target
106 * @param target the new PNode the Camera should look at.
[2096]107*/
[3635]108void Camera::lookAt(PNode* target)
[2068]109{
[10379]110  this->target->setParentSoft(target,0.2);
[2068]111}
112
[3638]113/**
[4836]114 * @returns The PNode of the Target (from there you can get position and so on
[3638]115*/
[7014]116PNode* Camera::getTargetNode() const
[2068]117{
[3635]118  return (PNode*)this->target;
[2068]119}
120
[10379]121void Camera::setTargetNode(PNode* target)
122{
123  this->target->setParent(target);
124}
125
[2096]126/**
[4836]127 *  sets a new AspectRatio
128 * @param aspectRatio the new aspect ratio to set (width / height)
[3636]129*/
130void Camera::setAspectRatio(float aspectRatio)
131{
132  this->aspectRatio = aspectRatio;
133}
134
135/**
[4992]136 * Sets a new clipping region
137 * @param nearClip The near clip plane
138 * @param farClip The far clip plane
[3636]139*/
140void Camera::setClipRegion(float nearClip, float farClip)
141{
142  this->nearClip = nearClip;
143  this->farClip = farClip;
144}
145
[4490]146/**
[4836]147 *  sets the new VideoMode and initializes iteration to it.
148 * @param mode the mode to change to.
[4490]149*/
[3639]150void Camera::setViewMode(ViewMode mode)
151{
[6034]152  currentMode = mode;
[3639]153  switch (mode)
[6424]154  {
[3639]155    default:
[7347]156    case Camera::ViewNormal:
[10368]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);
[4992]163      this->target->setRelCoorSoft(0,0,0);
[3639]164      break;
[10368]165    }
[7347]166    case Camera::ViewBehind:
[3639]167      break;
[7347]168    case Camera::ViewFront:
[10368]169    {
170      this->fovy = viewFrontFovy;
171      this->toFovy = viewFrontFovy;
172      this->setRelCoorSoft(this->viewFrontDistance, 0, 0, 5);
[6424]173      this->target->setRelCoorSoft(Vector(10,0,0), 5);
[3639]174      break;
[10368]175    }
[7347]176    case Camera::ViewLeft:
[10368]177    {
178      this->fovy = viewLeftFovy;
179      this->toFovy = viewLeftFovy;
180      this->setRelCoorSoft(0, 1, -viewLeftDistance, .5);
[4992]181      this->target->setRelCoorSoft(0,0,0);
[3639]182      break;
[10368]183    }
[7347]184    case Camera::ViewRight:
[10368]185    {
186      this->fovy = viewRightFovy;
187      this->toFovy = viewRightFovy;
188      this->setRelCoorSoft(Vector(0, 1, viewRightDistance), 0.5);
[4992]189      this->target->setRelCoorSoft(0,0,0);
[3639]190      break;
[10368]191    }
[7347]192    case Camera::ViewTop:
[10368]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    }
[6424]199  }
[3639]200}
201
202
[3636]203/**
[4836]204 *  Updates the position of the camera.
205 * @param dt: The time that elapsed.
[3639]206*/
207void Camera::tick(float dt)
208{
[7009]209  //update frustum plane
[7014]210  this->viewVector = (this->target->getAbsCoor() - this->getAbsCoor()).getNormalized();
211  this->frustumPlane = Plane(this->viewVector, this->getAbsCoor() + this->viewVector * 0.1);
[7009]212  this->upVector =  this->getAbsDirV();
213
[10379]214  // iteration for fovy
[7173]215  float tmpFovy = (this->toFovy - this->fovy);
[10368]216  if (fabsf(tmpFovy) > 0.01)
[5354]217    this->fovy += tmpFovy * fabsf(dt);
[10391]218
[10389]219  if(this->entityTrack)
220    this->entityTrack->tick(dt);
[10379]221
222
223
224
225  //iterate(float dt, translate, target)
226  target->translate(dt);
[3639]227}
228
229
230/**
[4836]231 *  initialize rendering perspective according to this camera
[6772]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 */
[2068]236void Camera::apply ()
237{
[3636]238  // switching to Projection Matrix
[2551]239  glMatrixMode (GL_PROJECTION);
[2112]240  glLoadIdentity ();
[3635]241
[3636]242  gluPerspective(this->fovy,
[4832]243                 this->aspectRatio,
244                 this->nearClip,
245                 this->farClip);
[6778]246
247
248    // setting up the perspective
[3636]249  // speed-up feature
[7108]250  glMatrixMode (GL_MODELVIEW);
251  glLoadIdentity();
252
253
254}
255
256void Camera::project()
257{
[3635]258  Vector cameraPosition = this->getAbsCoor();
259  Vector targetPosition = this->target->getAbsCoor();
[2551]260
[10403]261
[10379]262        //Setting the Camera Eye, lookAt and up Vectors
[7009]263  gluLookAt(cameraPosition.x, cameraPosition.y, cameraPosition.z,
[6965]264            targetPosition.x, targetPosition.y, targetPosition.z,
[7009]265            this->upVector.x, this->upVector.y, this->upVector.z);
[7108]266}
[3636]267
[6965]268
[4490]269/**
[4836]270 *  processes an event
271 * @param event: the event to process
[4490]272*/
[4414]273void Camera::process(const Event &event)
274{
[10368]275  if (eventHandling == true)
[6424]276  {
[10368]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    }
[6424]301  }
[4414]302}
[3365]303
[10379]304
[10368]305void Camera::loadParams(const TiXmlElement* root)
306{
307  // Do the PNode loading stuff
[10391]308  WorldEntity::loadParams(root);
[4414]309
[10368]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
[10379]325
[10368]326void Camera::setViewTopFovy(float fovy)
327{
328  this->viewTopFovy = fovy;
329}
330
331void Camera::setViewFrontFovy(float fovy)
332{
333  this->viewFrontFovy = fovy;
334}
335
336void Camera::setViewLeftFovy(float fovy)
337{
338  this->viewLeftFovy = fovy;
339}
340
341void Camera::setViewRightFovy(float fovy)
342{
343  this->viewRightFovy = fovy;
344}
345
346void Camera::setViewBehindFovy(float fovy)
347{
348  this->viewBehindFovy = fovy;
349}
350
351void Camera::setViewNormalFovy(float fovy)
352{
353  this->viewNormalFovy = fovy;
354}
355
356void Camera::setViewTopDistance(float Distance)
357{
358  this->viewTopDistance = Distance;
359}
360
361void Camera::setViewFrontDistance(float Distance)
362{
363  this->viewFrontDistance = Distance;
364}
365
366void Camera::setViewLeftDistance(float Distance)
367{
368  this->viewLeftDistance = Distance;
369}
370
371void Camera::setViewRightDistance(float Distance)
372{
373  this->viewRightDistance = Distance;
374}
375
376void Camera::setViewBehindDistance(float Distance)
377{
378  this->viewBehindDistance = Distance;
379}
380
381void Camera::setViewNormalDistance(float Distance)
382{
383  this->viewNormalDistance = Distance;
384}
385
386
[10379]387
388
389void 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
417Vector* Camera::VectorProd(Vector* v1, Vector* v2)
418{
419Vector* temp= new Vector();
420temp->x=v1->y * v2->z - v1->z * v2->y;
421temp->y=v1->z * v2->x - v1->x * v2->z;
422temp->z=v1->x * v2->y - v1->y * v2->x;
423return temp;
424}
425
426
427
428
429
430
431
432
433
434
435
436
437
438
[3635]439///////////////////
440// CAMERA-TARGET //
441///////////////////
[10379]442//REATE_FACTORY(CameraTarget);
[2636]443
[10379]444
[9869]445ObjectListDefinition(CameraTarget);
[10379]446
447
[3635]448CameraTarget::CameraTarget()
[2636]449{
[9869]450  this->registerObject(this, CameraTarget::_objectList);
[6424]451  //  this->setParentMode(PNODE_MOVEMENT);
[10379]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();
[2636]460}
[3213]461
[10379]462
463void CameraTarget::detach()
464{
465  masta->setParentSoft(target);
466  masta->getTargetNode()->setParentSoft(target);
467}
468
469PNode* CameraTarget::createStick()
470{
471  return new Targets();
472}
473
474
475void CameraTarget::atach(PNode* object)
476{
477  masta->setParentSoft(object);
478  masta->getTargetNode()->setParentSoft(object);
479}
480
481
482
483
484Vector 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
496void 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
505Vector * CameraTarget::rotate(Vector* newPos, float speed)
506{
507
508}
509
510void CameraTarget::jump(float x, float y, float z)
511{
512target->setAbsCoor(x,y,z);
513}
514
515
516void 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
524void CameraTarget::translateNow(Vector* vec)
525{
526translateTo=*vec;
527}
528
529void CameraTarget::changeSpeed(float speed)
530{
531  if (speed!=0)
532this->speed=speed;
533  return;
534}
535
536
537bool CameraTarget::isDone()
538{
539  if (fabs(translateTo.len()  - (target->getAbsCoor()).len()) >= 11 )
540    return 0;
541  else
542    return 1;
543}
Note: See TracBrowser for help on using the repository browser.