[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 | { |
---|
| 53 | } |
---|
| 54 | |
---|
[2096] | 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 | */ |
---|
[3225] | 62 | void Camera::timeSlice (Uint32 deltaT) |
---|
[2068] | 63 | { |
---|
[3215] | 64 | if( this->t <= deltaTime) |
---|
| 65 | {this->t += deltaT;} |
---|
[2551] | 66 | //printf("time is: t=%f\n", t ); |
---|
[3236] | 67 | updateDesiredPlace(); |
---|
[3365] | 68 | //jump(NULL); |
---|
[2068] | 69 | } |
---|
| 70 | |
---|
[2096] | 71 | /** |
---|
[2551] | 72 | \brief this calculates the location where the track wants the camera to be |
---|
[2096] | 73 | |
---|
[2551] | 74 | This refreshes the placement the camera should have according to the |
---|
| 75 | bound entity's position on the track. |
---|
[2096] | 76 | */ |
---|
[3228] | 77 | void Camera::updateDesiredPlace () |
---|
[2068] | 78 | { |
---|
[2551] | 79 | switch(cameraMode) |
---|
| 80 | { |
---|
| 81 | |
---|
| 82 | case ELLIPTICAL: |
---|
| 83 | { |
---|
[3365] | 84 | /* |
---|
[2551] | 85 | //r = actual_place.r |
---|
| 86 | Orxonox *orx = Orxonox::getInstance(); |
---|
| 87 | Location lookat; |
---|
| 88 | Placement plFocus; |
---|
| 89 | if( bound != NULL) |
---|
| 90 | { |
---|
[3236] | 91 | bound->getLookat (&lookat); |
---|
[3228] | 92 | orx->getWorld()->calcCameraPos (&lookat, &plFocus); |
---|
[2551] | 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); |
---|
[2068] | 100 | |
---|
[2551] | 101 | Vector op(1.0, 0.0, 0.0); |
---|
[3228] | 102 | float angle = angleDeg(op, *start); |
---|
[2551] | 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(); |
---|
[3365] | 121 | if(t < 30) |
---|
[2551] | 122 | { |
---|
| 123 | ka = rAbs / deltaTime*deltaTime; |
---|
| 124 | } |
---|
[3365] | 125 | |
---|
[2551] | 126 | res->quatSlerp(to, from, t/deltaTime, res); |
---|
| 127 | |
---|
| 128 | Vector ursp(0.0, 0.0, 0.0); |
---|
[3365] | 129 | desiredPlace.r = ursp - res->apply(r); |
---|
[2551] | 130 | |
---|
[3228] | 131 | printf("desired place is: %f, %f, %f\n", desiredPlace.r.x, desiredPlace.r.y, desiredPlace.r.z); |
---|
[2551] | 132 | //plLastBPlace = *bound->get_placement(); |
---|
[3365] | 133 | |
---|
[2551] | 134 | } |
---|
[3365] | 135 | */ |
---|
[2551] | 136 | } |
---|
| 137 | break; |
---|
| 138 | case SMOTH_FOLLOW: |
---|
| 139 | { |
---|
[3365] | 140 | /* |
---|
[3228] | 141 | Placement *plBound = bound->getPlacement(); |
---|
[2551] | 142 | Location lcBound; |
---|
| 143 | if(bound != null) |
---|
| 144 | { |
---|
[3228] | 145 | bound->getLookat(&lcBound); |
---|
[2551] | 146 | Vector vDirection(0.0, 0.0, 1.0); |
---|
| 147 | vDirection = plBound->w.apply(vDirection); |
---|
[3365] | 148 | desiredPlace.r = (vDirection * ((lcBound.dist-10.0))) + Vector(0,0,5.0); |
---|
[2551] | 149 | } |
---|
[3365] | 150 | */ |
---|
[2551] | 151 | break; |
---|
| 152 | } |
---|
| 153 | /* this is a camera mode that tries just to follow the entity. */ |
---|
| 154 | case STICKY: |
---|
| 155 | { |
---|
[3365] | 156 | /* |
---|
[2551] | 157 | if(bound != null) |
---|
| 158 | { |
---|
[3236] | 159 | Placement *plBound = bound->getPlacement(); |
---|
[2551] | 160 | Vector vDirection(0.0, 0.0, 1.0); |
---|
| 161 | Vector eclipticOffset(0.0, 0.0, 5.0); |
---|
| 162 | vDirection = plBound->w.apply(vDirection); |
---|
[3228] | 163 | desiredPlace.r = plBound->r - vDirection*10 + eclipticOffset; |
---|
[2551] | 164 | } |
---|
[3365] | 165 | */ |
---|
[2551] | 166 | break; |
---|
| 167 | } |
---|
| 168 | /* the camera is handled like an entity and rolls on the track */ |
---|
| 169 | case NORMAL: |
---|
[2636] | 170 | if( bound != NULL && world != NULL ) |
---|
[2551] | 171 | { |
---|
[3365] | 172 | //FIXME: camera should be made via relative coordinates |
---|
| 173 | Vector* cameraOffset = new Vector (-10, 5, 0); |
---|
| 174 | this->setRelCoor (cameraOffset); |
---|
[2551] | 175 | } |
---|
| 176 | else |
---|
| 177 | { |
---|
[3365] | 178 | /* |
---|
[3228] | 179 | desiredPlace.r = Vector (0,0,0); |
---|
| 180 | desiredPlace.w = Quaternion (); |
---|
[3365] | 181 | */ |
---|
[2551] | 182 | } |
---|
| 183 | break; |
---|
| 184 | } |
---|
[2068] | 185 | } |
---|
| 186 | |
---|
[2096] | 187 | /** |
---|
[2551] | 188 | \brief initialize rendering perspective according to this camera |
---|
[2096] | 189 | |
---|
[2551] | 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. |
---|
[2096] | 192 | */ |
---|
[2068] | 193 | void Camera::apply () |
---|
| 194 | { |
---|
[2551] | 195 | glMatrixMode (GL_PROJECTION); |
---|
[2112] | 196 | glLoadIdentity (); |
---|
[2551] | 197 | // view |
---|
| 198 | // TO DO: implement options for frustum generation |
---|
[3175] | 199 | //glFrustum(-1.0, 1.0, -1.0, 1.0, 1.5, 250.0); |
---|
| 200 | gluPerspective(60, 1.2f, 0.1, 250); |
---|
| 201 | |
---|
[2551] | 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(); |
---|
[2115] | 226 | |
---|
[2551] | 227 | // ===== first camera control calculation option |
---|
| 228 | // rotation |
---|
[2112] | 229 | float matrix[4][4]; |
---|
[3365] | 230 | //this->absDirection.conjugate().matrix (matrix); |
---|
[2551] | 231 | /* orientation and */ |
---|
[3365] | 232 | //glMultMatrixf ((float*)matrix); |
---|
| 233 | |
---|
[2551] | 234 | /* translation */ |
---|
[3365] | 235 | //glTranslatef (this->absCoordinate.x, this->absCoordinate.y, this->absCoordinate.z ); |
---|
[2551] | 236 | |
---|
[3365] | 237 | |
---|
[2551] | 238 | // ===== second camera control calculation option |
---|
[3365] | 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 | |
---|
[2551] | 244 | |
---|
[2068] | 245 | glMatrixMode (GL_MODELVIEW); |
---|
[2080] | 246 | glLoadIdentity (); |
---|
[2068] | 247 | } |
---|
| 248 | |
---|
| 249 | |
---|
[3365] | 250 | |
---|
[2096] | 251 | /** |
---|
| 252 | \brief bind the camera to an entity |
---|
| 253 | \param entity: The enitity to bind the camera to |
---|
| 254 | |
---|
[3213] | 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. |
---|
[2096] | 258 | */ |
---|
[2068] | 259 | void Camera::bind (WorldEntity* entity) |
---|
| 260 | { |
---|
[2551] | 261 | if( entity != NULL) |
---|
| 262 | { |
---|
[3213] | 263 | if( entity->isFree()) printf("Cannot bind camera to free entity"); |
---|
[2551] | 264 | else |
---|
[2080] | 265 | { |
---|
[3213] | 266 | this->bound = entity; |
---|
[2551] | 267 | } |
---|
| 268 | } |
---|
[2068] | 269 | } |
---|
[2636] | 270 | |
---|
| 271 | |
---|
| 272 | void Camera::setWorld(World* world) |
---|
| 273 | { |
---|
| 274 | this->world = world; |
---|
| 275 | } |
---|
[3213] | 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 | } |
---|