CmFrustum.h 23 KB

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