[2068] | 1 | |
---|
| 2 | |
---|
| 3 | /* |
---|
| 4 | orxonox - the future of 3D-vertical-scrollers |
---|
| 5 | |
---|
| 6 | Copyright (C) 2004 orx |
---|
| 7 | |
---|
| 8 | This program is free software; you can redistribute it and/or modify |
---|
| 9 | it under the terms of the GNU General Public License as published by |
---|
| 10 | the Free Software Foundation; either version 2, or (at your option) |
---|
| 11 | any later version. |
---|
| 12 | |
---|
| 13 | ### File Specific: |
---|
[2080] | 14 | main-programmer: Christian Meyer |
---|
[2068] | 15 | co-programmer: ... |
---|
| 16 | */ |
---|
| 17 | |
---|
| 18 | #include "camera.h" |
---|
[3608] | 19 | |
---|
[2100] | 20 | #include "world.h" |
---|
| 21 | #include "world_entity.h" |
---|
[3608] | 22 | #include "vector.h" |
---|
[2068] | 23 | |
---|
| 24 | using namespace std; |
---|
| 25 | |
---|
[3635] | 26 | //////////// |
---|
| 27 | // CAMERA // |
---|
| 28 | //////////// |
---|
| 29 | |
---|
[2096] | 30 | /** |
---|
| 31 | \brief creates a Camera |
---|
| 32 | */ |
---|
[3635] | 33 | Camera::Camera(void) |
---|
[2068] | 34 | { |
---|
[3639] | 35 | this->setClassName("Camera"); |
---|
[3635] | 36 | this->target = new CameraTarget(); |
---|
[3636] | 37 | |
---|
[3641] | 38 | this->setFovy(90); |
---|
[3636] | 39 | this->setAspectRatio(1.2f); |
---|
| 40 | this->setClipRegion(.1, 2000); |
---|
[3641] | 41 | |
---|
[3639] | 42 | this->setViewMode(VIEW_NORMAL); |
---|
[2068] | 43 | } |
---|
| 44 | |
---|
[2096] | 45 | /** |
---|
| 46 | \brief default destructor |
---|
| 47 | */ |
---|
[3635] | 48 | Camera::~Camera(void) |
---|
[2068] | 49 | { |
---|
[3543] | 50 | } |
---|
| 51 | |
---|
| 52 | /** |
---|
[3635] | 53 | \brief focuses the Camera onto a Target |
---|
| 54 | \param target the new PNode the Camera should look at. |
---|
[2096] | 55 | */ |
---|
[3635] | 56 | void Camera::lookAt(PNode* target) |
---|
[2068] | 57 | { |
---|
[3635] | 58 | this->target->setParent(target); |
---|
[2068] | 59 | } |
---|
| 60 | |
---|
[3638] | 61 | /** |
---|
| 62 | \returns The PNode of the Target (from there you can get position and so on |
---|
| 63 | */ |
---|
[3635] | 64 | PNode* Camera::getTarget(void) |
---|
[2068] | 65 | { |
---|
[3635] | 66 | return (PNode*)this->target; |
---|
[2068] | 67 | } |
---|
| 68 | |
---|
[3636] | 69 | |
---|
[2096] | 70 | /** |
---|
[3636] | 71 | \brief sets a new AspectRatio |
---|
| 72 | \param aspectRatio the new aspect ratio to set (width / height) |
---|
| 73 | */ |
---|
| 74 | void Camera::setAspectRatio(float aspectRatio) |
---|
| 75 | { |
---|
| 76 | this->aspectRatio = aspectRatio; |
---|
| 77 | } |
---|
| 78 | |
---|
| 79 | /** |
---|
| 80 | \brief sets the Field of View to fofy |
---|
| 81 | \param fovy new field of view factor (in degrees) |
---|
| 82 | */ |
---|
| 83 | void Camera::setFovy(float fovy) |
---|
| 84 | { |
---|
| 85 | this->fovy = fovy; |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | /** |
---|
| 89 | \brief Sets a new clipping region |
---|
| 90 | \param nearClip The near clip plane |
---|
| 91 | \param farClip The far clip plane |
---|
| 92 | */ |
---|
| 93 | void Camera::setClipRegion(float nearClip, float farClip) |
---|
| 94 | { |
---|
| 95 | this->nearClip = nearClip; |
---|
| 96 | this->farClip = farClip; |
---|
| 97 | } |
---|
| 98 | |
---|
[3639] | 99 | void Camera::setViewMode(ViewMode mode) |
---|
| 100 | { |
---|
| 101 | switch (mode) |
---|
| 102 | { |
---|
| 103 | default: |
---|
| 104 | case VIEW_NORMAL: |
---|
| 105 | this->toFovy = 60.0; |
---|
[3643] | 106 | this->toRelCoor = Vector(-10, 5, 0); |
---|
[3639] | 107 | break; |
---|
| 108 | case VIEW_BEHIND: |
---|
[3643] | 109 | this->toFovy = 120.0; |
---|
| 110 | this->toRelCoor = Vector(-7, 0, 0); |
---|
[3639] | 111 | break; |
---|
| 112 | case VIEW_FRONT: |
---|
| 113 | this->toFovy = 95.0; |
---|
[3643] | 114 | this->toRelCoor = Vector(12, 5, 0); |
---|
[3639] | 115 | break; |
---|
| 116 | case VIEW_LEFT: |
---|
| 117 | this->toFovy = 90; |
---|
[3643] | 118 | this->toRelCoor = Vector(0, 2, -10); |
---|
[3639] | 119 | break; |
---|
| 120 | case VIEW_RIGHT: |
---|
| 121 | this->toFovy = 90; |
---|
[3643] | 122 | this->toRelCoor = Vector(0, 2, 10); |
---|
[3639] | 123 | break; |
---|
[3643] | 124 | case VIEW_TOP: |
---|
| 125 | this->toFovy= 120; |
---|
| 126 | this->toRelCoor = Vector(0, 4, 0); |
---|
[3639] | 127 | } |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | |
---|
[3636] | 131 | /** |
---|
[3639] | 132 | \brief Updates the position of the camera. |
---|
| 133 | \param dt The time that elapsed. |
---|
| 134 | */ |
---|
| 135 | void Camera::tick(float dt) |
---|
| 136 | { |
---|
[3643] | 137 | dt /= 500; |
---|
[3645] | 138 | float tmpFovy = (this->toFovy - this->fovy) * dt; |
---|
| 139 | if (tmpFovy > .001) |
---|
| 140 | this->fovy += (this->toFovy - this->fovy) * dt; |
---|
| 141 | Vector tmpPos = (this->toRelCoor - this->getRelCoor()) * dt; |
---|
| 142 | if (tmpPos.len() >= .001) |
---|
| 143 | { |
---|
| 144 | tmpPos = tmpPos + this->getRelCoor(); |
---|
| 145 | this->setRelCoor(&tmpPos); |
---|
| 146 | } |
---|
[3639] | 147 | } |
---|
| 148 | |
---|
| 149 | |
---|
| 150 | /** |
---|
[2551] | 151 | \brief initialize rendering perspective according to this camera |
---|
[2096] | 152 | |
---|
[2551] | 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. |
---|
[2096] | 155 | */ |
---|
[2068] | 156 | void Camera::apply () |
---|
| 157 | { |
---|
[3636] | 158 | // switching to Projection Matrix |
---|
[2551] | 159 | glMatrixMode (GL_PROJECTION); |
---|
[2112] | 160 | glLoadIdentity (); |
---|
[3635] | 161 | |
---|
[3636] | 162 | // setting up the perspective |
---|
| 163 | gluPerspective(this->fovy, |
---|
| 164 | this->aspectRatio, |
---|
| 165 | this->nearClip, |
---|
| 166 | this->farClip); |
---|
[3365] | 167 | |
---|
[3636] | 168 | // speed-up feature |
---|
[3635] | 169 | Vector cameraPosition = this->getAbsCoor(); |
---|
| 170 | Vector targetPosition = this->target->getAbsCoor(); |
---|
[3638] | 171 | Vector up = Vector(0, 1, 0); |
---|
| 172 | up = this->getAbsDir().apply(up); |
---|
[2551] | 173 | |
---|
[3636] | 174 | // Setting the Camera Eye, lookAt and up Vectors |
---|
[3635] | 175 | gluLookAt(cameraPosition.x, cameraPosition.y, cameraPosition.z, |
---|
| 176 | targetPosition.x, targetPosition.y, targetPosition.z, |
---|
[3638] | 177 | up.x, up.y, up.z); |
---|
[3636] | 178 | |
---|
| 179 | // switching back to Modeling Matrix |
---|
[2068] | 180 | glMatrixMode (GL_MODELVIEW); |
---|
| 181 | } |
---|
| 182 | |
---|
| 183 | |
---|
[3365] | 184 | |
---|
[3635] | 185 | /////////////////// |
---|
| 186 | // CAMERA-TARGET // |
---|
| 187 | /////////////////// |
---|
[2636] | 188 | |
---|
| 189 | |
---|
[3635] | 190 | CameraTarget::CameraTarget() |
---|
[2636] | 191 | { |
---|
[3639] | 192 | this->setClassName("CameraTarget"); |
---|
[3638] | 193 | this->setMode(PNODE_MOVEMENT); |
---|
[2636] | 194 | } |
---|
[3213] | 195 | |
---|
[3635] | 196 | CameraTarget::~CameraTarget() |
---|
| 197 | { |
---|
[3213] | 198 | |
---|
[3635] | 199 | } |
---|