CmCamera.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  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 "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 CamelotEngine {
  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 CM_EXPORT Camera : public Component
  95. {
  96. protected:
  97. /// Orthographic or perspective?
  98. ProjectionType mProjType;
  99. /// y-direction field-of-view (default 45)
  100. Radian mFOVy;
  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. 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 Plane mFrustumPlanes[6];
  115. /// Stored versions of parent orientation / position
  116. mutable Quaternion mLastParentOrientation;
  117. mutable Vector3 mLastParentPosition;
  118. /// Pre-calced projection matrix for the specific render system
  119. mutable Matrix4 mProjMatrixRS;
  120. /// Pre-calced standard projection matrix but with render system depth range
  121. mutable Matrix4 mProjMatrixRSDepth;
  122. /// Pre-calced standard projection matrix
  123. mutable Matrix4 mProjMatrix;
  124. /// Pre-calced view matrix
  125. mutable 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. /// Frustum extents
  141. mutable float mLeft, mRight, mTop, mBottom;
  142. // Internal functions for calcs
  143. virtual void calcProjectionParameters(float& left, float& right, float& bottom, float& top) const;
  144. /// Update frustum if out of date
  145. virtual void updateFrustum(void) const;
  146. /// Implementation of updateFrustum (called if out of date)
  147. virtual void updateFrustumImpl(void) const;
  148. virtual void updateFrustumPlanes(void) const;
  149. /// Implementation of updateFrustumPlanes (called if out of date)
  150. virtual void updateFrustumPlanesImpl(void) const;
  151. virtual void updateWorldSpaceCorners(void) const;
  152. /// Implementation of updateWorldSpaceCorners (called if out of date)
  153. virtual void updateWorldSpaceCornersImpl(void) const;
  154. virtual void updateView(void) const;
  155. virtual bool isFrustumOutOfDate(void) const;
  156. /// Signal to update frustum information.
  157. virtual void invalidateFrustum(void) const;
  158. mutable AxisAlignedBox mBoundingBox;
  159. mutable Vector3 mWorldSpaceCorners[8];
  160. public:
  161. /** Sets the Y-dimension Field Of View (FOV) of the frustum.
  162. @remarks
  163. Field Of View (FOV) is the angle made between the frustum's position, and the edges
  164. of the 'screen' onto which the scene is projected. High values (90+ degrees) result in a wide-angle,
  165. fish-eye kind of view, low values (30- degrees) in a stretched, telescopic kind of view. Typical values
  166. are between 45 and 60 degrees.
  167. @par
  168. This value represents the VERTICAL field-of-view. The horizontal field of view is calculated from
  169. this depending on the dimensions of the viewport (they will only be the same if the viewport is square).
  170. @note
  171. Setting the FOV overrides the value supplied for frustum::setNearClipPlane.
  172. */
  173. virtual void setFOVy(const Radian& fovy);
  174. /** Retrieves the frustums Y-dimension Field Of View (FOV).
  175. */
  176. virtual const Radian& getFOVy(void) const;
  177. /** Sets the position of the near clipping plane.
  178. @remarks
  179. The position of the near clipping plane is the distance from the frustums position to the screen
  180. on which the world is projected. The near plane distance, combined with the field-of-view and the
  181. aspect ratio, determines the size of the viewport through which the world is viewed (in world
  182. co-ordinates). Note that this world viewport is different to a screen viewport, which has it's
  183. dimensions expressed in pixels. The frustums viewport should have the same aspect ratio as the
  184. screen viewport it renders into to avoid distortion.
  185. @param
  186. near The distance to the near clipping plane from the frustum in world coordinates.
  187. */
  188. virtual void setNearClipDistance(float nearDist);
  189. /** Sets the position of the near clipping plane.
  190. */
  191. virtual float getNearClipDistance(void) const;
  192. /** Sets the distance to the far clipping plane.
  193. @remarks
  194. The view frustum is a pyramid created from the frustum position and the edges of the viewport.
  195. This method sets the distance for the far end of that pyramid.
  196. Different applications need different values: e.g. a flight sim
  197. needs a much further far clipping plane than a first-person
  198. shooter. An important point here is that the larger the ratio
  199. between near and far clipping planes, the lower the accuracy of
  200. the Z-buffer used to depth-cue pixels. This is because the
  201. Z-range is limited to the size of the Z buffer (16 or 32-bit)
  202. and the max values must be spread over the gap between near and
  203. far clip planes. As it happens, you can affect the accuracy far
  204. more by altering the near distance rather than the far distance,
  205. but keep this in mind.
  206. @param
  207. far The distance to the far clipping plane from the frustum in
  208. world coordinates.If you specify 0, this means an infinite view
  209. distance which is useful especially when projecting shadows; but
  210. be careful not to use a near distance too close.
  211. */
  212. virtual void setFarClipDistance(float farDist);
  213. /** Retrieves the distance from the frustum to the far clipping plane.
  214. */
  215. virtual float getFarClipDistance(void) const;
  216. /** Sets the aspect ratio for the frustum viewport.
  217. @remarks
  218. The ratio between the x and y dimensions of the rectangular area visible through the frustum
  219. is known as aspect ratio: aspect = width / height .
  220. @par
  221. The default for most fullscreen windows is 1.3333 - this is also assumed by Ogre unless you
  222. use this method to state otherwise.
  223. */
  224. virtual void setAspectRatio(float ratio);
  225. /** Retreives the current aspect ratio.
  226. */
  227. virtual float getAspectRatio(void) const;
  228. /** Sets frustum offsets, used in stereo rendering.
  229. @remarks
  230. You can set both horizontal and vertical plane offsets of "eye"; in
  231. stereo rendering frustum is moved in horizontal plane. To be able to
  232. render from two "eyes" you'll need two cameras rendering on two
  233. RenderTargets.
  234. @par
  235. The frustum offsets is in world coordinates, and default to (0, 0) - no offsets.
  236. @param
  237. offset The horizontal and vertical plane offsets.
  238. */
  239. virtual void setFrustumOffset(const Vector2& offset);
  240. /** Sets frustum offsets, used in stereo rendering.
  241. @remarks
  242. You can set both horizontal and vertical plane offsets of "eye"; in
  243. stereo rendering frustum is moved in horizontal plane. To be able to
  244. render from two "eyes" you'll need two cameras rendering on two
  245. RenderTargets.
  246. @par
  247. The frustum offsets is in world coordinates, and default to (0, 0) - no offsets.
  248. @param
  249. horizontal The horizontal plane offset.
  250. @param
  251. vertical The vertical plane offset.
  252. */
  253. virtual void setFrustumOffset(float horizontal = 0.0, float vertical = 0.0);
  254. /** Retrieves the frustum offsets.
  255. */
  256. virtual const Vector2& getFrustumOffset() const;
  257. /** Sets frustum focal length (used in stereo rendering).
  258. @param
  259. focalLength The distance to the focal plane from the frustum in world coordinates.
  260. */
  261. virtual void setFocalLength(float focalLength = 1.0);
  262. /** Returns focal length of frustum.
  263. */
  264. virtual float getFocalLength() const;
  265. /** Manually set the extents of the frustum.
  266. @param left, right, top, bottom The position where the side clip planes intersect
  267. the near clip plane, in eye space
  268. */
  269. virtual void setFrustumExtents(float left, float right, float top, float bottom);
  270. /** Reset the frustum extents to be automatically derived from other params. */
  271. virtual void resetFrustumExtents();
  272. /** Get the extents of the frustum in view space. */
  273. virtual void getFrustumExtents(float& outleft, float& outright, float& outtop, float& outbottom) const;
  274. /** Gets the projection matrix for this frustum adjusted for the current
  275. rendersystem specifics (may be right or left-handed, depth range
  276. may vary).
  277. @remarks
  278. This method retrieves the rendering-API dependent version of the projection
  279. matrix. If you want a 'typical' projection matrix then use
  280. getProjectionMatrix.
  281. */
  282. virtual const Matrix4& getProjectionMatrixRS(void) const;
  283. /** Gets the depth-adjusted projection matrix for the current rendersystem,
  284. but one which still conforms to right-hand rules.
  285. @remarks
  286. This differs from the rendering-API dependent getProjectionMatrix
  287. in that it always returns a right-handed projection matrix result
  288. no matter what rendering API is being used - this is required for
  289. vertex and fragment programs for example. However, the resulting depth
  290. range may still vary between render systems since D3D uses [0,1] and
  291. GL uses [-1,1], and the range must be kept the same between programmable
  292. and fixed-function pipelines.
  293. */
  294. virtual const Matrix4& getProjectionMatrixWithRSDepth(void) const;
  295. /** Gets the normal projection matrix for this frustum, ie the
  296. projection matrix which conforms to standard right-handed rules and
  297. uses depth range [-1,+1].
  298. @remarks
  299. This differs from the rendering-API dependent getProjectionMatrixRS
  300. in that it always returns a right-handed projection matrix with depth
  301. range [-1,+1], result no matter what rendering API is being used - this
  302. is required for some uniform algebra for example.
  303. */
  304. virtual const Matrix4& getProjectionMatrix(void) const;
  305. /** Gets the view matrix for this frustum. Mainly for use by OGRE internally.
  306. */
  307. virtual const Matrix4& getViewMatrix(void) const;
  308. /** Set whether to use a custom view matrix on this frustum.
  309. @remarks
  310. This is an advanced method which allows you to manually set
  311. the view matrix on this frustum, rather than having it calculate
  312. itself based on it's position and orientation.
  313. @note
  314. After enabling a custom view matrix, the frustum will no longer
  315. update on its own based on position / orientation changes. You
  316. are completely responsible for keeping the view matrix up to date.
  317. The custom matrix will be returned from getViewMatrix.
  318. @param enable If true, the custom view matrix passed as the second
  319. parameter will be used in preference to an auto calculated one. If
  320. false, the frustum will revert to auto calculating the view matrix.
  321. @param viewMatrix The custom view matrix to use, the matrix must be an
  322. affine matrix.
  323. @see Frustum::setCustomProjectionMatrix, Matrix4::isAffine
  324. */
  325. virtual void setCustomViewMatrix(bool enable,
  326. const Matrix4& viewMatrix = Matrix4::IDENTITY);
  327. /// Returns whether a custom view matrix is in use
  328. virtual bool isCustomViewMatrixEnabled(void) const
  329. { return mCustomViewMatrix; }
  330. /** Set whether to use a custom projection matrix on this frustum.
  331. @remarks
  332. This is an advanced method which allows you to manually set
  333. the projection matrix on this frustum, rather than having it
  334. calculate itself based on it's position and orientation.
  335. @note
  336. After enabling a custom projection matrix, the frustum will no
  337. longer update on its own based on field of view and near / far
  338. distance changes. You are completely responsible for keeping the
  339. projection matrix up to date if those values change. The custom
  340. matrix will be returned from getProjectionMatrix and derivative
  341. functions.
  342. @param enable If true, the custom projection matrix passed as the
  343. second parameter will be used in preference to an auto calculated
  344. one. If false, the frustum will revert to auto calculating the
  345. projection matrix.
  346. @param projectionMatrix The custom view matrix to use
  347. @see Frustum::setCustomViewMatrix
  348. */
  349. virtual void setCustomProjectionMatrix(bool enable,
  350. const Matrix4& projectionMatrix = Matrix4::IDENTITY);
  351. /// Returns whether a custom projection matrix is in use
  352. virtual bool isCustomProjectionMatrixEnabled(void) const
  353. { return mCustomProjMatrix; }
  354. /** Retrieves the clipping planes of the frustum (world space).
  355. @remarks
  356. The clipping planes are ordered as declared in enumerate constants FrustumPlane.
  357. */
  358. virtual const Plane* getFrustumPlanes(void) const;
  359. /** Retrieves a specified plane of the frustum (world space).
  360. @remarks
  361. Gets a reference to one of the planes which make up the frustum frustum, e.g. for clipping purposes.
  362. */
  363. virtual const Plane& getFrustumPlane( unsigned short plane ) const;
  364. /** Tests whether the given container is visible in the Frustum.
  365. @param
  366. bound Bounding box to be checked (world space)
  367. @param
  368. culledBy Optional pointer to an int which will be filled by the plane number which culled
  369. the box if the result was false;
  370. @returns
  371. If the box was visible, true is returned.
  372. @par
  373. Otherwise, false is returned.
  374. */
  375. virtual bool isVisible(const AxisAlignedBox& bound, FrustumPlane* culledBy = 0) const;
  376. /** Tests whether the given container is visible in the Frustum.
  377. @param
  378. bound Bounding sphere to be checked (world space)
  379. @param
  380. culledBy Optional pointer to an int which will be filled by the plane number which culled
  381. the box if the result was false;
  382. @returns
  383. If the sphere was visible, true is returned.
  384. @par
  385. Otherwise, false is returned.
  386. */
  387. virtual bool isVisible(const Sphere& bound, FrustumPlane* culledBy = 0) const;
  388. /** Tests whether the given vertex is visible in the Frustum.
  389. @param
  390. vert Vertex to be checked (world space)
  391. @param
  392. culledBy Optional pointer to an int which will be filled by the plane number which culled
  393. the box if the result was false;
  394. @returns
  395. If the box was visible, true is returned.
  396. @par
  397. Otherwise, false is returned.
  398. */
  399. virtual bool isVisible(const Vector3& vert, FrustumPlane* culledBy = 0) const;
  400. /** Overridden from MovableObject */
  401. const AxisAlignedBox& getBoundingBox(void) const;
  402. /** Overridden from MovableObject */
  403. float getBoundingRadius(void) const;
  404. /** Gets the world space corners of the frustum.
  405. @remarks
  406. The corners are ordered as follows: top-right near,
  407. top-left near, bottom-left near, bottom-right near,
  408. top-right far, top-left far, bottom-left far, bottom-right far.
  409. */
  410. virtual const Vector3* getWorldSpaceCorners(void) const;
  411. /** Sets the type of projection to use (orthographic or perspective). Default is perspective.
  412. */
  413. virtual void setProjectionType(ProjectionType pt);
  414. /** Retrieves info on the type of projection used (orthographic or perspective).
  415. */
  416. virtual ProjectionType getProjectionType(void) const;
  417. /** Sets the orthographic window settings, for use with orthographic rendering only.
  418. @note Calling this method will recalculate the aspect ratio, use
  419. setOrthoWindowHeight or setOrthoWindowWidth alone if you wish to
  420. preserve the aspect ratio but just fit one or other dimension to a
  421. particular size.
  422. @param w, h The dimensions of the view window in world units
  423. */
  424. virtual void setOrthoWindow(float w, float h);
  425. /** Sets the orthographic window height, for use with orthographic rendering only.
  426. @note The width of the window will be calculated from the aspect ratio.
  427. @param h The height of the view window in world units
  428. */
  429. virtual void setOrthoWindowHeight(float h);
  430. /** Sets the orthographic window width, for use with orthographic rendering only.
  431. @note The height of the window will be calculated from the aspect ratio.
  432. @param w The width of the view window in world units
  433. */
  434. virtual void setOrthoWindowWidth(float w);
  435. /** Gets the orthographic window height, for use with orthographic rendering only.
  436. */
  437. virtual float getOrthoWindowHeight() const;
  438. /** Gets the orthographic window width, for use with orthographic rendering only.
  439. @note This is calculated from the orthographic height and the aspect ratio
  440. */
  441. virtual float getOrthoWindowWidth() const;
  442. /** Project a sphere onto the near plane and get the bounding rectangle.
  443. @param sphere The world-space sphere to project
  444. @param radius Radius of the sphere
  445. @param left, top, right, bottom Pointers to destination values, these
  446. will be completed with the normalised device coordinates (in the
  447. range {-1,1})
  448. @returns true if the sphere was projected to a subset of the near plane,
  449. false if the entire near plane was contained
  450. */
  451. virtual bool projectSphere(const Sphere& sphere,
  452. float* left, float* top, float* right, float* bottom) const;
  453. /// Small constant used to reduce far plane projection to avoid inaccuracies
  454. static const float INFINITE_FAR_PLANE_ADJUST;
  455. protected:
  456. ViewportPtr mViewport;
  457. public:
  458. /** Standard destructor.
  459. */
  460. virtual ~Camera();
  461. void init(RenderTargetPtr target = nullptr,
  462. float left = 0.0f, float top = 0.0f,
  463. float width = 1.0f, float height = 1.0f,
  464. int ZOrder = 0);
  465. ViewportPtr getViewport() { return mViewport; }
  466. /************************************************************************/
  467. /* COMPONENT OVERRIDES */
  468. /************************************************************************/
  469. private:
  470. friend class GameObject;
  471. /** Standard constructor.
  472. */
  473. Camera(GameObjectPtr parent);
  474. public:
  475. virtual void update() {}
  476. /************************************************************************/
  477. /* RTTI */
  478. /************************************************************************/
  479. public:
  480. friend class CameraRTTI;
  481. static RTTITypeBase* getRTTIStatic();
  482. virtual RTTITypeBase* getRTTI() const;
  483. protected:
  484. Camera() {} // Serialization only
  485. };
  486. /** @} */
  487. /** @} */
  488. } // namespace CamelotEngine
  489. #endif