| 1 | /* |
|---|
| 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: |
|---|
| 12 | main-programmer: Christian Meyer |
|---|
| 13 | co-programmer: Benjamin Grauer |
|---|
| 14 | */ |
|---|
| 15 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD_ENTITY |
|---|
| 16 | |
|---|
| 17 | #include "camera.h" |
|---|
| 18 | #include "key_mapper.h" |
|---|
| 19 | #include "glincl.h" |
|---|
| 20 | //#include "util/loading/load_param.h" |
|---|
| 21 | #include "world_entity.h" |
|---|
| 22 | |
|---|
| 23 | ObjectListDefinition(Camera); |
|---|
| 24 | |
|---|
| 25 | /** |
|---|
| 26 | * creates a Camera |
|---|
| 27 | */ |
|---|
| 28 | Camera::Camera() |
|---|
| 29 | { |
|---|
| 30 | this->registerObject(this, Camera::_objectList); |
|---|
| 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 | { |
|---|
| 51 | this->setName("camera"); |
|---|
| 52 | this->target = new CameraTarget(); |
|---|
| 53 | |
|---|
| 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); |
|---|
| 60 | |
|---|
| 61 | //this->setFovy(90); |
|---|
| 62 | this->setAspectRatio(1.33f); |
|---|
| 63 | this->setClipRegion(.1, 10000); |
|---|
| 64 | |
|---|
| 65 | this->viewTopFovy = 60; |
|---|
| 66 | this->viewNormalFovy = 90; |
|---|
| 67 | this->viewFrontFovy = 120; |
|---|
| 68 | this->viewRightFovy = 90; |
|---|
| 69 | this->viewLeftFovy = 90; |
|---|
| 70 | |
|---|
| 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 | |
|---|
| 79 | this->setViewMode(Camera::ViewNormal); |
|---|
| 80 | |
|---|
| 81 | this->setParentMode(PNODE_ALL); |
|---|
| 82 | this->eventHandling = true; |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | /** |
|---|
| 86 | * focuses the Camera onto a Target |
|---|
| 87 | * @param target the new PNode the Camera should look at. |
|---|
| 88 | */ |
|---|
| 89 | void Camera::lookAt(PNode* target) |
|---|
| 90 | { |
|---|
| 91 | this->target->setParent(target); |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | /** |
|---|
| 95 | * @returns The PNode of the Target (from there you can get position and so on |
|---|
| 96 | */ |
|---|
| 97 | PNode* Camera::getTargetNode() const |
|---|
| 98 | { |
|---|
| 99 | return (PNode*)this->target; |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | /** |
|---|
| 103 | * sets a new AspectRatio |
|---|
| 104 | * @param aspectRatio the new aspect ratio to set (width / height) |
|---|
| 105 | */ |
|---|
| 106 | void Camera::setAspectRatio(float aspectRatio) |
|---|
| 107 | { |
|---|
| 108 | this->aspectRatio = aspectRatio; |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | /** |
|---|
| 112 | * Sets a new clipping region |
|---|
| 113 | * @param nearClip The near clip plane |
|---|
| 114 | * @param farClip The far clip plane |
|---|
| 115 | */ |
|---|
| 116 | void Camera::setClipRegion(float nearClip, float farClip) |
|---|
| 117 | { |
|---|
| 118 | this->nearClip = nearClip; |
|---|
| 119 | this->farClip = farClip; |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | /** |
|---|
| 123 | * sets the new VideoMode and initializes iteration to it. |
|---|
| 124 | * @param mode the mode to change to. |
|---|
| 125 | */ |
|---|
| 126 | void Camera::setViewMode(ViewMode mode) |
|---|
| 127 | { |
|---|
| 128 | currentMode = mode; |
|---|
| 129 | switch (mode) |
|---|
| 130 | { |
|---|
| 131 | default: |
|---|
| 132 | case Camera::ViewNormal: |
|---|
| 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); |
|---|
| 139 | this->target->setRelCoorSoft(0,0,0); |
|---|
| 140 | break; |
|---|
| 141 | } |
|---|
| 142 | case Camera::ViewBehind: |
|---|
| 143 | break; |
|---|
| 144 | case Camera::ViewFront: |
|---|
| 145 | { |
|---|
| 146 | this->fovy = viewFrontFovy; |
|---|
| 147 | this->toFovy = viewFrontFovy; |
|---|
| 148 | this->setRelCoorSoft(this->viewFrontDistance, 0, 0, 5); |
|---|
| 149 | this->target->setRelCoorSoft(Vector(10,0,0), 5); |
|---|
| 150 | break; |
|---|
| 151 | } |
|---|
| 152 | case Camera::ViewLeft: |
|---|
| 153 | { |
|---|
| 154 | this->fovy = viewLeftFovy; |
|---|
| 155 | this->toFovy = viewLeftFovy; |
|---|
| 156 | this->setRelCoorSoft(0, 1, -viewLeftDistance, .5); |
|---|
| 157 | this->target->setRelCoorSoft(0,0,0); |
|---|
| 158 | break; |
|---|
| 159 | } |
|---|
| 160 | case Camera::ViewRight: |
|---|
| 161 | { |
|---|
| 162 | this->fovy = viewRightFovy; |
|---|
| 163 | this->toFovy = viewRightFovy; |
|---|
| 164 | this->setRelCoorSoft(Vector(0, 1, viewRightDistance), 0.5); |
|---|
| 165 | this->target->setRelCoorSoft(0,0,0); |
|---|
| 166 | break; |
|---|
| 167 | } |
|---|
| 168 | case Camera::ViewTop: |
|---|
| 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 | } |
|---|
| 175 | } |
|---|
| 176 | } |
|---|
| 177 | |
|---|
| 178 | |
|---|
| 179 | /** |
|---|
| 180 | * Updates the position of the camera. |
|---|
| 181 | * @param dt: The time that elapsed. |
|---|
| 182 | */ |
|---|
| 183 | void Camera::tick(float dt) |
|---|
| 184 | { |
|---|
| 185 | //update frustum plane |
|---|
| 186 | this->viewVector = (this->target->getAbsCoor() - this->getAbsCoor()).getNormalized(); |
|---|
| 187 | this->frustumPlane = Plane(this->viewVector, this->getAbsCoor() + this->viewVector * 0.1); |
|---|
| 188 | |
|---|
| 189 | this->upVector = this->getAbsDirV(); |
|---|
| 190 | |
|---|
| 191 | |
|---|
| 192 | float tmpFovy = (this->toFovy - this->fovy); |
|---|
| 193 | if (fabsf(tmpFovy) > 0.01) |
|---|
| 194 | this->fovy += tmpFovy * fabsf(dt); |
|---|
| 195 | } |
|---|
| 196 | |
|---|
| 197 | |
|---|
| 198 | /** |
|---|
| 199 | * initialize rendering perspective according to this camera |
|---|
| 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 | */ |
|---|
| 204 | void Camera::apply () |
|---|
| 205 | { |
|---|
| 206 | // switching to Projection Matrix |
|---|
| 207 | glMatrixMode (GL_PROJECTION); |
|---|
| 208 | glLoadIdentity (); |
|---|
| 209 | |
|---|
| 210 | gluPerspective(this->fovy, |
|---|
| 211 | this->aspectRatio, |
|---|
| 212 | this->nearClip, |
|---|
| 213 | this->farClip); |
|---|
| 214 | |
|---|
| 215 | |
|---|
| 216 | // setting up the perspective |
|---|
| 217 | // speed-up feature |
|---|
| 218 | glMatrixMode (GL_MODELVIEW); |
|---|
| 219 | glLoadIdentity(); |
|---|
| 220 | |
|---|
| 221 | |
|---|
| 222 | } |
|---|
| 223 | |
|---|
| 224 | void Camera::project() |
|---|
| 225 | { |
|---|
| 226 | Vector cameraPosition = this->getAbsCoor(); |
|---|
| 227 | Vector targetPosition = this->target->getAbsCoor(); |
|---|
| 228 | |
|---|
| 229 | // Setting the Camera Eye, lookAt and up Vectors |
|---|
| 230 | gluLookAt(cameraPosition.x, cameraPosition.y, cameraPosition.z, |
|---|
| 231 | targetPosition.x, targetPosition.y, targetPosition.z, |
|---|
| 232 | this->upVector.x, this->upVector.y, this->upVector.z); |
|---|
| 233 | } |
|---|
| 234 | |
|---|
| 235 | |
|---|
| 236 | /** |
|---|
| 237 | * processes an event |
|---|
| 238 | * @param event: the event to process |
|---|
| 239 | */ |
|---|
| 240 | void Camera::process(const Event &event) |
|---|
| 241 | { |
|---|
| 242 | if (eventHandling == true) |
|---|
| 243 | { |
|---|
| 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 | } |
|---|
| 268 | } |
|---|
| 269 | } |
|---|
| 270 | |
|---|
| 271 | /* |
|---|
| 272 | void Camera::loadParams(const TiXmlElement* root) |
|---|
| 273 | { |
|---|
| 274 | // Do the PNode loading stuff |
|---|
| 275 | PNode::loadParams(root); |
|---|
| 276 | |
|---|
| 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 | |
|---|
| 354 | /////////////////// |
|---|
| 355 | // CAMERA-TARGET // |
|---|
| 356 | /////////////////// |
|---|
| 357 | |
|---|
| 358 | ObjectListDefinition(CameraTarget); |
|---|
| 359 | CameraTarget::CameraTarget() |
|---|
| 360 | { |
|---|
| 361 | this->registerObject(this, CameraTarget::_objectList); |
|---|
| 362 | // this->setParentMode(PNODE_MOVEMENT); |
|---|
| 363 | } |
|---|
| 364 | |
|---|