[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 | |
---|
[3365] | 44 | |
---|
| 45 | this->setDrawable (false); |
---|
[2068] | 46 | } |
---|
| 47 | |
---|
[2096] | 48 | /** |
---|
| 49 | \brief default destructor |
---|
| 50 | */ |
---|
[2068] | 51 | Camera::~Camera () |
---|
| 52 | { |
---|
[3543] | 53 | this->destroy(); |
---|
[2068] | 54 | } |
---|
| 55 | |
---|
[2096] | 56 | /** |
---|
[3543] | 57 | \brief deletes all allocated memory |
---|
| 58 | */ |
---|
| 59 | void Camera::destroy(void) |
---|
| 60 | { |
---|
| 61 | this->bound = NULL; |
---|
| 62 | this->world = NULL; |
---|
| 63 | |
---|
| 64 | static_cast<WorldEntity*>(this)->destroy(); |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | /** |
---|
[2096] | 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 | */ |
---|
[3225] | 74 | void Camera::timeSlice (Uint32 deltaT) |
---|
[2068] | 75 | { |
---|
[3215] | 76 | if( this->t <= deltaTime) |
---|
| 77 | {this->t += deltaT;} |
---|
[2551] | 78 | //printf("time is: t=%f\n", t ); |
---|
[3236] | 79 | updateDesiredPlace(); |
---|
[3365] | 80 | //jump(NULL); |
---|
[2068] | 81 | } |
---|
| 82 | |
---|
[2096] | 83 | /** |
---|
[2551] | 84 | \brief this calculates the location where the track wants the camera to be |
---|
[2096] | 85 | |
---|
[2551] | 86 | This refreshes the placement the camera should have according to the |
---|
| 87 | bound entity's position on the track. |
---|
[2096] | 88 | */ |
---|
[3228] | 89 | void Camera::updateDesiredPlace () |
---|
[2068] | 90 | { |
---|
[2551] | 91 | switch(cameraMode) |
---|
| 92 | { |
---|
| 93 | |
---|
| 94 | case ELLIPTICAL: |
---|
| 95 | { |
---|
[3365] | 96 | /* |
---|
[2551] | 97 | //r = actual_place.r |
---|
| 98 | Orxonox *orx = Orxonox::getInstance(); |
---|
| 99 | Location lookat; |
---|
| 100 | Placement plFocus; |
---|
| 101 | if( bound != NULL) |
---|
| 102 | { |
---|
[3236] | 103 | bound->getLookat (&lookat); |
---|
[3228] | 104 | orx->getWorld()->calcCameraPos (&lookat, &plFocus); |
---|
[2551] | 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); |
---|
[2068] | 112 | |
---|
[2551] | 113 | Vector op(1.0, 0.0, 0.0); |
---|
[3228] | 114 | float angle = angleDeg(op, *start); |
---|
[2551] | 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(); |
---|
[3365] | 133 | if(t < 30) |
---|
[2551] | 134 | { |
---|
| 135 | ka = rAbs / deltaTime*deltaTime; |
---|
| 136 | } |
---|
[3365] | 137 | |
---|
[2551] | 138 | res->quatSlerp(to, from, t/deltaTime, res); |
---|
| 139 | |
---|
| 140 | Vector ursp(0.0, 0.0, 0.0); |
---|
[3365] | 141 | desiredPlace.r = ursp - res->apply(r); |
---|
[2551] | 142 | |
---|
[3228] | 143 | printf("desired place is: %f, %f, %f\n", desiredPlace.r.x, desiredPlace.r.y, desiredPlace.r.z); |
---|
[2551] | 144 | //plLastBPlace = *bound->get_placement(); |
---|
[3365] | 145 | |
---|
[2551] | 146 | } |
---|
[3365] | 147 | */ |
---|
[2551] | 148 | } |
---|
| 149 | break; |
---|
| 150 | case SMOTH_FOLLOW: |
---|
| 151 | { |
---|
[3365] | 152 | /* |
---|
[3228] | 153 | Placement *plBound = bound->getPlacement(); |
---|
[2551] | 154 | Location lcBound; |
---|
| 155 | if(bound != null) |
---|
| 156 | { |
---|
[3228] | 157 | bound->getLookat(&lcBound); |
---|
[2551] | 158 | Vector vDirection(0.0, 0.0, 1.0); |
---|
| 159 | vDirection = plBound->w.apply(vDirection); |
---|
[3365] | 160 | desiredPlace.r = (vDirection * ((lcBound.dist-10.0))) + Vector(0,0,5.0); |
---|
[2551] | 161 | } |
---|
[3365] | 162 | */ |
---|
[2551] | 163 | break; |
---|
| 164 | } |
---|
| 165 | /* this is a camera mode that tries just to follow the entity. */ |
---|
| 166 | case STICKY: |
---|
| 167 | { |
---|
[3365] | 168 | /* |
---|
[2551] | 169 | if(bound != null) |
---|
| 170 | { |
---|
[3236] | 171 | Placement *plBound = bound->getPlacement(); |
---|
[2551] | 172 | Vector vDirection(0.0, 0.0, 1.0); |
---|
| 173 | Vector eclipticOffset(0.0, 0.0, 5.0); |
---|
| 174 | vDirection = plBound->w.apply(vDirection); |
---|
[3228] | 175 | desiredPlace.r = plBound->r - vDirection*10 + eclipticOffset; |
---|
[2551] | 176 | } |
---|
[3365] | 177 | */ |
---|
[2551] | 178 | break; |
---|
| 179 | } |
---|
| 180 | /* the camera is handled like an entity and rolls on the track */ |
---|
| 181 | case NORMAL: |
---|
[2636] | 182 | if( bound != NULL && world != NULL ) |
---|
[2551] | 183 | { |
---|
[3365] | 184 | //FIXME: camera should be made via relative coordinates |
---|
[3537] | 185 | |
---|
[2551] | 186 | } |
---|
| 187 | else |
---|
| 188 | { |
---|
[3365] | 189 | /* |
---|
[3228] | 190 | desiredPlace.r = Vector (0,0,0); |
---|
| 191 | desiredPlace.w = Quaternion (); |
---|
[3365] | 192 | */ |
---|
[2551] | 193 | } |
---|
| 194 | break; |
---|
| 195 | } |
---|
[2068] | 196 | } |
---|
| 197 | |
---|
[2096] | 198 | /** |
---|
[2551] | 199 | \brief initialize rendering perspective according to this camera |
---|
[2096] | 200 | |
---|
[2551] | 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. |
---|
[2096] | 203 | */ |
---|
[2068] | 204 | void Camera::apply () |
---|
| 205 | { |
---|
[2551] | 206 | glMatrixMode (GL_PROJECTION); |
---|
[2112] | 207 | glLoadIdentity (); |
---|
[2551] | 208 | // view |
---|
| 209 | // TO DO: implement options for frustum generation |
---|
[3175] | 210 | //glFrustum(-1.0, 1.0, -1.0, 1.0, 1.5, 250.0); |
---|
| 211 | gluPerspective(60, 1.2f, 0.1, 250); |
---|
| 212 | |
---|
[2551] | 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(); |
---|
[2115] | 237 | |
---|
[2551] | 238 | // ===== first camera control calculation option |
---|
| 239 | // rotation |
---|
[2112] | 240 | float matrix[4][4]; |
---|
[3365] | 241 | //this->absDirection.conjugate().matrix (matrix); |
---|
[2551] | 242 | /* orientation and */ |
---|
[3365] | 243 | //glMultMatrixf ((float*)matrix); |
---|
| 244 | |
---|
[2551] | 245 | /* translation */ |
---|
[3365] | 246 | //glTranslatef (this->absCoordinate.x, this->absCoordinate.y, this->absCoordinate.z ); |
---|
[2551] | 247 | |
---|
[3365] | 248 | |
---|
[2551] | 249 | // ===== second camera control calculation option |
---|
[3365] | 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 | |
---|
[2551] | 255 | |
---|
[2068] | 256 | glMatrixMode (GL_MODELVIEW); |
---|
[2080] | 257 | glLoadIdentity (); |
---|
[2068] | 258 | } |
---|
| 259 | |
---|
| 260 | |
---|
[3365] | 261 | |
---|
[2096] | 262 | /** |
---|
| 263 | \brief bind the camera to an entity |
---|
| 264 | \param entity: The enitity to bind the camera to |
---|
| 265 | |
---|
[3213] | 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. |
---|
[2096] | 269 | */ |
---|
[2068] | 270 | void Camera::bind (WorldEntity* entity) |
---|
| 271 | { |
---|
[2551] | 272 | if( entity != NULL) |
---|
| 273 | { |
---|
[3213] | 274 | if( entity->isFree()) printf("Cannot bind camera to free entity"); |
---|
[2551] | 275 | else |
---|
[2080] | 276 | { |
---|
[3213] | 277 | this->bound = entity; |
---|
[2551] | 278 | } |
---|
| 279 | } |
---|
[2068] | 280 | } |
---|
[2636] | 281 | |
---|
| 282 | |
---|
| 283 | void Camera::setWorld(World* world) |
---|
| 284 | { |
---|
| 285 | this->world = world; |
---|
| 286 | } |
---|
[3213] | 287 | |
---|
| 288 | |
---|