[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" |
---|
[2100] | 19 | #include "world.h" |
---|
| 20 | #include "world_entity.h" |
---|
[2068] | 21 | |
---|
| 22 | using namespace std; |
---|
| 23 | |
---|
[2096] | 24 | /** |
---|
| 25 | \brief creates a Camera |
---|
| 26 | |
---|
| 27 | This standard constructor sets all parameters to zero |
---|
| 28 | */ |
---|
[2636] | 29 | Camera::Camera (World* world) |
---|
[2068] | 30 | { |
---|
[2636] | 31 | this->world = world; |
---|
[3215] | 32 | this->bound = NULL; |
---|
[2551] | 33 | /* give it some physical live */ |
---|
[3215] | 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; |
---|
[2551] | 43 | |
---|
[3236] | 44 | this->actualPlace.r.x = 0.0; |
---|
| 45 | this->actualPlace.r.y = 10.0; |
---|
| 46 | this->actualPlace.r.z = -5.0; |
---|
[3302] | 47 | |
---|
| 48 | this->setDrawable (false); |
---|
[2068] | 49 | } |
---|
| 50 | |
---|
[2096] | 51 | /** |
---|
| 52 | \brief default destructor |
---|
| 53 | */ |
---|
[2068] | 54 | Camera::~Camera () |
---|
| 55 | { |
---|
| 56 | } |
---|
| 57 | |
---|
[2096] | 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 | */ |
---|
[3225] | 65 | void Camera::timeSlice (Uint32 deltaT) |
---|
[2068] | 66 | { |
---|
[3215] | 67 | if( this->t <= deltaTime) |
---|
| 68 | {this->t += deltaT;} |
---|
[2551] | 69 | //printf("time is: t=%f\n", t ); |
---|
[3236] | 70 | updateDesiredPlace(); |
---|
[3215] | 71 | jump(NULL); |
---|
[2068] | 72 | } |
---|
| 73 | |
---|
[2096] | 74 | /** |
---|
[2551] | 75 | \brief this calculates the location where the track wants the camera to be |
---|
[2096] | 76 | |
---|
[2551] | 77 | This refreshes the placement the camera should have according to the |
---|
| 78 | bound entity's position on the track. |
---|
[2096] | 79 | */ |
---|
[3228] | 80 | void Camera::updateDesiredPlace () |
---|
[2068] | 81 | { |
---|
[2551] | 82 | switch(cameraMode) |
---|
| 83 | { |
---|
| 84 | |
---|
| 85 | case ELLIPTICAL: |
---|
| 86 | { |
---|
[3302] | 87 | /* |
---|
[2551] | 88 | //r = actual_place.r |
---|
| 89 | Orxonox *orx = Orxonox::getInstance(); |
---|
| 90 | Location lookat; |
---|
| 91 | Placement plFocus; |
---|
| 92 | if( bound != NULL) |
---|
| 93 | { |
---|
[3236] | 94 | bound->getLookat (&lookat); |
---|
[3228] | 95 | orx->getWorld()->calcCameraPos (&lookat, &plFocus); |
---|
[2551] | 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); |
---|
[2068] | 103 | |
---|
[2551] | 104 | Vector op(1.0, 0.0, 0.0); |
---|
[3228] | 105 | float angle = angleDeg(op, *start); |
---|
[2551] | 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(); |
---|
[3302] | 124 | if(t < 30) |
---|
[2551] | 125 | { |
---|
| 126 | ka = rAbs / deltaTime*deltaTime; |
---|
| 127 | } |
---|
[3302] | 128 | |
---|
[2551] | 129 | res->quatSlerp(to, from, t/deltaTime, res); |
---|
| 130 | |
---|
| 131 | Vector ursp(0.0, 0.0, 0.0); |
---|
[3302] | 132 | desiredPlace.r = ursp - res->apply(r); |
---|
[2551] | 133 | |
---|
[3228] | 134 | printf("desired place is: %f, %f, %f\n", desiredPlace.r.x, desiredPlace.r.y, desiredPlace.r.z); |
---|
[2551] | 135 | //plLastBPlace = *bound->get_placement(); |
---|
[3302] | 136 | |
---|
[2551] | 137 | } |
---|
[3302] | 138 | */ |
---|
[2551] | 139 | } |
---|
| 140 | break; |
---|
| 141 | case SMOTH_FOLLOW: |
---|
| 142 | { |
---|
[3302] | 143 | /* |
---|
[3228] | 144 | Placement *plBound = bound->getPlacement(); |
---|
[2551] | 145 | Location lcBound; |
---|
| 146 | if(bound != null) |
---|
| 147 | { |
---|
[3228] | 148 | bound->getLookat(&lcBound); |
---|
[2551] | 149 | Vector vDirection(0.0, 0.0, 1.0); |
---|
| 150 | vDirection = plBound->w.apply(vDirection); |
---|
[3302] | 151 | desiredPlace.r = (vDirection * ((lcBound.dist-10.0))) + Vector(0,0,5.0); |
---|
[2551] | 152 | } |
---|
[3302] | 153 | */ |
---|
[2551] | 154 | break; |
---|
| 155 | } |
---|
| 156 | /* this is a camera mode that tries just to follow the entity. */ |
---|
| 157 | case STICKY: |
---|
| 158 | { |
---|
[3302] | 159 | /* |
---|
[2551] | 160 | if(bound != null) |
---|
| 161 | { |
---|
[3236] | 162 | Placement *plBound = bound->getPlacement(); |
---|
[2551] | 163 | Vector vDirection(0.0, 0.0, 1.0); |
---|
| 164 | Vector eclipticOffset(0.0, 0.0, 5.0); |
---|
| 165 | vDirection = plBound->w.apply(vDirection); |
---|
[3228] | 166 | desiredPlace.r = plBound->r - vDirection*10 + eclipticOffset; |
---|
[2551] | 167 | } |
---|
[3302] | 168 | */ |
---|
[2551] | 169 | break; |
---|
| 170 | } |
---|
| 171 | /* the camera is handled like an entity and rolls on the track */ |
---|
| 172 | case NORMAL: |
---|
| 173 | Location lookat; |
---|
[2636] | 174 | if( bound != NULL && world != NULL ) |
---|
[2551] | 175 | { |
---|
[3302] | 176 | //bound->getLookat (&lookat); |
---|
| 177 | //bound->getAbsCoor () |
---|
| 178 | //world->calcCameraPos (&lookat, &desiredPlace); |
---|
| 179 | //FIXME: camera should be made via relative coordinates |
---|
| 180 | Vector* cameraOffset = new Vector (-10, 0, 5); |
---|
| 181 | this->setRelCoor (cameraOffset); |
---|
[2551] | 182 | } |
---|
| 183 | else |
---|
| 184 | { |
---|
[3302] | 185 | /* |
---|
[3228] | 186 | desiredPlace.r = Vector (0,0,0); |
---|
| 187 | desiredPlace.w = Quaternion (); |
---|
[3302] | 188 | */ |
---|
[2551] | 189 | } |
---|
| 190 | break; |
---|
| 191 | } |
---|
[2068] | 192 | } |
---|
| 193 | |
---|
[2096] | 194 | /** |
---|
[2551] | 195 | \brief initialize rendering perspective according to this camera |
---|
[2096] | 196 | |
---|
[2551] | 197 | This is called immediately before the rendering cycle starts, it sets all global |
---|
| 198 | rendering options as well as the GL_PROJECTION matrix according to the camera. |
---|
[2096] | 199 | */ |
---|
[2068] | 200 | void Camera::apply () |
---|
| 201 | { |
---|
[2551] | 202 | glMatrixMode (GL_PROJECTION); |
---|
[2112] | 203 | glLoadIdentity (); |
---|
[2551] | 204 | // view |
---|
| 205 | // TO DO: implement options for frustum generation |
---|
[3175] | 206 | //glFrustum(-1.0, 1.0, -1.0, 1.0, 1.5, 250.0); |
---|
| 207 | gluPerspective(60, 1.2f, 0.1, 250); |
---|
| 208 | |
---|
[2551] | 209 | //Vector up(0,0,1); |
---|
| 210 | //Vector dir(1,0,0); |
---|
| 211 | //Quaternion q(dir,up); |
---|
| 212 | //float matrix[4][4]; |
---|
| 213 | //q.conjugate().matrix (matrix); |
---|
| 214 | //glMultMatrixf ((float*)matrix); |
---|
| 215 | //glTranslatef (10,0,-5); |
---|
| 216 | // |
---|
| 217 | //dir = Vector(-1,-1,0); |
---|
| 218 | //q = Quaternion( dir, up); |
---|
| 219 | //glMatrixMode (GL_MODELVIEW); |
---|
| 220 | //glLoadIdentity (); |
---|
| 221 | //q.matrix (matrix); |
---|
| 222 | //glMultMatrixf ((float*)matrix); |
---|
| 223 | //glTranslatef (2,2,0); |
---|
| 224 | // |
---|
| 225 | //glBegin(GL_TRIANGLES); |
---|
| 226 | //glColor3f(1,0,0); |
---|
| 227 | //glVertex3f(0,0,0.5); |
---|
| 228 | //glColor3f(0,1,0); |
---|
| 229 | //glVertex3f(-0.5,0,-1); |
---|
| 230 | //glColor3f(0,0,1); |
---|
| 231 | //glVertex3f(0.5,0,-1); |
---|
| 232 | //glEnd(); |
---|
[2115] | 233 | |
---|
[2551] | 234 | // ===== first camera control calculation option |
---|
| 235 | // rotation |
---|
[2112] | 236 | float matrix[4][4]; |
---|
[3302] | 237 | //this->absDirection.conjugate().matrix (matrix); |
---|
[2551] | 238 | /* orientation and */ |
---|
[3302] | 239 | //glMultMatrixf ((float*)matrix); |
---|
[3307] | 240 | |
---|
[2551] | 241 | /* translation */ |
---|
[3307] | 242 | //glTranslatef (this->absCoordinate.x, this->absCoordinate.y, this->absCoordinate.z ); |
---|
[2551] | 243 | |
---|
[3307] | 244 | |
---|
[2551] | 245 | // ===== second camera control calculation option |
---|
[3302] | 246 | |
---|
| 247 | gluLookAt(this->absCoordinate.x, this->absCoordinate.y, this->absCoordinate.z, |
---|
| 248 | this->parent->getAbsCoor ().x, this->parent->getAbsCoor ().y, this->parent->getAbsCoor ().z, |
---|
| 249 | 0.0, 0.0, 1.0); |
---|
| 250 | |
---|
[2551] | 251 | |
---|
[2068] | 252 | glMatrixMode (GL_MODELVIEW); |
---|
[2080] | 253 | glLoadIdentity (); |
---|
[2068] | 254 | } |
---|
| 255 | |
---|
[2096] | 256 | /** |
---|
| 257 | \brief set the camera position |
---|
| 258 | \param plc: The Placement to set the camera to |
---|
| 259 | |
---|
| 260 | This will set the actual and desired placement of the camera to plc |
---|
| 261 | */ |
---|
[2068] | 262 | void Camera::jump (Placement* plc = NULL) |
---|
| 263 | { |
---|
[2551] | 264 | if( plc == NULL) |
---|
| 265 | { |
---|
[3228] | 266 | actualPlace = desiredPlace; |
---|
[2551] | 267 | //printf("Camera|jump: camer@ %f, %f, %f\n\n", actual_place.r.x, actual_place.r.y, actual_place.r.z); |
---|
| 268 | } |
---|
| 269 | else |
---|
| 270 | { |
---|
[3228] | 271 | desiredPlace = *plc; |
---|
| 272 | actualPlace = *plc; |
---|
[2551] | 273 | } |
---|
[2068] | 274 | } |
---|
| 275 | |
---|
[2096] | 276 | /** |
---|
| 277 | \brief bind the camera to an entity |
---|
| 278 | \param entity: The enitity to bind the camera to |
---|
| 279 | |
---|
[3213] | 280 | This sets the focus of the camera to the given entity. This means that it will use the given WorldEntity's |
---|
| 281 | Location and get_lookat() to determine the viewpoint the camera will render from. |
---|
| 282 | Note that you cannot bind a camera to a free entity. |
---|
[2096] | 283 | */ |
---|
[2068] | 284 | void Camera::bind (WorldEntity* entity) |
---|
| 285 | { |
---|
[2551] | 286 | if( entity != NULL) |
---|
| 287 | { |
---|
[3213] | 288 | if( entity->isFree()) printf("Cannot bind camera to free entity"); |
---|
[2551] | 289 | else |
---|
[2080] | 290 | { |
---|
[3213] | 291 | this->bound = entity; |
---|
[2551] | 292 | } |
---|
| 293 | } |
---|
[2068] | 294 | } |
---|
[2636] | 295 | |
---|
| 296 | |
---|
| 297 | void Camera::setWorld(World* world) |
---|
| 298 | { |
---|
| 299 | this->world = world; |
---|
| 300 | } |
---|
[3213] | 301 | |
---|
| 302 | |
---|
| 303 | /** |
---|
| 304 | \brief destroy, reset the camera so that it doesn't perform anything anymore |
---|
| 305 | |
---|
| 306 | */ |
---|
| 307 | void Camera::destroy() |
---|
| 308 | { |
---|
| 309 | this->bound = NULL; |
---|
| 310 | this->world = NULL; |
---|
| 311 | } |
---|