[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" |
---|
[10122] | 21 | #include "targets.h" |
---|
[3608] | 22 | |
---|
[10128] | 23 | |
---|
[10131] | 24 | |
---|
[10236] | 25 | |
---|
[9869] | 26 | ObjectListDefinition(Camera); |
---|
| 27 | |
---|
[10068] | 28 | |
---|
[2096] | 29 | /** |
---|
[4836] | 30 | * creates a Camera |
---|
[2096] | 31 | */ |
---|
[4746] | 32 | Camera::Camera() |
---|
[2068] | 33 | { |
---|
[9869] | 34 | this->registerObject(this, Camera::_objectList); |
---|
[4987] | 35 | this->setName("camera"); |
---|
[3635] | 36 | this->target = new CameraTarget(); |
---|
[10238] | 37 | this->target->masta=this; |
---|
[7868] | 38 | this->subscribeEvent(ES_GAME, KeyMapper::PEV_VIEW0); |
---|
| 39 | this->subscribeEvent(ES_GAME, KeyMapper::PEV_VIEW1); |
---|
| 40 | this->subscribeEvent(ES_GAME, KeyMapper::PEV_VIEW2); |
---|
| 41 | this->subscribeEvent(ES_GAME, KeyMapper::PEV_VIEW3); |
---|
| 42 | this->subscribeEvent(ES_GAME, KeyMapper::PEV_VIEW4); |
---|
| 43 | this->subscribeEvent(ES_GAME, KeyMapper::PEV_VIEW5); |
---|
[4414] | 44 | |
---|
[3641] | 45 | this->setFovy(90); |
---|
[3636] | 46 | this->setAspectRatio(1.2f); |
---|
[9235] | 47 | this->setClipRegion(.1, 10000); |
---|
[3641] | 48 | |
---|
[7347] | 49 | this->setViewMode(Camera::ViewNormal); |
---|
[4987] | 50 | |
---|
[5004] | 51 | this->setParentMode(PNODE_ALL); |
---|
[2068] | 52 | } |
---|
| 53 | |
---|
[2096] | 54 | /** |
---|
[4836] | 55 | * default destructor |
---|
[2096] | 56 | */ |
---|
[4746] | 57 | Camera::~Camera() |
---|
[6424] | 58 | {} |
---|
[3543] | 59 | |
---|
| 60 | /** |
---|
[4836] | 61 | * focuses the Camera onto a Target |
---|
| 62 | * @param target the new PNode the Camera should look at. |
---|
[2096] | 63 | */ |
---|
[3635] | 64 | void Camera::lookAt(PNode* target) |
---|
[2068] | 65 | { |
---|
[3635] | 66 | this->target->setParent(target); |
---|
[2068] | 67 | } |
---|
| 68 | |
---|
[3638] | 69 | /** |
---|
[4836] | 70 | * @returns The PNode of the Target (from there you can get position and so on |
---|
[3638] | 71 | */ |
---|
[7014] | 72 | PNode* Camera::getTargetNode() const |
---|
[2068] | 73 | { |
---|
[3635] | 74 | return (PNode*)this->target; |
---|
[2068] | 75 | } |
---|
| 76 | |
---|
[10204] | 77 | void Camera::setTargetNode(PNode* target) |
---|
| 78 | { |
---|
| 79 | this->target->setParent(target); |
---|
| 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 | /** |
---|
[4992] | 92 | * Sets a new clipping region |
---|
| 93 | * @param nearClip The near clip plane |
---|
| 94 | * @param farClip The far clip plane |
---|
[3636] | 95 | */ |
---|
| 96 | void Camera::setClipRegion(float nearClip, float farClip) |
---|
| 97 | { |
---|
| 98 | this->nearClip = nearClip; |
---|
| 99 | this->farClip = farClip; |
---|
| 100 | } |
---|
| 101 | |
---|
[4490] | 102 | /** |
---|
[4836] | 103 | * sets the new VideoMode and initializes iteration to it. |
---|
| 104 | * @param mode the mode to change to. |
---|
[4490] | 105 | */ |
---|
[3639] | 106 | void Camera::setViewMode(ViewMode mode) |
---|
| 107 | { |
---|
[6034] | 108 | currentMode = mode; |
---|
[3639] | 109 | switch (mode) |
---|
[6424] | 110 | { |
---|
[3639] | 111 | default: |
---|
[7347] | 112 | case Camera::ViewNormal: |
---|
[3639] | 113 | this->toFovy = 60.0; |
---|
[4992] | 114 | this->setRelCoorSoft(-10, 5, 0); |
---|
| 115 | this->target->setRelCoorSoft(0,0,0); |
---|
[3639] | 116 | break; |
---|
[7347] | 117 | case Camera::ViewBehind: |
---|
[3639] | 118 | break; |
---|
[7347] | 119 | case Camera::ViewFront: |
---|
[4992] | 120 | this->toFovy = 120.0; |
---|
[6424] | 121 | this->setRelCoorSoft(4, 0, 0, 5); |
---|
| 122 | this->target->setRelCoorSoft(Vector(10,0,0), 5); |
---|
[3639] | 123 | break; |
---|
[7347] | 124 | case Camera::ViewLeft: |
---|
[3639] | 125 | this->toFovy = 90; |
---|
[4992] | 126 | this->setRelCoorSoft(0, 1, -10, .5); |
---|
| 127 | this->target->setRelCoorSoft(0,0,0); |
---|
[3639] | 128 | break; |
---|
[7347] | 129 | case Camera::ViewRight: |
---|
[3639] | 130 | this->toFovy = 90; |
---|
[4987] | 131 | this->setRelCoorSoft(Vector(0, 1, 10)); |
---|
[4992] | 132 | this->target->setRelCoorSoft(0,0,0); |
---|
[3639] | 133 | break; |
---|
[7347] | 134 | case Camera::ViewTop: |
---|
[3643] | 135 | this->toFovy= 120; |
---|
[5751] | 136 | this->setRelCoorSoft(Vector(30, 50, 0)); |
---|
| 137 | this->target->setRelCoorSoft(35,0,0); |
---|
[6424] | 138 | } |
---|
[3639] | 139 | } |
---|
| 140 | |
---|
| 141 | |
---|
[3636] | 142 | /** |
---|
[4836] | 143 | * Updates the position of the camera. |
---|
| 144 | * @param dt: The time that elapsed. |
---|
[3639] | 145 | */ |
---|
| 146 | void Camera::tick(float dt) |
---|
| 147 | { |
---|
[7009] | 148 | //update frustum plane |
---|
[7014] | 149 | this->viewVector = (this->target->getAbsCoor() - this->getAbsCoor()).getNormalized(); |
---|
| 150 | this->frustumPlane = Plane(this->viewVector, this->getAbsCoor() + this->viewVector * 0.1); |
---|
[7009] | 151 | this->upVector = this->getAbsDirV(); |
---|
| 152 | |
---|
[10105] | 153 | // iteration for fovy |
---|
| 154 | float tmpFovy = (this->toFovy - this->fovy); |
---|
| 155 | if (tmpFovy > 0.01) |
---|
| 156 | this->fovy += tmpFovy * fabsf(dt); |
---|
[7009] | 157 | |
---|
[10065] | 158 | |
---|
| 159 | |
---|
| 160 | |
---|
[10105] | 161 | //iterate(float dt, translate, target) |
---|
[10204] | 162 | target->translate(dt); |
---|
[3639] | 163 | } |
---|
| 164 | |
---|
| 165 | |
---|
| 166 | /** |
---|
[4836] | 167 | * initialize rendering perspective according to this camera |
---|
[6772] | 168 | * |
---|
| 169 | * This is called immediately before the rendering cycle starts, it sets all global |
---|
| 170 | * rendering options as well as the GL_PROJECTION matrix according to the camera. |
---|
| 171 | */ |
---|
[2068] | 172 | void Camera::apply () |
---|
| 173 | { |
---|
[3636] | 174 | // switching to Projection Matrix |
---|
[2551] | 175 | glMatrixMode (GL_PROJECTION); |
---|
[2112] | 176 | glLoadIdentity (); |
---|
[3635] | 177 | |
---|
[3636] | 178 | gluPerspective(this->fovy, |
---|
[4832] | 179 | this->aspectRatio, |
---|
| 180 | this->nearClip, |
---|
| 181 | this->farClip); |
---|
[6778] | 182 | |
---|
| 183 | |
---|
| 184 | // setting up the perspective |
---|
[3636] | 185 | // speed-up feature |
---|
[7108] | 186 | glMatrixMode (GL_MODELVIEW); |
---|
| 187 | glLoadIdentity(); |
---|
| 188 | |
---|
| 189 | |
---|
| 190 | } |
---|
| 191 | |
---|
| 192 | void Camera::project() |
---|
| 193 | { |
---|
[3635] | 194 | Vector cameraPosition = this->getAbsCoor(); |
---|
| 195 | Vector targetPosition = this->target->getAbsCoor(); |
---|
[2551] | 196 | |
---|
[10065] | 197 | //Setting the Camera Eye, lookAt and up Vectors |
---|
[10068] | 198 | gluLookAt(cameraPosition.x, cameraPosition.y, cameraPosition.z, |
---|
[6965] | 199 | targetPosition.x, targetPosition.y, targetPosition.z, |
---|
[7009] | 200 | this->upVector.x, this->upVector.y, this->upVector.z); |
---|
[7108] | 201 | } |
---|
[3636] | 202 | |
---|
[6965] | 203 | |
---|
[4490] | 204 | /** |
---|
[4836] | 205 | * processes an event |
---|
| 206 | * @param event: the event to process |
---|
[4490] | 207 | */ |
---|
[4414] | 208 | void Camera::process(const Event &event) |
---|
| 209 | { |
---|
| 210 | if( event.type == KeyMapper::PEV_VIEW0) |
---|
[6424] | 211 | { |
---|
[7347] | 212 | this->setViewMode(Camera::ViewNormal); |
---|
[6424] | 213 | } |
---|
[4414] | 214 | else if( event.type == KeyMapper::PEV_VIEW1) |
---|
[6424] | 215 | { |
---|
[7347] | 216 | this->setViewMode(Camera::ViewBehind); |
---|
[6424] | 217 | } |
---|
[4414] | 218 | else if( event.type == KeyMapper::PEV_VIEW2) |
---|
[6424] | 219 | { |
---|
[7347] | 220 | this->setViewMode(Camera::ViewFront); |
---|
[6424] | 221 | } |
---|
[4414] | 222 | else if( event.type == KeyMapper::PEV_VIEW3) |
---|
[6424] | 223 | { |
---|
[7347] | 224 | this->setViewMode(Camera::ViewLeft); |
---|
[6424] | 225 | } |
---|
[4414] | 226 | else if( event.type == KeyMapper::PEV_VIEW4) |
---|
[6424] | 227 | { |
---|
[7347] | 228 | this->setViewMode(Camera::ViewRight); |
---|
[6424] | 229 | } |
---|
[4414] | 230 | else if( event.type == KeyMapper::PEV_VIEW5) |
---|
[6424] | 231 | { |
---|
[7347] | 232 | this->setViewMode(Camera::ViewTop); |
---|
[6424] | 233 | } |
---|
[4414] | 234 | } |
---|
[3365] | 235 | |
---|
[4414] | 236 | |
---|
[10065] | 237 | |
---|
| 238 | |
---|
| 239 | void Camera::glLookAt(float eyex, float eyey, float eyez, float centerx, float centery, float centerz, float upx, float upy, float upz) |
---|
| 240 | { |
---|
| 241 | //Vector* eye=new Vector(eyex, eyey, eyez); |
---|
| 242 | Vector* center=new Vector (centerx, centery, centerz); |
---|
| 243 | Vector* up=new Vector(upx, upy, upz); |
---|
| 244 | |
---|
| 245 | center->x-=eyex; |
---|
| 246 | center->y-=eyey; |
---|
| 247 | center->z-=eyez; |
---|
| 248 | |
---|
| 249 | center->normalize(); |
---|
| 250 | up->normalize(); |
---|
| 251 | Vector* s = VectorProd(center, up); |
---|
| 252 | Vector* u = VectorProd(s, center); |
---|
| 253 | 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}; |
---|
| 254 | |
---|
| 255 | glMultMatrixf(Matrix); |
---|
| 256 | glTranslated(-eyex, -eyey, -eyez); |
---|
| 257 | delete center; |
---|
| 258 | delete up; |
---|
| 259 | delete s; |
---|
| 260 | delete u; |
---|
| 261 | |
---|
| 262 | } |
---|
| 263 | |
---|
| 264 | |
---|
| 265 | |
---|
| 266 | |
---|
| 267 | Vector* Camera::VectorProd(Vector* v1, Vector* v2) |
---|
| 268 | { |
---|
| 269 | Vector* temp= new Vector(); |
---|
| 270 | temp->x=v1->y * v2->z - v1->z * v2->y; |
---|
| 271 | temp->y=v1->z * v2->x - v1->x * v2->z; |
---|
| 272 | temp->z=v1->x * v2->y - v1->y * v2->x; |
---|
| 273 | return temp; |
---|
| 274 | } |
---|
| 275 | |
---|
| 276 | |
---|
| 277 | |
---|
| 278 | |
---|
| 279 | |
---|
| 280 | |
---|
| 281 | |
---|
| 282 | |
---|
| 283 | |
---|
| 284 | |
---|
| 285 | |
---|
| 286 | |
---|
| 287 | |
---|
| 288 | |
---|
[3635] | 289 | /////////////////// |
---|
| 290 | // CAMERA-TARGET // |
---|
| 291 | /////////////////// |
---|
[10068] | 292 | //REATE_FACTORY(CameraTarget); |
---|
[2636] | 293 | |
---|
[10068] | 294 | |
---|
[9869] | 295 | ObjectListDefinition(CameraTarget); |
---|
[10068] | 296 | |
---|
| 297 | |
---|
[3635] | 298 | CameraTarget::CameraTarget() |
---|
[2636] | 299 | { |
---|
[9869] | 300 | this->registerObject(this, CameraTarget::_objectList); |
---|
[6424] | 301 | // this->setParentMode(PNODE_MOVEMENT); |
---|
[10105] | 302 | this->speed=1; |
---|
| 303 | translateTo.x=0; |
---|
| 304 | translateTo.y=0; |
---|
| 305 | translateTo.z=0; |
---|
| 306 | rotateBy.x=0; |
---|
| 307 | rotateBy.y=0; |
---|
| 308 | rotateBy.z=0; |
---|
[10128] | 309 | target=createStick(); |
---|
[2636] | 310 | } |
---|
[3213] | 311 | |
---|
[10065] | 312 | |
---|
[10068] | 313 | void CameraTarget::detach() |
---|
[10065] | 314 | { |
---|
[10238] | 315 | masta->setParentSoft(target); |
---|
| 316 | masta->getTargetNode()->setParentSoft(target); |
---|
[10065] | 317 | } |
---|
| 318 | |
---|
[10128] | 319 | PNode* CameraTarget::createStick() |
---|
| 320 | { |
---|
| 321 | return new Targets(); |
---|
| 322 | } |
---|
| 323 | |
---|
| 324 | |
---|
[10093] | 325 | void CameraTarget::atach(PNode* object) |
---|
| 326 | { |
---|
[10238] | 327 | masta->setParentSoft(object); |
---|
| 328 | masta->getTargetNode()->setParentSoft(object); |
---|
[10093] | 329 | } |
---|
[10065] | 330 | |
---|
| 331 | |
---|
[10236] | 332 | |
---|
| 333 | |
---|
[10238] | 334 | Vector CameraTarget::iterate(float dt, const Vector* Target, const Vector* cam) |
---|
[10093] | 335 | { |
---|
[10103] | 336 | |
---|
[10110] | 337 | |
---|
[10093] | 338 | Vector tmpVec; |
---|
[10238] | 339 | tmpVec= (*Target - *cam); |
---|
[10103] | 340 | tmpVec.normalize(); |
---|
| 341 | return tmpVec; |
---|
[10065] | 342 | |
---|
[10093] | 343 | } |
---|
| 344 | |
---|
| 345 | |
---|
[10204] | 346 | void CameraTarget::translate(float dt) |
---|
[10093] | 347 | { |
---|
[10189] | 348 | if (fabs(translateTo.len() - (target->getAbsCoor()).len()) >= 11 ) |
---|
| 349 | { |
---|
| 350 | |
---|
[10238] | 351 | Vector tmpVec= iterate(dt, &translateTo, &(masta->getAbsCoor())); |
---|
[10110] | 352 | glLoadIdentity(); |
---|
[10204] | 353 | target->shiftCoor(speed*tmpVec.x, speed*tmpVec.y, speed*tmpVec.z); |
---|
[10189] | 354 | |
---|
[10105] | 355 | } |
---|
| 356 | } |
---|
[10093] | 357 | |
---|
[10105] | 358 | Vector * CameraTarget::rotate(Vector* newPos, float speed) |
---|
| 359 | { |
---|
| 360 | |
---|
[10093] | 361 | } |
---|
| 362 | |
---|
[10206] | 363 | void CameraTarget::jump(float x, float y, float z) |
---|
[10105] | 364 | { |
---|
[10206] | 365 | target->setAbsCoor(x,y,z); |
---|
[10105] | 366 | } |
---|
| 367 | |
---|
[10128] | 368 | |
---|
| 369 | void CameraTarget::trans(float x, float y, float z) |
---|
| 370 | { |
---|
| 371 | Vector tmpVec=Vector(x,y,z); |
---|
| 372 | translateNow(&tmpVec); |
---|
| 373 | } |
---|
| 374 | |
---|
[10110] | 375 | void CameraTarget::translateNow(Vector* vec) |
---|
| 376 | { |
---|
| 377 | translateTo=*vec; |
---|
| 378 | } |
---|
| 379 | |
---|
[10103] | 380 | void CameraTarget::changeSpeed(float speed) |
---|
| 381 | { |
---|
| 382 | if (speed!=0) |
---|
| 383 | this->speed=speed; |
---|
| 384 | return; |
---|
[10105] | 385 | } |
---|
[10110] | 386 | |
---|
| 387 | |
---|
[10338] | 388 | bool CameraTarget::isDone() |
---|
[10110] | 389 | { |
---|
[10338] | 390 | if (fabs(translateTo.len() - (target->getAbsCoor()).len()) >= 11 ) |
---|
| 391 | return 0; |
---|
| 392 | else |
---|
| 393 | return 1; |
---|
[10116] | 394 | } |
---|