Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

more on camera loading and many planet properties

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{
[4987]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
[10379]261        //Setting the Camera Eye, lookAt and up Vectors
[7009]262  gluLookAt(cameraPosition.x, cameraPosition.y, cameraPosition.z,
[6965]263            targetPosition.x, targetPosition.y, targetPosition.z,
[7009]264            this->upVector.x, this->upVector.y, this->upVector.z);
[7108]265}
[3636]266
[6965]267
[4490]268/**
[4836]269 *  processes an event
270 * @param event: the event to process
[4490]271*/
[4414]272void Camera::process(const Event &event)
273{
[10368]274  if (eventHandling == true)
[6424]275  {
[10368]276    if( event.type == KeyMapper::PEV_VIEW0)
277    {
278      this->setViewMode(Camera::ViewNormal);
279    }
280    else if( event.type == KeyMapper::PEV_VIEW1)
281    {
282      this->setViewMode(Camera::ViewBehind);
283    }
284    else if( event.type == KeyMapper::PEV_VIEW2)
285    {
286      this->setViewMode(Camera::ViewFront);
287    }
288    else if( event.type == KeyMapper::PEV_VIEW3)
289    {
290      this->setViewMode(Camera::ViewLeft);
291    }
292    else if( event.type == KeyMapper::PEV_VIEW4)
293    {
294      this->setViewMode(Camera::ViewRight);
295    }
296    else if( event.type == KeyMapper::PEV_VIEW5)
297    {
298      this->setViewMode(Camera::ViewTop);
299    }
[6424]300  }
[4414]301}
[3365]302
[10379]303
[10368]304void Camera::loadParams(const TiXmlElement* root)
305{
306  // Do the PNode loading stuff
[10391]307  WorldEntity::loadParams(root);
[4414]308
[10368]309  LoadParam(root, "viewTopFovy", this, Camera, setViewTopFovy);
310  LoadParam(root, "viewFrontFovy", this, Camera, setViewFrontFovy);
311  LoadParam(root, "viewLeftFovy", this, Camera, setViewLeftFovy);
312  LoadParam(root, "viewRightFovy", this, Camera, setViewRightFovy);
313  LoadParam(root, "viewBehindFovy", this, Camera, setViewBehindFovy);
314  LoadParam(root, "viewNormalFovy", this, Camera, setViewNormalFovy);
315
316  LoadParam(root, "viewTopDistance", this, Camera, setViewTopDistance);
317  LoadParam(root, "viewFrontDistance", this, Camera, setViewFrontDistance);
318  LoadParam(root, "viewLeftDistance", this, Camera, setViewLeftDistance);
319  LoadParam(root, "viewRightDistance", this, Camera, setViewRightDistance);
320  LoadParam(root, "viewBehindDistance", this, Camera, setViewBehindDistance);
321  LoadParam(root, "viewNormalDistance", this, Camera, setViewNormalDistance);
322}
323
[10379]324
[10368]325void Camera::setViewTopFovy(float fovy)
326{
327  this->viewTopFovy = fovy;
328}
329
330void Camera::setViewFrontFovy(float fovy)
331{
332  this->viewFrontFovy = fovy;
333}
334
335void Camera::setViewLeftFovy(float fovy)
336{
337  this->viewLeftFovy = fovy;
338}
339
340void Camera::setViewRightFovy(float fovy)
341{
342  this->viewRightFovy = fovy;
343}
344
345void Camera::setViewBehindFovy(float fovy)
346{
347  this->viewBehindFovy = fovy;
348}
349
350void Camera::setViewNormalFovy(float fovy)
351{
352  this->viewNormalFovy = fovy;
353}
354
355void Camera::setViewTopDistance(float Distance)
356{
357  this->viewTopDistance = Distance;
358}
359
360void Camera::setViewFrontDistance(float Distance)
361{
362  this->viewFrontDistance = Distance;
363}
364
365void Camera::setViewLeftDistance(float Distance)
366{
367  this->viewLeftDistance = Distance;
368}
369
370void Camera::setViewRightDistance(float Distance)
371{
372  this->viewRightDistance = Distance;
373}
374
375void Camera::setViewBehindDistance(float Distance)
376{
377  this->viewBehindDistance = Distance;
378}
379
380void Camera::setViewNormalDistance(float Distance)
381{
382  this->viewNormalDistance = Distance;
383}
384
385
[10379]386
387
388void Camera::glLookAt(float eyex, float eyey, float eyez, float centerx, float centery, float centerz, float upx, float upy, float upz)
389{
390  //Vector* eye=new Vector(eyex, eyey, eyez);
391  Vector* center=new Vector (centerx, centery, centerz);
392  Vector* up=new Vector(upx, upy, upz);
393
394  center->x-=eyex;
395  center->y-=eyey;
396  center->z-=eyez;
397
398  center->normalize();
399  up->normalize();
400  Vector* s = VectorProd(center, up);
401  Vector* u = VectorProd(s, center);
402  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};
403
404  glMultMatrixf(Matrix);
405  glTranslated(-eyex, -eyey, -eyez);
406  delete center;
407  delete up;
408  delete s;
409  delete u;
410
411}
412
413
414
415
416Vector* Camera::VectorProd(Vector* v1, Vector* v2)
417{
418Vector* temp= new Vector();
419temp->x=v1->y * v2->z - v1->z * v2->y;
420temp->y=v1->z * v2->x - v1->x * v2->z;
421temp->z=v1->x * v2->y - v1->y * v2->x;
422return temp;
423}
424
425
426
427
428
429
430
431
432
433
434
435
436
437
[3635]438///////////////////
439// CAMERA-TARGET //
440///////////////////
[10379]441//REATE_FACTORY(CameraTarget);
[2636]442
[10379]443
[9869]444ObjectListDefinition(CameraTarget);
[10379]445
446
[3635]447CameraTarget::CameraTarget()
[2636]448{
[9869]449  this->registerObject(this, CameraTarget::_objectList);
[6424]450  //  this->setParentMode(PNODE_MOVEMENT);
[10379]451  this->speed=1;
452  translateTo.x=0;
453  translateTo.y=0;
454  translateTo.z=0;
455  rotateBy.x=0;
456  rotateBy.y=0;
457  rotateBy.z=0;
458  target=createStick();
[2636]459}
[3213]460
[10379]461
462void CameraTarget::detach()
463{
464  masta->setParentSoft(target);
465  masta->getTargetNode()->setParentSoft(target);
466}
467
468PNode* CameraTarget::createStick()
469{
470  return new Targets();
471}
472
473
474void CameraTarget::atach(PNode* object)
475{
476  masta->setParentSoft(object);
477  masta->getTargetNode()->setParentSoft(object);
478}
479
480
481
482
483Vector CameraTarget::iterate(float dt, const Vector* Target, const Vector* cam)
484{
485
486
487  Vector tmpVec;
488  tmpVec= (*Target - *cam);
489  tmpVec.normalize();
490  return  tmpVec;
491
492}
493
494
495void CameraTarget::translate(float dt)
496{
497  if (fabs(translateTo.len()  - (target->getAbsCoor()).len()) >= 11 )
498 {
499   printf("translate\n");
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.