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