CmCamera.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. /*
  2. -----------------------------------------------------------------------------
  3. This source file is part of OGRE
  4. (Object-oriented Graphics Rendering Engine)
  5. For the latest info, see http://www.ogre3d.org
  6. Copyright (c) 2000-2011 Torus Knot Software Ltd
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. -----------------------------------------------------------------------------
  23. */
  24. #ifndef __Camera_H__
  25. #define __Camera_H__
  26. // Default options
  27. #include "CmPrerequisites.h"
  28. #include "CmString.h"
  29. // Matrices & Vectors
  30. #include "CmMatrix4.h"
  31. #include "CmVector3.h"
  32. #include "CmPlane.h"
  33. #include "CmQuaternion.h"
  34. #include "CmCommon.h"
  35. #include "CmFrustum.h"
  36. #include "CmRay.h"
  37. namespace CamelotEngine {
  38. /** \addtogroup Core
  39. * @{
  40. */
  41. /** \addtogroup Scene
  42. * @{
  43. */
  44. /** A viewpoint from which the scene will be rendered.
  45. @remarks
  46. OGRE renders scenes from a camera viewpoint into a buffer of
  47. some sort, normally a window or a texture (a subclass of
  48. RenderTarget). OGRE cameras support both perspective projection (the default,
  49. meaning objects get smaller the further away they are) and
  50. orthographic projection (blueprint-style, no decrease in size
  51. with distance). Each camera carries with it a style of rendering,
  52. e.g. full textured, flat shaded, wireframe), field of view,
  53. rendering distances etc, allowing you to use OGRE to create
  54. complex multi-window views if required. In addition, more than
  55. one camera can point at a single render target if required,
  56. each rendering to a subset of the target, allowing split screen
  57. and picture-in-picture views.
  58. @par
  59. Cameras maintain their own aspect ratios, field of view, and frustum,
  60. and project co-ordinates into a space measured from -1 to 1 in x and y,
  61. and 0 to 1 in z. At render time, the camera will be rendering to a
  62. Viewport which will translate these parametric co-ordinates into real screen
  63. co-ordinates. Obviously it is advisable that the viewport has the same
  64. aspect ratio as the camera to avoid distortion (unless you want it!).
  65. @par
  66. Note that a Camera can be attached to a SceneNode, using the method
  67. SceneNode::attachObject. If this is done the Camera will combine it's own
  68. position/orientation settings with it's parent SceneNode.
  69. This is useful for implementing more complex Camera / object
  70. relationships i.e. having a camera attached to a world object.
  71. */
  72. class CM_EXPORT Camera : public Frustum
  73. {
  74. protected:
  75. /// Camera orientation, quaternion style
  76. Quaternion mOrientation;
  77. /// Camera position - default (0,0,0)
  78. Vector3 mPosition;
  79. /// Derived orientation/position of the camera, including reflection
  80. mutable Quaternion mDerivedOrientation;
  81. mutable Vector3 mDerivedPosition;
  82. /// float world orientation/position of the camera
  83. mutable Quaternion mRealOrientation;
  84. mutable Vector3 mRealPosition;
  85. /// Whether to yaw around a fixed axis.
  86. bool mYawFixed;
  87. /// Fixed axis to yaw around
  88. Vector3 mYawFixedAxis;
  89. /// Rendering type
  90. PolygonMode mSceneDetail;
  91. /** Viewing window.
  92. @remarks
  93. Generalize camera class for the case, when viewing frustum doesn't cover all viewport.
  94. */
  95. float mWLeft, mWTop, mWRight, mWBottom;
  96. /// Is viewing window used.
  97. bool mWindowSet;
  98. /// Windowed viewport clip planes
  99. mutable vector<Plane>::type mWindowClipPlanes;
  100. // Was viewing window changed.
  101. mutable bool mRecalcWindow;
  102. /// The last viewport to be added using this camera
  103. Viewport* mLastViewport;
  104. /** Whether aspect ratio will automatically be recalculated
  105. when a viewport changes its size
  106. */
  107. bool mAutoAspectRatio;
  108. /// Custom culling frustum
  109. Frustum *mCullFrustum;
  110. /// Whether or not the rendering distance of objects should take effect for this camera
  111. bool mUseRenderingDistance;
  112. // Internal functions for calcs
  113. bool isViewOutOfDate(void) const;
  114. /// Signal to update frustum information.
  115. void invalidateFrustum(void) const;
  116. /// Signal to update view information.
  117. void invalidateView(void) const;
  118. /** Do actual window setting, using parameters set in SetWindow call
  119. @remarks
  120. The method will called on demand.
  121. */
  122. virtual void setWindowImpl(void) const;
  123. /** Helper function for forwardIntersect that intersects rays with canonical plane */
  124. virtual vector<Vector4>::type getRayForwardIntersect(const Vector3& anchor, const Vector3 *dir, float planeOffset) const;
  125. public:
  126. /** Standard constructor.
  127. */
  128. Camera( const String& name);
  129. /** Standard destructor.
  130. */
  131. virtual ~Camera();
  132. /** Sets the level of rendering detail required from this camera.
  133. @remarks
  134. Each camera is set to render at full detail by default, that is
  135. with full texturing, lighting etc. This method lets you change
  136. that behaviour, allowing you to make the camera just render a
  137. wireframe view, for example.
  138. */
  139. void setPolygonMode(PolygonMode sd);
  140. /** Retrieves the level of detail that the camera will render.
  141. */
  142. PolygonMode getPolygonMode(void) const;
  143. /** Sets the camera's position.
  144. */
  145. void setPosition(float x, float y, float z);
  146. /** Sets the camera's position.
  147. */
  148. void setPosition(const Vector3& vec);
  149. /** Retrieves the camera's position.
  150. */
  151. const Vector3& getPosition(void) const;
  152. /** Moves the camera's position by the vector offset provided along world axes.
  153. */
  154. void move(const Vector3& vec);
  155. /** Moves the camera's position by the vector offset provided along it's own axes (relative to orientation).
  156. */
  157. void moveRelative(const Vector3& vec);
  158. /** Sets the camera's direction vector.
  159. @remarks
  160. Note that the 'up' vector for the camera will automatically be recalculated based on the
  161. current 'up' vector (i.e. the roll will remain the same).
  162. */
  163. void setDirection(float x, float y, float z);
  164. /** Sets the camera's direction vector.
  165. */
  166. void setDirection(const Vector3& vec);
  167. /* Gets the camera's direction.
  168. */
  169. Vector3 getDirection(void) const;
  170. /** Gets the camera's up vector.
  171. */
  172. Vector3 getUp(void) const;
  173. /** Gets the camera's right vector.
  174. */
  175. Vector3 getRight(void) const;
  176. /** Points the camera at a location in worldspace.
  177. @remarks
  178. This is a helper method to automatically generate the
  179. direction vector for the camera, based on it's current position
  180. and the supplied look-at point.
  181. @param
  182. targetPoint A vector specifying the look at point.
  183. */
  184. void lookAt( const Vector3& targetPoint );
  185. /** Points the camera at a location in worldspace.
  186. @remarks
  187. This is a helper method to automatically generate the
  188. direction vector for the camera, based on it's current position
  189. and the supplied look-at point.
  190. @param
  191. x
  192. @param
  193. y
  194. @param
  195. z Co-ordinates of the point to look at.
  196. */
  197. void lookAt(float x, float y, float z);
  198. /** Rolls the camera anticlockwise, around its local z axis.
  199. */
  200. void roll(const Radian& angle);
  201. /** Rotates the camera anticlockwise around it's local y axis.
  202. */
  203. void yaw(const Radian& angle);
  204. /** Pitches the camera up/down anticlockwise around it's local z axis.
  205. */
  206. void pitch(const Radian& angle);
  207. /** Rotate the camera around an arbitrary axis.
  208. */
  209. void rotate(const Vector3& axis, const Radian& angle);
  210. /** Rotate the camera around an arbitrary axis using a Quaternion.
  211. */
  212. void rotate(const Quaternion& q);
  213. /** Tells the camera whether to yaw around it's own local Y axis or a
  214. fixed axis of choice.
  215. @remarks
  216. This method allows you to change the yaw behaviour of the camera
  217. - by default, the camera yaws around a fixed Y axis. This is
  218. often what you want - for example if you're making a first-person
  219. shooter, you really don't want the yaw axis to reflect the local
  220. camera Y, because this would mean a different yaw axis if the
  221. player is looking upwards rather than when they are looking
  222. straight ahead. You can change this behaviour by calling this
  223. method, which you will want to do if you are making a completely
  224. free camera like the kind used in a flight simulator.
  225. @param
  226. useFixed If true, the axis passed in the second parameter will
  227. always be the yaw axis no matter what the camera orientation.
  228. If false, the camera yaws around the local Y.
  229. @param
  230. fixedAxis The axis to use if the first parameter is true.
  231. */
  232. void setFixedYawAxis( bool useFixed, const Vector3& fixedAxis = Vector3::UNIT_Y );
  233. /** Returns the camera's current orientation.
  234. */
  235. const Quaternion& getOrientation(void) const;
  236. /** Sets the camera's orientation.
  237. */
  238. void setOrientation(const Quaternion& q);
  239. /** Tells the Camera to contact the SceneManager to render from it's viewpoint.
  240. @param vp The viewport to render to
  241. @param includeOverlays Whether or not any overlay objects should be included
  242. */
  243. void _renderScene(Viewport *vp, bool includeOverlays);
  244. /** Gets the derived orientation of the camera, including any
  245. rotation inherited from a node attachment and reflection matrix. */
  246. const Quaternion& getDerivedOrientation(void) const;
  247. /** Gets the derived position of the camera, including any
  248. translation inherited from a node attachment and reflection matrix. */
  249. const Vector3& getDerivedPosition(void) const;
  250. /** Gets the derived direction vector of the camera, including any
  251. rotation inherited from a node attachment and reflection matrix. */
  252. Vector3 getDerivedDirection(void) const;
  253. /** Gets the derived up vector of the camera, including any
  254. rotation inherited from a node attachment and reflection matrix. */
  255. Vector3 getDerivedUp(void) const;
  256. /** Gets the derived right vector of the camera, including any
  257. rotation inherited from a node attachment and reflection matrix. */
  258. Vector3 getDerivedRight(void) const;
  259. /** Gets the real world orientation of the camera, including any
  260. rotation inherited from a node attachment */
  261. const Quaternion& getRealOrientation(void) const;
  262. /** Gets the real world position of the camera, including any
  263. translation inherited from a node attachment. */
  264. const Vector3& getRealPosition(void) const;
  265. /** Gets the real world direction vector of the camera, including any
  266. rotation inherited from a node attachment. */
  267. Vector3 getRealDirection(void) const;
  268. /** Gets the real world up vector of the camera, including any
  269. rotation inherited from a node attachment. */
  270. Vector3 getRealUp(void) const;
  271. /** Gets the real world right vector of the camera, including any
  272. rotation inherited from a node attachment. */
  273. Vector3 getRealRight(void) const;
  274. /** Gets a world space ray as cast from the camera through a viewport position.
  275. @param screenx, screeny The x and y position at which the ray should intersect the viewport,
  276. in normalised screen coordinates [0,1]
  277. */
  278. Ray getCameraToViewportRay(float screenx, float screeny) const;
  279. /** Gets a world space ray as cast from the camera through a viewport position.
  280. @param screenx, screeny The x and y position at which the ray should intersect the viewport,
  281. in normalised screen coordinates [0,1]
  282. @param outRay Ray instance to populate with result
  283. */
  284. void getCameraToViewportRay(float screenx, float screeny, Ray* outRay) const;
  285. /** Sets the viewing window inside of viewport.
  286. @remarks
  287. This method can be used to set a subset of the viewport as the rendering
  288. target.
  289. @param Left Relative to Viewport - 0 corresponds to left edge, 1 - to right edge (default - 0).
  290. @param Top Relative to Viewport - 0 corresponds to top edge, 1 - to bottom edge (default - 0).
  291. @param Right Relative to Viewport - 0 corresponds to left edge, 1 - to right edge (default - 1).
  292. @param Bottom Relative to Viewport - 0 corresponds to top edge, 1 - to bottom edge (default - 1).
  293. */
  294. virtual void setWindow (float Left, float Top, float Right, float Bottom);
  295. /// Cancel view window.
  296. virtual void resetWindow (void);
  297. /// Returns if a viewport window is being used
  298. virtual bool isWindowSet(void) const { return mWindowSet; }
  299. /// Gets the window clip planes, only applicable if isWindowSet == true
  300. const vector<Plane>::type& getWindowPlanes(void) const;
  301. /** Overridden from MovableObject */
  302. float getBoundingRadius(void) const;
  303. /** Get the last viewport which was attached to this camera.
  304. @note This is not guaranteed to be the only viewport which is
  305. using this camera, just the last once which was created referring
  306. to it.
  307. */
  308. Viewport* getViewport(void) const {return mLastViewport;}
  309. /** Notifies this camera that a viewport is using it.*/
  310. void _notifyViewport(Viewport* viewport) {mLastViewport = viewport;}
  311. /** If set to true a viewport that owns this frustum will be able to
  312. recalculate the aspect ratio whenever the frustum is resized.
  313. @remarks
  314. You should set this to true only if the frustum / camera is used by
  315. one viewport at the same time. Otherwise the aspect ratio for other
  316. viewports may be wrong.
  317. */
  318. void setAutoAspectRatio(bool autoratio);
  319. /** Retrieves if AutoAspectRatio is currently set or not
  320. */
  321. bool getAutoAspectRatio(void) const;
  322. /** Tells the camera to use a separate Frustum instance to perform culling.
  323. @remarks
  324. By calling this method, you can tell the camera to perform culling
  325. against a different frustum to it's own. This is mostly useful for
  326. debug cameras that allow you to show the culling behaviour of another
  327. camera, or a manual frustum instance.
  328. @param frustum Pointer to a frustum to use; this can either be a manual
  329. Frustum instance (which you can attach to scene nodes like any other
  330. MovableObject), or another camera. If you pass 0 to this method it
  331. reverts the camera to normal behaviour.
  332. */
  333. void setCullingFrustum(Frustum* frustum) { mCullFrustum = frustum; }
  334. /** Returns the custom culling frustum in use. */
  335. Frustum* getCullingFrustum(void) const { return mCullFrustum; }
  336. /** Forward projects frustum rays to find forward intersection with plane.
  337. @remarks
  338. Forward projection may lead to intersections at infinity.
  339. */
  340. virtual void forwardIntersect(const Plane& worldPlane, vector<Vector4>::type* intersect3d) const;
  341. /// @copydoc Frustum::isVisible
  342. bool isVisible(const AxisAlignedBox& bound, FrustumPlane* culledBy = 0) const;
  343. /// @copydoc Frustum::isVisible
  344. bool isVisible(const Sphere& bound, FrustumPlane* culledBy = 0) const;
  345. /// @copydoc Frustum::isVisible
  346. bool isVisible(const Vector3& vert, FrustumPlane* culledBy = 0) const;
  347. /// @copydoc Frustum::getWorldSpaceCorners
  348. const Vector3* getWorldSpaceCorners(void) const;
  349. /// @copydoc Frustum::getFrustumPlane
  350. const Plane& getFrustumPlane( unsigned short plane ) const;
  351. /// @copydoc Frustum::projectSphere
  352. bool projectSphere(const Sphere& sphere,
  353. float* left, float* top, float* right, float* bottom) const;
  354. /// @copydoc Frustum::getNearClipDistance
  355. float getNearClipDistance(void) const;
  356. /// @copydoc Frustum::getFarClipDistance
  357. float getFarClipDistance(void) const;
  358. /// @copydoc Frustum::getViewMatrix
  359. const Matrix4& getViewMatrix(void) const;
  360. /** Specialised version of getViewMatrix allowing caller to differentiate
  361. whether the custom culling frustum should be allowed or not.
  362. @remarks
  363. The default behaviour of the standard getViewMatrix is to delegate to
  364. the alternate culling frustum, if it is set. This is expected when
  365. performing CPU calculations, but the final rendering must be performed
  366. using the real view matrix in order to display the correct debug view.
  367. */
  368. const Matrix4& getViewMatrix(bool ownFrustumOnly) const;
  369. /** Synchronise core camera settings with another.
  370. @remarks
  371. Copies the position, orientation, clip distances, projection type,
  372. FOV, focal length and aspect ratio from another camera. Other settings like query flags,
  373. reflection etc are preserved.
  374. */
  375. virtual void synchroniseBaseSettingsWith(const Camera* cam);
  376. /** Get the derived position of this frustum. */
  377. const Vector3& getPositionForViewUpdate(void) const;
  378. /** Get the derived orientation of this frustum. */
  379. const Quaternion& getOrientationForViewUpdate(void) const;
  380. };
  381. /** @} */
  382. /** @} */
  383. } // namespace CamelotEngine
  384. #endif