| 1 | /* |
|---|
| 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: |
|---|
| 12 | main-programmer: Christian Meyer |
|---|
| 13 | co-programmer: Benjamin Grauer |
|---|
| 14 | */ |
|---|
| 15 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD_ENTITY |
|---|
| 16 | |
|---|
| 17 | #include "camera.h" |
|---|
| 18 | #include "key_mapper.h" |
|---|
| 19 | #include "glincl.h" |
|---|
| 20 | #include "vector.h" |
|---|
| 21 | #include "state.h" |
|---|
| 22 | #include "shell_command.h" |
|---|
| 23 | |
|---|
| 24 | ObjectListDefinition(Camera); |
|---|
| 25 | |
|---|
| 26 | |
|---|
| 27 | /** |
|---|
| 28 | * creates a Camera |
|---|
| 29 | */ |
|---|
| 30 | Camera::Camera() |
|---|
| 31 | { |
|---|
| 32 | this->registerObject(this, Camera::_objectList); |
|---|
| 33 | this->setName("camera"); |
|---|
| 34 | this->target = new CameraTarget(); |
|---|
| 35 | |
|---|
| 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); |
|---|
| 42 | |
|---|
| 43 | this->setFovy(90); |
|---|
| 44 | this->setAspectRatio(1.2f); |
|---|
| 45 | this->setClipRegion(.1, 10000); |
|---|
| 46 | |
|---|
| 47 | this->setViewMode(Camera::ViewNormal); |
|---|
| 48 | |
|---|
| 49 | this->setParentMode(PNODE_ALL); |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | /** |
|---|
| 53 | * default destructor |
|---|
| 54 | */ |
|---|
| 55 | Camera::~Camera() |
|---|
| 56 | {} |
|---|
| 57 | |
|---|
| 58 | /** |
|---|
| 59 | * focuses the Camera onto a Target |
|---|
| 60 | * @param target the new PNode the Camera should look at. |
|---|
| 61 | */ |
|---|
| 62 | void Camera::lookAt(PNode* target) |
|---|
| 63 | { |
|---|
| 64 | this->target->setParent(target); |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | /** |
|---|
| 68 | * @returns The PNode of the Target (from there you can get position and so on |
|---|
| 69 | */ |
|---|
| 70 | PNode* Camera::getTargetNode() const |
|---|
| 71 | { |
|---|
| 72 | return (PNode*)this->target; |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | /** |
|---|
| 76 | * sets a new AspectRatio |
|---|
| 77 | * @param aspectRatio the new aspect ratio to set (width / height) |
|---|
| 78 | */ |
|---|
| 79 | void Camera::setAspectRatio(float aspectRatio) |
|---|
| 80 | { |
|---|
| 81 | this->aspectRatio = aspectRatio; |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | /** |
|---|
| 85 | * Sets a new clipping region |
|---|
| 86 | * @param nearClip The near clip plane |
|---|
| 87 | * @param farClip The far clip plane |
|---|
| 88 | */ |
|---|
| 89 | void Camera::setClipRegion(float nearClip, float farClip) |
|---|
| 90 | { |
|---|
| 91 | this->nearClip = nearClip; |
|---|
| 92 | this->farClip = farClip; |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | /** |
|---|
| 96 | * sets the new VideoMode and initializes iteration to it. |
|---|
| 97 | * @param mode the mode to change to. |
|---|
| 98 | */ |
|---|
| 99 | void Camera::setViewMode(ViewMode mode) |
|---|
| 100 | { |
|---|
| 101 | currentMode = mode; |
|---|
| 102 | switch (mode) |
|---|
| 103 | { |
|---|
| 104 | default: |
|---|
| 105 | case Camera::ViewNormal: |
|---|
| 106 | this->toFovy = 60.0; |
|---|
| 107 | this->setRelCoorSoft(-10, 5, 0); |
|---|
| 108 | this->target->setRelCoorSoft(0,0,0); |
|---|
| 109 | break; |
|---|
| 110 | case Camera::ViewBehind: |
|---|
| 111 | break; |
|---|
| 112 | case Camera::ViewFront: |
|---|
| 113 | this->toFovy = 120.0; |
|---|
| 114 | this->setRelCoorSoft(4, 0, 0, 5); |
|---|
| 115 | this->target->setRelCoorSoft(Vector(10,0,0), 5); |
|---|
| 116 | break; |
|---|
| 117 | case Camera::ViewLeft: |
|---|
| 118 | this->toFovy = 90; |
|---|
| 119 | this->setRelCoorSoft(0, 1, -10, .5); |
|---|
| 120 | this->target->setRelCoorSoft(0,0,0); |
|---|
| 121 | break; |
|---|
| 122 | case Camera::ViewRight: |
|---|
| 123 | this->toFovy = 90; |
|---|
| 124 | this->setRelCoorSoft(Vector(0, 1, 10)); |
|---|
| 125 | this->target->setRelCoorSoft(0,0,0); |
|---|
| 126 | break; |
|---|
| 127 | case Camera::ViewTop: |
|---|
| 128 | this->toFovy= 120; |
|---|
| 129 | this->setRelCoorSoft(Vector(30, 50, 0)); |
|---|
| 130 | this->target->setRelCoorSoft(35,0,0); |
|---|
| 131 | } |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | |
|---|
| 135 | /** |
|---|
| 136 | * Updates the position of the camera. |
|---|
| 137 | * @param dt: The time that elapsed. |
|---|
| 138 | */ |
|---|
| 139 | void Camera::tick(float dt) |
|---|
| 140 | { |
|---|
| 141 | //update frustum plane |
|---|
| 142 | this->viewVector = (this->target->getAbsCoor() - this->getAbsCoor()).getNormalized(); |
|---|
| 143 | this->frustumPlane = Plane(this->viewVector, this->getAbsCoor() + this->viewVector * 0.1); |
|---|
| 144 | this->upVector = this->getAbsDirV(); |
|---|
| 145 | |
|---|
| 146 | // iteration for fovy |
|---|
| 147 | float tmpFovy = (this->toFovy - this->fovy); |
|---|
| 148 | if (tmpFovy > 0.01) |
|---|
| 149 | this->fovy += tmpFovy * fabsf(dt); |
|---|
| 150 | |
|---|
| 151 | |
|---|
| 152 | |
|---|
| 153 | |
|---|
| 154 | //iterate(float dt, translate, target) |
|---|
| 155 | target->translate( 1,dt); |
|---|
| 156 | } |
|---|
| 157 | |
|---|
| 158 | |
|---|
| 159 | /** |
|---|
| 160 | * initialize rendering perspective according to this camera |
|---|
| 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 | */ |
|---|
| 165 | void Camera::apply () |
|---|
| 166 | { |
|---|
| 167 | // switching to Projection Matrix |
|---|
| 168 | glMatrixMode (GL_PROJECTION); |
|---|
| 169 | glLoadIdentity (); |
|---|
| 170 | |
|---|
| 171 | gluPerspective(this->fovy, |
|---|
| 172 | this->aspectRatio, |
|---|
| 173 | this->nearClip, |
|---|
| 174 | this->farClip); |
|---|
| 175 | |
|---|
| 176 | |
|---|
| 177 | // setting up the perspective |
|---|
| 178 | // speed-up feature |
|---|
| 179 | glMatrixMode (GL_MODELVIEW); |
|---|
| 180 | glLoadIdentity(); |
|---|
| 181 | |
|---|
| 182 | |
|---|
| 183 | } |
|---|
| 184 | |
|---|
| 185 | void Camera::project() |
|---|
| 186 | { |
|---|
| 187 | Vector cameraPosition = this->getAbsCoor(); |
|---|
| 188 | Vector targetPosition = this->target->getAbsCoor(); |
|---|
| 189 | |
|---|
| 190 | //Setting the Camera Eye, lookAt and up Vectors |
|---|
| 191 | gluLookAt(cameraPosition.x, cameraPosition.y, cameraPosition.z, |
|---|
| 192 | targetPosition.x, targetPosition.y, targetPosition.z, |
|---|
| 193 | this->upVector.x, this->upVector.y, this->upVector.z); |
|---|
| 194 | } |
|---|
| 195 | |
|---|
| 196 | |
|---|
| 197 | /** |
|---|
| 198 | * processes an event |
|---|
| 199 | * @param event: the event to process |
|---|
| 200 | */ |
|---|
| 201 | void Camera::process(const Event &event) |
|---|
| 202 | { |
|---|
| 203 | if( event.type == KeyMapper::PEV_VIEW0) |
|---|
| 204 | { |
|---|
| 205 | this->setViewMode(Camera::ViewNormal); |
|---|
| 206 | } |
|---|
| 207 | else if( event.type == KeyMapper::PEV_VIEW1) |
|---|
| 208 | { |
|---|
| 209 | this->setViewMode(Camera::ViewBehind); |
|---|
| 210 | } |
|---|
| 211 | else if( event.type == KeyMapper::PEV_VIEW2) |
|---|
| 212 | { |
|---|
| 213 | this->setViewMode(Camera::ViewFront); |
|---|
| 214 | } |
|---|
| 215 | else if( event.type == KeyMapper::PEV_VIEW3) |
|---|
| 216 | { |
|---|
| 217 | this->setViewMode(Camera::ViewLeft); |
|---|
| 218 | } |
|---|
| 219 | else if( event.type == KeyMapper::PEV_VIEW4) |
|---|
| 220 | { |
|---|
| 221 | this->setViewMode(Camera::ViewRight); |
|---|
| 222 | } |
|---|
| 223 | else if( event.type == KeyMapper::PEV_VIEW5) |
|---|
| 224 | { |
|---|
| 225 | this->setViewMode(Camera::ViewTop); |
|---|
| 226 | } |
|---|
| 227 | } |
|---|
| 228 | |
|---|
| 229 | |
|---|
| 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 | |
|---|
| 282 | /////////////////// |
|---|
| 283 | // CAMERA-TARGET // |
|---|
| 284 | /////////////////// |
|---|
| 285 | //REATE_FACTORY(CameraTarget); |
|---|
| 286 | |
|---|
| 287 | |
|---|
| 288 | ObjectListDefinition(CameraTarget); |
|---|
| 289 | |
|---|
| 290 | SHELL_COMMAND(det, CameraTarget, detach); |
|---|
| 291 | SHELL_COMMAND(tes, CameraTarget, test); |
|---|
| 292 | |
|---|
| 293 | |
|---|
| 294 | |
|---|
| 295 | |
|---|
| 296 | |
|---|
| 297 | |
|---|
| 298 | CameraTarget::CameraTarget() |
|---|
| 299 | { |
|---|
| 300 | this->registerObject(this, CameraTarget::_objectList); |
|---|
| 301 | // this->setParentMode(PNODE_MOVEMENT); |
|---|
| 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; |
|---|
| 309 | |
|---|
| 310 | |
|---|
| 311 | } |
|---|
| 312 | |
|---|
| 313 | |
|---|
| 314 | void CameraTarget::detach() |
|---|
| 315 | { |
|---|
| 316 | glLoadIdentity(); |
|---|
| 317 | State::getCameraNode()->setParentSoft(PNode::getNullParent()); |
|---|
| 318 | //State::getCameraTargetNode()->setParentSoft(this); |
|---|
| 319 | } |
|---|
| 320 | |
|---|
| 321 | void CameraTarget::atach(PNode* object) |
|---|
| 322 | { |
|---|
| 323 | State::getCameraNode()->setParentSoft(object); |
|---|
| 324 | } |
|---|
| 325 | |
|---|
| 326 | |
|---|
| 327 | Vector CameraTarget::iterate(float dt, const Vector* target, const Vector* cam) |
|---|
| 328 | { |
|---|
| 329 | |
|---|
| 330 | |
|---|
| 331 | Vector tmpVec; |
|---|
| 332 | tmpVec= (*target - *cam); |
|---|
| 333 | tmpVec.normalize(); |
|---|
| 334 | return tmpVec; |
|---|
| 335 | |
|---|
| 336 | } |
|---|
| 337 | |
|---|
| 338 | |
|---|
| 339 | void CameraTarget::translate(float speed, float dt) |
|---|
| 340 | { |
|---|
| 341 | if (translateTo.x >= 0.01+State::getCameraNode()->getAbsCoorX() || translateTo.y |
|---|
| 342 | >= 0.01+State::getCameraNode()->getAbsCoorX() || translateTo.z |
|---|
| 343 | >= 0.01+State::getCameraNode()->getAbsCoorX()) |
|---|
| 344 | { |
|---|
| 345 | Vector tmpVec= iterate(dt, &translateTo, &(State::getCameraNode()->getAbsCoor())); |
|---|
| 346 | glLoadIdentity(); |
|---|
| 347 | glTranslatef(tmpVec.x, tmpVec.y, tmpVec.z); |
|---|
| 348 | } |
|---|
| 349 | } |
|---|
| 350 | |
|---|
| 351 | Vector * CameraTarget::rotate(Vector* newPos, float speed) |
|---|
| 352 | { |
|---|
| 353 | |
|---|
| 354 | } |
|---|
| 355 | |
|---|
| 356 | void CameraTarget::jump(Vector* newPos) |
|---|
| 357 | { |
|---|
| 358 | |
|---|
| 359 | } |
|---|
| 360 | |
|---|
| 361 | void CameraTarget::translateNow(Vector* vec) |
|---|
| 362 | { |
|---|
| 363 | translateTo=*vec; |
|---|
| 364 | } |
|---|
| 365 | |
|---|
| 366 | void CameraTarget::changeSpeed(float speed) |
|---|
| 367 | { |
|---|
| 368 | if (speed!=0) |
|---|
| 369 | this->speed=speed; |
|---|
| 370 | return; |
|---|
| 371 | } |
|---|
| 372 | |
|---|
| 373 | |
|---|
| 374 | void CameraTarget::test() |
|---|
| 375 | { |
|---|
| 376 | //Vector* vec=new Vector(100,100,100); |
|---|
| 377 | //translateNow(vec); |
|---|
| 378 | glMatrixMode (GL_PROJECTION); |
|---|
| 379 | glLoadIdentity(); |
|---|
| 380 | glTranslatef(100, 100, 100); |
|---|
| 381 | } |
|---|