Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10386 was 10379, checked in by patrick, 19 years ago

merged branche camera to trunk. resolved many conflicts as in the other projects too

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