BsCamera.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  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 "BsPrerequisites.h"
  28. #include "CmString.h"
  29. // Matrices & Vectors
  30. #include "CmMatrix4.h"
  31. #include "CmVector3.h"
  32. #include "CmVector2.h"
  33. #include "CmAxisAlignedBox.h"
  34. #include "CmVertexData.h"
  35. #include "CmIndexData.h"
  36. #include "CmPlane.h"
  37. #include "CmQuaternion.h"
  38. #include "CmCommonEnums.h"
  39. #include "CmRay.h"
  40. #include "CmComponent.h"
  41. namespace BansheeEngine {
  42. /** Specifies perspective (realistic) or orthographic (architectural) projection.
  43. */
  44. enum ProjectionType
  45. {
  46. PT_ORTHOGRAPHIC,
  47. PT_PERSPECTIVE
  48. };
  49. /** Worldspace clipping planes.
  50. */
  51. enum FrustumPlane
  52. {
  53. FRUSTUM_PLANE_NEAR = 0,
  54. FRUSTUM_PLANE_FAR = 1,
  55. FRUSTUM_PLANE_LEFT = 2,
  56. FRUSTUM_PLANE_RIGHT = 3,
  57. FRUSTUM_PLANE_TOP = 4,
  58. FRUSTUM_PLANE_BOTTOM = 5
  59. };
  60. /** \addtogroup Core
  61. * @{
  62. */
  63. /** \addtogroup Scene
  64. * @{
  65. */
  66. /** A viewpoint from which the scene will be rendered.
  67. @remarks
  68. OGRE renders scenes from a camera viewpoint into a buffer of
  69. some sort, normally a window or a texture (a subclass of
  70. RenderTarget). OGRE cameras support both perspective projection (the default,
  71. meaning objects get smaller the further away they are) and
  72. orthographic projection (blueprint-style, no decrease in size
  73. with distance). Each camera carries with it a style of rendering,
  74. e.g. full textured, flat shaded, wireframe), field of view,
  75. rendering distances etc, allowing you to use OGRE to create
  76. complex multi-window views if required. In addition, more than
  77. one camera can point at a single render target if required,
  78. each rendering to a subset of the target, allowing split screen
  79. and picture-in-picture views.
  80. @par
  81. Cameras maintain their own aspect ratios, field of view, and frustum,
  82. and project co-ordinates into a space measured from -1 to 1 in x and y,
  83. and 0 to 1 in z. At render time, the camera will be rendering to a
  84. Viewport which will translate these parametric co-ordinates into real screen
  85. co-ordinates. Obviously it is advisable that the viewport has the same
  86. aspect ratio as the camera to avoid distortion (unless you want it!).
  87. @par
  88. Note that a Camera can be attached to a SceneNode, using the method
  89. SceneNode::attachObject. If this is done the Camera will combine it's own
  90. position/orientation settings with it's parent SceneNode.
  91. This is useful for implementing more complex Camera / object
  92. relationships i.e. having a camera attached to a world object.
  93. */
  94. class BS_EXPORT Camera : public CM::Component
  95. {
  96. protected:
  97. /// Orthographic or perspective?
  98. ProjectionType mProjType;
  99. /// y-direction field-of-view (default 45)
  100. CM::Radian mHorzFOV;
  101. /// Far clip distance - default 10000
  102. float mFarDist;
  103. /// Near clip distance - default 100
  104. float mNearDist;
  105. /// x/y viewport ratio - default 1.3333
  106. float mAspect;
  107. /// Ortho height size (world units)
  108. float mOrthoHeight;
  109. /// Off-axis frustum center offset - default (0.0, 0.0)
  110. CM::Vector2 mFrustumOffset;
  111. /// Focal length of frustum (for stereo rendering, defaults to 1.0)
  112. float mFocalLength;
  113. /// The 6 main clipping planes
  114. mutable CM::Plane mFrustumPlanes[6];
  115. /// Stored versions of parent orientation / position
  116. mutable CM::Quaternion mLastParentOrientation;
  117. mutable CM::Vector3 mLastParentPosition;
  118. /// Pre-calced projection matrix for the specific render system
  119. mutable CM::Matrix4 mProjMatrixRS;
  120. /// Pre-calced standard projection matrix but with render system depth range
  121. mutable CM::Matrix4 mProjMatrixRSDepth;
  122. /// Pre-calced standard projection matrix
  123. mutable CM::Matrix4 mProjMatrix;
  124. /// Pre-calced view matrix
  125. mutable CM::Matrix4 mViewMatrix;
  126. /// Something's changed in the frustum shape?
  127. mutable bool mRecalcFrustum;
  128. /// Something re the frustum planes has changed
  129. mutable bool mRecalcFrustumPlanes;
  130. /// Something re the world space corners has changed
  131. mutable bool mRecalcWorldSpaceCorners;
  132. /// Something re the vertex data has changed
  133. mutable bool mRecalcVertexData;
  134. /// Are we using a custom view matrix?
  135. bool mCustomViewMatrix;
  136. /// Are we using a custom projection matrix?
  137. bool mCustomProjMatrix;
  138. /// Have the frustum extents been manually set?
  139. bool mFrustumExtentsManuallySet;
  140. bool mIgnoreSceneRenderables;
  141. /// Frustum extents
  142. mutable float mLeft, mRight, mTop, mBottom;
  143. // Internal functions for calcs
  144. virtual void calcProjectionParameters(float& left, float& right, float& bottom, float& top) const;
  145. /// Update frustum if out of date
  146. virtual void updateFrustum(void) const;
  147. /// Implementation of updateFrustum (called if out of date)
  148. virtual void updateFrustumImpl(void) const;
  149. virtual void updateFrustumPlanes(void) const;
  150. /// Implementation of updateFrustumPlanes (called if out of date)
  151. virtual void updateFrustumPlanesImpl(void) const;
  152. virtual void updateWorldSpaceCorners(void) const;
  153. /// Implementation of updateWorldSpaceCorners (called if out of date)
  154. virtual void updateWorldSpaceCornersImpl(void) const;
  155. virtual void updateView(void) const;
  156. virtual bool isFrustumOutOfDate(void) const;
  157. /// Signal to update frustum information.
  158. virtual void invalidateFrustum(void) const;
  159. mutable CM::AxisAlignedBox mBoundingBox;
  160. mutable CM::Vector3 mWorldSpaceCorners[8];
  161. public:
  162. /** Sets the Y-dimension Field Of View (FOV) of the frustum.
  163. @remarks
  164. Field Of View (FOV) is the angle made between the frustum's position, and the edges
  165. of the 'screen' onto which the scene is projected. High values (90+ degrees) result in a wide-angle,
  166. fish-eye kind of view, low values (30- degrees) in a stretched, telescopic kind of view. Typical values
  167. are between 45 and 60 degrees.
  168. @par
  169. This value represents the VERTICAL field-of-view. The horizontal field of view is calculated from
  170. this depending on the dimensions of the viewport (they will only be the same if the viewport is square).
  171. @note
  172. Setting the FOV overrides the value supplied for frustum::setNearClipPlane.
  173. */
  174. virtual void setHorzFOV(const CM::Radian& fovy);
  175. /** Retrieves the frustums Y-dimension Field Of View (FOV).
  176. */
  177. virtual const CM::Radian& getHorzFOV(void) const;
  178. /** Sets the position of the near clipping plane.
  179. @remarks
  180. The position of the near clipping plane is the distance from the frustums position to the screen
  181. on which the world is projected. The near plane distance, combined with the field-of-view and the
  182. aspect ratio, determines the size of the viewport through which the world is viewed (in world
  183. co-ordinates). Note that this world viewport is different to a screen viewport, which has it's
  184. dimensions expressed in pixels. The frustums viewport should have the same aspect ratio as the
  185. screen viewport it renders into to avoid distortion.
  186. @param
  187. near The distance to the near clipping plane from the frustum in world coordinates.
  188. */
  189. virtual void setNearClipDistance(float nearDist);
  190. /** Sets the position of the near clipping plane.
  191. */
  192. virtual float getNearClipDistance(void) const;
  193. /** Sets the distance to the far clipping plane.
  194. @remarks
  195. The view frustum is a pyramid created from the frustum position and the edges of the viewport.
  196. This method sets the distance for the far end of that pyramid.
  197. Different applications need different values: e.g. a flight sim
  198. needs a much further far clipping plane than a first-person
  199. shooter. An important point here is that the larger the ratio
  200. between near and far clipping planes, the lower the accuracy of
  201. the Z-buffer used to depth-cue pixels. This is because the
  202. Z-range is limited to the size of the Z buffer (16 or 32-bit)
  203. and the max values must be spread over the gap between near and
  204. far clip planes. As it happens, you can affect the accuracy far
  205. more by altering the near distance rather than the far distance,
  206. but keep this in mind.
  207. @param
  208. far The distance to the far clipping plane from the frustum in
  209. world coordinates.If you specify 0, this means an infinite view
  210. distance which is useful especially when projecting shadows; but
  211. be careful not to use a near distance too close.
  212. */
  213. virtual void setFarClipDistance(float farDist);
  214. /** Retrieves the distance from the frustum to the far clipping plane.
  215. */
  216. virtual float getFarClipDistance(void) const;
  217. /** Sets the aspect ratio for the frustum viewport.
  218. @remarks
  219. The ratio between the x and y dimensions of the rectangular area visible through the frustum
  220. is known as aspect ratio: aspect = width / height .
  221. @par
  222. The default for most fullscreen windows is 1.3333 - this is also assumed by Ogre unless you
  223. use this method to state otherwise.
  224. */
  225. virtual void setAspectRatio(float ratio);
  226. /** Retreives the current aspect ratio.
  227. */
  228. virtual float getAspectRatio(void) const;
  229. /** Sets frustum offsets, used in stereo rendering.
  230. @remarks
  231. You can set both horizontal and vertical plane offsets of "eye"; in
  232. stereo rendering frustum is moved in horizontal plane. To be able to
  233. render from two "eyes" you'll need two cameras rendering on two
  234. RenderTargets.
  235. @par
  236. The frustum offsets is in world coordinates, and default to (0, 0) - no offsets.
  237. @param
  238. offset The horizontal and vertical plane offsets.
  239. */
  240. virtual void setFrustumOffset(const CM::Vector2& offset);
  241. /** Sets frustum offsets, used in stereo rendering.
  242. @remarks
  243. You can set both horizontal and vertical plane offsets of "eye"; in
  244. stereo rendering frustum is moved in horizontal plane. To be able to
  245. render from two "eyes" you'll need two cameras rendering on two
  246. RenderTargets.
  247. @par
  248. The frustum offsets is in world coordinates, and default to (0, 0) - no offsets.
  249. @param
  250. horizontal The horizontal plane offset.
  251. @param
  252. vertical The vertical plane offset.
  253. */
  254. virtual void setFrustumOffset(float horizontal = 0.0, float vertical = 0.0);
  255. /** Retrieves the frustum offsets.
  256. */
  257. virtual const CM::Vector2& getFrustumOffset() const;
  258. /** Sets frustum focal length (used in stereo rendering).
  259. @param
  260. focalLength The distance to the focal plane from the frustum in world coordinates.
  261. */
  262. virtual void setFocalLength(float focalLength = 1.0);
  263. /** Returns focal length of frustum.
  264. */
  265. virtual float getFocalLength() const;
  266. /** Manually set the extents of the frustum.
  267. @param left, right, top, bottom The position where the side clip planes intersect
  268. the near clip plane, in eye space
  269. */
  270. virtual void setFrustumExtents(float left, float right, float top, float bottom);
  271. /** Reset the frustum extents to be automatically derived from other params. */
  272. virtual void resetFrustumExtents();
  273. /** Get the extents of the frustum in view space. */
  274. virtual void getFrustumExtents(float& outleft, float& outright, float& outtop, float& outbottom) const;
  275. /** Gets the projection matrix for this frustum adjusted for the current
  276. rendersystem specifics (may be right or left-handed, depth range
  277. may vary).
  278. @remarks
  279. This method retrieves the rendering-API dependent version of the projection
  280. matrix. If you want a 'typical' projection matrix then use
  281. getProjectionMatrix.
  282. */
  283. virtual const CM::Matrix4& getProjectionMatrixRS(void) const;
  284. /** Gets the depth-adjusted projection matrix for the current rendersystem,
  285. but one which still conforms to right-hand rules.
  286. @remarks
  287. This differs from the rendering-API dependent getProjectionMatrix
  288. in that it always returns a right-handed projection matrix result
  289. no matter what rendering API is being used - this is required for
  290. vertex and fragment programs for example. However, the resulting depth
  291. range may still vary between render systems since D3D uses [0,1] and
  292. GL uses [-1,1], and the range must be kept the same between programmable
  293. and fixed-function pipelines.
  294. */
  295. virtual const CM::Matrix4& getProjectionMatrixWithRSDepth(void) const;
  296. /** Gets the normal projection matrix for this frustum, ie the
  297. projection matrix which conforms to standard right-handed rules and
  298. uses depth range [-1,+1].
  299. @remarks
  300. This differs from the rendering-API dependent getProjectionMatrixRS
  301. in that it always returns a right-handed projection matrix with depth
  302. range [-1,+1], result no matter what rendering API is being used - this
  303. is required for some uniform algebra for example.
  304. */
  305. virtual const CM::Matrix4& getProjectionMatrix(void) const;
  306. /** Gets the view matrix for this frustum. Mainly for use by OGRE internally.
  307. */
  308. virtual const CM::Matrix4& getViewMatrix(void) const;
  309. /** Set whether to use a custom view matrix on this frustum.
  310. @remarks
  311. This is an advanced method which allows you to manually set
  312. the view matrix on this frustum, rather than having it calculate
  313. itself based on it's position and orientation.
  314. @note
  315. After enabling a custom view matrix, the frustum will no longer
  316. update on its own based on position / orientation changes. You
  317. are completely responsible for keeping the view matrix up to date.
  318. The custom matrix will be returned from getViewMatrix.
  319. @param enable If true, the custom view matrix passed as the second
  320. parameter will be used in preference to an auto calculated one. If
  321. false, the frustum will revert to auto calculating the view matrix.
  322. @param viewMatrix The custom view matrix to use, the matrix must be an
  323. affine matrix.
  324. @see Frustum::setCustomProjectionMatrix, Matrix4::isAffine
  325. */
  326. virtual void setCustomViewMatrix(bool enable,
  327. const CM::Matrix4& viewMatrix = CM::Matrix4::IDENTITY);
  328. /// Returns whether a custom view matrix is in use
  329. virtual bool isCustomViewMatrixEnabled(void) const
  330. { return mCustomViewMatrix; }
  331. /** Set whether to use a custom projection matrix on this frustum.
  332. @remarks
  333. This is an advanced method which allows you to manually set
  334. the projection matrix on this frustum, rather than having it
  335. calculate itself based on it's position and orientation.
  336. @note
  337. After enabling a custom projection matrix, the frustum will no
  338. longer update on its own based on field of view and near / far
  339. distance changes. You are completely responsible for keeping the
  340. projection matrix up to date if those values change. The custom
  341. matrix will be returned from getProjectionMatrix and derivative
  342. functions.
  343. @param enable If true, the custom projection matrix passed as the
  344. second parameter will be used in preference to an auto calculated
  345. one. If false, the frustum will revert to auto calculating the
  346. projection matrix.
  347. @param projectionMatrix The custom view matrix to use
  348. @see Frustum::setCustomViewMatrix
  349. */
  350. virtual void setCustomProjectionMatrix(bool enable,
  351. const CM::Matrix4& projectionMatrix = CM::Matrix4::IDENTITY);
  352. /// Returns whether a custom projection matrix is in use
  353. virtual bool isCustomProjectionMatrixEnabled(void) const
  354. { return mCustomProjMatrix; }
  355. /** Retrieves the clipping planes of the frustum (world space).
  356. @remarks
  357. The clipping planes are ordered as declared in enumerate constants FrustumPlane.
  358. */
  359. virtual const CM::Plane* getFrustumPlanes(void) const;
  360. /** Retrieves a specified plane of the frustum (world space).
  361. @remarks
  362. Gets a reference to one of the planes which make up the frustum frustum, e.g. for clipping purposes.
  363. */
  364. virtual const CM::Plane& getFrustumPlane( unsigned short plane ) const;
  365. /** Tests whether the given container is visible in the Frustum.
  366. @param
  367. bound Bounding box to be checked (world space)
  368. @param
  369. culledBy Optional pointer to an int which will be filled by the plane number which culled
  370. the box if the result was false;
  371. @returns
  372. If the box was visible, true is returned.
  373. @par
  374. Otherwise, false is returned.
  375. */
  376. virtual bool isVisible(const CM::AxisAlignedBox& bound, FrustumPlane* culledBy = 0) const;
  377. /** Tests whether the given container is visible in the Frustum.
  378. @param
  379. bound Bounding sphere to be checked (world space)
  380. @param
  381. culledBy Optional pointer to an int which will be filled by the plane number which culled
  382. the box if the result was false;
  383. @returns
  384. If the sphere was visible, true is returned.
  385. @par
  386. Otherwise, false is returned.
  387. */
  388. virtual bool isVisible(const CM::Sphere& bound, FrustumPlane* culledBy = 0) const;
  389. /** Tests whether the given vertex is visible in the Frustum.
  390. @param
  391. vert Vertex to be checked (world space)
  392. @param
  393. culledBy Optional pointer to an int which will be filled by the plane number which culled
  394. the box if the result was false;
  395. @returns
  396. If the box was visible, true is returned.
  397. @par
  398. Otherwise, false is returned.
  399. */
  400. virtual bool isVisible(const CM::Vector3& vert, FrustumPlane* culledBy = 0) const;
  401. /** Overridden from MovableObject */
  402. const CM::AxisAlignedBox& getBoundingBox(void) const;
  403. /** Overridden from MovableObject */
  404. float getBoundingRadius(void) const;
  405. /** Gets the world space corners of the frustum.
  406. @remarks
  407. The corners are ordered as follows: top-right near,
  408. top-left near, bottom-left near, bottom-right near,
  409. top-right far, top-left far, bottom-left far, bottom-right far.
  410. */
  411. virtual const CM::Vector3* getWorldSpaceCorners(void) const;
  412. /** Sets the type of projection to use (orthographic or perspective). Default is perspective.
  413. */
  414. virtual void setProjectionType(ProjectionType pt);
  415. /** Retrieves info on the type of projection used (orthographic or perspective).
  416. */
  417. virtual ProjectionType getProjectionType(void) const;
  418. /** Sets the orthographic window settings, for use with orthographic rendering only.
  419. @note Calling this method will recalculate the aspect ratio, use
  420. setOrthoWindowHeight or setOrthoWindowWidth alone if you wish to
  421. preserve the aspect ratio but just fit one or other dimension to a
  422. particular size.
  423. @param w, h The dimensions of the view window in world units
  424. */
  425. virtual void setOrthoWindow(float w, float h);
  426. /** Sets the orthographic window height, for use with orthographic rendering only.
  427. @note The width of the window will be calculated from the aspect ratio.
  428. @param h The height of the view window in world units
  429. */
  430. virtual void setOrthoWindowHeight(float h);
  431. /** Sets the orthographic window width, for use with orthographic rendering only.
  432. @note The height of the window will be calculated from the aspect ratio.
  433. @param w The width of the view window in world units
  434. */
  435. virtual void setOrthoWindowWidth(float w);
  436. /** Gets the orthographic window height, for use with orthographic rendering only.
  437. */
  438. virtual float getOrthoWindowHeight() const;
  439. /** Gets the orthographic window width, for use with orthographic rendering only.
  440. @note This is calculated from the orthographic height and the aspect ratio
  441. */
  442. virtual float getOrthoWindowWidth() const;
  443. void setIgnoreSceneRenderables(bool value) { mIgnoreSceneRenderables = true; }
  444. bool getIgnoreSceneRenderables() const { return mIgnoreSceneRenderables; }
  445. /// Small constant used to reduce far plane projection to avoid inaccuracies
  446. static const float INFINITE_FAR_PLANE_ADJUST;
  447. protected:
  448. CM::ViewportPtr mViewport;
  449. public:
  450. /** Standard destructor.
  451. */
  452. virtual ~Camera();
  453. void initialize(CM::RenderTargetPtr target = nullptr,
  454. float left = 0.0f, float top = 0.0f,
  455. float width = 1.0f, float height = 1.0f,
  456. int ZOrder = 0);
  457. CM::ViewportPtr getViewport() const { return mViewport; }
  458. /************************************************************************/
  459. /* COMPONENT OVERRIDES */
  460. /************************************************************************/
  461. protected:
  462. friend class CM::SceneObject;
  463. /** Standard constructor.
  464. */
  465. Camera(const CM::HSceneObject& parent);
  466. public:
  467. virtual void update() {}
  468. /************************************************************************/
  469. /* RTTI */
  470. /************************************************************************/
  471. public:
  472. friend class CameraRTTI;
  473. static CM::RTTITypeBase* getRTTIStatic();
  474. virtual CM::RTTITypeBase* getRTTI() const;
  475. protected:
  476. Camera() {} // Serialization only
  477. };
  478. /** @} */
  479. /** @} */
  480. } // namespace CamelotFramework
  481. #endif