| 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 |  | 
|---|
| 21 | ObjectListDefinition(Camera); | 
|---|
| 22 |  | 
|---|
| 23 | /** | 
|---|
| 24 | *  creates a Camera | 
|---|
| 25 | */ | 
|---|
| 26 | Camera::Camera() | 
|---|
| 27 | { | 
|---|
| 28 | this->registerObject(this, Camera::_objectList); | 
|---|
| 29 | this->setName("camera"); | 
|---|
| 30 | this->target = new CameraTarget(); | 
|---|
| 31 |  | 
|---|
| 32 | this->subscribeEvent(ES_GAME, KeyMapper::PEV_VIEW0); | 
|---|
| 33 | this->subscribeEvent(ES_GAME, KeyMapper::PEV_VIEW1); | 
|---|
| 34 | this->subscribeEvent(ES_GAME, KeyMapper::PEV_VIEW2); | 
|---|
| 35 | this->subscribeEvent(ES_GAME, KeyMapper::PEV_VIEW3); | 
|---|
| 36 | this->subscribeEvent(ES_GAME, KeyMapper::PEV_VIEW4); | 
|---|
| 37 | this->subscribeEvent(ES_GAME, KeyMapper::PEV_VIEW5); | 
|---|
| 38 |  | 
|---|
| 39 | this->setFovy(90); | 
|---|
| 40 | this->setAspectRatio(1.2f); | 
|---|
| 41 | this->setClipRegion(.1, 10000); | 
|---|
| 42 |  | 
|---|
| 43 | this->setViewMode(Camera::ViewNormal); | 
|---|
| 44 |  | 
|---|
| 45 | this->setParentMode(PNODE_ALL); | 
|---|
| 46 | } | 
|---|
| 47 |  | 
|---|
| 48 | /** | 
|---|
| 49 | *  default destructor | 
|---|
| 50 | */ | 
|---|
| 51 | Camera::~Camera() | 
|---|
| 52 | {} | 
|---|
| 53 |  | 
|---|
| 54 | /** | 
|---|
| 55 | *  focuses the Camera onto a Target | 
|---|
| 56 | * @param target the new PNode the Camera should look at. | 
|---|
| 57 | */ | 
|---|
| 58 | void Camera::lookAt(PNode* target) | 
|---|
| 59 | { | 
|---|
| 60 | this->target->setParent(target); | 
|---|
| 61 | } | 
|---|
| 62 |  | 
|---|
| 63 | /** | 
|---|
| 64 | * @returns The PNode of the Target (from there you can get position and so on | 
|---|
| 65 | */ | 
|---|
| 66 | PNode* Camera::getTargetNode() const | 
|---|
| 67 | { | 
|---|
| 68 | return (PNode*)this->target; | 
|---|
| 69 | } | 
|---|
| 70 |  | 
|---|
| 71 | /** | 
|---|
| 72 | *  sets a new AspectRatio | 
|---|
| 73 | * @param aspectRatio the new aspect ratio to set (width / height) | 
|---|
| 74 | */ | 
|---|
| 75 | void Camera::setAspectRatio(float aspectRatio) | 
|---|
| 76 | { | 
|---|
| 77 | this->aspectRatio = aspectRatio; | 
|---|
| 78 | } | 
|---|
| 79 |  | 
|---|
| 80 | /** | 
|---|
| 81 | * Sets a new clipping region | 
|---|
| 82 | * @param nearClip The near clip plane | 
|---|
| 83 | * @param farClip The far clip plane | 
|---|
| 84 | */ | 
|---|
| 85 | void Camera::setClipRegion(float nearClip, float farClip) | 
|---|
| 86 | { | 
|---|
| 87 | this->nearClip = nearClip; | 
|---|
| 88 | this->farClip = farClip; | 
|---|
| 89 | } | 
|---|
| 90 |  | 
|---|
| 91 | /** | 
|---|
| 92 | *  sets the new VideoMode and initializes iteration to it. | 
|---|
| 93 | * @param mode the mode to change to. | 
|---|
| 94 | */ | 
|---|
| 95 | void Camera::setViewMode(ViewMode mode) | 
|---|
| 96 | { | 
|---|
| 97 | currentMode = mode; | 
|---|
| 98 | switch (mode) | 
|---|
| 99 | { | 
|---|
| 100 | default: | 
|---|
| 101 | case Camera::ViewNormal: | 
|---|
| 102 | this->toFovy = 60.0; | 
|---|
| 103 | this->setRelCoorSoft(-10, 5, 0); | 
|---|
| 104 | this->target->setRelCoorSoft(0,0,0); | 
|---|
| 105 | break; | 
|---|
| 106 | case Camera::ViewBehind: | 
|---|
| 107 | break; | 
|---|
| 108 | case Camera::ViewFront: | 
|---|
| 109 | this->toFovy = 120.0; | 
|---|
| 110 | this->setRelCoorSoft(4, 0, 0, 5); | 
|---|
| 111 | this->target->setRelCoorSoft(Vector(10,0,0), 5); | 
|---|
| 112 | break; | 
|---|
| 113 | case Camera::ViewLeft: | 
|---|
| 114 | this->toFovy = 90; | 
|---|
| 115 | this->setRelCoorSoft(0, 1, -10, .5); | 
|---|
| 116 | this->target->setRelCoorSoft(0,0,0); | 
|---|
| 117 | break; | 
|---|
| 118 | case Camera::ViewRight: | 
|---|
| 119 | this->toFovy = 90; | 
|---|
| 120 | this->setRelCoorSoft(Vector(0, 1, 10)); | 
|---|
| 121 | this->target->setRelCoorSoft(0,0,0); | 
|---|
| 122 | break; | 
|---|
| 123 | case Camera::ViewTop: | 
|---|
| 124 | this->toFovy= 120; | 
|---|
| 125 | this->setRelCoorSoft(Vector(30, 50, 0)); | 
|---|
| 126 | this->target->setRelCoorSoft(35,0,0); | 
|---|
| 127 | } | 
|---|
| 128 | } | 
|---|
| 129 |  | 
|---|
| 130 |  | 
|---|
| 131 | /** | 
|---|
| 132 | *  Updates the position of the camera. | 
|---|
| 133 | * @param dt: The time that elapsed. | 
|---|
| 134 | */ | 
|---|
| 135 | void Camera::tick(float dt) | 
|---|
| 136 | { | 
|---|
| 137 | //update frustum plane | 
|---|
| 138 | this->viewVector = (this->target->getAbsCoor() - this->getAbsCoor()).getNormalized(); | 
|---|
| 139 | this->frustumPlane = Plane(this->viewVector, this->getAbsCoor() + this->viewVector * 0.1); | 
|---|
| 140 |  | 
|---|
| 141 | this->upVector =  this->getAbsDirV(); | 
|---|
| 142 |  | 
|---|
| 143 |  | 
|---|
| 144 | float tmpFovy = (this->toFovy - this->fovy); | 
|---|
| 145 | if (tmpFovy > 0.01) | 
|---|
| 146 | this->fovy += tmpFovy * fabsf(dt); | 
|---|
| 147 | } | 
|---|
| 148 |  | 
|---|
| 149 |  | 
|---|
| 150 | /** | 
|---|
| 151 | *  initialize rendering perspective according to this camera | 
|---|
| 152 | * | 
|---|
| 153 | * This is called immediately before the rendering cycle starts, it sets all global | 
|---|
| 154 | * rendering options as well as the GL_PROJECTION matrix according to the camera. | 
|---|
| 155 | */ | 
|---|
| 156 | void Camera::apply () | 
|---|
| 157 | { | 
|---|
| 158 | // switching to Projection Matrix | 
|---|
| 159 | glMatrixMode (GL_PROJECTION); | 
|---|
| 160 | glLoadIdentity (); | 
|---|
| 161 |  | 
|---|
| 162 | gluPerspective(this->fovy, | 
|---|
| 163 | this->aspectRatio, | 
|---|
| 164 | this->nearClip, | 
|---|
| 165 | this->farClip); | 
|---|
| 166 |  | 
|---|
| 167 |  | 
|---|
| 168 | // setting up the perspective | 
|---|
| 169 | // speed-up feature | 
|---|
| 170 | glMatrixMode (GL_MODELVIEW); | 
|---|
| 171 | glLoadIdentity(); | 
|---|
| 172 |  | 
|---|
| 173 |  | 
|---|
| 174 | } | 
|---|
| 175 |  | 
|---|
| 176 | void Camera::project() | 
|---|
| 177 | { | 
|---|
| 178 | Vector cameraPosition = this->getAbsCoor(); | 
|---|
| 179 | Vector targetPosition = this->target->getAbsCoor(); | 
|---|
| 180 |  | 
|---|
| 181 | // Setting the Camera Eye, lookAt and up Vectors | 
|---|
| 182 | gluLookAt(cameraPosition.x, cameraPosition.y, cameraPosition.z, | 
|---|
| 183 | targetPosition.x, targetPosition.y, targetPosition.z, | 
|---|
| 184 | this->upVector.x, this->upVector.y, this->upVector.z); | 
|---|
| 185 | } | 
|---|
| 186 |  | 
|---|
| 187 |  | 
|---|
| 188 | /** | 
|---|
| 189 | *  processes an event | 
|---|
| 190 | * @param event: the event to process | 
|---|
| 191 | */ | 
|---|
| 192 | void Camera::process(const Event &event) | 
|---|
| 193 | { | 
|---|
| 194 | if( event.type == KeyMapper::PEV_VIEW0) | 
|---|
| 195 | { | 
|---|
| 196 | this->setViewMode(Camera::ViewNormal); | 
|---|
| 197 | } | 
|---|
| 198 | else if( event.type == KeyMapper::PEV_VIEW1) | 
|---|
| 199 | { | 
|---|
| 200 | this->setViewMode(Camera::ViewBehind); | 
|---|
| 201 | } | 
|---|
| 202 | else if( event.type == KeyMapper::PEV_VIEW2) | 
|---|
| 203 | { | 
|---|
| 204 | this->setViewMode(Camera::ViewFront); | 
|---|
| 205 | } | 
|---|
| 206 | else if( event.type == KeyMapper::PEV_VIEW3) | 
|---|
| 207 | { | 
|---|
| 208 | this->setViewMode(Camera::ViewLeft); | 
|---|
| 209 | } | 
|---|
| 210 | else if( event.type == KeyMapper::PEV_VIEW4) | 
|---|
| 211 | { | 
|---|
| 212 | this->setViewMode(Camera::ViewRight); | 
|---|
| 213 | } | 
|---|
| 214 | else if( event.type == KeyMapper::PEV_VIEW5) | 
|---|
| 215 | { | 
|---|
| 216 | this->setViewMode(Camera::ViewTop); | 
|---|
| 217 | } | 
|---|
| 218 | } | 
|---|
| 219 |  | 
|---|
| 220 |  | 
|---|
| 221 | /////////////////// | 
|---|
| 222 | // CAMERA-TARGET // | 
|---|
| 223 | /////////////////// | 
|---|
| 224 |  | 
|---|
| 225 | ObjectListDefinition(CameraTarget); | 
|---|
| 226 | CameraTarget::CameraTarget() | 
|---|
| 227 | { | 
|---|
| 228 | this->registerObject(this, CameraTarget::_objectList); | 
|---|
| 229 | //  this->setParentMode(PNODE_MOVEMENT); | 
|---|
| 230 | } | 
|---|
| 231 |  | 
|---|