| 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 | } | 
|---|
| 54 |  | 
|---|
| 55 | /** | 
|---|
| 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). | 
|---|
| 61 | */ | 
|---|
| 62 | void Camera::timeSlice (Uint32 deltaT) | 
|---|
| 63 | { | 
|---|
| 64 | if( this->t <= deltaTime) | 
|---|
| 65 | {this->t += deltaT;} | 
|---|
| 66 | //printf("time is: t=%f\n", t ); | 
|---|
| 67 | updateDesiredPlace(); | 
|---|
| 68 | //jump(NULL); | 
|---|
| 69 | } | 
|---|
| 70 |  | 
|---|
| 71 | /** | 
|---|
| 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. | 
|---|
| 76 | */ | 
|---|
| 77 | void Camera::updateDesiredPlace () | 
|---|
| 78 | { | 
|---|
| 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 |  | 
|---|
| 112 |  | 
|---|
| 113 |  | 
|---|
| 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 | } | 
|---|
| 125 |  | 
|---|
| 126 | res->quatSlerp(to, from, t/deltaTime, res); | 
|---|
| 127 |  | 
|---|
| 128 | Vector ursp(0.0, 0.0, 0.0); | 
|---|
| 129 | desiredPlace.r =  ursp - res->apply(r); | 
|---|
| 130 |  | 
|---|
| 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 | } | 
|---|
| 137 | 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 | } | 
|---|
| 183 | break; | 
|---|
| 184 | } | 
|---|
| 185 | } | 
|---|
| 186 |  | 
|---|
| 187 | /** | 
|---|
| 188 | \brief initialize rendering perspective according to this camera | 
|---|
| 189 |  | 
|---|
| 190 | This is called immediately before the rendering cycle starts, it sets all global | 
|---|
| 191 | rendering options as well as the GL_PROJECTION matrix according to the camera. | 
|---|
| 192 | */ | 
|---|
| 193 | void Camera::apply () | 
|---|
| 194 | { | 
|---|
| 195 | glMatrixMode (GL_PROJECTION); | 
|---|
| 196 | 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(); | 
|---|
| 226 |  | 
|---|
| 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); | 
|---|
| 233 |  | 
|---|
| 234 | /*  translation */ | 
|---|
| 235 | //glTranslatef (this->absCoordinate.x, this->absCoordinate.y, this->absCoordinate.z ); | 
|---|
| 236 |  | 
|---|
| 237 |  | 
|---|
| 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 |  | 
|---|
| 245 | glMatrixMode (GL_MODELVIEW); | 
|---|
| 246 | glLoadIdentity (); | 
|---|
| 247 | } | 
|---|
| 248 |  | 
|---|
| 249 |  | 
|---|
| 250 |  | 
|---|
| 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) | 
|---|
| 260 | { | 
|---|
| 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 | } | 
|---|
| 269 | } | 
|---|
| 270 |  | 
|---|
| 271 |  | 
|---|
| 272 | void Camera::setWorld(World* world) | 
|---|
| 273 | { | 
|---|
| 274 | this->world = world; | 
|---|
| 275 | } | 
|---|
| 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 | } | 
|---|