Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10413 was 10410, checked in by patrick, 19 years ago

the track now is been drawn for the camera

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