Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10390 was 10389, checked in by bknecht, 19 years ago

made the camera able to follow a track

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