[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" |
---|
| 21 | #include <iostream.h> |
---|
[10068] | 22 | #include "state.h" |
---|
| 23 | #include "shell_command.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 | |
---|
| 147 | |
---|
[10065] | 148 | |
---|
| 149 | |
---|
| 150 | |
---|
[7173] | 151 | float tmpFovy = (this->toFovy - this->fovy); |
---|
[6034] | 152 | if (tmpFovy > 0.01) |
---|
[5354] | 153 | this->fovy += tmpFovy * fabsf(dt); |
---|
[3639] | 154 | } |
---|
| 155 | |
---|
| 156 | |
---|
| 157 | /** |
---|
[4836] | 158 | * initialize rendering perspective according to this camera |
---|
[6772] | 159 | * |
---|
| 160 | * This is called immediately before the rendering cycle starts, it sets all global |
---|
| 161 | * rendering options as well as the GL_PROJECTION matrix according to the camera. |
---|
| 162 | */ |
---|
[2068] | 163 | void Camera::apply () |
---|
| 164 | { |
---|
[3636] | 165 | // switching to Projection Matrix |
---|
[2551] | 166 | glMatrixMode (GL_PROJECTION); |
---|
[2112] | 167 | glLoadIdentity (); |
---|
[3635] | 168 | |
---|
[3636] | 169 | gluPerspective(this->fovy, |
---|
[4832] | 170 | this->aspectRatio, |
---|
| 171 | this->nearClip, |
---|
| 172 | this->farClip); |
---|
[6778] | 173 | |
---|
| 174 | |
---|
| 175 | // setting up the perspective |
---|
[3636] | 176 | // speed-up feature |
---|
[7108] | 177 | glMatrixMode (GL_MODELVIEW); |
---|
| 178 | glLoadIdentity(); |
---|
| 179 | |
---|
| 180 | |
---|
| 181 | } |
---|
| 182 | |
---|
| 183 | void Camera::project() |
---|
| 184 | { |
---|
[3635] | 185 | Vector cameraPosition = this->getAbsCoor(); |
---|
| 186 | Vector targetPosition = this->target->getAbsCoor(); |
---|
[2551] | 187 | |
---|
[10065] | 188 | //Setting the Camera Eye, lookAt and up Vectors |
---|
[10068] | 189 | gluLookAt(cameraPosition.x, cameraPosition.y, cameraPosition.z, |
---|
[6965] | 190 | targetPosition.x, targetPosition.y, targetPosition.z, |
---|
[7009] | 191 | this->upVector.x, this->upVector.y, this->upVector.z); |
---|
[7108] | 192 | } |
---|
[3636] | 193 | |
---|
[6965] | 194 | |
---|
[4490] | 195 | /** |
---|
[4836] | 196 | * processes an event |
---|
| 197 | * @param event: the event to process |
---|
[4490] | 198 | */ |
---|
[4414] | 199 | void Camera::process(const Event &event) |
---|
| 200 | { |
---|
| 201 | if( event.type == KeyMapper::PEV_VIEW0) |
---|
[6424] | 202 | { |
---|
[7347] | 203 | this->setViewMode(Camera::ViewNormal); |
---|
[6424] | 204 | } |
---|
[4414] | 205 | else if( event.type == KeyMapper::PEV_VIEW1) |
---|
[6424] | 206 | { |
---|
[7347] | 207 | this->setViewMode(Camera::ViewBehind); |
---|
[6424] | 208 | } |
---|
[4414] | 209 | else if( event.type == KeyMapper::PEV_VIEW2) |
---|
[6424] | 210 | { |
---|
[7347] | 211 | this->setViewMode(Camera::ViewFront); |
---|
[6424] | 212 | } |
---|
[4414] | 213 | else if( event.type == KeyMapper::PEV_VIEW3) |
---|
[6424] | 214 | { |
---|
[7347] | 215 | this->setViewMode(Camera::ViewLeft); |
---|
[6424] | 216 | } |
---|
[4414] | 217 | else if( event.type == KeyMapper::PEV_VIEW4) |
---|
[6424] | 218 | { |
---|
[7347] | 219 | this->setViewMode(Camera::ViewRight); |
---|
[6424] | 220 | } |
---|
[4414] | 221 | else if( event.type == KeyMapper::PEV_VIEW5) |
---|
[6424] | 222 | { |
---|
[7347] | 223 | this->setViewMode(Camera::ViewTop); |
---|
[6424] | 224 | } |
---|
[4414] | 225 | } |
---|
[3365] | 226 | |
---|
[10065] | 227 | /* |
---|
| 228 | Vector* Camera::translate(Vector* newPos, float speed) |
---|
| 229 | { |
---|
| 230 | glTranslatef(Vector); |
---|
[4414] | 231 | |
---|
[10065] | 232 | } |
---|
| 233 | |
---|
| 234 | |
---|
| 235 | float Camera::iterate(float dt, Vector target) |
---|
| 236 | { |
---|
| 237 | Vector tmpVec = (this->getAbsCorr() - target); |
---|
| 238 | if (tmpVec > 0.01) |
---|
| 239 | Vector ret = this->getAbsCorr; |
---|
| 240 | ret += tmpVec * fabsf(dt); |
---|
| 241 | return ret; |
---|
| 242 | } |
---|
| 243 | */ |
---|
| 244 | void Camera::Rotate() |
---|
| 245 | { |
---|
| 246 | this->fovy=this->fovy+1; |
---|
| 247 | } |
---|
| 248 | |
---|
| 249 | |
---|
| 250 | void Camera::glLookAt(float eyex, float eyey, float eyez, float centerx, float centery, float centerz, float upx, float upy, float upz) |
---|
| 251 | { |
---|
| 252 | //Vector* eye=new Vector(eyex, eyey, eyez); |
---|
| 253 | Vector* center=new Vector (centerx, centery, centerz); |
---|
| 254 | Vector* up=new Vector(upx, upy, upz); |
---|
| 255 | |
---|
| 256 | center->x-=eyex; |
---|
| 257 | center->y-=eyey; |
---|
| 258 | center->z-=eyez; |
---|
| 259 | |
---|
| 260 | center->normalize(); |
---|
| 261 | up->normalize(); |
---|
| 262 | Vector* s = VectorProd(center, up); |
---|
| 263 | Vector* u = VectorProd(s, center); |
---|
| 264 | 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}; |
---|
| 265 | |
---|
| 266 | glMultMatrixf(Matrix); |
---|
| 267 | glTranslated(-eyex, -eyey, -eyez); |
---|
| 268 | delete center; |
---|
| 269 | delete up; |
---|
| 270 | delete s; |
---|
| 271 | delete u; |
---|
| 272 | |
---|
| 273 | } |
---|
| 274 | |
---|
| 275 | |
---|
| 276 | |
---|
| 277 | |
---|
| 278 | Vector* Camera::VectorProd(Vector* v1, Vector* v2) |
---|
| 279 | { |
---|
| 280 | Vector* temp= new Vector(); |
---|
| 281 | temp->x=v1->y * v2->z - v1->z * v2->y; |
---|
| 282 | temp->y=v1->z * v2->x - v1->x * v2->z; |
---|
| 283 | temp->z=v1->x * v2->y - v1->y * v2->x; |
---|
| 284 | return temp; |
---|
| 285 | } |
---|
| 286 | |
---|
| 287 | |
---|
| 288 | |
---|
| 289 | |
---|
| 290 | |
---|
| 291 | |
---|
| 292 | |
---|
| 293 | |
---|
| 294 | |
---|
| 295 | |
---|
| 296 | |
---|
| 297 | |
---|
| 298 | |
---|
| 299 | |
---|
[3635] | 300 | /////////////////// |
---|
| 301 | // CAMERA-TARGET // |
---|
| 302 | /////////////////// |
---|
[10068] | 303 | //REATE_FACTORY(CameraTarget); |
---|
[2636] | 304 | |
---|
[10068] | 305 | |
---|
[9869] | 306 | ObjectListDefinition(CameraTarget); |
---|
[10068] | 307 | |
---|
| 308 | SHELL_COMMAND(det, CameraTarget, detach); |
---|
| 309 | |
---|
| 310 | |
---|
[10094] | 311 | Vector itarateTo(0,0,0); |
---|
| 312 | |
---|
| 313 | |
---|
[3635] | 314 | CameraTarget::CameraTarget() |
---|
[2636] | 315 | { |
---|
[9869] | 316 | this->registerObject(this, CameraTarget::_objectList); |
---|
[6424] | 317 | // this->setParentMode(PNODE_MOVEMENT); |
---|
[10065] | 318 | |
---|
| 319 | |
---|
[2636] | 320 | } |
---|
[3213] | 321 | |
---|
[10065] | 322 | |
---|
[10068] | 323 | void CameraTarget::detach() |
---|
[10065] | 324 | { |
---|
[10093] | 325 | glLoadIdentity(); |
---|
| 326 | State::getCameraNode()->setParentSoft(PNode::getNullParent()); |
---|
| 327 | //State::getCameraTargetNode()->setParentSoft(this); |
---|
[10065] | 328 | } |
---|
| 329 | |
---|
[10093] | 330 | void CameraTarget::atach(PNode* object) |
---|
| 331 | { |
---|
| 332 | State::getCameraNode()->setParentSoft(object); |
---|
| 333 | } |
---|
[10065] | 334 | |
---|
| 335 | |
---|
[10094] | 336 | float CameraTarget::iterate(float dt, Vector target, Vector cam) |
---|
[10093] | 337 | { |
---|
| 338 | Vector tmpVec; |
---|
[10065] | 339 | |
---|
[10094] | 340 | tmpVec= (target - cam); |
---|
[10065] | 341 | |
---|
[10093] | 342 | if (tmpVec.x >= 0.01) |
---|
[10094] | 343 | |
---|
| 344 | return 0;//tmpVec * fabsf(dt); |
---|
| 345 | |
---|
[10093] | 346 | } |
---|
| 347 | |
---|
| 348 | |
---|
[10094] | 349 | Vector* CameraTarget::translate(Vector* newPos, float speed) |
---|
[10093] | 350 | { |
---|
[10094] | 351 | //glTranslatef(Vector); |
---|
[10093] | 352 | |
---|
| 353 | } |
---|
| 354 | |
---|
| 355 | |
---|
| 356 | |
---|