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