[2068] | 1 | |
---|
| 2 | |
---|
| 3 | /* |
---|
| 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 | /** |
---|
| 33 | \brief creates a Camera |
---|
| 34 | */ |
---|
[3635] | 35 | Camera::Camera(void) |
---|
[2068] | 36 | { |
---|
[4320] | 37 | this->setClassID(CL_CAMERA, "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); |
---|
[2068] | 52 | } |
---|
| 53 | |
---|
[2096] | 54 | /** |
---|
| 55 | \brief default destructor |
---|
| 56 | */ |
---|
[3635] | 57 | Camera::~Camera(void) |
---|
[2068] | 58 | { |
---|
[4420] | 59 | EventHandler::getInstance()->unsubscribe(this); |
---|
[3543] | 60 | } |
---|
| 61 | |
---|
| 62 | /** |
---|
[3635] | 63 | \brief focuses the Camera onto a Target |
---|
| 64 | \param target the new PNode the Camera should look at. |
---|
[2096] | 65 | */ |
---|
[3635] | 66 | void Camera::lookAt(PNode* target) |
---|
[2068] | 67 | { |
---|
[3635] | 68 | this->target->setParent(target); |
---|
[2068] | 69 | } |
---|
| 70 | |
---|
[3638] | 71 | /** |
---|
| 72 | \returns The PNode of the Target (from there you can get position and so on |
---|
| 73 | */ |
---|
[3635] | 74 | PNode* Camera::getTarget(void) |
---|
[2068] | 75 | { |
---|
[3635] | 76 | return (PNode*)this->target; |
---|
[2068] | 77 | } |
---|
| 78 | |
---|
[2096] | 79 | /** |
---|
[3636] | 80 | \brief sets a new AspectRatio |
---|
| 81 | \param aspectRatio the new aspect ratio to set (width / height) |
---|
| 82 | */ |
---|
| 83 | void Camera::setAspectRatio(float aspectRatio) |
---|
| 84 | { |
---|
| 85 | this->aspectRatio = aspectRatio; |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | /** |
---|
| 89 | \brief sets the Field of View to fofy |
---|
| 90 | \param fovy new field of view factor (in degrees) |
---|
| 91 | */ |
---|
| 92 | void Camera::setFovy(float fovy) |
---|
| 93 | { |
---|
| 94 | this->fovy = fovy; |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | /** |
---|
| 98 | \brief Sets a new clipping region |
---|
| 99 | \param nearClip The near clip plane |
---|
| 100 | \param farClip The far clip plane |
---|
| 101 | */ |
---|
| 102 | void Camera::setClipRegion(float nearClip, float farClip) |
---|
| 103 | { |
---|
| 104 | this->nearClip = nearClip; |
---|
| 105 | this->farClip = farClip; |
---|
| 106 | } |
---|
| 107 | |
---|
[4490] | 108 | /** |
---|
| 109 | \brief sets the new VideoMode and initializes iteration to it. |
---|
| 110 | \param mode the mode to change to. |
---|
| 111 | */ |
---|
[3639] | 112 | void Camera::setViewMode(ViewMode mode) |
---|
| 113 | { |
---|
| 114 | switch (mode) |
---|
| 115 | { |
---|
| 116 | default: |
---|
| 117 | case VIEW_NORMAL: |
---|
| 118 | this->toFovy = 60.0; |
---|
[3643] | 119 | this->toRelCoor = Vector(-10, 5, 0); |
---|
[3639] | 120 | break; |
---|
| 121 | case VIEW_BEHIND: |
---|
[3643] | 122 | this->toFovy = 120.0; |
---|
| 123 | this->toRelCoor = Vector(-7, 0, 0); |
---|
[3639] | 124 | break; |
---|
| 125 | case VIEW_FRONT: |
---|
| 126 | this->toFovy = 95.0; |
---|
[3643] | 127 | this->toRelCoor = Vector(12, 5, 0); |
---|
[3639] | 128 | break; |
---|
| 129 | case VIEW_LEFT: |
---|
| 130 | this->toFovy = 90; |
---|
[3643] | 131 | this->toRelCoor = Vector(0, 2, -10); |
---|
[3639] | 132 | break; |
---|
| 133 | case VIEW_RIGHT: |
---|
| 134 | this->toFovy = 90; |
---|
[3643] | 135 | this->toRelCoor = Vector(0, 2, 10); |
---|
[3639] | 136 | break; |
---|
[3643] | 137 | case VIEW_TOP: |
---|
| 138 | this->toFovy= 120; |
---|
| 139 | this->toRelCoor = Vector(0, 4, 0); |
---|
[3639] | 140 | } |
---|
| 141 | } |
---|
| 142 | |
---|
| 143 | |
---|
[3636] | 144 | /** |
---|
[3639] | 145 | \brief Updates the position of the camera. |
---|
[4490] | 146 | \param dt: The time that elapsed. |
---|
[3639] | 147 | */ |
---|
| 148 | void Camera::tick(float dt) |
---|
| 149 | { |
---|
[3643] | 150 | dt /= 500; |
---|
[3645] | 151 | float tmpFovy = (this->toFovy - this->fovy) * dt; |
---|
| 152 | if (tmpFovy > .001) |
---|
| 153 | this->fovy += (this->toFovy - this->fovy) * dt; |
---|
[3966] | 154 | Vector tmpPos = (this->toRelCoor - this->getRelCoor()) * dt; |
---|
[3645] | 155 | if (tmpPos.len() >= .001) |
---|
| 156 | { |
---|
[3966] | 157 | tmpPos = tmpPos + this->getRelCoor(); |
---|
[3810] | 158 | this->setRelCoor(tmpPos); |
---|
[3645] | 159 | } |
---|
[3639] | 160 | } |
---|
| 161 | |
---|
| 162 | |
---|
| 163 | /** |
---|
[2551] | 164 | \brief initialize rendering perspective according to this camera |
---|
[2096] | 165 | |
---|
[2551] | 166 | This is called immediately before the rendering cycle starts, it sets all global |
---|
| 167 | rendering options as well as the GL_PROJECTION matrix according to the camera. |
---|
[2096] | 168 | */ |
---|
[2068] | 169 | void Camera::apply () |
---|
| 170 | { |
---|
[3636] | 171 | // switching to Projection Matrix |
---|
[2551] | 172 | glMatrixMode (GL_PROJECTION); |
---|
[2112] | 173 | glLoadIdentity (); |
---|
[3635] | 174 | |
---|
[3636] | 175 | // setting up the perspective |
---|
| 176 | gluPerspective(this->fovy, |
---|
| 177 | this->aspectRatio, |
---|
| 178 | this->nearClip, |
---|
| 179 | this->farClip); |
---|
[3365] | 180 | |
---|
[3636] | 181 | // speed-up feature |
---|
[3635] | 182 | Vector cameraPosition = this->getAbsCoor(); |
---|
| 183 | Vector targetPosition = this->target->getAbsCoor(); |
---|
[3638] | 184 | Vector up = Vector(0, 1, 0); |
---|
| 185 | up = this->getAbsDir().apply(up); |
---|
[2551] | 186 | |
---|
[3636] | 187 | // Setting the Camera Eye, lookAt and up Vectors |
---|
[3635] | 188 | gluLookAt(cameraPosition.x, cameraPosition.y, cameraPosition.z, |
---|
| 189 | targetPosition.x, targetPosition.y, targetPosition.z, |
---|
[3638] | 190 | up.x, up.y, up.z); |
---|
[3636] | 191 | |
---|
| 192 | // switching back to Modeling Matrix |
---|
[2068] | 193 | glMatrixMode (GL_MODELVIEW); |
---|
| 194 | } |
---|
| 195 | |
---|
[4490] | 196 | /** |
---|
| 197 | \brief processes an event |
---|
| 198 | \param event: the event to process |
---|
| 199 | */ |
---|
[4414] | 200 | void Camera::process(const Event &event) |
---|
| 201 | { |
---|
| 202 | if( event.type == KeyMapper::PEV_VIEW0) |
---|
| 203 | { |
---|
| 204 | this->setViewMode(VIEW_NORMAL); |
---|
| 205 | } |
---|
| 206 | else if( event.type == KeyMapper::PEV_VIEW1) |
---|
| 207 | { |
---|
| 208 | this->setViewMode(VIEW_BEHIND); |
---|
| 209 | } |
---|
| 210 | else if( event.type == KeyMapper::PEV_VIEW2) |
---|
| 211 | { |
---|
| 212 | this->setViewMode(VIEW_FRONT); |
---|
| 213 | } |
---|
| 214 | else if( event.type == KeyMapper::PEV_VIEW3) |
---|
| 215 | { |
---|
| 216 | this->setViewMode(VIEW_LEFT); |
---|
| 217 | } |
---|
| 218 | else if( event.type == KeyMapper::PEV_VIEW4) |
---|
| 219 | { |
---|
| 220 | this->setViewMode(VIEW_RIGHT); |
---|
| 221 | } |
---|
| 222 | else if( event.type == KeyMapper::PEV_VIEW5) |
---|
| 223 | { |
---|
| 224 | this->setViewMode(VIEW_TOP); |
---|
| 225 | } |
---|
| 226 | } |
---|
[3365] | 227 | |
---|
[4414] | 228 | |
---|
[3635] | 229 | /////////////////// |
---|
| 230 | // CAMERA-TARGET // |
---|
| 231 | /////////////////// |
---|
[2636] | 232 | |
---|
| 233 | |
---|
[3635] | 234 | CameraTarget::CameraTarget() |
---|
[2636] | 235 | { |
---|
[4320] | 236 | this->setClassID(CL_CAMERA_TARGET, "CameraTarget"); |
---|
[4444] | 237 | this->setParentMode(PNODE_MOVEMENT); |
---|
[2636] | 238 | } |
---|
[3213] | 239 | |
---|
[3635] | 240 | CameraTarget::~CameraTarget() |
---|
| 241 | { |
---|
[3213] | 242 | |
---|
[3635] | 243 | } |
---|