1 // 2 // SuperTuxKart - a fun racing game with go-kart 3 // Copyright (C) 2004-2015 Steve Baker <sjbaker1@airmail.net> 4 // Copyright (C) 2006-2015 SuperTuxKart-Team, Steve Baker 5 // 6 // This program is free software; you can redistribute it and/or 7 // modify it under the terms of the GNU General Public License 8 // as published by the Free Software Foundation; either version 3 9 // of the License, or (at your option) any later version. 10 // 11 12 // This program is distributed in the hope that it will be useful, 13 // but WITHOUT ANY WARRANTY; without even the implied warranty of 14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 // GNU General Public License for more details. 16 // 17 // You should have received a copy of the GNU General Public License 18 // along with this program; if not, write to the Free Software 19 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 20 21 #ifndef HEADER_CAMERA_HPP 22 #define HEADER_CAMERA_HPP 23 24 #include "io/xml_node.hpp" 25 #include "utils/no_copy.hpp" 26 #include "utils/aligned_array.hpp" 27 #include "utils/leak_check.hpp" 28 #include "utils/log.hpp" 29 #include "utils/vec3.hpp" 30 31 #include "matrix4.h" 32 #include "rect.h" 33 #include "SColor.h" 34 #include "vector2d.h" 35 36 #include <vector> 37 38 #include "ICameraSceneNode.h" 39 40 class AbstractKart; 41 42 /** 43 * \brief This is the base class for all cameras. It also includes some 44 * static functios to keep track of all cameras (e.g. a static function to 45 * create a camera, get a camera with a specified index). 46 * \ingroup graphics 47 */ 48 class Camera : public NoCopy 49 { 50 public: 51 /** The different camera types that can be used. */ 52 enum CameraType 53 { 54 CM_TYPE_NORMAL, 55 CM_TYPE_DEBUG, //!< A debug camera. 56 CM_TYPE_FPS, //!< FPS Camera 57 CM_TYPE_END //!< End camera 58 }; // CameraType 59 60 /* Only used for the normal camera. */ 61 enum Mode 62 { 63 CM_NORMAL, //!< Normal camera mode 64 CM_CLOSEUP, //!< Closer to kart 65 CM_REVERSE, //!< Looking backwards 66 CM_LEADER_MODE, //!< for deleted player karts in follow the leader 67 CM_SPECTATOR_SOCCER, //!< for spectator (in soccer mode) 68 CM_SPECTATOR_TOP_VIEW, //!< for spectator (top view on ball if soccer or top view on kart) 69 CM_SIMPLE_REPLAY, 70 CM_FALLING 71 }; // Mode 72 73 74 private: 75 static Camera* s_active_camera; 76 77 /** The project-view matrix of the previous frame, used for the blur shader. */ 78 core::matrix4 m_previous_pv_matrix; 79 80 /** Camera's mode. */ 81 Mode m_mode; 82 Mode m_previous_mode; 83 84 /** The type of the camera. */ 85 CameraType m_type; 86 87 /** The default type for any newly created camera. Used to store command 88 * line parameters. */ 89 static CameraType m_default_type; 90 91 /** The index of this camera which is the index of the kart it is 92 * attached to. */ 93 unsigned int m_index; 94 95 /** Current ambient light for this camera. */ 96 video::SColor m_ambient_light; 97 98 /** A pointer to the original kart the camera was pointing at when it 99 * was created. Used when restarting a race (since the camera might 100 * get attached to another kart if a kart is elimiated). */ 101 AbstractKart *m_original_kart; 102 103 /** The viewport for this camera (portion of the game window covered by this camera) */ 104 core::recti m_viewport; 105 106 /** The scaling necessary for each axis. */ 107 core::vector2df m_scaling; 108 109 /** Field of view for the camera. */ 110 float m_fov; 111 112 /** Aspect ratio for camera. */ 113 float m_aspect; 114 115 116 /** List of all cameras. */ 117 static std::vector<Camera*> m_all_cameras; 118 119 protected: 120 /** The camera scene node. */ 121 scene::ICameraSceneNode *m_camera; 122 123 /** The kart that the camera follows. It can't be const, 124 * since in profile mode the camera might change its owner. 125 * May be NULL (example: cutscene camera) 126 */ 127 AbstractKart *m_kart; 128 129 static Camera* createCamera(unsigned int index, CameraType type, 130 AbstractKart* kart); 131 132 Camera(CameraType type, int camera_index, AbstractKart* kart); 133 virtual ~Camera(); 134 virtual void reset(); 135 public: 136 LEAK_CHECK() 137 138 // ======================================================================== 139 // Static functions 140 static Camera* createCamera(AbstractKart* kart, const int index); 141 static void resetAllCameras(); 142 static void changeCamera(unsigned int camera_index, CameraType type); 143 144 // ------------------------------------------------------------------------ 145 /** Sets the default type for each camera that will be created. Used for 146 * command line parameters to select a debug etc camera. */ setDefaultCameraType(CameraType type)147 static void setDefaultCameraType(CameraType type) { m_default_type = type;} 148 // ------------------------------------------------------------------------ 149 /** Returns the default type for each camera that will be created. Used 150 * for command line parameters to select a debug etc camera. */ getDefaultCameraType()151 static CameraType getDefaultCameraType() { return m_default_type;} 152 // ------------------------------------------------------------------------ 153 /** Returns the number of cameras used. */ getNumCameras()154 static unsigned int getNumCameras() 155 { 156 return (unsigned int)m_all_cameras.size(); 157 } // getNumCameras 158 // ------------------------------------------------------------------------ 159 /** Returns a camera. */ getCamera(unsigned int n)160 static Camera *getCamera(unsigned int n) { return m_all_cameras[n]; } 161 // ------------------------------------------------------------------------ 162 /** Returns the currently active camera. */ getActiveCamera()163 static Camera* getActiveCamera() { return s_active_camera; } 164 // ------------------------------------------------------------------------ 165 /** Remove all cameras. */ removeAllCameras()166 static void removeAllCameras() 167 { 168 for(unsigned int i=0; i<m_all_cameras.size(); i++) 169 delete m_all_cameras[i]; 170 m_all_cameras.clear(); 171 } // removeAllCameras 172 173 // ======================================================================== 174 175 void setMode(Mode mode); /** Set the camera to the given mode */ 176 Mode getMode(); 177 Mode getPreviousMode(); 178 bool isSpectatorMode(); 179 void setNextSpectatorMode(); 180 void setKart(AbstractKart *new_kart); 181 virtual void setInitialTransform(); 182 virtual void activate(bool alsoActivateInIrrlicht=true); 183 virtual void update(float dt); 184 // ------------------------------------------------------------------------ 185 /** Returns the type of this camera. */ getType()186 CameraType getType() { return m_type; } 187 // ------------------------------------------------------------------------ 188 /** Sets the field of view for the irrlicht camera. */ setFoV()189 void setFoV() { m_camera->setFOV(m_fov); } 190 // ------------------------------------------------------------------------ 191 /** Returns the camera index (or player kart index, which is the same). */ getIndex() const192 int getIndex() const {return m_index;} 193 // ------------------------------------------------------------------------ 194 /** Returns the project-view matrix of the previous frame. */ getPreviousPVMatrix() const195 core::matrix4 getPreviousPVMatrix() const { return m_previous_pv_matrix; } 196 197 // ------------------------------------------------------------------------ 198 /** Returns the project-view matrix of the previous frame. */ setPreviousPVMatrix(core::matrix4 mat)199 void setPreviousPVMatrix(core::matrix4 mat) { m_previous_pv_matrix = mat; } 200 201 // ------------------------------------------------------------------------ 202 /** Returns the kart to which this camera is attached. */ getKart() const203 const AbstractKart* getKart() const { return m_kart; } 204 205 // ------------------------------------------------------------------------ 206 /** Returns the kart to which this camera is attached. */ getKart()207 AbstractKart* getKart() { return m_kart; } 208 209 // ------------------------------------------------------------------------ 210 /** Sets the ambient light for this camera. */ setAmbientLight(const video::SColor & color)211 void setAmbientLight(const video::SColor &color) { m_ambient_light=color; } 212 213 // ------------------------------------------------------------------------ 214 /** Returns the current ambient light. */ getAmbientLight() const215 const video::SColor &getAmbientLight() const {return m_ambient_light; } 216 217 // ------------------------------------------------------------------------ 218 /** Returns the viewport of this camera. */ getViewport() const219 const core::recti& getViewport() const {return m_viewport; } 220 221 // ------------------------------------------------------------------------ 222 /** Returns the scaling in x/y direction for this camera. */ getScaling() const223 const core::vector2df& getScaling() const {return m_scaling; } 224 225 // ------------------------------------------------------------------------ 226 /** Returns the camera scene node. */ getCameraSceneNode()227 scene::ICameraSceneNode *getCameraSceneNode() { return m_camera; } 228 // ------------------------------------------------------------------------ 229 /** Returs the absolute position of the camera. */ getXYZ()230 Vec3 getXYZ() { return Vec3(m_camera->getPosition()); } 231 // ------------------------------------------------------------------------ 232 void setupCamera(); 233 }; // class Camera 234 235 #endif 236 237 /* EOF */ 238