Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 3543 was 3543, checked in by bensch, 19 years ago

orxonox/trunk: some more classes now destroy themselves via virtual-destructors and call to predecessing destroy-function
also made
#include "stdincl.h" out of unnecessary h-files, so we got faster compile time.

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