| [4832] | 1 | /* | 
|---|
| [2068] | 2 |    orxonox - the future of 3D-vertical-scrollers | 
|---|
 | 3 |  | 
|---|
 | 4 |    Copyright (C) 2004 orx | 
|---|
 | 5 |  | 
|---|
 | 6 |    This program is free software; you can redistribute it and/or modify | 
|---|
 | 7 |    it under the terms of the GNU General Public License as published by | 
|---|
 | 8 |    the Free Software Foundation; either version 2, or (at your option) | 
|---|
 | 9 |    any later version. | 
|---|
 | 10 |  | 
|---|
 | 11 |    ### File Specific: | 
|---|
| [2080] | 12 |    main-programmer: Christian Meyer | 
|---|
| [6424] | 13 |    co-programmer: Benjamin Grauer | 
|---|
| [2068] | 14 | */ | 
|---|
| [5357] | 15 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD_ENTITY | 
|---|
| [2068] | 16 |  | 
|---|
 | 17 | #include "camera.h" | 
|---|
| [7868] | 18 | #include "key_mapper.h" | 
|---|
| [9406] | 19 | #include "glincl.h" | 
|---|
| [10368] | 20 | //#include "util/loading/load_param.h" | 
|---|
 | 21 | #include "world_entity.h" | 
|---|
| [3608] | 22 |  | 
|---|
| [9869] | 23 | ObjectListDefinition(Camera); | 
|---|
 | 24 |  | 
|---|
| [2096] | 25 | /** | 
|---|
| [4836] | 26 |  *  creates a Camera | 
|---|
| [2096] | 27 | */ | 
|---|
| [4746] | 28 | Camera::Camera() | 
|---|
| [2068] | 29 | { | 
|---|
| [9869] | 30 |   this->registerObject(this, Camera::_objectList); | 
|---|
| [10368] | 31 |   this->init(); | 
|---|
 | 32 | } | 
|---|
 | 33 |  | 
|---|
 | 34 | /* | 
|---|
 | 35 | Camera::Camera(const TiXmlElement* root) | 
|---|
 | 36 | { | 
|---|
 | 37 |   this->registerObject(this, Camera::_objectList); | 
|---|
 | 38 |   this->init(); | 
|---|
 | 39 |   this->loadParams(root); | 
|---|
 | 40 | } | 
|---|
 | 41 | */ | 
|---|
 | 42 |  | 
|---|
 | 43 | /** | 
|---|
 | 44 |  *  default destructor | 
|---|
 | 45 | */ | 
|---|
 | 46 | Camera::~Camera() | 
|---|
 | 47 | {} | 
|---|
 | 48 |  | 
|---|
 | 49 | void Camera::init() | 
|---|
 | 50 | { | 
|---|
| [4987] | 51 |   this->setName("camera"); | 
|---|
| [3635] | 52 |   this->target = new CameraTarget(); | 
|---|
| [3636] | 53 |  | 
|---|
| [7868] | 54 |   this->subscribeEvent(ES_GAME, KeyMapper::PEV_VIEW0); | 
|---|
 | 55 |   this->subscribeEvent(ES_GAME, KeyMapper::PEV_VIEW1); | 
|---|
 | 56 |   this->subscribeEvent(ES_GAME, KeyMapper::PEV_VIEW2); | 
|---|
 | 57 |   this->subscribeEvent(ES_GAME, KeyMapper::PEV_VIEW3); | 
|---|
 | 58 |   this->subscribeEvent(ES_GAME, KeyMapper::PEV_VIEW4); | 
|---|
 | 59 |   this->subscribeEvent(ES_GAME, KeyMapper::PEV_VIEW5); | 
|---|
| [4414] | 60 |  | 
|---|
| [10368] | 61 |   //this->setFovy(90); | 
|---|
 | 62 |   this->setAspectRatio(1.33f); | 
|---|
| [9235] | 63 |   this->setClipRegion(.1, 10000); | 
|---|
| [10368] | 64 |    | 
|---|
 | 65 |   this->viewTopFovy = 60; | 
|---|
 | 66 |   this->viewNormalFovy = 90; | 
|---|
 | 67 |   this->viewFrontFovy = 120; | 
|---|
 | 68 |   this->viewRightFovy = 90; | 
|---|
 | 69 |   this->viewLeftFovy = 90; | 
|---|
| [3641] | 70 |  | 
|---|
| [10368] | 71 |   this->viewTopDistance = 70; | 
|---|
 | 72 |   this->viewNormalDistance = 10; | 
|---|
 | 73 |   this->viewFrontDistance = 4; | 
|---|
 | 74 |   this->viewRightDistance = 10; | 
|---|
 | 75 |   this->viewLeftDistance = 10; | 
|---|
 | 76 |  | 
|---|
 | 77 |   //this->loadParams(doc.RootElement()); | 
|---|
 | 78 |  | 
|---|
| [7347] | 79 |   this->setViewMode(Camera::ViewNormal); | 
|---|
| [4987] | 80 |  | 
|---|
| [5004] | 81 |   this->setParentMode(PNODE_ALL); | 
|---|
| [10368] | 82 |   this->eventHandling = true; | 
|---|
| [2068] | 83 | } | 
|---|
 | 84 |  | 
|---|
| [2096] | 85 | /** | 
|---|
| [4836] | 86 |  *  focuses the Camera onto a Target | 
|---|
 | 87 |  * @param target the new PNode the Camera should look at. | 
|---|
| [2096] | 88 | */ | 
|---|
| [3635] | 89 | void Camera::lookAt(PNode* target) | 
|---|
| [2068] | 90 | { | 
|---|
| [3635] | 91 |   this->target->setParent(target); | 
|---|
| [2068] | 92 | } | 
|---|
 | 93 |  | 
|---|
| [3638] | 94 | /** | 
|---|
| [4836] | 95 |  * @returns The PNode of the Target (from there you can get position and so on | 
|---|
| [3638] | 96 | */ | 
|---|
| [7014] | 97 | PNode* Camera::getTargetNode() const | 
|---|
| [2068] | 98 | { | 
|---|
| [3635] | 99 |   return (PNode*)this->target; | 
|---|
| [2068] | 100 | } | 
|---|
 | 101 |  | 
|---|
| [2096] | 102 | /** | 
|---|
| [4836] | 103 |  *  sets a new AspectRatio | 
|---|
 | 104 |  * @param aspectRatio the new aspect ratio to set (width / height) | 
|---|
| [3636] | 105 | */ | 
|---|
 | 106 | void Camera::setAspectRatio(float aspectRatio) | 
|---|
 | 107 | { | 
|---|
 | 108 |   this->aspectRatio = aspectRatio; | 
|---|
 | 109 | } | 
|---|
 | 110 |  | 
|---|
 | 111 | /** | 
|---|
| [4992] | 112 |  * Sets a new clipping region | 
|---|
 | 113 |  * @param nearClip The near clip plane | 
|---|
 | 114 |  * @param farClip The far clip plane | 
|---|
| [3636] | 115 | */ | 
|---|
 | 116 | void Camera::setClipRegion(float nearClip, float farClip) | 
|---|
 | 117 | { | 
|---|
 | 118 |   this->nearClip = nearClip; | 
|---|
 | 119 |   this->farClip = farClip; | 
|---|
 | 120 | } | 
|---|
 | 121 |  | 
|---|
| [4490] | 122 | /** | 
|---|
| [4836] | 123 |  *  sets the new VideoMode and initializes iteration to it. | 
|---|
 | 124 |  * @param mode the mode to change to. | 
|---|
| [4490] | 125 | */ | 
|---|
| [3639] | 126 | void Camera::setViewMode(ViewMode mode) | 
|---|
 | 127 | { | 
|---|
| [6034] | 128 |   currentMode = mode; | 
|---|
| [3639] | 129 |   switch (mode) | 
|---|
| [6424] | 130 |   { | 
|---|
| [3639] | 131 |     default: | 
|---|
| [7347] | 132 |     case Camera::ViewNormal: | 
|---|
| [10368] | 133 |     { | 
|---|
 | 134 |       this->fovy = viewNormalFovy; | 
|---|
 | 135 |       this->toFovy = viewNormalFovy; | 
|---|
 | 136 |       //this->fovy = 60; | 
|---|
 | 137 |       //this->toFovy = 60; | 
|---|
 | 138 |       this->setRelCoorSoft(-2.0/3.0 * this->viewNormalDistance, 1.0/3.0 * this->viewNormalDistance, 0); | 
|---|
| [4992] | 139 |       this->target->setRelCoorSoft(0,0,0); | 
|---|
| [3639] | 140 |       break; | 
|---|
| [10368] | 141 |     } | 
|---|
| [7347] | 142 |     case Camera::ViewBehind: | 
|---|
| [3639] | 143 |       break; | 
|---|
| [7347] | 144 |     case Camera::ViewFront: | 
|---|
| [10368] | 145 |     { | 
|---|
 | 146 |       this->fovy = viewFrontFovy; | 
|---|
 | 147 |       this->toFovy = viewFrontFovy; | 
|---|
 | 148 |       this->setRelCoorSoft(this->viewFrontDistance, 0, 0, 5); | 
|---|
| [6424] | 149 |       this->target->setRelCoorSoft(Vector(10,0,0), 5); | 
|---|
| [3639] | 150 |       break; | 
|---|
| [10368] | 151 |     } | 
|---|
| [7347] | 152 |     case Camera::ViewLeft: | 
|---|
| [10368] | 153 |     { | 
|---|
 | 154 |       this->fovy = viewLeftFovy; | 
|---|
 | 155 |       this->toFovy = viewLeftFovy; | 
|---|
 | 156 |       this->setRelCoorSoft(0, 1, -viewLeftDistance, .5); | 
|---|
| [4992] | 157 |       this->target->setRelCoorSoft(0,0,0); | 
|---|
| [3639] | 158 |       break; | 
|---|
| [10368] | 159 |     } | 
|---|
| [7347] | 160 |     case Camera::ViewRight: | 
|---|
| [10368] | 161 |     { | 
|---|
 | 162 |       this->fovy = viewRightFovy; | 
|---|
 | 163 |       this->toFovy = viewRightFovy; | 
|---|
 | 164 |       this->setRelCoorSoft(Vector(0, 1, viewRightDistance), 0.5); | 
|---|
| [4992] | 165 |       this->target->setRelCoorSoft(0,0,0); | 
|---|
| [3639] | 166 |       break; | 
|---|
| [10368] | 167 |     } | 
|---|
| [7347] | 168 |     case Camera::ViewTop: | 
|---|
| [10368] | 169 |     { | 
|---|
 | 170 |       this->fovy= viewTopFovy; | 
|---|
 | 171 |       this->toFovy = viewTopFovy; | 
|---|
 | 172 |       this->setRelCoor(Vector(-0.05, this->viewTopDistance , 0)); | 
|---|
 | 173 |       this->target->setRelCoor(0,0,0); | 
|---|
 | 174 |     } | 
|---|
| [6424] | 175 |   } | 
|---|
| [3639] | 176 | } | 
|---|
 | 177 |  | 
|---|
 | 178 |  | 
|---|
| [3636] | 179 | /** | 
|---|
| [4836] | 180 |  *  Updates the position of the camera. | 
|---|
 | 181 |  * @param dt: The time that elapsed. | 
|---|
| [3639] | 182 | */ | 
|---|
 | 183 | void Camera::tick(float dt) | 
|---|
 | 184 | { | 
|---|
| [7009] | 185 |   //update frustum plane | 
|---|
| [7014] | 186 |   this->viewVector = (this->target->getAbsCoor() - this->getAbsCoor()).getNormalized(); | 
|---|
 | 187 |   this->frustumPlane = Plane(this->viewVector, this->getAbsCoor() + this->viewVector * 0.1); | 
|---|
| [7009] | 188 |  | 
|---|
 | 189 |   this->upVector =  this->getAbsDirV(); | 
|---|
 | 190 |  | 
|---|
 | 191 |  | 
|---|
| [7173] | 192 |   float tmpFovy = (this->toFovy - this->fovy); | 
|---|
| [10368] | 193 |   if (fabsf(tmpFovy) > 0.01) | 
|---|
| [5354] | 194 |     this->fovy += tmpFovy * fabsf(dt); | 
|---|
| [3639] | 195 | } | 
|---|
 | 196 |  | 
|---|
 | 197 |  | 
|---|
 | 198 | /** | 
|---|
| [4836] | 199 |  *  initialize rendering perspective according to this camera | 
|---|
| [6772] | 200 |  * | 
|---|
 | 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. | 
|---|
 | 203 |  */ | 
|---|
| [2068] | 204 | void Camera::apply () | 
|---|
 | 205 | { | 
|---|
| [3636] | 206 |   // switching to Projection Matrix | 
|---|
| [2551] | 207 |   glMatrixMode (GL_PROJECTION); | 
|---|
| [2112] | 208 |   glLoadIdentity (); | 
|---|
| [3635] | 209 |  | 
|---|
| [3636] | 210 |   gluPerspective(this->fovy, | 
|---|
| [4832] | 211 |                  this->aspectRatio, | 
|---|
 | 212 |                  this->nearClip, | 
|---|
 | 213 |                  this->farClip); | 
|---|
| [6778] | 214 |  | 
|---|
 | 215 |  | 
|---|
 | 216 |     // setting up the perspective | 
|---|
| [3636] | 217 |   // speed-up feature | 
|---|
| [7108] | 218 |   glMatrixMode (GL_MODELVIEW); | 
|---|
 | 219 |   glLoadIdentity(); | 
|---|
 | 220 |  | 
|---|
 | 221 |  | 
|---|
 | 222 | } | 
|---|
 | 223 |  | 
|---|
 | 224 | void Camera::project() | 
|---|
 | 225 | { | 
|---|
| [3635] | 226 |   Vector cameraPosition = this->getAbsCoor(); | 
|---|
 | 227 |   Vector targetPosition = this->target->getAbsCoor(); | 
|---|
| [2551] | 228 |  | 
|---|
| [7108] | 229 |        // Setting the Camera Eye, lookAt and up Vectors | 
|---|
| [7009] | 230 |   gluLookAt(cameraPosition.x, cameraPosition.y, cameraPosition.z, | 
|---|
| [6965] | 231 |             targetPosition.x, targetPosition.y, targetPosition.z, | 
|---|
| [7009] | 232 |             this->upVector.x, this->upVector.y, this->upVector.z); | 
|---|
| [7108] | 233 | } | 
|---|
| [3636] | 234 |  | 
|---|
| [6965] | 235 |  | 
|---|
| [4490] | 236 | /** | 
|---|
| [4836] | 237 |  *  processes an event | 
|---|
 | 238 |  * @param event: the event to process | 
|---|
| [4490] | 239 | */ | 
|---|
| [4414] | 240 | void Camera::process(const Event &event) | 
|---|
 | 241 | { | 
|---|
| [10368] | 242 |   if (eventHandling == true) | 
|---|
| [6424] | 243 |   { | 
|---|
| [10368] | 244 |     if( event.type == KeyMapper::PEV_VIEW0) | 
|---|
 | 245 |     { | 
|---|
 | 246 |       this->setViewMode(Camera::ViewNormal); | 
|---|
 | 247 |     } | 
|---|
 | 248 |     else if( event.type == KeyMapper::PEV_VIEW1) | 
|---|
 | 249 |     { | 
|---|
 | 250 |       this->setViewMode(Camera::ViewBehind); | 
|---|
 | 251 |     } | 
|---|
 | 252 |     else if( event.type == KeyMapper::PEV_VIEW2) | 
|---|
 | 253 |     { | 
|---|
 | 254 |       this->setViewMode(Camera::ViewFront); | 
|---|
 | 255 |     } | 
|---|
 | 256 |     else if( event.type == KeyMapper::PEV_VIEW3) | 
|---|
 | 257 |     { | 
|---|
 | 258 |       this->setViewMode(Camera::ViewLeft); | 
|---|
 | 259 |     } | 
|---|
 | 260 |     else if( event.type == KeyMapper::PEV_VIEW4) | 
|---|
 | 261 |     { | 
|---|
 | 262 |       this->setViewMode(Camera::ViewRight); | 
|---|
 | 263 |     } | 
|---|
 | 264 |     else if( event.type == KeyMapper::PEV_VIEW5) | 
|---|
 | 265 |     { | 
|---|
 | 266 |       this->setViewMode(Camera::ViewTop); | 
|---|
 | 267 |     } | 
|---|
| [6424] | 268 |   } | 
|---|
| [4414] | 269 | } | 
|---|
| [3365] | 270 |  | 
|---|
| [10368] | 271 | /* | 
|---|
 | 272 | void Camera::loadParams(const TiXmlElement* root) | 
|---|
 | 273 | { | 
|---|
 | 274 |   // Do the PNode loading stuff | 
|---|
 | 275 |   PNode::loadParams(root); | 
|---|
| [4414] | 276 |  | 
|---|
| [10368] | 277 |   LoadParam(root, "viewTopFovy", this, Camera, setViewTopFovy); | 
|---|
 | 278 |   LoadParam(root, "viewFrontFovy", this, Camera, setViewFrontFovy); | 
|---|
 | 279 |   LoadParam(root, "viewLeftFovy", this, Camera, setViewLeftFovy); | 
|---|
 | 280 |   LoadParam(root, "viewRightFovy", this, Camera, setViewRightFovy); | 
|---|
 | 281 |   LoadParam(root, "viewBehindFovy", this, Camera, setViewBehindFovy); | 
|---|
 | 282 |   LoadParam(root, "viewNormalFovy", this, Camera, setViewNormalFovy); | 
|---|
 | 283 |  | 
|---|
 | 284 |   LoadParam(root, "viewTopDistance", this, Camera, setViewTopDistance); | 
|---|
 | 285 |   LoadParam(root, "viewFrontDistance", this, Camera, setViewFrontDistance); | 
|---|
 | 286 |   LoadParam(root, "viewLeftDistance", this, Camera, setViewLeftDistance); | 
|---|
 | 287 |   LoadParam(root, "viewRightDistance", this, Camera, setViewRightDistance); | 
|---|
 | 288 |   LoadParam(root, "viewBehindDistance", this, Camera, setViewBehindDistance); | 
|---|
 | 289 |   LoadParam(root, "viewNormalDistance", this, Camera, setViewNormalDistance); | 
|---|
 | 290 | } | 
|---|
 | 291 | */ | 
|---|
 | 292 |  | 
|---|
 | 293 | void Camera::setViewTopFovy(float fovy) | 
|---|
 | 294 | { | 
|---|
 | 295 |   this->viewTopFovy = fovy; | 
|---|
 | 296 | } | 
|---|
 | 297 |  | 
|---|
 | 298 | void Camera::setViewFrontFovy(float fovy) | 
|---|
 | 299 | { | 
|---|
 | 300 |   this->viewFrontFovy = fovy; | 
|---|
 | 301 | } | 
|---|
 | 302 |  | 
|---|
 | 303 | void Camera::setViewLeftFovy(float fovy) | 
|---|
 | 304 | { | 
|---|
 | 305 |   this->viewLeftFovy = fovy; | 
|---|
 | 306 | } | 
|---|
 | 307 |  | 
|---|
 | 308 | void Camera::setViewRightFovy(float fovy) | 
|---|
 | 309 | { | 
|---|
 | 310 |   this->viewRightFovy = fovy; | 
|---|
 | 311 | } | 
|---|
 | 312 |  | 
|---|
 | 313 | void Camera::setViewBehindFovy(float fovy) | 
|---|
 | 314 | { | 
|---|
 | 315 |   this->viewBehindFovy = fovy; | 
|---|
 | 316 | } | 
|---|
 | 317 |  | 
|---|
 | 318 | void Camera::setViewNormalFovy(float fovy) | 
|---|
 | 319 | { | 
|---|
 | 320 |   this->viewNormalFovy = fovy; | 
|---|
 | 321 | } | 
|---|
 | 322 |  | 
|---|
 | 323 | void Camera::setViewTopDistance(float Distance) | 
|---|
 | 324 | { | 
|---|
 | 325 |   this->viewTopDistance = Distance; | 
|---|
 | 326 | } | 
|---|
 | 327 |  | 
|---|
 | 328 | void Camera::setViewFrontDistance(float Distance) | 
|---|
 | 329 | { | 
|---|
 | 330 |   this->viewFrontDistance = Distance; | 
|---|
 | 331 | } | 
|---|
 | 332 |  | 
|---|
 | 333 | void Camera::setViewLeftDistance(float Distance) | 
|---|
 | 334 | { | 
|---|
 | 335 |   this->viewLeftDistance = Distance; | 
|---|
 | 336 | } | 
|---|
 | 337 |  | 
|---|
 | 338 | void Camera::setViewRightDistance(float Distance) | 
|---|
 | 339 | { | 
|---|
 | 340 |   this->viewRightDistance = Distance; | 
|---|
 | 341 | } | 
|---|
 | 342 |  | 
|---|
 | 343 | void Camera::setViewBehindDistance(float Distance) | 
|---|
 | 344 | { | 
|---|
 | 345 |   this->viewBehindDistance = Distance; | 
|---|
 | 346 | } | 
|---|
 | 347 |  | 
|---|
 | 348 | void Camera::setViewNormalDistance(float Distance) | 
|---|
 | 349 | { | 
|---|
 | 350 |   this->viewNormalDistance = Distance; | 
|---|
 | 351 | } | 
|---|
 | 352 |  | 
|---|
 | 353 |  | 
|---|
| [3635] | 354 | /////////////////// | 
|---|
 | 355 | // CAMERA-TARGET // | 
|---|
 | 356 | /////////////////// | 
|---|
| [2636] | 357 |  | 
|---|
| [9869] | 358 | ObjectListDefinition(CameraTarget); | 
|---|
| [3635] | 359 | CameraTarget::CameraTarget() | 
|---|
| [2636] | 360 | { | 
|---|
| [9869] | 361 |   this->registerObject(this, CameraTarget::_objectList); | 
|---|
| [6424] | 362 |   //  this->setParentMode(PNODE_MOVEMENT); | 
|---|
| [2636] | 363 | } | 
|---|
| [3213] | 364 |  | 
|---|