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