Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 3746 in orxonox.OLD for orxonox/branches/levelloader/src/camera.cc


Ignore:
Timestamp:
Apr 7, 2005, 3:54:49 PM (19 years ago)
Author:
chris
Message:

orxonox/branches/levelloader: Merged trunk into branch… still not working though…

File:
1 edited

Legend:

Unmodified
Added
Removed
  • orxonox/branches/levelloader/src/camera.cc

    r3605 r3746  
    1717
    1818#include "camera.h"
     19
    1920#include "world.h"
    2021#include "world_entity.h"
     22#include "vector.h"
    2123
    2224using namespace std;
    2325
     26////////////
     27// CAMERA //
     28////////////
     29
    2430/**
    2531   \brief creates a Camera
    26    
    27    This standard constructor sets all parameters to zero
    2832*/
    29 Camera::Camera (World* world)
     33Camera::Camera(void)
    3034{
    31   this->world = world;
    32   this->bound = NULL;
    33   /* give it some physical live */
    34   this->m = 10;
    35   this->a = new Vector(0.0, 0.0, 0.0);
    36   this->v = new Vector(0.0, 0.0, 0.0);
    37   this->fs = new Vector(0.0, 0.0, 0.0);
    38   this->cameraMode = NORMAL;
    39   this->deltaTime = 3000.0;
    40   this->cameraOffset = 1.0;
    41   this->cameraOffsetZ = 10.0;
    42   this->t = 0.0;
     35  this->setClassName("Camera");
     36  this->target = new CameraTarget();
    4337
     38  this->setFovy(90);
     39  this->setAspectRatio(1.2f);
     40  this->setClipRegion(.1, 2000);
    4441
    45   this->setDrawable (false);
     42  this->setViewMode(VIEW_NORMAL);
    4643}
    4744
     
    4946   \brief default destructor
    5047*/
    51 Camera::~Camera ()
     48Camera::~Camera(void)
    5249{
    53   this->bound = NULL;
    54   this->world = NULL;
    55 
    5650}
    5751
    5852/**
    59    \brief time based actualisation of camera parameters
    60    \param deltaT: The amount of time that has passed in milliseconds
    61    
    62    This is called by the World in every time_slice, use it to do fancy time dependant effects (such
    63    as smooth camera movement or swaying).
     53   \brief focuses the Camera onto a Target
     54   \param target the new PNode the Camera should look at.
    6455*/
    65 void Camera::tick (Uint32 deltaT)
     56void Camera::lookAt(PNode* target)
    6657{
    67   if( this->t <= deltaTime)
    68     {this->t += deltaT;}
    69   //printf("time is: t=%f\n", t );
    70   updateDesiredPlace();
    71   //jump(NULL);
     58  this->target->setParent(target);
    7259}
    7360
    7461/**
    75    \brief this calculates the location where the track wants the camera to be
    76    
    77    This refreshes the placement the camera should have according to the
    78    bound entity's position on the track.
     62   \returns The PNode of the Target (from there you can get position and so on
    7963*/
    80 void Camera::updateDesiredPlace ()
     64PNode* Camera::getTarget(void)
    8165{
    82   switch(cameraMode)
    83     {
    84      
    85     case ELLIPTICAL:
    86       {
    87         /*
    88         //r = actual_place.r
    89         Orxonox *orx = Orxonox::getInstance();
    90         Location lookat; 
    91         Placement plFocus;
    92         if( bound != NULL)
    93           {
    94             bound->getLookat (&lookat);
    95             orx->getWorld()->calcCameraPos (&lookat, &plFocus);
    96             Quaternion *fr;
    97             if(t < 20.0)
    98               {
    99                 Vector *start = new Vector(0.0, 1.0, 0.0);
    100                 r = *(new Vector(0.0, 5.0, 0.0));
    101 
    102                 Vector up(0.0, 0.0, 1.0);
    103                
    104                 Vector op(1.0, 0.0, 0.0);
    105                 float angle = angleDeg(op, *start);
    106                 printf("angle is: %f\n", angle);
    107 
    108                 //if in one plane
    109                 from = new Quaternion(angle, up);
    110 
    111                 //from = new Quaternion(*start, *up);
    112                 //&from = &plFocus.w;
    113                 //fr = &plFocus.w; real quaternion use
    114                
     66  return (PNode*)this->target;
     67}
    11568
    11669
    117                 Vector vDirection(1.0, 0.0, 0.0);
    118                 //vDirection = plFocus.w.apply(vDirection);
    119                 to = new Quaternion(vDirection, *start);
    120                 res = new Quaternion();
    121               }
    122             //printf("vector r = %f, %f, %f\n",r.x, r.y, r.z );
    123             rAbs = r.len();
    124             if(t < 30)
    125               {
    126                 ka = rAbs / deltaTime*deltaTime;
    127               }
     70/**
     71   \brief sets a new AspectRatio
     72   \param aspectRatio the new aspect ratio to set (width / height)
     73*/
     74void Camera::setAspectRatio(float aspectRatio)
     75{
     76  this->aspectRatio = aspectRatio;
     77}
    12878
    129             res->quatSlerp(to, from, t/deltaTime, res);
     79/**
     80   \brief sets the Field of View to fofy
     81   \param fovy new field of view factor (in degrees)
     82*/
     83void Camera::setFovy(float fovy)
     84{
     85  this->fovy = fovy;
     86}
    13087
    131             Vector ursp(0.0, 0.0, 0.0);
    132             desiredPlace.r =  ursp - res->apply(r);
     88/**
     89  \brief Sets a new clipping region
     90  \param nearClip The near clip plane
     91  \param farClip The far clip plane
     92*/
     93void Camera::setClipRegion(float nearClip, float farClip)
     94{
     95  this->nearClip = nearClip;
     96  this->farClip = farClip;
     97}
    13398
    134             printf("desired place is: %f, %f, %f\n", desiredPlace.r.x, desiredPlace.r.y, desiredPlace.r.z);
    135             //plLastBPlace = *bound->get_placement();
    136            
    137           }
    138       */
    139       }
     99void Camera::setViewMode(ViewMode mode)
     100{
     101  switch (mode)
     102    {
     103    default:
     104    case VIEW_NORMAL:
     105      this->toFovy = 60.0;
     106      this->toRelCoor = Vector(-10, 5, 0);
    140107      break;
    141     case SMOTH_FOLLOW:
    142       {
    143         /*
    144         Placement *plBound = bound->getPlacement();
    145         Location lcBound;
    146         if(bound != null)
    147           {
    148             bound->getLookat(&lcBound);
    149             Vector vDirection(0.0, 0.0, 1.0);
    150             vDirection = plBound->w.apply(vDirection);
    151             desiredPlace.r = (vDirection * ((lcBound.dist-10.0))) + Vector(0,0,5.0);
    152           }
    153         */
    154         break;
    155       }
    156       /* this is a camera mode that tries just to follow the entity. */
    157     case STICKY:
    158       {
    159         /*
    160         if(bound != null)
    161           {
    162             Placement *plBound = bound->getPlacement();
    163             Vector vDirection(0.0, 0.0, 1.0);
    164             Vector eclipticOffset(0.0, 0.0, 5.0);
    165             vDirection = plBound->w.apply(vDirection);
    166             desiredPlace.r = plBound->r - vDirection*10 + eclipticOffset;
    167           }
    168         */
    169         break;
    170       }
    171       /* the camera is handled like an entity and rolls on the track */
    172     case NORMAL:
    173       if( bound != NULL && world != NULL )
    174         {
    175           //FIXME: camera should be made via relative coordinates
    176           Vector* cameraOffset = new Vector (-10, 5, 0);
    177           this->setRelCoor (cameraOffset);
    178         }
    179       else
    180         {
    181           /*
    182           desiredPlace.r = Vector (0,0,0);
    183           desiredPlace.w = Quaternion ();
    184           */
    185         }
     108    case VIEW_BEHIND:
     109      this->toFovy = 120.0;
     110      this->toRelCoor = Vector(-7, 0, 0);
    186111      break;
     112    case VIEW_FRONT:
     113      this->toFovy = 95.0;
     114      this->toRelCoor = Vector(12, 5, 0);
     115      break;
     116    case VIEW_LEFT:
     117      this->toFovy = 90;
     118      this->toRelCoor = Vector(0, 2, -10);
     119      break;
     120    case VIEW_RIGHT:
     121      this->toFovy = 90;
     122      this->toRelCoor = Vector(0, 2, 10);
     123      break;
     124    case VIEW_TOP:
     125      this->toFovy= 120;
     126      this->toRelCoor = Vector(0, 4, 0);
    187127    }
    188128}
     129
     130
     131/**
     132   \brief Updates the position of the camera.
     133   \param dt The time that elapsed.
     134*/
     135void Camera::tick(float dt)
     136{
     137  dt /= 500;
     138  float tmpFovy = (this->toFovy - this->fovy) * dt;
     139  if (tmpFovy > .001)
     140    this->fovy += (this->toFovy - this->fovy) * dt;
     141  Vector tmpPos = (this->toRelCoor - *this->getRelCoor()) * dt;
     142  if (tmpPos.len() >= .001)
     143    {
     144      tmpPos = tmpPos + *this->getRelCoor();
     145      this->setRelCoor(&tmpPos);
     146    }
     147}
     148
    189149
    190150/**
     
    196156void Camera::apply ()
    197157{
     158  // switching to Projection Matrix
    198159  glMatrixMode (GL_PROJECTION);
    199160  glLoadIdentity ();
    200   // view
    201   // TO DO: implement options for frustum generation
    202   //glFrustum(-1.0, 1.0, -1.0, 1.0, 1.5, 250.0);
    203   gluPerspective(60, 1.2f, 0.1, 2000);
    204  
    205   //Vector up(0,0,1);
    206   //Vector dir(1,0,0);
    207   //Quaternion q(dir,up);
    208   //float matrix[4][4];
    209   //q.conjugate().matrix (matrix);
    210   //glMultMatrixf ((float*)matrix);
    211   //glTranslatef (10,0,-5);
    212   //
    213   //dir = Vector(-1,-1,0);
    214   //q = Quaternion( dir, up);
    215   //glMatrixMode (GL_MODELVIEW);
    216   //glLoadIdentity ();
    217   //q.matrix (matrix);
    218   //glMultMatrixf ((float*)matrix);
    219   //glTranslatef (2,2,0);
    220   //
    221   //glBegin(GL_TRIANGLES);
    222   //glColor3f(1,0,0);
    223   //glVertex3f(0,0,0.5);
    224   //glColor3f(0,1,0);
    225   //glVertex3f(-0.5,0,-1);
    226   //glColor3f(0,0,1);
    227   //glVertex3f(0.5,0,-1);
    228   //glEnd();   
    229161
    230   // ===== first camera control calculation option
    231   // rotation
    232   float matrix[4][4];
    233   //this->absDirection.conjugate().matrix (matrix);
    234   /* orientation and */
    235   //glMultMatrixf ((float*)matrix);
     162  // setting up the perspective
     163  gluPerspective(this->fovy,
     164                 this->aspectRatio,
     165                 this->nearClip,
     166                 this->farClip);
    236167
    237   /*  translation */
    238   //glTranslatef (this->absCoordinate.x, this->absCoordinate.y, this->absCoordinate.z );
     168  // speed-up feature
     169  Vector cameraPosition = this->getAbsCoor();
     170  Vector targetPosition = this->target->getAbsCoor();
     171  Vector up = Vector(0, 1, 0);
     172  up = this->getAbsDir().apply(up);
    239173
     174  // Setting the Camera Eye, lookAt and up Vectors
     175  gluLookAt(cameraPosition.x, cameraPosition.y, cameraPosition.z,
     176            targetPosition.x, targetPosition.y, targetPosition.z,
     177            up.x, up.y, up.z);
    240178
    241   // ===== second camera control calculation option
    242  
    243   gluLookAt(this->absCoordinate.x, this->absCoordinate.y, this->absCoordinate.z,
    244             this->parent->getAbsCoor ().x, this->parent->getAbsCoor ().y, this->parent->getAbsCoor ().z,
    245             0.0, 1.0, 0.0);
    246  
    247 
     179  // switching back to Modeling Matrix
    248180  glMatrixMode (GL_MODELVIEW);
    249   glLoadIdentity ();
    250181}
    251182
    252183
    253184
    254 /**
    255   \brief bind the camera to an entity
    256   \param entity: The enitity to bind the camera to
    257        
    258   This sets the focus of the camera to the given entity. This means that it will use the given WorldEntity's
    259   Location and get_lookat() to determine the viewpoint the camera will render from.
    260   Note that you cannot bind a camera to a free entity.
    261 */
    262 void Camera::bind (WorldEntity* entity)
     185///////////////////
     186// CAMERA-TARGET //
     187///////////////////
     188
     189
     190CameraTarget::CameraTarget()
    263191{
    264   if( entity != NULL)
    265     {
    266       if( entity->isFree()) printf("Cannot bind camera to free entity");
    267       else
    268         {
    269           this->bound = entity;
    270         }
    271     }
     192  this->setClassName("CameraTarget");
     193  this->setMode(PNODE_MOVEMENT);
    272194}
    273195
     196CameraTarget::~CameraTarget()
     197{
    274198
    275 void Camera::setWorld(World* world)
    276 {
    277   this->world = world;
    278199}
    279 
    280 
Note: See TracChangeset for help on using the changeset viewer.