Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/trunk/src/camera.cc @ 3618

Last change on this file since 3618 was 3608, checked in by patrick, 19 years ago

orxonox/trunk: now there is a real speedup in compiling time when dependencies are modified: just realy only includes, what is needed. Byside the speedup, there is more overview! never add an orxonox class to stdincl.h if it doesn't have to be

File size: 6.4 KB
RevLine 
[2068]1
2
3/*
4   orxonox - the future of 3D-vertical-scrollers
5
6   Copyright (C) 2004 orx
7
8   This program is free software; you can redistribute it and/or modify
9   it under the terms of the GNU General Public License as published by
10   the Free Software Foundation; either version 2, or (at your option)
11   any later version.
12
13   ### File Specific:
[2080]14   main-programmer: Christian Meyer
[2068]15   co-programmer: ...
16*/
17
18#include "camera.h"
[3608]19
[2100]20#include "world.h"
21#include "world_entity.h"
[3608]22#include "vector.h"
[2068]23
24using namespace std;
25
[2096]26/**
27   \brief creates a Camera
28   
29   This standard constructor sets all parameters to zero
30*/
[2636]31Camera::Camera (World* world)
[2068]32{
[2636]33  this->world = world;
[3215]34  this->bound = NULL;
[2551]35  /* give it some physical live */
[3215]36  this->m = 10;
37  this->a = new Vector(0.0, 0.0, 0.0);
38  this->v = new Vector(0.0, 0.0, 0.0);
39  this->fs = new Vector(0.0, 0.0, 0.0);
40  this->cameraMode = NORMAL;
41  this->deltaTime = 3000.0;
42  this->cameraOffset = 1.0;
43  this->cameraOffsetZ = 10.0;
44  this->t = 0.0;
[2551]45
[3365]46
47  this->setDrawable (false);
[2068]48}
49
[2096]50/**
51   \brief default destructor
52*/
[2068]53Camera::~Camera ()
54{
[3543]55  this->bound = NULL;
56  this->world = NULL;
57
58}
59
60/**
[2096]61   \brief time based actualisation of camera parameters
62   \param deltaT: The amount of time that has passed in milliseconds
63   
64   This is called by the World in every time_slice, use it to do fancy time dependant effects (such
65   as smooth camera movement or swaying).
66*/
[3551]67void Camera::tick (Uint32 deltaT)
[2068]68{
[3215]69  if( this->t <= deltaTime)
70    {this->t += deltaT;}
[2551]71  //printf("time is: t=%f\n", t );
[3236]72  updateDesiredPlace();
[3365]73  //jump(NULL);
[2068]74}
75
[2096]76/**
[2551]77   \brief this calculates the location where the track wants the camera to be
[2096]78   
[2551]79   This refreshes the placement the camera should have according to the
80   bound entity's position on the track.
[2096]81*/
[3228]82void Camera::updateDesiredPlace ()
[2068]83{
[2551]84  switch(cameraMode)
85    {
86     
87    case ELLIPTICAL:
88      {
[3365]89        /*
[2551]90        //r = actual_place.r
91        Orxonox *orx = Orxonox::getInstance();
92        Location lookat; 
93        Placement plFocus;
94        if( bound != NULL)
95          {
[3236]96            bound->getLookat (&lookat);
[3228]97            orx->getWorld()->calcCameraPos (&lookat, &plFocus);
[2551]98            Quaternion *fr;
99            if(t < 20.0)
100              {
101                Vector *start = new Vector(0.0, 1.0, 0.0);
102                r = *(new Vector(0.0, 5.0, 0.0));
103
104                Vector up(0.0, 0.0, 1.0);
[2068]105               
[2551]106                Vector op(1.0, 0.0, 0.0);
[3228]107                float angle = angleDeg(op, *start);
[2551]108                printf("angle is: %f\n", angle);
109
110                //if in one plane
111                from = new Quaternion(angle, up);
112
113                //from = new Quaternion(*start, *up);
114                //&from = &plFocus.w;
115                //fr = &plFocus.w; real quaternion use
116               
117
118
119                Vector vDirection(1.0, 0.0, 0.0);
120                //vDirection = plFocus.w.apply(vDirection);
121                to = new Quaternion(vDirection, *start);
122                res = new Quaternion();
123              }
124            //printf("vector r = %f, %f, %f\n",r.x, r.y, r.z );
125            rAbs = r.len();
[3365]126            if(t < 30)
[2551]127              {
128                ka = rAbs / deltaTime*deltaTime;
129              }
[3365]130
[2551]131            res->quatSlerp(to, from, t/deltaTime, res);
132
133            Vector ursp(0.0, 0.0, 0.0);
[3365]134            desiredPlace.r =  ursp - res->apply(r);
[2551]135
[3228]136            printf("desired place is: %f, %f, %f\n", desiredPlace.r.x, desiredPlace.r.y, desiredPlace.r.z);
[2551]137            //plLastBPlace = *bound->get_placement();
[3365]138           
[2551]139          }
[3365]140      */
[2551]141      }
142      break;
143    case SMOTH_FOLLOW:
144      {
[3365]145        /*
[3228]146        Placement *plBound = bound->getPlacement();
[2551]147        Location lcBound;
148        if(bound != null)
149          {
[3228]150            bound->getLookat(&lcBound);
[2551]151            Vector vDirection(0.0, 0.0, 1.0);
152            vDirection = plBound->w.apply(vDirection);
[3365]153            desiredPlace.r = (vDirection * ((lcBound.dist-10.0))) + Vector(0,0,5.0);
[2551]154          }
[3365]155        */
[2551]156        break;
157      }
158      /* this is a camera mode that tries just to follow the entity. */
159    case STICKY:
160      {
[3365]161        /*
[2551]162        if(bound != null)
163          {
[3236]164            Placement *plBound = bound->getPlacement();
[2551]165            Vector vDirection(0.0, 0.0, 1.0);
166            Vector eclipticOffset(0.0, 0.0, 5.0);
167            vDirection = plBound->w.apply(vDirection);
[3228]168            desiredPlace.r = plBound->r - vDirection*10 + eclipticOffset;
[2551]169          }
[3365]170        */
[2551]171        break;
172      }
173      /* the camera is handled like an entity and rolls on the track */
174    case NORMAL:
[2636]175      if( bound != NULL && world != NULL )
[2551]176        {
[3365]177          //FIXME: camera should be made via relative coordinates
[3550]178          Vector* cameraOffset = new Vector (-10, 5, 0);
179          this->setRelCoor (cameraOffset);
[2551]180        } 
181      else
182        {
[3365]183          /*
[3228]184          desiredPlace.r = Vector (0,0,0);
185          desiredPlace.w = Quaternion ();
[3365]186          */
[2551]187        }
188      break;
189    }
[2068]190}
191
[2096]192/**
[2551]193   \brief initialize rendering perspective according to this camera
[2096]194   
[2551]195   This is called immediately before the rendering cycle starts, it sets all global
196   rendering options as well as the GL_PROJECTION matrix according to the camera.
[2096]197*/
[2068]198void Camera::apply ()
199{
[2551]200  glMatrixMode (GL_PROJECTION);
[2112]201  glLoadIdentity ();
[2551]202  // view
203  // TO DO: implement options for frustum generation
[3175]204  //glFrustum(-1.0, 1.0, -1.0, 1.0, 1.5, 250.0);
[3566]205  gluPerspective(60, 1.2f, 0.1, 2000);
[3175]206 
[2551]207  //Vector up(0,0,1);
208  //Vector dir(1,0,0);
209  //Quaternion q(dir,up);
210  //float matrix[4][4];
211  //q.conjugate().matrix (matrix);
212  //glMultMatrixf ((float*)matrix);
213  //glTranslatef (10,0,-5);
214  //
215  //dir = Vector(-1,-1,0);
216  //q = Quaternion( dir, up);
217  //glMatrixMode (GL_MODELVIEW);
218  //glLoadIdentity ();
219  //q.matrix (matrix);
220  //glMultMatrixf ((float*)matrix);
221  //glTranslatef (2,2,0);
222  //
223  //glBegin(GL_TRIANGLES);
224  //glColor3f(1,0,0);
225  //glVertex3f(0,0,0.5);
226  //glColor3f(0,1,0);
227  //glVertex3f(-0.5,0,-1);
228  //glColor3f(0,0,1);
229  //glVertex3f(0.5,0,-1);
230  //glEnd();   
[2115]231
[2551]232  // ===== first camera control calculation option
233  // rotation
[2112]234  float matrix[4][4];
[3365]235  //this->absDirection.conjugate().matrix (matrix);
[2551]236  /* orientation and */
[3365]237  //glMultMatrixf ((float*)matrix);
238
[2551]239  /*  translation */
[3365]240  //glTranslatef (this->absCoordinate.x, this->absCoordinate.y, this->absCoordinate.z );
[2551]241
[3365]242
[2551]243  // ===== second camera control calculation option
[3607]244
245  Vector r = this->getAbsCoor();
246  gluLookAt(r.x, r.y, r.z, 
[3365]247            this->parent->getAbsCoor ().x, this->parent->getAbsCoor ().y, this->parent->getAbsCoor ().z,
248            0.0, 1.0, 0.0);
249 
[2551]250
[2068]251  glMatrixMode (GL_MODELVIEW);
[2080]252  glLoadIdentity ();
[2068]253}
254
255
[3365]256
[2096]257/**
258  \brief bind the camera to an entity
259  \param entity: The enitity to bind the camera to
260       
[3213]261  This sets the focus of the camera to the given entity. This means that it will use the given WorldEntity's
262  Location and get_lookat() to determine the viewpoint the camera will render from.
263  Note that you cannot bind a camera to a free entity.
[2096]264*/
[2068]265void Camera::bind (WorldEntity* entity)
266{
[2551]267  if( entity != NULL)
268    {
[3213]269      if( entity->isFree()) printf("Cannot bind camera to free entity");
[2551]270      else 
[2080]271        {
[3213]272          this->bound = entity;
[2551]273        }
274    } 
[2068]275}
[2636]276
277
278void Camera::setWorld(World* world)
279{
280  this->world = world;
281}
[3213]282
283
Note: See TracBrowser for help on using the repository browser.