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