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