| 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 |  | 
|---|
| 20 | #include "world.h" | 
|---|
| 21 | #include "world_entity.h" | 
|---|
| 22 | #include "vector.h" | 
|---|
| 23 |  | 
|---|
| 24 | using namespace std; | 
|---|
| 25 |  | 
|---|
| 26 | /** | 
|---|
| 27 | \brief creates a Camera | 
|---|
| 28 |  | 
|---|
| 29 | This standard constructor sets all parameters to zero | 
|---|
| 30 | */ | 
|---|
| 31 | Camera::Camera (World* world) | 
|---|
| 32 | { | 
|---|
| 33 | this->world = world; | 
|---|
| 34 | this->bound = NULL; | 
|---|
| 35 | /* give it some physical live */ | 
|---|
| 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; | 
|---|
| 45 |  | 
|---|
| 46 |  | 
|---|
| 47 | this->setDrawable (false); | 
|---|
| 48 | } | 
|---|
| 49 |  | 
|---|
| 50 | /** | 
|---|
| 51 | \brief default destructor | 
|---|
| 52 | */ | 
|---|
| 53 | Camera::~Camera () | 
|---|
| 54 | { | 
|---|
| 55 | this->bound = NULL; | 
|---|
| 56 | this->world = NULL; | 
|---|
| 57 |  | 
|---|
| 58 | } | 
|---|
| 59 |  | 
|---|
| 60 | /** | 
|---|
| 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 | */ | 
|---|
| 67 | void Camera::tick (Uint32 deltaT) | 
|---|
| 68 | { | 
|---|
| 69 | if( this->t <= deltaTime) | 
|---|
| 70 | {this->t += deltaT;} | 
|---|
| 71 | //printf("time is: t=%f\n", t ); | 
|---|
| 72 | updateDesiredPlace(); | 
|---|
| 73 | //jump(NULL); | 
|---|
| 74 | } | 
|---|
| 75 |  | 
|---|
| 76 | /** | 
|---|
| 77 | \brief this calculates the location where the track wants the camera to be | 
|---|
| 78 |  | 
|---|
| 79 | This refreshes the placement the camera should have according to the | 
|---|
| 80 | bound entity's position on the track. | 
|---|
| 81 | */ | 
|---|
| 82 | void Camera::updateDesiredPlace () | 
|---|
| 83 | { | 
|---|
| 84 | switch(cameraMode) | 
|---|
| 85 | { | 
|---|
| 86 |  | 
|---|
| 87 | case ELLIPTICAL: | 
|---|
| 88 | { | 
|---|
| 89 | /* | 
|---|
| 90 | //r = actual_place.r | 
|---|
| 91 | Orxonox *orx = Orxonox::getInstance(); | 
|---|
| 92 | Location lookat; | 
|---|
| 93 | Placement plFocus; | 
|---|
| 94 | if( bound != NULL) | 
|---|
| 95 | { | 
|---|
| 96 | bound->getLookat (&lookat); | 
|---|
| 97 | orx->getWorld()->calcCameraPos (&lookat, &plFocus); | 
|---|
| 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); | 
|---|
| 105 |  | 
|---|
| 106 | Vector op(1.0, 0.0, 0.0); | 
|---|
| 107 | float angle = angleDeg(op, *start); | 
|---|
| 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(); | 
|---|
| 126 | if(t < 30) | 
|---|
| 127 | { | 
|---|
| 128 | ka = rAbs / deltaTime*deltaTime; | 
|---|
| 129 | } | 
|---|
| 130 |  | 
|---|
| 131 | res->quatSlerp(to, from, t/deltaTime, res); | 
|---|
| 132 |  | 
|---|
| 133 | Vector ursp(0.0, 0.0, 0.0); | 
|---|
| 134 | desiredPlace.r =  ursp - res->apply(r); | 
|---|
| 135 |  | 
|---|
| 136 | printf("desired place is: %f, %f, %f\n", desiredPlace.r.x, desiredPlace.r.y, desiredPlace.r.z); | 
|---|
| 137 | //plLastBPlace = *bound->get_placement(); | 
|---|
| 138 |  | 
|---|
| 139 | } | 
|---|
| 140 | */ | 
|---|
| 141 | } | 
|---|
| 142 | break; | 
|---|
| 143 | case SMOTH_FOLLOW: | 
|---|
| 144 | { | 
|---|
| 145 | /* | 
|---|
| 146 | Placement *plBound = bound->getPlacement(); | 
|---|
| 147 | Location lcBound; | 
|---|
| 148 | if(bound != null) | 
|---|
| 149 | { | 
|---|
| 150 | bound->getLookat(&lcBound); | 
|---|
| 151 | Vector vDirection(0.0, 0.0, 1.0); | 
|---|
| 152 | vDirection = plBound->w.apply(vDirection); | 
|---|
| 153 | desiredPlace.r = (vDirection * ((lcBound.dist-10.0))) + Vector(0,0,5.0); | 
|---|
| 154 | } | 
|---|
| 155 | */ | 
|---|
| 156 | break; | 
|---|
| 157 | } | 
|---|
| 158 | /* this is a camera mode that tries just to follow the entity. */ | 
|---|
| 159 | case STICKY: | 
|---|
| 160 | { | 
|---|
| 161 | /* | 
|---|
| 162 | if(bound != null) | 
|---|
| 163 | { | 
|---|
| 164 | Placement *plBound = bound->getPlacement(); | 
|---|
| 165 | Vector vDirection(0.0, 0.0, 1.0); | 
|---|
| 166 | Vector eclipticOffset(0.0, 0.0, 5.0); | 
|---|
| 167 | vDirection = plBound->w.apply(vDirection); | 
|---|
| 168 | desiredPlace.r = plBound->r - vDirection*10 + eclipticOffset; | 
|---|
| 169 | } | 
|---|
| 170 | */ | 
|---|
| 171 | break; | 
|---|
| 172 | } | 
|---|
| 173 | /* the camera is handled like an entity and rolls on the track */ | 
|---|
| 174 | case NORMAL: | 
|---|
| 175 | if( bound != NULL && world != NULL ) | 
|---|
| 176 | { | 
|---|
| 177 | //FIXME: camera should be made via relative coordinates | 
|---|
| 178 | Vector* cameraOffset = new Vector (-10, 5, 0); | 
|---|
| 179 | this->setRelCoor (cameraOffset); | 
|---|
| 180 | } | 
|---|
| 181 | else | 
|---|
| 182 | { | 
|---|
| 183 | /* | 
|---|
| 184 | desiredPlace.r = Vector (0,0,0); | 
|---|
| 185 | desiredPlace.w = Quaternion (); | 
|---|
| 186 | */ | 
|---|
| 187 | } | 
|---|
| 188 | break; | 
|---|
| 189 | } | 
|---|
| 190 | } | 
|---|
| 191 |  | 
|---|
| 192 | /** | 
|---|
| 193 | \brief initialize rendering perspective according to this camera | 
|---|
| 194 |  | 
|---|
| 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. | 
|---|
| 197 | */ | 
|---|
| 198 | void Camera::apply () | 
|---|
| 199 | { | 
|---|
| 200 | glMatrixMode (GL_PROJECTION); | 
|---|
| 201 | glLoadIdentity (); | 
|---|
| 202 | // view | 
|---|
| 203 | // TO DO: implement options for frustum generation | 
|---|
| 204 | //glFrustum(-1.0, 1.0, -1.0, 1.0, 1.5, 250.0); | 
|---|
| 205 | gluPerspective(60, 1.2f, 0.1, 2000); | 
|---|
| 206 |  | 
|---|
| 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(); | 
|---|
| 231 |  | 
|---|
| 232 | // ===== first camera control calculation option | 
|---|
| 233 | // rotation | 
|---|
| 234 | float matrix[4][4]; | 
|---|
| 235 | //this->absDirection.conjugate().matrix (matrix); | 
|---|
| 236 | /* orientation and */ | 
|---|
| 237 | //glMultMatrixf ((float*)matrix); | 
|---|
| 238 |  | 
|---|
| 239 | /*  translation */ | 
|---|
| 240 | //glTranslatef (this->absCoordinate.x, this->absCoordinate.y, this->absCoordinate.z ); | 
|---|
| 241 |  | 
|---|
| 242 |  | 
|---|
| 243 | // ===== second camera control calculation option | 
|---|
| 244 |  | 
|---|
| 245 | Vector r = this->getAbsCoor(); | 
|---|
| 246 | gluLookAt(r.x, r.y, r.z, | 
|---|
| 247 | this->parent->getAbsCoor ().x, this->parent->getAbsCoor ().y, this->parent->getAbsCoor ().z, | 
|---|
| 248 | 0.0, 1.0, 0.0); | 
|---|
| 249 |  | 
|---|
| 250 |  | 
|---|
| 251 | glMatrixMode (GL_MODELVIEW); | 
|---|
| 252 | glLoadIdentity (); | 
|---|
| 253 | } | 
|---|
| 254 |  | 
|---|
| 255 |  | 
|---|
| 256 |  | 
|---|
| 257 | /** | 
|---|
| 258 | \brief bind the camera to an entity | 
|---|
| 259 | \param entity: The enitity to bind the camera to | 
|---|
| 260 |  | 
|---|
| 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. | 
|---|
| 264 | */ | 
|---|
| 265 | void Camera::bind (WorldEntity* entity) | 
|---|
| 266 | { | 
|---|
| 267 | if( entity != NULL) | 
|---|
| 268 | { | 
|---|
| 269 | if( entity->isFree()) printf("Cannot bind camera to free entity"); | 
|---|
| 270 | else | 
|---|
| 271 | { | 
|---|
| 272 | this->bound = entity; | 
|---|
| 273 | } | 
|---|
| 274 | } | 
|---|
| 275 | } | 
|---|
| 276 |  | 
|---|
| 277 |  | 
|---|
| 278 | void Camera::setWorld(World* world) | 
|---|
| 279 | { | 
|---|
| 280 | this->world = world; | 
|---|
| 281 | } | 
|---|
| 282 |  | 
|---|
| 283 |  | 
|---|