[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 |
---|
[3537] | 173 | |
---|
[2551] | 174 | } |
---|
| 175 | else |
---|
| 176 | { |
---|
[3365] | 177 | /* |
---|
[3228] | 178 | desiredPlace.r = Vector (0,0,0); |
---|
| 179 | desiredPlace.w = Quaternion (); |
---|
[3365] | 180 | */ |
---|
[2551] | 181 | } |
---|
| 182 | break; |
---|
| 183 | } |
---|
[2068] | 184 | } |
---|
| 185 | |
---|
[2096] | 186 | /** |
---|
[2551] | 187 | \brief initialize rendering perspective according to this camera |
---|
[2096] | 188 | |
---|
[2551] | 189 | This is called immediately before the rendering cycle starts, it sets all global |
---|
| 190 | rendering options as well as the GL_PROJECTION matrix according to the camera. |
---|
[2096] | 191 | */ |
---|
[2068] | 192 | void Camera::apply () |
---|
| 193 | { |
---|
[2551] | 194 | glMatrixMode (GL_PROJECTION); |
---|
[2112] | 195 | glLoadIdentity (); |
---|
[2551] | 196 | // view |
---|
| 197 | // TO DO: implement options for frustum generation |
---|
[3175] | 198 | //glFrustum(-1.0, 1.0, -1.0, 1.0, 1.5, 250.0); |
---|
| 199 | gluPerspective(60, 1.2f, 0.1, 250); |
---|
| 200 | |
---|
[2551] | 201 | //Vector up(0,0,1); |
---|
| 202 | //Vector dir(1,0,0); |
---|
| 203 | //Quaternion q(dir,up); |
---|
| 204 | //float matrix[4][4]; |
---|
| 205 | //q.conjugate().matrix (matrix); |
---|
| 206 | //glMultMatrixf ((float*)matrix); |
---|
| 207 | //glTranslatef (10,0,-5); |
---|
| 208 | // |
---|
| 209 | //dir = Vector(-1,-1,0); |
---|
| 210 | //q = Quaternion( dir, up); |
---|
| 211 | //glMatrixMode (GL_MODELVIEW); |
---|
| 212 | //glLoadIdentity (); |
---|
| 213 | //q.matrix (matrix); |
---|
| 214 | //glMultMatrixf ((float*)matrix); |
---|
| 215 | //glTranslatef (2,2,0); |
---|
| 216 | // |
---|
| 217 | //glBegin(GL_TRIANGLES); |
---|
| 218 | //glColor3f(1,0,0); |
---|
| 219 | //glVertex3f(0,0,0.5); |
---|
| 220 | //glColor3f(0,1,0); |
---|
| 221 | //glVertex3f(-0.5,0,-1); |
---|
| 222 | //glColor3f(0,0,1); |
---|
| 223 | //glVertex3f(0.5,0,-1); |
---|
| 224 | //glEnd(); |
---|
[2115] | 225 | |
---|
[2551] | 226 | // ===== first camera control calculation option |
---|
| 227 | // rotation |
---|
[2112] | 228 | float matrix[4][4]; |
---|
[3365] | 229 | //this->absDirection.conjugate().matrix (matrix); |
---|
[2551] | 230 | /* orientation and */ |
---|
[3365] | 231 | //glMultMatrixf ((float*)matrix); |
---|
| 232 | |
---|
[2551] | 233 | /* translation */ |
---|
[3365] | 234 | //glTranslatef (this->absCoordinate.x, this->absCoordinate.y, this->absCoordinate.z ); |
---|
[2551] | 235 | |
---|
[3365] | 236 | |
---|
[2551] | 237 | // ===== second camera control calculation option |
---|
[3365] | 238 | |
---|
| 239 | gluLookAt(this->absCoordinate.x, this->absCoordinate.y, this->absCoordinate.z, |
---|
| 240 | this->parent->getAbsCoor ().x, this->parent->getAbsCoor ().y, this->parent->getAbsCoor ().z, |
---|
| 241 | 0.0, 1.0, 0.0); |
---|
| 242 | |
---|
[2551] | 243 | |
---|
[2068] | 244 | glMatrixMode (GL_MODELVIEW); |
---|
[2080] | 245 | glLoadIdentity (); |
---|
[2068] | 246 | } |
---|
| 247 | |
---|
| 248 | |
---|
[3365] | 249 | |
---|
[2096] | 250 | /** |
---|
| 251 | \brief bind the camera to an entity |
---|
| 252 | \param entity: The enitity to bind the camera to |
---|
| 253 | |
---|
[3213] | 254 | This sets the focus of the camera to the given entity. This means that it will use the given WorldEntity's |
---|
| 255 | Location and get_lookat() to determine the viewpoint the camera will render from. |
---|
| 256 | Note that you cannot bind a camera to a free entity. |
---|
[2096] | 257 | */ |
---|
[2068] | 258 | void Camera::bind (WorldEntity* entity) |
---|
| 259 | { |
---|
[2551] | 260 | if( entity != NULL) |
---|
| 261 | { |
---|
[3213] | 262 | if( entity->isFree()) printf("Cannot bind camera to free entity"); |
---|
[2551] | 263 | else |
---|
[2080] | 264 | { |
---|
[3213] | 265 | this->bound = entity; |
---|
[2551] | 266 | } |
---|
| 267 | } |
---|
[2068] | 268 | } |
---|
[2636] | 269 | |
---|
| 270 | |
---|
| 271 | void Camera::setWorld(World* world) |
---|
| 272 | { |
---|
| 273 | this->world = world; |
---|
| 274 | } |
---|
[3213] | 275 | |
---|
| 276 | |
---|
| 277 | /** |
---|
| 278 | \brief destroy, reset the camera so that it doesn't perform anything anymore |
---|
| 279 | |
---|
| 280 | */ |
---|
| 281 | void Camera::destroy() |
---|
| 282 | { |
---|
| 283 | this->bound = NULL; |
---|
| 284 | this->world = NULL; |
---|
| 285 | } |
---|