[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 | |
---|
[3639] | 38 | this->toRelCoor = new Vector; |
---|
| 39 | |
---|
[3636] | 40 | this->setFovy(60); |
---|
| 41 | this->setAspectRatio(1.2f); |
---|
| 42 | this->setClipRegion(.1, 2000); |
---|
[3639] | 43 | this->setViewMode(VIEW_NORMAL); |
---|
| 44 | |
---|
[2068] | 45 | } |
---|
| 46 | |
---|
[2096] | 47 | /** |
---|
| 48 | \brief default destructor |
---|
| 49 | */ |
---|
[3635] | 50 | Camera::~Camera(void) |
---|
[2068] | 51 | { |
---|
[3543] | 52 | } |
---|
| 53 | |
---|
| 54 | /** |
---|
[3635] | 55 | \brief focuses the Camera onto a Target |
---|
| 56 | \param target the new PNode the Camera should look at. |
---|
[2096] | 57 | */ |
---|
[3635] | 58 | void Camera::lookAt(PNode* target) |
---|
[2068] | 59 | { |
---|
[3635] | 60 | this->target->setParent(target); |
---|
[2068] | 61 | } |
---|
| 62 | |
---|
[3638] | 63 | /** |
---|
| 64 | \returns The PNode of the Target (from there you can get position and so on |
---|
| 65 | */ |
---|
[3635] | 66 | PNode* Camera::getTarget(void) |
---|
[2068] | 67 | { |
---|
[3635] | 68 | return (PNode*)this->target; |
---|
[2068] | 69 | } |
---|
| 70 | |
---|
[3636] | 71 | |
---|
[2096] | 72 | /** |
---|
[3636] | 73 | \brief sets a new AspectRatio |
---|
| 74 | \param aspectRatio the new aspect ratio to set (width / height) |
---|
| 75 | */ |
---|
| 76 | void Camera::setAspectRatio(float aspectRatio) |
---|
| 77 | { |
---|
| 78 | this->aspectRatio = aspectRatio; |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | /** |
---|
| 82 | \brief sets the Field of View to fofy |
---|
| 83 | \param fovy new field of view factor (in degrees) |
---|
| 84 | */ |
---|
| 85 | void Camera::setFovy(float fovy) |
---|
| 86 | { |
---|
| 87 | this->fovy = fovy; |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | /** |
---|
| 91 | \brief Sets a new clipping region |
---|
| 92 | \param nearClip The near clip plane |
---|
| 93 | \param farClip The far clip plane |
---|
| 94 | */ |
---|
| 95 | void Camera::setClipRegion(float nearClip, float farClip) |
---|
| 96 | { |
---|
| 97 | this->nearClip = nearClip; |
---|
| 98 | this->farClip = farClip; |
---|
| 99 | } |
---|
| 100 | |
---|
[3639] | 101 | void Camera::setViewMode(ViewMode mode) |
---|
| 102 | { |
---|
| 103 | switch (mode) |
---|
| 104 | { |
---|
| 105 | default: |
---|
| 106 | case VIEW_NORMAL: |
---|
| 107 | this->toFovy = 60.0; |
---|
| 108 | *this->toRelCoor = Vector (-10, 5, 0); |
---|
| 109 | break; |
---|
| 110 | case VIEW_BEHIND: |
---|
| 111 | this->toFovy = 90.0; |
---|
| 112 | *this->toRelCoor = Vector (-6, 2, 0); |
---|
| 113 | break; |
---|
| 114 | case VIEW_FRONT: |
---|
| 115 | this->toFovy = 95.0; |
---|
| 116 | *this->toRelCoor = Vector (10, 5, 0); |
---|
| 117 | break; |
---|
| 118 | case VIEW_LEFT: |
---|
| 119 | this->toFovy = 90; |
---|
| 120 | *this->toRelCoor = Vector (0, 5, -10); |
---|
| 121 | break; |
---|
| 122 | case VIEW_RIGHT: |
---|
| 123 | this->toFovy = 90; |
---|
| 124 | *this->toRelCoor = Vector (0, 5, 10); |
---|
| 125 | break; |
---|
| 126 | } |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | |
---|
[3636] | 130 | /** |
---|
[3639] | 131 | \brief Updates the position of the camera. |
---|
| 132 | \param dt The time that elapsed. |
---|
| 133 | */ |
---|
| 134 | void Camera::tick(float dt) |
---|
| 135 | { |
---|
| 136 | dt /= 1000; |
---|
| 137 | this->fovy += (this->toFovy - this->fovy) * dt; |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | |
---|
| 141 | /** |
---|
[2551] | 142 | \brief initialize rendering perspective according to this camera |
---|
[2096] | 143 | |
---|
[2551] | 144 | This is called immediately before the rendering cycle starts, it sets all global |
---|
| 145 | rendering options as well as the GL_PROJECTION matrix according to the camera. |
---|
[2096] | 146 | */ |
---|
[2068] | 147 | void Camera::apply () |
---|
| 148 | { |
---|
[3636] | 149 | // switching to Projection Matrix |
---|
[2551] | 150 | glMatrixMode (GL_PROJECTION); |
---|
[2112] | 151 | glLoadIdentity (); |
---|
[3635] | 152 | |
---|
[3636] | 153 | // setting up the perspective |
---|
| 154 | gluPerspective(this->fovy, |
---|
| 155 | this->aspectRatio, |
---|
| 156 | this->nearClip, |
---|
| 157 | this->farClip); |
---|
[3365] | 158 | |
---|
[3636] | 159 | // speed-up feature |
---|
[3635] | 160 | Vector cameraPosition = this->getAbsCoor(); |
---|
| 161 | Vector targetPosition = this->target->getAbsCoor(); |
---|
[3638] | 162 | Vector up = Vector(0, 1, 0); |
---|
| 163 | up = this->getAbsDir().apply(up); |
---|
[2551] | 164 | |
---|
[3636] | 165 | // Setting the Camera Eye, lookAt and up Vectors |
---|
[3635] | 166 | gluLookAt(cameraPosition.x, cameraPosition.y, cameraPosition.z, |
---|
| 167 | targetPosition.x, targetPosition.y, targetPosition.z, |
---|
[3638] | 168 | up.x, up.y, up.z); |
---|
[3636] | 169 | |
---|
| 170 | // switching back to Modeling Matrix |
---|
[2068] | 171 | glMatrixMode (GL_MODELVIEW); |
---|
| 172 | } |
---|
| 173 | |
---|
| 174 | |
---|
[3365] | 175 | |
---|
[3635] | 176 | /////////////////// |
---|
| 177 | // CAMERA-TARGET // |
---|
| 178 | /////////////////// |
---|
[2636] | 179 | |
---|
| 180 | |
---|
[3635] | 181 | CameraTarget::CameraTarget() |
---|
[2636] | 182 | { |
---|
[3639] | 183 | this->setClassName("CameraTarget"); |
---|
[3638] | 184 | this->setMode(PNODE_MOVEMENT); |
---|
[2636] | 185 | } |
---|
[3213] | 186 | |
---|
[3635] | 187 | CameraTarget::~CameraTarget() |
---|
| 188 | { |
---|
[3213] | 189 | |
---|
[3635] | 190 | } |
---|