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