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