[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" |
---|
[9406] | 19 | #include "glincl.h" |
---|
[10065] | 20 | #include "vector.h" |
---|
| 21 | #include <iostream.h> |
---|
[3608] | 22 | |
---|
[10065] | 23 | |
---|
[9869] | 24 | ObjectListDefinition(Camera); |
---|
| 25 | |
---|
[2096] | 26 | /** |
---|
[4836] | 27 | * creates a Camera |
---|
[2096] | 28 | */ |
---|
[4746] | 29 | Camera::Camera() |
---|
[2068] | 30 | { |
---|
[9869] | 31 | this->registerObject(this, Camera::_objectList); |
---|
[4987] | 32 | this->setName("camera"); |
---|
[3635] | 33 | this->target = new CameraTarget(); |
---|
[3636] | 34 | |
---|
[7868] | 35 | this->subscribeEvent(ES_GAME, KeyMapper::PEV_VIEW0); |
---|
| 36 | this->subscribeEvent(ES_GAME, KeyMapper::PEV_VIEW1); |
---|
| 37 | this->subscribeEvent(ES_GAME, KeyMapper::PEV_VIEW2); |
---|
| 38 | this->subscribeEvent(ES_GAME, KeyMapper::PEV_VIEW3); |
---|
| 39 | this->subscribeEvent(ES_GAME, KeyMapper::PEV_VIEW4); |
---|
| 40 | this->subscribeEvent(ES_GAME, KeyMapper::PEV_VIEW5); |
---|
[4414] | 41 | |
---|
[3641] | 42 | this->setFovy(90); |
---|
[3636] | 43 | this->setAspectRatio(1.2f); |
---|
[9235] | 44 | this->setClipRegion(.1, 10000); |
---|
[3641] | 45 | |
---|
[7347] | 46 | this->setViewMode(Camera::ViewNormal); |
---|
[4987] | 47 | |
---|
[5004] | 48 | this->setParentMode(PNODE_ALL); |
---|
[2068] | 49 | } |
---|
| 50 | |
---|
[2096] | 51 | /** |
---|
[4836] | 52 | * default destructor |
---|
[2096] | 53 | */ |
---|
[4746] | 54 | Camera::~Camera() |
---|
[6424] | 55 | {} |
---|
[3543] | 56 | |
---|
| 57 | /** |
---|
[4836] | 58 | * focuses the Camera onto a Target |
---|
| 59 | * @param target the new PNode the Camera should look at. |
---|
[2096] | 60 | */ |
---|
[3635] | 61 | void Camera::lookAt(PNode* target) |
---|
[2068] | 62 | { |
---|
[3635] | 63 | this->target->setParent(target); |
---|
[2068] | 64 | } |
---|
| 65 | |
---|
[3638] | 66 | /** |
---|
[4836] | 67 | * @returns The PNode of the Target (from there you can get position and so on |
---|
[3638] | 68 | */ |
---|
[7014] | 69 | PNode* Camera::getTargetNode() const |
---|
[2068] | 70 | { |
---|
[3635] | 71 | return (PNode*)this->target; |
---|
[2068] | 72 | } |
---|
| 73 | |
---|
[2096] | 74 | /** |
---|
[4836] | 75 | * sets a new AspectRatio |
---|
| 76 | * @param aspectRatio the new aspect ratio to set (width / height) |
---|
[3636] | 77 | */ |
---|
| 78 | void Camera::setAspectRatio(float aspectRatio) |
---|
| 79 | { |
---|
| 80 | this->aspectRatio = aspectRatio; |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | /** |
---|
[4992] | 84 | * Sets a new clipping region |
---|
| 85 | * @param nearClip The near clip plane |
---|
| 86 | * @param farClip The far clip plane |
---|
[3636] | 87 | */ |
---|
| 88 | void Camera::setClipRegion(float nearClip, float farClip) |
---|
| 89 | { |
---|
| 90 | this->nearClip = nearClip; |
---|
| 91 | this->farClip = farClip; |
---|
| 92 | } |
---|
| 93 | |
---|
[4490] | 94 | /** |
---|
[4836] | 95 | * sets the new VideoMode and initializes iteration to it. |
---|
| 96 | * @param mode the mode to change to. |
---|
[4490] | 97 | */ |
---|
[3639] | 98 | void Camera::setViewMode(ViewMode mode) |
---|
| 99 | { |
---|
[6034] | 100 | currentMode = mode; |
---|
[3639] | 101 | switch (mode) |
---|
[6424] | 102 | { |
---|
[3639] | 103 | default: |
---|
[7347] | 104 | case Camera::ViewNormal: |
---|
[3639] | 105 | this->toFovy = 60.0; |
---|
[4992] | 106 | this->setRelCoorSoft(-10, 5, 0); |
---|
| 107 | this->target->setRelCoorSoft(0,0,0); |
---|
[3639] | 108 | break; |
---|
[7347] | 109 | case Camera::ViewBehind: |
---|
[3639] | 110 | break; |
---|
[7347] | 111 | case Camera::ViewFront: |
---|
[4992] | 112 | this->toFovy = 120.0; |
---|
[6424] | 113 | this->setRelCoorSoft(4, 0, 0, 5); |
---|
| 114 | this->target->setRelCoorSoft(Vector(10,0,0), 5); |
---|
[3639] | 115 | break; |
---|
[7347] | 116 | case Camera::ViewLeft: |
---|
[3639] | 117 | this->toFovy = 90; |
---|
[4992] | 118 | this->setRelCoorSoft(0, 1, -10, .5); |
---|
| 119 | this->target->setRelCoorSoft(0,0,0); |
---|
[3639] | 120 | break; |
---|
[7347] | 121 | case Camera::ViewRight: |
---|
[3639] | 122 | this->toFovy = 90; |
---|
[4987] | 123 | this->setRelCoorSoft(Vector(0, 1, 10)); |
---|
[4992] | 124 | this->target->setRelCoorSoft(0,0,0); |
---|
[3639] | 125 | break; |
---|
[7347] | 126 | case Camera::ViewTop: |
---|
[3643] | 127 | this->toFovy= 120; |
---|
[5751] | 128 | this->setRelCoorSoft(Vector(30, 50, 0)); |
---|
| 129 | this->target->setRelCoorSoft(35,0,0); |
---|
[6424] | 130 | } |
---|
[3639] | 131 | } |
---|
| 132 | |
---|
| 133 | |
---|
[3636] | 134 | /** |
---|
[4836] | 135 | * Updates the position of the camera. |
---|
| 136 | * @param dt: The time that elapsed. |
---|
[3639] | 137 | */ |
---|
| 138 | void Camera::tick(float dt) |
---|
| 139 | { |
---|
[7009] | 140 | //update frustum plane |
---|
[7014] | 141 | this->viewVector = (this->target->getAbsCoor() - this->getAbsCoor()).getNormalized(); |
---|
| 142 | this->frustumPlane = Plane(this->viewVector, this->getAbsCoor() + this->viewVector * 0.1); |
---|
[7009] | 143 | this->upVector = this->getAbsDirV(); |
---|
| 144 | |
---|
| 145 | |
---|
[10065] | 146 | |
---|
| 147 | |
---|
| 148 | |
---|
[7173] | 149 | float tmpFovy = (this->toFovy - this->fovy); |
---|
[6034] | 150 | if (tmpFovy > 0.01) |
---|
[5354] | 151 | this->fovy += tmpFovy * fabsf(dt); |
---|
[3639] | 152 | } |
---|
| 153 | |
---|
| 154 | |
---|
| 155 | /** |
---|
[4836] | 156 | * initialize rendering perspective according to this camera |
---|
[6772] | 157 | * |
---|
| 158 | * This is called immediately before the rendering cycle starts, it sets all global |
---|
| 159 | * rendering options as well as the GL_PROJECTION matrix according to the camera. |
---|
| 160 | */ |
---|
[2068] | 161 | void Camera::apply () |
---|
| 162 | { |
---|
[3636] | 163 | // switching to Projection Matrix |
---|
[2551] | 164 | glMatrixMode (GL_PROJECTION); |
---|
[2112] | 165 | glLoadIdentity (); |
---|
[3635] | 166 | |
---|
[3636] | 167 | gluPerspective(this->fovy, |
---|
[4832] | 168 | this->aspectRatio, |
---|
| 169 | this->nearClip, |
---|
| 170 | this->farClip); |
---|
[6778] | 171 | |
---|
| 172 | |
---|
| 173 | // setting up the perspective |
---|
[3636] | 174 | // speed-up feature |
---|
[7108] | 175 | glMatrixMode (GL_MODELVIEW); |
---|
| 176 | glLoadIdentity(); |
---|
| 177 | |
---|
| 178 | |
---|
| 179 | } |
---|
| 180 | |
---|
| 181 | void Camera::project() |
---|
| 182 | { |
---|
[3635] | 183 | Vector cameraPosition = this->getAbsCoor(); |
---|
| 184 | Vector targetPosition = this->target->getAbsCoor(); |
---|
[2551] | 185 | |
---|
[10065] | 186 | //Setting the Camera Eye, lookAt and up Vectors |
---|
| 187 | glLookAt(cameraPosition.x, cameraPosition.y, cameraPosition.z, |
---|
[6965] | 188 | targetPosition.x, targetPosition.y, targetPosition.z, |
---|
[7009] | 189 | this->upVector.x, this->upVector.y, this->upVector.z); |
---|
[7108] | 190 | } |
---|
[3636] | 191 | |
---|
[6965] | 192 | |
---|
[4490] | 193 | /** |
---|
[4836] | 194 | * processes an event |
---|
| 195 | * @param event: the event to process |
---|
[4490] | 196 | */ |
---|
[4414] | 197 | void Camera::process(const Event &event) |
---|
| 198 | { |
---|
| 199 | if( event.type == KeyMapper::PEV_VIEW0) |
---|
[6424] | 200 | { |
---|
[7347] | 201 | this->setViewMode(Camera::ViewNormal); |
---|
[6424] | 202 | } |
---|
[4414] | 203 | else if( event.type == KeyMapper::PEV_VIEW1) |
---|
[6424] | 204 | { |
---|
[7347] | 205 | this->setViewMode(Camera::ViewBehind); |
---|
[6424] | 206 | } |
---|
[4414] | 207 | else if( event.type == KeyMapper::PEV_VIEW2) |
---|
[6424] | 208 | { |
---|
[7347] | 209 | this->setViewMode(Camera::ViewFront); |
---|
[6424] | 210 | } |
---|
[4414] | 211 | else if( event.type == KeyMapper::PEV_VIEW3) |
---|
[6424] | 212 | { |
---|
[7347] | 213 | this->setViewMode(Camera::ViewLeft); |
---|
[6424] | 214 | } |
---|
[4414] | 215 | else if( event.type == KeyMapper::PEV_VIEW4) |
---|
[6424] | 216 | { |
---|
[7347] | 217 | this->setViewMode(Camera::ViewRight); |
---|
[6424] | 218 | } |
---|
[4414] | 219 | else if( event.type == KeyMapper::PEV_VIEW5) |
---|
[6424] | 220 | { |
---|
[7347] | 221 | this->setViewMode(Camera::ViewTop); |
---|
[6424] | 222 | } |
---|
[4414] | 223 | } |
---|
[3365] | 224 | |
---|
[10065] | 225 | /* |
---|
| 226 | Vector* Camera::translate(Vector* newPos, float speed) |
---|
| 227 | { |
---|
| 228 | glTranslatef(Vector); |
---|
[4414] | 229 | |
---|
[10065] | 230 | } |
---|
| 231 | |
---|
| 232 | |
---|
| 233 | float Camera::iterate(float dt, Vector target) |
---|
| 234 | { |
---|
| 235 | Vector tmpVec = (this->getAbsCorr() - target); |
---|
| 236 | if (tmpVec > 0.01) |
---|
| 237 | Vector ret = this->getAbsCorr; |
---|
| 238 | ret += tmpVec * fabsf(dt); |
---|
| 239 | return ret; |
---|
| 240 | } |
---|
| 241 | */ |
---|
| 242 | void Camera::Rotate() |
---|
| 243 | { |
---|
| 244 | this->fovy=this->fovy+1; |
---|
| 245 | } |
---|
| 246 | |
---|
| 247 | |
---|
| 248 | void Camera::glLookAt(float eyex, float eyey, float eyez, float centerx, float centery, float centerz, float upx, float upy, float upz) |
---|
| 249 | { |
---|
| 250 | //Vector* eye=new Vector(eyex, eyey, eyez); |
---|
| 251 | Vector* center=new Vector (centerx, centery, centerz); |
---|
| 252 | Vector* up=new Vector(upx, upy, upz); |
---|
| 253 | |
---|
| 254 | center->x-=eyex; |
---|
| 255 | center->y-=eyey; |
---|
| 256 | center->z-=eyez; |
---|
| 257 | |
---|
| 258 | center->normalize(); |
---|
| 259 | up->normalize(); |
---|
| 260 | Vector* s = VectorProd(center, up); |
---|
| 261 | Vector* u = VectorProd(s, center); |
---|
| 262 | GLfloat Matrix[]={s->x, s->y, s->z, 0, u->x, u->y, u->z, 0, -center->x, -center->y, -center->z, 0, 0, 0, 0, 1}; |
---|
| 263 | |
---|
| 264 | glMultMatrixf(Matrix); |
---|
| 265 | glTranslated(-eyex, -eyey, -eyez); |
---|
| 266 | delete center; |
---|
| 267 | delete up; |
---|
| 268 | delete s; |
---|
| 269 | delete u; |
---|
| 270 | |
---|
| 271 | } |
---|
| 272 | |
---|
| 273 | |
---|
| 274 | |
---|
| 275 | |
---|
| 276 | Vector* Camera::VectorProd(Vector* v1, Vector* v2) |
---|
| 277 | { |
---|
| 278 | Vector* temp= new Vector(); |
---|
| 279 | temp->x=v1->y * v2->z - v1->z * v2->y; |
---|
| 280 | temp->y=v1->z * v2->x - v1->x * v2->z; |
---|
| 281 | temp->z=v1->x * v2->y - v1->y * v2->x; |
---|
| 282 | return temp; |
---|
| 283 | } |
---|
| 284 | |
---|
| 285 | |
---|
| 286 | |
---|
| 287 | |
---|
| 288 | |
---|
| 289 | |
---|
| 290 | |
---|
| 291 | |
---|
| 292 | |
---|
| 293 | |
---|
| 294 | |
---|
| 295 | |
---|
| 296 | |
---|
| 297 | |
---|
[3635] | 298 | /////////////////// |
---|
| 299 | // CAMERA-TARGET // |
---|
| 300 | /////////////////// |
---|
[2636] | 301 | |
---|
[9869] | 302 | ObjectListDefinition(CameraTarget); |
---|
[3635] | 303 | CameraTarget::CameraTarget() |
---|
[2636] | 304 | { |
---|
[9869] | 305 | this->registerObject(this, CameraTarget::_objectList); |
---|
[6424] | 306 | // this->setParentMode(PNODE_MOVEMENT); |
---|
[10065] | 307 | |
---|
| 308 | |
---|
[2636] | 309 | } |
---|
[3213] | 310 | |
---|
[10065] | 311 | |
---|
| 312 | CameraTarget::detach() |
---|
| 313 | { |
---|
| 314 | State::getCameraTargetNode()->setParentSoft(NULL); |
---|
| 315 | } |
---|
| 316 | |
---|
| 317 | |
---|
| 318 | |
---|
| 319 | |
---|
| 320 | |
---|