[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 | */ |
---|
| 17 | |
---|
| 18 | #include "camera.h" |
---|
[3608] | 19 | |
---|
[2100] | 20 | #include "world.h" |
---|
| 21 | #include "world_entity.h" |
---|
[3608] | 22 | #include "vector.h" |
---|
[4414] | 23 | #include "event.h" |
---|
| 24 | #include "event_handler.h" |
---|
[2068] | 25 | |
---|
| 26 | using namespace std; |
---|
| 27 | |
---|
[3635] | 28 | //////////// |
---|
| 29 | // CAMERA // |
---|
| 30 | //////////// |
---|
| 31 | |
---|
[2096] | 32 | /** |
---|
[4836] | 33 | * creates a Camera |
---|
[2096] | 34 | */ |
---|
[4746] | 35 | Camera::Camera() |
---|
[2068] | 36 | { |
---|
[4320] | 37 | this->setClassID(CL_CAMERA, "Camera"); |
---|
[4987] | 38 | this->setName("camera"); |
---|
[3635] | 39 | this->target = new CameraTarget(); |
---|
[3636] | 40 | |
---|
[4414] | 41 | EventHandler::getInstance()->subscribe(this, ES_GAME, KeyMapper::PEV_VIEW0); |
---|
| 42 | EventHandler::getInstance()->subscribe(this, ES_GAME, KeyMapper::PEV_VIEW1); |
---|
| 43 | EventHandler::getInstance()->subscribe(this, ES_GAME, KeyMapper::PEV_VIEW2); |
---|
| 44 | EventHandler::getInstance()->subscribe(this, ES_GAME, KeyMapper::PEV_VIEW3); |
---|
| 45 | EventHandler::getInstance()->subscribe(this, ES_GAME, KeyMapper::PEV_VIEW4); |
---|
| 46 | EventHandler::getInstance()->subscribe(this, ES_GAME, KeyMapper::PEV_VIEW5); |
---|
| 47 | |
---|
[3641] | 48 | this->setFovy(90); |
---|
[3636] | 49 | this->setAspectRatio(1.2f); |
---|
| 50 | this->setClipRegion(.1, 2000); |
---|
[3641] | 51 | |
---|
[3639] | 52 | this->setViewMode(VIEW_NORMAL); |
---|
[4987] | 53 | |
---|
[5004] | 54 | this->setParentMode(PNODE_ALL); |
---|
[2068] | 55 | } |
---|
| 56 | |
---|
[2096] | 57 | /** |
---|
[4836] | 58 | * default destructor |
---|
[2096] | 59 | */ |
---|
[4746] | 60 | Camera::~Camera() |
---|
[2068] | 61 | { |
---|
[3543] | 62 | } |
---|
| 63 | |
---|
| 64 | /** |
---|
[4836] | 65 | * focuses the Camera onto a Target |
---|
| 66 | * @param target the new PNode the Camera should look at. |
---|
[2096] | 67 | */ |
---|
[3635] | 68 | void Camera::lookAt(PNode* target) |
---|
[2068] | 69 | { |
---|
[3635] | 70 | this->target->setParent(target); |
---|
[2068] | 71 | } |
---|
| 72 | |
---|
[3638] | 73 | /** |
---|
[4836] | 74 | * @returns The PNode of the Target (from there you can get position and so on |
---|
[3638] | 75 | */ |
---|
[4746] | 76 | PNode* Camera::getTarget() |
---|
[2068] | 77 | { |
---|
[3635] | 78 | return (PNode*)this->target; |
---|
[2068] | 79 | } |
---|
| 80 | |
---|
[2096] | 81 | /** |
---|
[4836] | 82 | * sets a new AspectRatio |
---|
| 83 | * @param aspectRatio the new aspect ratio to set (width / height) |
---|
[3636] | 84 | */ |
---|
| 85 | void Camera::setAspectRatio(float aspectRatio) |
---|
| 86 | { |
---|
| 87 | this->aspectRatio = aspectRatio; |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | /** |
---|
[4836] | 91 | * sets the Field of View to fofy |
---|
| 92 | * @param fovy new field of view factor (in degrees) |
---|
[3636] | 93 | */ |
---|
| 94 | void Camera::setFovy(float fovy) |
---|
| 95 | { |
---|
| 96 | this->fovy = fovy; |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | /** |
---|
[4992] | 100 | * Sets a new clipping region |
---|
| 101 | * @param nearClip The near clip plane |
---|
| 102 | * @param farClip The far clip plane |
---|
[3636] | 103 | */ |
---|
| 104 | void Camera::setClipRegion(float nearClip, float farClip) |
---|
| 105 | { |
---|
| 106 | this->nearClip = nearClip; |
---|
| 107 | this->farClip = farClip; |
---|
| 108 | } |
---|
| 109 | |
---|
[4490] | 110 | /** |
---|
[4836] | 111 | * sets the new VideoMode and initializes iteration to it. |
---|
| 112 | * @param mode the mode to change to. |
---|
[4490] | 113 | */ |
---|
[3639] | 114 | void Camera::setViewMode(ViewMode mode) |
---|
| 115 | { |
---|
| 116 | switch (mode) |
---|
| 117 | { |
---|
| 118 | default: |
---|
| 119 | case VIEW_NORMAL: |
---|
| 120 | this->toFovy = 60.0; |
---|
[4992] | 121 | this->softReparent("TrackNode"); |
---|
| 122 | this->target->softReparent("TrackNode"); |
---|
| 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")) |
---|
| 132 | this->target->softReparent("TrackNode"); |
---|
| 133 | else |
---|
| 134 | this->target->softReparent("Player"); |
---|
[4992] | 135 | this->getParent()->debug(0); |
---|
[4989] | 136 | |
---|
[4988] | 137 | // this->setParent("main-Turret"); |
---|
| 138 | // this->setParentMode(PNODE_ALL); |
---|
| 139 | // this->target->softReparent("Player"); |
---|
| 140 | |
---|
[3639] | 141 | break; |
---|
| 142 | case VIEW_FRONT: |
---|
[4992] | 143 | this->toFovy = 120.0; |
---|
[5000] | 144 | this->softReparent("Player"); |
---|
| 145 | this->target->softReparent("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; |
---|
[4992] | 152 | this->softReparent("TrackNode"); |
---|
| 153 | this->target->softReparent("TrackNode"); |
---|
| 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; |
---|
[4992] | 159 | this->softReparent("TrackNode"); |
---|
| 160 | this->target->softReparent("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; |
---|
[4992] | 166 | this->softReparent("TrackNode"); |
---|
| 167 | this->target->softReparent("TrackNode"); |
---|
[4987] | 168 | this->setRelCoorSoft(Vector(0, 10, 0)); |
---|
[4992] | 169 | this->target->setRelCoorSoft(0,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 | { |
---|
[3645] | 180 | float tmpFovy = (this->toFovy - this->fovy) * dt; |
---|
| 181 | if (tmpFovy > .001) |
---|
| 182 | this->fovy += (this->toFovy - this->fovy) * 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 | { |
---|
[5007] | 194 | this->target->getParent()->debugDraw(0, 2); |
---|
[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); |
---|
| 217 | } |
---|
| 218 | |
---|
[4490] | 219 | /** |
---|
[4836] | 220 | * processes an event |
---|
| 221 | * @param event: the event to process |
---|
[4490] | 222 | */ |
---|
[4414] | 223 | void Camera::process(const Event &event) |
---|
| 224 | { |
---|
| 225 | if( event.type == KeyMapper::PEV_VIEW0) |
---|
| 226 | { |
---|
| 227 | this->setViewMode(VIEW_NORMAL); |
---|
| 228 | } |
---|
| 229 | else if( event.type == KeyMapper::PEV_VIEW1) |
---|
| 230 | { |
---|
| 231 | this->setViewMode(VIEW_BEHIND); |
---|
| 232 | } |
---|
| 233 | else if( event.type == KeyMapper::PEV_VIEW2) |
---|
| 234 | { |
---|
| 235 | this->setViewMode(VIEW_FRONT); |
---|
| 236 | } |
---|
| 237 | else if( event.type == KeyMapper::PEV_VIEW3) |
---|
| 238 | { |
---|
| 239 | this->setViewMode(VIEW_LEFT); |
---|
| 240 | } |
---|
| 241 | else if( event.type == KeyMapper::PEV_VIEW4) |
---|
| 242 | { |
---|
| 243 | this->setViewMode(VIEW_RIGHT); |
---|
| 244 | } |
---|
| 245 | else if( event.type == KeyMapper::PEV_VIEW5) |
---|
| 246 | { |
---|
| 247 | this->setViewMode(VIEW_TOP); |
---|
| 248 | } |
---|
| 249 | } |
---|
[3365] | 250 | |
---|
[4414] | 251 | |
---|
[3635] | 252 | /////////////////// |
---|
| 253 | // CAMERA-TARGET // |
---|
| 254 | /////////////////// |
---|
[2636] | 255 | |
---|
| 256 | |
---|
[3635] | 257 | CameraTarget::CameraTarget() |
---|
[2636] | 258 | { |
---|
[4320] | 259 | this->setClassID(CL_CAMERA_TARGET, "CameraTarget"); |
---|
[4997] | 260 | // this->setParentMode(PNODE_MOVEMENT); |
---|
[2636] | 261 | } |
---|
[3213] | 262 | |
---|