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