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