[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" |
---|
[7868] | 18 | #include "key_mapper.h" |
---|
[3608] | 19 | |
---|
[2096] | 20 | /** |
---|
[4836] | 21 | * creates a Camera |
---|
[2096] | 22 | */ |
---|
[4746] | 23 | Camera::Camera() |
---|
[2068] | 24 | { |
---|
[4320] | 25 | this->setClassID(CL_CAMERA, "Camera"); |
---|
[4987] | 26 | this->setName("camera"); |
---|
[3635] | 27 | this->target = new CameraTarget(); |
---|
[3636] | 28 | |
---|
[7868] | 29 | this->subscribeEvent(ES_GAME, KeyMapper::PEV_VIEW0); |
---|
| 30 | this->subscribeEvent(ES_GAME, KeyMapper::PEV_VIEW1); |
---|
| 31 | this->subscribeEvent(ES_GAME, KeyMapper::PEV_VIEW2); |
---|
| 32 | this->subscribeEvent(ES_GAME, KeyMapper::PEV_VIEW3); |
---|
| 33 | this->subscribeEvent(ES_GAME, KeyMapper::PEV_VIEW4); |
---|
| 34 | this->subscribeEvent(ES_GAME, KeyMapper::PEV_VIEW5); |
---|
[4414] | 35 | |
---|
[3641] | 36 | this->setFovy(90); |
---|
[3636] | 37 | this->setAspectRatio(1.2f); |
---|
| 38 | this->setClipRegion(.1, 2000); |
---|
[3641] | 39 | |
---|
[7347] | 40 | this->setViewMode(Camera::ViewNormal); |
---|
[4987] | 41 | |
---|
[5004] | 42 | this->setParentMode(PNODE_ALL); |
---|
[2068] | 43 | } |
---|
| 44 | |
---|
[2096] | 45 | /** |
---|
[4836] | 46 | * default destructor |
---|
[2096] | 47 | */ |
---|
[4746] | 48 | Camera::~Camera() |
---|
[6424] | 49 | {} |
---|
[3543] | 50 | |
---|
| 51 | /** |
---|
[4836] | 52 | * focuses the Camera onto a Target |
---|
| 53 | * @param target the new PNode the Camera should look at. |
---|
[2096] | 54 | */ |
---|
[3635] | 55 | void Camera::lookAt(PNode* target) |
---|
[2068] | 56 | { |
---|
[3635] | 57 | this->target->setParent(target); |
---|
[2068] | 58 | } |
---|
| 59 | |
---|
[3638] | 60 | /** |
---|
[4836] | 61 | * @returns The PNode of the Target (from there you can get position and so on |
---|
[3638] | 62 | */ |
---|
[7014] | 63 | PNode* Camera::getTargetNode() const |
---|
[2068] | 64 | { |
---|
[3635] | 65 | return (PNode*)this->target; |
---|
[2068] | 66 | } |
---|
| 67 | |
---|
[2096] | 68 | /** |
---|
[4836] | 69 | * sets a new AspectRatio |
---|
| 70 | * @param aspectRatio the new aspect ratio to set (width / height) |
---|
[3636] | 71 | */ |
---|
| 72 | void Camera::setAspectRatio(float aspectRatio) |
---|
| 73 | { |
---|
| 74 | this->aspectRatio = aspectRatio; |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | /** |
---|
[4992] | 78 | * Sets a new clipping region |
---|
| 79 | * @param nearClip The near clip plane |
---|
| 80 | * @param farClip The far clip plane |
---|
[3636] | 81 | */ |
---|
| 82 | void Camera::setClipRegion(float nearClip, float farClip) |
---|
| 83 | { |
---|
| 84 | this->nearClip = nearClip; |
---|
| 85 | this->farClip = farClip; |
---|
| 86 | } |
---|
| 87 | |
---|
[4490] | 88 | /** |
---|
[4836] | 89 | * sets the new VideoMode and initializes iteration to it. |
---|
| 90 | * @param mode the mode to change to. |
---|
[4490] | 91 | */ |
---|
[3639] | 92 | void Camera::setViewMode(ViewMode mode) |
---|
| 93 | { |
---|
[6034] | 94 | currentMode = mode; |
---|
[3639] | 95 | switch (mode) |
---|
[6424] | 96 | { |
---|
[3639] | 97 | default: |
---|
[7347] | 98 | case Camera::ViewNormal: |
---|
[3639] | 99 | this->toFovy = 60.0; |
---|
[4992] | 100 | this->setRelCoorSoft(-10, 5, 0); |
---|
| 101 | this->target->setRelCoorSoft(0,0,0); |
---|
[3639] | 102 | break; |
---|
[7347] | 103 | case Camera::ViewBehind: |
---|
[3639] | 104 | break; |
---|
[7347] | 105 | case Camera::ViewFront: |
---|
[4992] | 106 | this->toFovy = 120.0; |
---|
[6424] | 107 | this->setRelCoorSoft(4, 0, 0, 5); |
---|
| 108 | this->target->setRelCoorSoft(Vector(10,0,0), 5); |
---|
[3639] | 109 | break; |
---|
[7347] | 110 | case Camera::ViewLeft: |
---|
[3639] | 111 | this->toFovy = 90; |
---|
[4992] | 112 | this->setRelCoorSoft(0, 1, -10, .5); |
---|
| 113 | this->target->setRelCoorSoft(0,0,0); |
---|
[3639] | 114 | break; |
---|
[7347] | 115 | case Camera::ViewRight: |
---|
[3639] | 116 | this->toFovy = 90; |
---|
[4987] | 117 | this->setRelCoorSoft(Vector(0, 1, 10)); |
---|
[4992] | 118 | this->target->setRelCoorSoft(0,0,0); |
---|
[3639] | 119 | break; |
---|
[7347] | 120 | case Camera::ViewTop: |
---|
[3643] | 121 | this->toFovy= 120; |
---|
[5751] | 122 | this->setRelCoorSoft(Vector(30, 50, 0)); |
---|
| 123 | this->target->setRelCoorSoft(35,0,0); |
---|
[6424] | 124 | } |
---|
[3639] | 125 | } |
---|
| 126 | |
---|
| 127 | |
---|
[3636] | 128 | /** |
---|
[4836] | 129 | * Updates the position of the camera. |
---|
| 130 | * @param dt: The time that elapsed. |
---|
[3639] | 131 | */ |
---|
| 132 | void Camera::tick(float dt) |
---|
| 133 | { |
---|
[7009] | 134 | //update frustum plane |
---|
[7014] | 135 | this->viewVector = (this->target->getAbsCoor() - this->getAbsCoor()).getNormalized(); |
---|
| 136 | this->frustumPlane = Plane(this->viewVector, this->getAbsCoor() + this->viewVector * 0.1); |
---|
[7009] | 137 | |
---|
| 138 | this->upVector = this->getAbsDirV(); |
---|
| 139 | |
---|
| 140 | |
---|
[7173] | 141 | float tmpFovy = (this->toFovy - this->fovy); |
---|
[6034] | 142 | if (tmpFovy > 0.01) |
---|
[5354] | 143 | this->fovy += tmpFovy * fabsf(dt); |
---|
[3639] | 144 | } |
---|
| 145 | |
---|
| 146 | |
---|
| 147 | /** |
---|
[4836] | 148 | * initialize rendering perspective according to this camera |
---|
[6772] | 149 | * |
---|
| 150 | * This is called immediately before the rendering cycle starts, it sets all global |
---|
| 151 | * rendering options as well as the GL_PROJECTION matrix according to the camera. |
---|
| 152 | */ |
---|
[2068] | 153 | void Camera::apply () |
---|
| 154 | { |
---|
[3636] | 155 | // switching to Projection Matrix |
---|
[2551] | 156 | glMatrixMode (GL_PROJECTION); |
---|
[2112] | 157 | glLoadIdentity (); |
---|
[3635] | 158 | |
---|
[3636] | 159 | gluPerspective(this->fovy, |
---|
[4832] | 160 | this->aspectRatio, |
---|
| 161 | this->nearClip, |
---|
| 162 | this->farClip); |
---|
[6778] | 163 | |
---|
| 164 | |
---|
| 165 | // setting up the perspective |
---|
[3636] | 166 | // speed-up feature |
---|
[7108] | 167 | glMatrixMode (GL_MODELVIEW); |
---|
| 168 | glLoadIdentity(); |
---|
| 169 | |
---|
| 170 | |
---|
| 171 | } |
---|
| 172 | |
---|
| 173 | void Camera::project() |
---|
| 174 | { |
---|
[3635] | 175 | Vector cameraPosition = this->getAbsCoor(); |
---|
| 176 | Vector targetPosition = this->target->getAbsCoor(); |
---|
[2551] | 177 | |
---|
[7108] | 178 | // Setting the Camera Eye, lookAt and up Vectors |
---|
[7009] | 179 | gluLookAt(cameraPosition.x, cameraPosition.y, cameraPosition.z, |
---|
[6965] | 180 | targetPosition.x, targetPosition.y, targetPosition.z, |
---|
[7009] | 181 | this->upVector.x, this->upVector.y, this->upVector.z); |
---|
[7108] | 182 | } |
---|
[3636] | 183 | |
---|
[6965] | 184 | |
---|
[4490] | 185 | /** |
---|
[4836] | 186 | * processes an event |
---|
| 187 | * @param event: the event to process |
---|
[4490] | 188 | */ |
---|
[4414] | 189 | void Camera::process(const Event &event) |
---|
| 190 | { |
---|
| 191 | if( event.type == KeyMapper::PEV_VIEW0) |
---|
[6424] | 192 | { |
---|
[7347] | 193 | this->setViewMode(Camera::ViewNormal); |
---|
[6424] | 194 | } |
---|
[4414] | 195 | else if( event.type == KeyMapper::PEV_VIEW1) |
---|
[6424] | 196 | { |
---|
[7347] | 197 | this->setViewMode(Camera::ViewBehind); |
---|
[6424] | 198 | } |
---|
[4414] | 199 | else if( event.type == KeyMapper::PEV_VIEW2) |
---|
[6424] | 200 | { |
---|
[7347] | 201 | this->setViewMode(Camera::ViewFront); |
---|
[6424] | 202 | } |
---|
[4414] | 203 | else if( event.type == KeyMapper::PEV_VIEW3) |
---|
[6424] | 204 | { |
---|
[7347] | 205 | this->setViewMode(Camera::ViewLeft); |
---|
[6424] | 206 | } |
---|
[4414] | 207 | else if( event.type == KeyMapper::PEV_VIEW4) |
---|
[6424] | 208 | { |
---|
[7347] | 209 | this->setViewMode(Camera::ViewRight); |
---|
[6424] | 210 | } |
---|
[4414] | 211 | else if( event.type == KeyMapper::PEV_VIEW5) |
---|
[6424] | 212 | { |
---|
[7347] | 213 | this->setViewMode(Camera::ViewTop); |
---|
[6424] | 214 | } |
---|
[4414] | 215 | } |
---|
[3365] | 216 | |
---|
[4414] | 217 | |
---|
[3635] | 218 | /////////////////// |
---|
| 219 | // CAMERA-TARGET // |
---|
| 220 | /////////////////// |
---|
[2636] | 221 | |
---|
| 222 | |
---|
[3635] | 223 | CameraTarget::CameraTarget() |
---|
[2636] | 224 | { |
---|
[4320] | 225 | this->setClassID(CL_CAMERA_TARGET, "CameraTarget"); |
---|
[6424] | 226 | // this->setParentMode(PNODE_MOVEMENT); |
---|
[2636] | 227 | } |
---|
[3213] | 228 | |
---|