| 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 |  | 
|---|
| 22 | using namespace std; | 
|---|
| 23 |  | 
|---|
| 24 | /** | 
|---|
| 25 |    \brief creates a Camera | 
|---|
| 26 |     | 
|---|
| 27 |    This standard constructor sets all parameters to zero | 
|---|
| 28 | */ | 
|---|
| 29 | Camera::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 | */ | 
|---|
| 51 | Camera::~Camera () | 
|---|
| 52 | { | 
|---|
| 53 |   this->bound = NULL; | 
|---|
| 54 |   this->world = NULL; | 
|---|
| 55 |  | 
|---|
| 56 | } | 
|---|
| 57 |  | 
|---|
| 58 | /** | 
|---|
| 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). | 
|---|
| 64 | */ | 
|---|
| 65 | void Camera::timeSlice (Uint32 deltaT) | 
|---|
| 66 | { | 
|---|
| 67 |   if( this->t <= deltaTime) | 
|---|
| 68 |     {this->t += deltaT;} | 
|---|
| 69 |   //printf("time is: t=%f\n", t ); | 
|---|
| 70 |   updateDesiredPlace(); | 
|---|
| 71 |   //jump(NULL); | 
|---|
| 72 | } | 
|---|
| 73 |  | 
|---|
| 74 | /** | 
|---|
| 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. | 
|---|
| 79 | */ | 
|---|
| 80 | void Camera::updateDesiredPlace () | 
|---|
| 81 | { | 
|---|
| 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 |                  | 
|---|
| 115 |  | 
|---|
| 116 |  | 
|---|
| 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 |               } | 
|---|
| 128 |  | 
|---|
| 129 |             res->quatSlerp(to, from, t/deltaTime, res); | 
|---|
| 130 |  | 
|---|
| 131 |             Vector ursp(0.0, 0.0, 0.0); | 
|---|
| 132 |             desiredPlace.r =  ursp - res->apply(r);  | 
|---|
| 133 |  | 
|---|
| 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 |       } | 
|---|
| 140 |       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 |  | 
|---|
| 177 |         }  | 
|---|
| 178 |       else | 
|---|
| 179 |         { | 
|---|
| 180 |           /* | 
|---|
| 181 |           desiredPlace.r = Vector (0,0,0); | 
|---|
| 182 |           desiredPlace.w = Quaternion ();  | 
|---|
| 183 |           */ | 
|---|
| 184 |         } | 
|---|
| 185 |       break; | 
|---|
| 186 |     } | 
|---|
| 187 | } | 
|---|
| 188 |  | 
|---|
| 189 | /** | 
|---|
| 190 |    \brief initialize rendering perspective according to this camera | 
|---|
| 191 |     | 
|---|
| 192 |    This is called immediately before the rendering cycle starts, it sets all global | 
|---|
| 193 |    rendering options as well as the GL_PROJECTION matrix according to the camera. | 
|---|
| 194 | */ | 
|---|
| 195 | void Camera::apply () | 
|---|
| 196 | { | 
|---|
| 197 |   glMatrixMode (GL_PROJECTION); | 
|---|
| 198 |   glLoadIdentity (); | 
|---|
| 199 |   // view | 
|---|
| 200 |   // TO DO: implement options for frustum generation | 
|---|
| 201 |   //glFrustum(-1.0, 1.0, -1.0, 1.0, 1.5, 250.0); | 
|---|
| 202 |   gluPerspective(60, 1.2f, 0.1, 250); | 
|---|
| 203 |    | 
|---|
| 204 |   //Vector up(0,0,1); | 
|---|
| 205 |   //Vector dir(1,0,0); | 
|---|
| 206 |   //Quaternion q(dir,up); | 
|---|
| 207 |   //float matrix[4][4]; | 
|---|
| 208 |   //q.conjugate().matrix (matrix); | 
|---|
| 209 |   //glMultMatrixf ((float*)matrix); | 
|---|
| 210 |   //glTranslatef (10,0,-5); | 
|---|
| 211 |   // | 
|---|
| 212 |   //dir = Vector(-1,-1,0); | 
|---|
| 213 |   //q = Quaternion( dir, up); | 
|---|
| 214 |   //glMatrixMode (GL_MODELVIEW); | 
|---|
| 215 |   //glLoadIdentity (); | 
|---|
| 216 |   //q.matrix (matrix); | 
|---|
| 217 |   //glMultMatrixf ((float*)matrix); | 
|---|
| 218 |   //glTranslatef (2,2,0); | 
|---|
| 219 |   // | 
|---|
| 220 |   //glBegin(GL_TRIANGLES); | 
|---|
| 221 |   //glColor3f(1,0,0); | 
|---|
| 222 |   //glVertex3f(0,0,0.5); | 
|---|
| 223 |   //glColor3f(0,1,0); | 
|---|
| 224 |   //glVertex3f(-0.5,0,-1); | 
|---|
| 225 |   //glColor3f(0,0,1); | 
|---|
| 226 |   //glVertex3f(0.5,0,-1); | 
|---|
| 227 |   //glEnd();     | 
|---|
| 228 |  | 
|---|
| 229 |   // ===== first camera control calculation option | 
|---|
| 230 |   // rotation | 
|---|
| 231 |   float matrix[4][4]; | 
|---|
| 232 |   //this->absDirection.conjugate().matrix (matrix); | 
|---|
| 233 |   /* orientation and */ | 
|---|
| 234 |   //glMultMatrixf ((float*)matrix); | 
|---|
| 235 |  | 
|---|
| 236 |   /*  translation */ | 
|---|
| 237 |   //glTranslatef (this->absCoordinate.x, this->absCoordinate.y, this->absCoordinate.z ); | 
|---|
| 238 |  | 
|---|
| 239 |  | 
|---|
| 240 |   // ===== second camera control calculation option | 
|---|
| 241 |    | 
|---|
| 242 |   gluLookAt(this->absCoordinate.x, this->absCoordinate.y, this->absCoordinate.z,  | 
|---|
| 243 |             this->parent->getAbsCoor ().x, this->parent->getAbsCoor ().y, this->parent->getAbsCoor ().z, | 
|---|
| 244 |             0.0, 1.0, 0.0); | 
|---|
| 245 |    | 
|---|
| 246 |  | 
|---|
| 247 |   glMatrixMode (GL_MODELVIEW); | 
|---|
| 248 |   glLoadIdentity (); | 
|---|
| 249 | } | 
|---|
| 250 |  | 
|---|
| 251 |  | 
|---|
| 252 |  | 
|---|
| 253 | /** | 
|---|
| 254 |   \brief bind the camera to an entity | 
|---|
| 255 |   \param entity: The enitity to bind the camera to | 
|---|
| 256 |          | 
|---|
| 257 |   This sets the focus of the camera to the given entity. This means that it will use the given WorldEntity's | 
|---|
| 258 |   Location and get_lookat() to determine the viewpoint the camera will render from. | 
|---|
| 259 |   Note that you cannot bind a camera to a free entity. | 
|---|
| 260 | */ | 
|---|
| 261 | void Camera::bind (WorldEntity* entity) | 
|---|
| 262 | { | 
|---|
| 263 |   if( entity != NULL) | 
|---|
| 264 |     { | 
|---|
| 265 |       if( entity->isFree()) printf("Cannot bind camera to free entity"); | 
|---|
| 266 |       else  | 
|---|
| 267 |         { | 
|---|
| 268 |           this->bound = entity; | 
|---|
| 269 |         } | 
|---|
| 270 |     }  | 
|---|
| 271 | } | 
|---|
| 272 |  | 
|---|
| 273 |  | 
|---|
| 274 | void Camera::setWorld(World* world) | 
|---|
| 275 | { | 
|---|
| 276 |   this->world = world; | 
|---|
| 277 | } | 
|---|
| 278 |  | 
|---|
| 279 |  | 
|---|