Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 3681 in orxonox.OLD for orxonox/branches/textEngine/src/camera.cc


Ignore:
Timestamp:
Mar 30, 2005, 9:02:23 PM (20 years ago)
Author:
bensch
Message:

orxonox/branches/textEngine: merged trunk here.
merged with command:
svn merge ../trunk textEngine -r 3467:HEAD
no conflicts

File:
1 edited

Legend:

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

    r3365 r3681  
    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{
    5350}
    5451
    5552/**
    56    \brief time based actualisation of camera parameters
    57    \param deltaT: The amount of time that has passed in milliseconds
    58    
    59    This is called by the World in every time_slice, use it to do fancy time dependant effects (such
    60    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.
    6155*/
    62 void Camera::timeSlice (Uint32 deltaT)
     56void Camera::lookAt(PNode* target)
    6357{
    64   if( this->t <= deltaTime)
    65     {this->t += deltaT;}
    66   //printf("time is: t=%f\n", t );
    67   updateDesiredPlace();
    68   //jump(NULL);
     58  this->target->setParent(target);
    6959}
    7060
    7161/**
    72    \brief this calculates the location where the track wants the camera to be
    73    
    74    This refreshes the placement the camera should have according to the
    75    bound entity's position on the track.
     62   \returns The PNode of the Target (from there you can get position and so on
    7663*/
    77 void Camera::updateDesiredPlace ()
     64PNode* Camera::getTarget(void)
    7865{
    79   switch(cameraMode)
    80     {
    81      
    82     case ELLIPTICAL:
    83       {
    84         /*
    85         //r = actual_place.r
    86         Orxonox *orx = Orxonox::getInstance();
    87         Location lookat; 
    88         Placement plFocus;
    89         if( bound != NULL)
    90           {
    91             bound->getLookat (&lookat);
    92             orx->getWorld()->calcCameraPos (&lookat, &plFocus);
    93             Quaternion *fr;
    94             if(t < 20.0)
    95               {
    96                 Vector *start = new Vector(0.0, 1.0, 0.0);
    97                 r = *(new Vector(0.0, 5.0, 0.0));
    98 
    99                 Vector up(0.0, 0.0, 1.0);
    100                
    101                 Vector op(1.0, 0.0, 0.0);
    102                 float angle = angleDeg(op, *start);
    103                 printf("angle is: %f\n", angle);
    104 
    105                 //if in one plane
    106                 from = new Quaternion(angle, up);
    107 
    108                 //from = new Quaternion(*start, *up);
    109                 //&from = &plFocus.w;
    110                 //fr = &plFocus.w; real quaternion use
    111                
     66  return (PNode*)this->target;
     67}
    11268
    11369
    114                 Vector vDirection(1.0, 0.0, 0.0);
    115                 //vDirection = plFocus.w.apply(vDirection);
    116                 to = new Quaternion(vDirection, *start);
    117                 res = new Quaternion();
    118               }
    119             //printf("vector r = %f, %f, %f\n",r.x, r.y, r.z );
    120             rAbs = r.len();
    121             if(t < 30)
    122               {
    123                 ka = rAbs / deltaTime*deltaTime;
    124               }
     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}
    12578
    126             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}
    12787
    128             Vector ursp(0.0, 0.0, 0.0);
    129             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}
    13098
    131             printf("desired place is: %f, %f, %f\n", desiredPlace.r.x, desiredPlace.r.y, desiredPlace.r.z);
    132             //plLastBPlace = *bound->get_placement();
    133            
    134           }
    135       */
    136       }
     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);
    137107      break;
    138     case SMOTH_FOLLOW:
    139       {
    140         /*
    141         Placement *plBound = bound->getPlacement();
    142         Location lcBound;
    143         if(bound != null)
    144           {
    145             bound->getLookat(&lcBound);
    146             Vector vDirection(0.0, 0.0, 1.0);
    147             vDirection = plBound->w.apply(vDirection);
    148             desiredPlace.r = (vDirection * ((lcBound.dist-10.0))) + Vector(0,0,5.0);
    149           }
    150         */
    151         break;
    152       }
    153       /* this is a camera mode that tries just to follow the entity. */
    154     case STICKY:
    155       {
    156         /*
    157         if(bound != null)
    158           {
    159             Placement *plBound = bound->getPlacement();
    160             Vector vDirection(0.0, 0.0, 1.0);
    161             Vector eclipticOffset(0.0, 0.0, 5.0);
    162             vDirection = plBound->w.apply(vDirection);
    163             desiredPlace.r = plBound->r - vDirection*10 + eclipticOffset;
    164           }
    165         */
    166         break;
    167       }
    168       /* the camera is handled like an entity and rolls on the track */
    169     case NORMAL:
    170       if( bound != NULL && world != NULL )
    171         {
    172           //FIXME: camera should be made via relative coordinates
    173           Vector* cameraOffset = new Vector (-10, 5, 0);
    174           this->setRelCoor (cameraOffset);
    175         }
    176       else
    177         {
    178           /*
    179           desiredPlace.r = Vector (0,0,0);
    180           desiredPlace.w = Quaternion ();
    181           */
    182         }
     108    case VIEW_BEHIND:
     109      this->toFovy = 120.0;
     110      this->toRelCoor = Vector(-7, 0, 0);
    183111      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);
    184127    }
    185128}
     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
    186149
    187150/**
     
    193156void Camera::apply ()
    194157{
     158  // switching to Projection Matrix
    195159  glMatrixMode (GL_PROJECTION);
    196160  glLoadIdentity ();
    197   // view
    198   // TO DO: implement options for frustum generation
    199   //glFrustum(-1.0, 1.0, -1.0, 1.0, 1.5, 250.0);
    200   gluPerspective(60, 1.2f, 0.1, 250);
    201  
    202   //Vector up(0,0,1);
    203   //Vector dir(1,0,0);
    204   //Quaternion q(dir,up);
    205   //float matrix[4][4];
    206   //q.conjugate().matrix (matrix);
    207   //glMultMatrixf ((float*)matrix);
    208   //glTranslatef (10,0,-5);
    209   //
    210   //dir = Vector(-1,-1,0);
    211   //q = Quaternion( dir, up);
    212   //glMatrixMode (GL_MODELVIEW);
    213   //glLoadIdentity ();
    214   //q.matrix (matrix);
    215   //glMultMatrixf ((float*)matrix);
    216   //glTranslatef (2,2,0);
    217   //
    218   //glBegin(GL_TRIANGLES);
    219   //glColor3f(1,0,0);
    220   //glVertex3f(0,0,0.5);
    221   //glColor3f(0,1,0);
    222   //glVertex3f(-0.5,0,-1);
    223   //glColor3f(0,0,1);
    224   //glVertex3f(0.5,0,-1);
    225   //glEnd();   
    226161
    227   // ===== first camera control calculation option
    228   // rotation
    229   float matrix[4][4];
    230   //this->absDirection.conjugate().matrix (matrix);
    231   /* orientation and */
    232   //glMultMatrixf ((float*)matrix);
     162  // setting up the perspective
     163  gluPerspective(this->fovy,
     164                 this->aspectRatio,
     165                 this->nearClip,
     166                 this->farClip);
    233167
    234   /*  translation */
    235   //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);
    236173
     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);
    237178
    238   // ===== second camera control calculation option
    239  
    240   gluLookAt(this->absCoordinate.x, this->absCoordinate.y, this->absCoordinate.z,
    241             this->parent->getAbsCoor ().x, this->parent->getAbsCoor ().y, this->parent->getAbsCoor ().z,
    242             0.0, 1.0, 0.0);
    243  
    244 
     179  // switching back to Modeling Matrix
    245180  glMatrixMode (GL_MODELVIEW);
    246   glLoadIdentity ();
    247181}
    248182
    249183
    250184
    251 /**
    252   \brief bind the camera to an entity
    253   \param entity: The enitity to bind the camera to
    254        
    255   This sets the focus of the camera to the given entity. This means that it will use the given WorldEntity's
    256   Location and get_lookat() to determine the viewpoint the camera will render from.
    257   Note that you cannot bind a camera to a free entity.
    258 */
    259 void Camera::bind (WorldEntity* entity)
     185///////////////////
     186// CAMERA-TARGET //
     187///////////////////
     188
     189
     190CameraTarget::CameraTarget()
    260191{
    261   if( entity != NULL)
    262     {
    263       if( entity->isFree()) printf("Cannot bind camera to free entity");
    264       else
    265         {
    266           this->bound = entity;
    267         }
    268     }
     192  this->setClassName("CameraTarget");
     193  this->setMode(PNODE_MOVEMENT);
    269194}
    270195
     196CameraTarget::~CameraTarget()
     197{
    271198
    272 void Camera::setWorld(World* world)
    273 {
    274   this->world = world;
    275199}
    276 
    277 
    278 /**
    279    \brief destroy, reset the camera so that it doesn't perform anything anymore
    280 
    281 */
    282 void Camera::destroy()
    283 {
    284   this->bound = NULL;
    285   this->world = NULL;
    286 }
Note: See TracChangeset for help on using the changeset viewer.