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