| [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" | 
|---|
| [3608] | 20 |  | 
|---|
| [9869] | 21 | ObjectListDefinition(Camera); | 
|---|
 | 22 |  | 
|---|
| [2096] | 23 | /** | 
|---|
| [4836] | 24 |  *  creates a Camera | 
|---|
| [2096] | 25 | */ | 
|---|
| [4746] | 26 | Camera::Camera() | 
|---|
| [2068] | 27 | { | 
|---|
| [9869] | 28 |   this->registerObject(this, Camera::_objectList); | 
|---|
| [4987] | 29 |   this->setName("camera"); | 
|---|
| [3635] | 30 |   this->target = new CameraTarget(); | 
|---|
| [3636] | 31 |  | 
|---|
| [7868] | 32 |   this->subscribeEvent(ES_GAME, KeyMapper::PEV_VIEW0); | 
|---|
 | 33 |   this->subscribeEvent(ES_GAME, KeyMapper::PEV_VIEW1); | 
|---|
 | 34 |   this->subscribeEvent(ES_GAME, KeyMapper::PEV_VIEW2); | 
|---|
 | 35 |   this->subscribeEvent(ES_GAME, KeyMapper::PEV_VIEW3); | 
|---|
 | 36 |   this->subscribeEvent(ES_GAME, KeyMapper::PEV_VIEW4); | 
|---|
 | 37 |   this->subscribeEvent(ES_GAME, KeyMapper::PEV_VIEW5); | 
|---|
| [4414] | 38 |  | 
|---|
| [3641] | 39 |   this->setFovy(90); | 
|---|
| [3636] | 40 |   this->setAspectRatio(1.2f); | 
|---|
| [9235] | 41 |   this->setClipRegion(.1, 10000); | 
|---|
| [3641] | 42 |  | 
|---|
| [7347] | 43 |   this->setViewMode(Camera::ViewNormal); | 
|---|
| [4987] | 44 |  | 
|---|
| [5004] | 45 |   this->setParentMode(PNODE_ALL); | 
|---|
| [2068] | 46 | } | 
|---|
 | 47 |  | 
|---|
| [2096] | 48 | /** | 
|---|
| [4836] | 49 |  *  default destructor | 
|---|
| [2096] | 50 | */ | 
|---|
| [4746] | 51 | Camera::~Camera() | 
|---|
| [6424] | 52 | {} | 
|---|
| [3543] | 53 |  | 
|---|
 | 54 | /** | 
|---|
| [4836] | 55 |  *  focuses the Camera onto a Target | 
|---|
 | 56 |  * @param target the new PNode the Camera should look at. | 
|---|
| [2096] | 57 | */ | 
|---|
| [3635] | 58 | void Camera::lookAt(PNode* target) | 
|---|
| [2068] | 59 | { | 
|---|
| [3635] | 60 |   this->target->setParent(target); | 
|---|
| [2068] | 61 | } | 
|---|
 | 62 |  | 
|---|
| [3638] | 63 | /** | 
|---|
| [4836] | 64 |  * @returns The PNode of the Target (from there you can get position and so on | 
|---|
| [3638] | 65 | */ | 
|---|
| [7014] | 66 | PNode* Camera::getTargetNode() const | 
|---|
| [2068] | 67 | { | 
|---|
| [3635] | 68 |   return (PNode*)this->target; | 
|---|
| [2068] | 69 | } | 
|---|
 | 70 |  | 
|---|
| [2096] | 71 | /** | 
|---|
| [4836] | 72 |  *  sets a new AspectRatio | 
|---|
 | 73 |  * @param aspectRatio the new aspect ratio to set (width / height) | 
|---|
| [3636] | 74 | */ | 
|---|
 | 75 | void Camera::setAspectRatio(float aspectRatio) | 
|---|
 | 76 | { | 
|---|
 | 77 |   this->aspectRatio = aspectRatio; | 
|---|
 | 78 | } | 
|---|
 | 79 |  | 
|---|
 | 80 | /** | 
|---|
| [4992] | 81 |  * Sets a new clipping region | 
|---|
 | 82 |  * @param nearClip The near clip plane | 
|---|
 | 83 |  * @param farClip The far clip plane | 
|---|
| [3636] | 84 | */ | 
|---|
 | 85 | void Camera::setClipRegion(float nearClip, float farClip) | 
|---|
 | 86 | { | 
|---|
 | 87 |   this->nearClip = nearClip; | 
|---|
 | 88 |   this->farClip = farClip; | 
|---|
 | 89 | } | 
|---|
 | 90 |  | 
|---|
| [4490] | 91 | /** | 
|---|
| [4836] | 92 |  *  sets the new VideoMode and initializes iteration to it. | 
|---|
 | 93 |  * @param mode the mode to change to. | 
|---|
| [4490] | 94 | */ | 
|---|
| [3639] | 95 | void Camera::setViewMode(ViewMode mode) | 
|---|
 | 96 | { | 
|---|
| [6034] | 97 |   currentMode = mode; | 
|---|
| [3639] | 98 |   switch (mode) | 
|---|
| [6424] | 99 |   { | 
|---|
| [3639] | 100 |     default: | 
|---|
| [7347] | 101 |     case Camera::ViewNormal: | 
|---|
| [3639] | 102 |       this->toFovy = 60.0; | 
|---|
| [4992] | 103 |       this->setRelCoorSoft(-10, 5, 0); | 
|---|
 | 104 |       this->target->setRelCoorSoft(0,0,0); | 
|---|
| [3639] | 105 |       break; | 
|---|
| [7347] | 106 |     case Camera::ViewBehind: | 
|---|
| [3639] | 107 |       break; | 
|---|
| [7347] | 108 |     case Camera::ViewFront: | 
|---|
| [4992] | 109 |       this->toFovy = 120.0; | 
|---|
| [6424] | 110 |       this->setRelCoorSoft(4, 0, 0, 5); | 
|---|
 | 111 |       this->target->setRelCoorSoft(Vector(10,0,0), 5); | 
|---|
| [3639] | 112 |       break; | 
|---|
| [7347] | 113 |     case Camera::ViewLeft: | 
|---|
| [3639] | 114 |       this->toFovy = 90; | 
|---|
| [4992] | 115 |       this->setRelCoorSoft(0, 1, -10, .5); | 
|---|
 | 116 |       this->target->setRelCoorSoft(0,0,0); | 
|---|
| [3639] | 117 |       break; | 
|---|
| [7347] | 118 |     case Camera::ViewRight: | 
|---|
| [3639] | 119 |       this->toFovy = 90; | 
|---|
| [4987] | 120 |       this->setRelCoorSoft(Vector(0, 1, 10)); | 
|---|
| [4992] | 121 |       this->target->setRelCoorSoft(0,0,0); | 
|---|
| [3639] | 122 |       break; | 
|---|
| [7347] | 123 |     case Camera::ViewTop: | 
|---|
| [3643] | 124 |       this->toFovy= 120; | 
|---|
| [5751] | 125 |       this->setRelCoorSoft(Vector(30, 50, 0)); | 
|---|
 | 126 |       this->target->setRelCoorSoft(35,0,0); | 
|---|
| [6424] | 127 |   } | 
|---|
| [3639] | 128 | } | 
|---|
 | 129 |  | 
|---|
 | 130 |  | 
|---|
| [3636] | 131 | /** | 
|---|
| [4836] | 132 |  *  Updates the position of the camera. | 
|---|
 | 133 |  * @param dt: The time that elapsed. | 
|---|
| [3639] | 134 | */ | 
|---|
 | 135 | void Camera::tick(float dt) | 
|---|
 | 136 | { | 
|---|
| [7009] | 137 |   //update frustum plane | 
|---|
| [7014] | 138 |   this->viewVector = (this->target->getAbsCoor() - this->getAbsCoor()).getNormalized(); | 
|---|
 | 139 |   this->frustumPlane = Plane(this->viewVector, this->getAbsCoor() + this->viewVector * 0.1); | 
|---|
| [7009] | 140 |  | 
|---|
 | 141 |   this->upVector =  this->getAbsDirV(); | 
|---|
 | 142 |  | 
|---|
 | 143 |  | 
|---|
| [7173] | 144 |   float tmpFovy = (this->toFovy - this->fovy); | 
|---|
| [6034] | 145 |   if (tmpFovy > 0.01) | 
|---|
| [5354] | 146 |     this->fovy += tmpFovy * fabsf(dt); | 
|---|
| [3639] | 147 | } | 
|---|
 | 148 |  | 
|---|
 | 149 |  | 
|---|
 | 150 | /** | 
|---|
| [4836] | 151 |  *  initialize rendering perspective according to this camera | 
|---|
| [6772] | 152 |  * | 
|---|
 | 153 |  * This is called immediately before the rendering cycle starts, it sets all global | 
|---|
 | 154 |  * rendering options as well as the GL_PROJECTION matrix according to the camera. | 
|---|
 | 155 |  */ | 
|---|
| [2068] | 156 | void Camera::apply () | 
|---|
 | 157 | { | 
|---|
| [3636] | 158 |   // switching to Projection Matrix | 
|---|
| [2551] | 159 |   glMatrixMode (GL_PROJECTION); | 
|---|
| [2112] | 160 |   glLoadIdentity (); | 
|---|
| [3635] | 161 |  | 
|---|
| [3636] | 162 |   gluPerspective(this->fovy, | 
|---|
| [4832] | 163 |                  this->aspectRatio, | 
|---|
 | 164 |                  this->nearClip, | 
|---|
 | 165 |                  this->farClip); | 
|---|
| [6778] | 166 |  | 
|---|
 | 167 |  | 
|---|
 | 168 |     // setting up the perspective | 
|---|
| [3636] | 169 |   // speed-up feature | 
|---|
| [7108] | 170 |   glMatrixMode (GL_MODELVIEW); | 
|---|
 | 171 |   glLoadIdentity(); | 
|---|
 | 172 |  | 
|---|
 | 173 |  | 
|---|
 | 174 | } | 
|---|
 | 175 |  | 
|---|
 | 176 | void Camera::project() | 
|---|
 | 177 | { | 
|---|
| [3635] | 178 |   Vector cameraPosition = this->getAbsCoor(); | 
|---|
 | 179 |   Vector targetPosition = this->target->getAbsCoor(); | 
|---|
| [2551] | 180 |  | 
|---|
| [7108] | 181 |        // Setting the Camera Eye, lookAt and up Vectors | 
|---|
| [7009] | 182 |   gluLookAt(cameraPosition.x, cameraPosition.y, cameraPosition.z, | 
|---|
| [6965] | 183 |             targetPosition.x, targetPosition.y, targetPosition.z, | 
|---|
| [7009] | 184 |             this->upVector.x, this->upVector.y, this->upVector.z); | 
|---|
| [7108] | 185 | } | 
|---|
| [3636] | 186 |  | 
|---|
| [6965] | 187 |  | 
|---|
| [4490] | 188 | /** | 
|---|
| [4836] | 189 |  *  processes an event | 
|---|
 | 190 |  * @param event: the event to process | 
|---|
| [4490] | 191 | */ | 
|---|
| [4414] | 192 | void Camera::process(const Event &event) | 
|---|
 | 193 | { | 
|---|
 | 194 |   if( event.type == KeyMapper::PEV_VIEW0) | 
|---|
| [6424] | 195 |   { | 
|---|
| [7347] | 196 |     this->setViewMode(Camera::ViewNormal); | 
|---|
| [6424] | 197 |   } | 
|---|
| [4414] | 198 |   else if( event.type == KeyMapper::PEV_VIEW1) | 
|---|
| [6424] | 199 |   { | 
|---|
| [7347] | 200 |     this->setViewMode(Camera::ViewBehind); | 
|---|
| [6424] | 201 |   } | 
|---|
| [4414] | 202 |   else if( event.type == KeyMapper::PEV_VIEW2) | 
|---|
| [6424] | 203 |   { | 
|---|
| [7347] | 204 |     this->setViewMode(Camera::ViewFront); | 
|---|
| [6424] | 205 |   } | 
|---|
| [4414] | 206 |   else if( event.type == KeyMapper::PEV_VIEW3) | 
|---|
| [6424] | 207 |   { | 
|---|
| [7347] | 208 |     this->setViewMode(Camera::ViewLeft); | 
|---|
| [6424] | 209 |   } | 
|---|
| [4414] | 210 |   else if( event.type == KeyMapper::PEV_VIEW4) | 
|---|
| [6424] | 211 |   { | 
|---|
| [7347] | 212 |     this->setViewMode(Camera::ViewRight); | 
|---|
| [6424] | 213 |   } | 
|---|
| [4414] | 214 |   else if( event.type == KeyMapper::PEV_VIEW5) | 
|---|
| [6424] | 215 |   { | 
|---|
| [7347] | 216 |     this->setViewMode(Camera::ViewTop); | 
|---|
| [6424] | 217 |   } | 
|---|
| [4414] | 218 | } | 
|---|
| [3365] | 219 |  | 
|---|
| [4414] | 220 |  | 
|---|
| [3635] | 221 | /////////////////// | 
|---|
 | 222 | // CAMERA-TARGET // | 
|---|
 | 223 | /////////////////// | 
|---|
| [2636] | 224 |  | 
|---|
| [9869] | 225 | ObjectListDefinition(CameraTarget); | 
|---|
| [3635] | 226 | CameraTarget::CameraTarget() | 
|---|
| [2636] | 227 | { | 
|---|
| [9869] | 228 |   this->registerObject(this, CameraTarget::_objectList); | 
|---|
| [6424] | 229 |   //  this->setParentMode(PNODE_MOVEMENT); | 
|---|
| [2636] | 230 | } | 
|---|
| [3213] | 231 |  | 
|---|