| 1 |  | 
|---|
| 2 |  | 
|---|
| 3 | /* | 
|---|
| 4 | orxonox - the future of 3D-vertical-scrollers | 
|---|
| 5 |  | 
|---|
| 6 | Copyright (C) 2004 orx | 
|---|
| 7 |  | 
|---|
| 8 | This program is free software; you can redistribute it and/or modify | 
|---|
| 9 | it under the terms of the GNU General Public License as published by | 
|---|
| 10 | the Free Software Foundation; either version 2, or (at your option) | 
|---|
| 11 | any later version. | 
|---|
| 12 |  | 
|---|
| 13 | ### File Specific: | 
|---|
| 14 | main-programmer: Christian Meyer | 
|---|
| 15 | co-programmer: ... | 
|---|
| 16 | */ | 
|---|
| 17 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD_ENTITY | 
|---|
| 18 |  | 
|---|
| 19 | #include "camera.h" | 
|---|
| 20 |  | 
|---|
| 21 | #include "world.h" | 
|---|
| 22 | #include "world_entity.h" | 
|---|
| 23 | #include "vector.h" | 
|---|
| 24 | #include "event.h" | 
|---|
| 25 | #include "event_handler.h" | 
|---|
| 26 |  | 
|---|
| 27 | using namespace std; | 
|---|
| 28 |  | 
|---|
| 29 | //////////// | 
|---|
| 30 | // CAMERA // | 
|---|
| 31 | //////////// | 
|---|
| 32 |  | 
|---|
| 33 | /** | 
|---|
| 34 | *  creates a Camera | 
|---|
| 35 | */ | 
|---|
| 36 | Camera::Camera() | 
|---|
| 37 | { | 
|---|
| 38 | this->setClassID(CL_CAMERA, "Camera"); | 
|---|
| 39 | this->setName("camera"); | 
|---|
| 40 | this->target = new CameraTarget(); | 
|---|
| 41 |  | 
|---|
| 42 | EventHandler::getInstance()->subscribe(this, ES_GAME, KeyMapper::PEV_VIEW0); | 
|---|
| 43 | EventHandler::getInstance()->subscribe(this, ES_GAME, KeyMapper::PEV_VIEW1); | 
|---|
| 44 | EventHandler::getInstance()->subscribe(this, ES_GAME, KeyMapper::PEV_VIEW2); | 
|---|
| 45 | EventHandler::getInstance()->subscribe(this, ES_GAME, KeyMapper::PEV_VIEW3); | 
|---|
| 46 | EventHandler::getInstance()->subscribe(this, ES_GAME, KeyMapper::PEV_VIEW4); | 
|---|
| 47 | EventHandler::getInstance()->subscribe(this, ES_GAME, KeyMapper::PEV_VIEW5); | 
|---|
| 48 |  | 
|---|
| 49 | this->setFovy(90); | 
|---|
| 50 | this->setAspectRatio(1.2f); | 
|---|
| 51 | this->setClipRegion(.1, 2000); | 
|---|
| 52 |  | 
|---|
| 53 | this->setViewMode(VIEW_NORMAL); | 
|---|
| 54 |  | 
|---|
| 55 | this->setParentMode(PNODE_ALL); | 
|---|
| 56 | } | 
|---|
| 57 |  | 
|---|
| 58 | /** | 
|---|
| 59 | *  default destructor | 
|---|
| 60 | */ | 
|---|
| 61 | Camera::~Camera() | 
|---|
| 62 | { | 
|---|
| 63 | } | 
|---|
| 64 |  | 
|---|
| 65 | /** | 
|---|
| 66 | *  focuses the Camera onto a Target | 
|---|
| 67 | * @param target the new PNode the Camera should look at. | 
|---|
| 68 | */ | 
|---|
| 69 | void Camera::lookAt(PNode* target) | 
|---|
| 70 | { | 
|---|
| 71 | this->target->setParent(target); | 
|---|
| 72 | } | 
|---|
| 73 |  | 
|---|
| 74 | /** | 
|---|
| 75 | * @returns The PNode of the Target (from there you can get position and so on | 
|---|
| 76 | */ | 
|---|
| 77 | PNode* Camera::getTarget() | 
|---|
| 78 | { | 
|---|
| 79 | return (PNode*)this->target; | 
|---|
| 80 | } | 
|---|
| 81 |  | 
|---|
| 82 | /** | 
|---|
| 83 | *  sets a new AspectRatio | 
|---|
| 84 | * @param aspectRatio the new aspect ratio to set (width / height) | 
|---|
| 85 | */ | 
|---|
| 86 | void Camera::setAspectRatio(float aspectRatio) | 
|---|
| 87 | { | 
|---|
| 88 | this->aspectRatio = aspectRatio; | 
|---|
| 89 | } | 
|---|
| 90 |  | 
|---|
| 91 | /** | 
|---|
| 92 | *  sets the Field of View to fofy | 
|---|
| 93 | * @param fovy new field of view factor (in degrees) | 
|---|
| 94 | */ | 
|---|
| 95 | void Camera::setFovy(float fovy) | 
|---|
| 96 | { | 
|---|
| 97 | this->fovy = fovy; | 
|---|
| 98 | } | 
|---|
| 99 |  | 
|---|
| 100 | /** | 
|---|
| 101 | * Sets a new clipping region | 
|---|
| 102 | * @param nearClip The near clip plane | 
|---|
| 103 | * @param farClip The far clip plane | 
|---|
| 104 | */ | 
|---|
| 105 | void Camera::setClipRegion(float nearClip, float farClip) | 
|---|
| 106 | { | 
|---|
| 107 | this->nearClip = nearClip; | 
|---|
| 108 | this->farClip = farClip; | 
|---|
| 109 | } | 
|---|
| 110 |  | 
|---|
| 111 | /** | 
|---|
| 112 | *  sets the new VideoMode and initializes iteration to it. | 
|---|
| 113 | * @param mode the mode to change to. | 
|---|
| 114 | */ | 
|---|
| 115 | void Camera::setViewMode(ViewMode mode) | 
|---|
| 116 | { | 
|---|
| 117 | switch (mode) | 
|---|
| 118 | { | 
|---|
| 119 | default: | 
|---|
| 120 | case VIEW_NORMAL: | 
|---|
| 121 | this->toFovy = 60.0; | 
|---|
| 122 | this->setParentSoft("TrackNode"); | 
|---|
| 123 | this->target->setParentSoft("TrackNode"); | 
|---|
| 124 | this->setRelCoorSoft(-10, 5, 0); | 
|---|
| 125 | this->target->setRelCoorSoft(0,0,0); | 
|---|
| 126 | break; | 
|---|
| 127 | case VIEW_BEHIND: | 
|---|
| 128 | //      this->toFovy = 120.0; | 
|---|
| 129 | //      this->setRelCoorSoft(Vector(3.5, 0, 0)); | 
|---|
| 130 | //    this->target->setRelCoorSoft(Vector(10,0,0)); | 
|---|
| 131 |  | 
|---|
| 132 | if (!strcmp(this->target->getParent()->getName(), "Player")) | 
|---|
| 133 | this->target->setParentSoft("TrackNode"); | 
|---|
| 134 | else | 
|---|
| 135 | this->target->setParentSoft("Player"); | 
|---|
| 136 | this->getParent()->debug(0); | 
|---|
| 137 |  | 
|---|
| 138 | //      this->setParent("main-Turret"); | 
|---|
| 139 | //      this->setParentMode(PNODE_ALL); | 
|---|
| 140 | //      this->target->setParentSoft("Player"); | 
|---|
| 141 |  | 
|---|
| 142 | break; | 
|---|
| 143 | case VIEW_FRONT: | 
|---|
| 144 | this->toFovy = 120.0; | 
|---|
| 145 | this->setParentSoft("Player"); | 
|---|
| 146 | this->target->setParentSoft("Player"); | 
|---|
| 147 | this->setRelCoorSoft(4, 0, 0, 5); | 
|---|
| 148 | this->target->setRelCoorSoft(Vector(10,0,0), 5); | 
|---|
| 149 | //      this->target->setRelDirSoft(Quaternion(M_PI/4.0, Vector(0,1,0))); | 
|---|
| 150 | break; | 
|---|
| 151 | case VIEW_LEFT: | 
|---|
| 152 | this->toFovy = 90; | 
|---|
| 153 | this->setParentSoft("TrackNode"); | 
|---|
| 154 | this->target->setParentSoft("TrackNode"); | 
|---|
| 155 | this->setRelCoorSoft(0, 1, -10, .5); | 
|---|
| 156 | this->target->setRelCoorSoft(0,0,0); | 
|---|
| 157 | break; | 
|---|
| 158 | case VIEW_RIGHT: | 
|---|
| 159 | this->toFovy = 90; | 
|---|
| 160 | this->setParentSoft("TrackNode"); | 
|---|
| 161 | this->target->setParentSoft("TrackNode"); | 
|---|
| 162 | this->setRelCoorSoft(Vector(0, 1, 10)); | 
|---|
| 163 | this->target->setRelCoorSoft(0,0,0); | 
|---|
| 164 | break; | 
|---|
| 165 | case VIEW_TOP: | 
|---|
| 166 | this->toFovy= 120; | 
|---|
| 167 | this->setParentSoft("TrackNode"); | 
|---|
| 168 | this->target->setParentSoft("TrackNode"); | 
|---|
| 169 | this->setRelCoorSoft(Vector(0, 10, 0)); | 
|---|
| 170 | this->target->setRelCoorSoft(0,0,0); | 
|---|
| 171 | } | 
|---|
| 172 | } | 
|---|
| 173 |  | 
|---|
| 174 |  | 
|---|
| 175 | /** | 
|---|
| 176 | *  Updates the position of the camera. | 
|---|
| 177 | * @param dt: The time that elapsed. | 
|---|
| 178 | */ | 
|---|
| 179 | void Camera::tick(float dt) | 
|---|
| 180 | { | 
|---|
| 181 | float tmpFovy = (this->toFovy - this->fovy) ; | 
|---|
| 182 | if (tmpFovy > .01) | 
|---|
| 183 | this->fovy += tmpFovy * fabsf(dt); | 
|---|
| 184 | } | 
|---|
| 185 |  | 
|---|
| 186 |  | 
|---|
| 187 | /** | 
|---|
| 188 | *  initialize rendering perspective according to this camera | 
|---|
| 189 |  | 
|---|
| 190 | This is called immediately before the rendering cycle starts, it sets all global | 
|---|
| 191 | rendering options as well as the GL_PROJECTION matrix according to the camera. | 
|---|
| 192 | */ | 
|---|
| 193 | void Camera::apply () | 
|---|
| 194 | { | 
|---|
| 195 | // switching to Projection Matrix | 
|---|
| 196 | glMatrixMode (GL_PROJECTION); | 
|---|
| 197 | glLoadIdentity (); | 
|---|
| 198 |  | 
|---|
| 199 | // setting up the perspective | 
|---|
| 200 | gluPerspective(this->fovy, | 
|---|
| 201 | this->aspectRatio, | 
|---|
| 202 | this->nearClip, | 
|---|
| 203 | this->farClip); | 
|---|
| 204 |  | 
|---|
| 205 | // speed-up feature | 
|---|
| 206 | Vector cameraPosition = this->getAbsCoor(); | 
|---|
| 207 | Vector targetPosition = this->target->getAbsCoor(); | 
|---|
| 208 | Vector up = this->getAbsDirV(); | 
|---|
| 209 |  | 
|---|
| 210 | // Setting the Camera Eye, lookAt and up Vectors | 
|---|
| 211 | gluLookAt(cameraPosition.x, cameraPosition.y, cameraPosition.z, | 
|---|
| 212 | targetPosition.x, targetPosition.y, targetPosition.z, | 
|---|
| 213 | up.x, up.y, up.z); | 
|---|
| 214 |  | 
|---|
| 215 | // switching back to Modeling Matrix | 
|---|
| 216 | glMatrixMode (GL_MODELVIEW); | 
|---|
| 217 | //  this->target->getParent()->getParent()->debugDraw(0, 2, Vector(1,0,0)); | 
|---|
| 218 | } | 
|---|
| 219 |  | 
|---|
| 220 | /** | 
|---|
| 221 | *  processes an event | 
|---|
| 222 | * @param event: the event to process | 
|---|
| 223 | */ | 
|---|
| 224 | void Camera::process(const Event &event) | 
|---|
| 225 | { | 
|---|
| 226 | if( event.type == KeyMapper::PEV_VIEW0) | 
|---|
| 227 | { | 
|---|
| 228 | this->setViewMode(VIEW_NORMAL); | 
|---|
| 229 | } | 
|---|
| 230 | else if( event.type == KeyMapper::PEV_VIEW1) | 
|---|
| 231 | { | 
|---|
| 232 | this->setViewMode(VIEW_BEHIND); | 
|---|
| 233 | } | 
|---|
| 234 | else if( event.type == KeyMapper::PEV_VIEW2) | 
|---|
| 235 | { | 
|---|
| 236 | this->setViewMode(VIEW_FRONT); | 
|---|
| 237 | } | 
|---|
| 238 | else if( event.type == KeyMapper::PEV_VIEW3) | 
|---|
| 239 | { | 
|---|
| 240 | this->setViewMode(VIEW_LEFT); | 
|---|
| 241 | } | 
|---|
| 242 | else if( event.type == KeyMapper::PEV_VIEW4) | 
|---|
| 243 | { | 
|---|
| 244 | this->setViewMode(VIEW_RIGHT); | 
|---|
| 245 | } | 
|---|
| 246 | else if( event.type == KeyMapper::PEV_VIEW5) | 
|---|
| 247 | { | 
|---|
| 248 | this->setViewMode(VIEW_TOP); | 
|---|
| 249 | } | 
|---|
| 250 | } | 
|---|
| 251 |  | 
|---|
| 252 |  | 
|---|
| 253 | /////////////////// | 
|---|
| 254 | // CAMERA-TARGET // | 
|---|
| 255 | /////////////////// | 
|---|
| 256 |  | 
|---|
| 257 |  | 
|---|
| 258 | CameraTarget::CameraTarget() | 
|---|
| 259 | { | 
|---|
| 260 | this->setClassID(CL_CAMERA_TARGET, "CameraTarget"); | 
|---|
| 261 | //  this->setParentMode(PNODE_MOVEMENT); | 
|---|
| 262 | } | 
|---|
| 263 |  | 
|---|