Camera.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. //
  2. // Copyright (c) 2008-2017 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "../Graphics/GraphicsDefs.h"
  24. #include "../Math/Frustum.h"
  25. #include "../Math/Ray.h"
  26. #include "../Scene/Component.h"
  27. namespace Atomic
  28. {
  29. static const float DEFAULT_NEARCLIP = 0.1f;
  30. static const float DEFAULT_FARCLIP = 1000.0f;
  31. static const float DEFAULT_CAMERA_FOV = 45.0f;
  32. static const float DEFAULT_ORTHOSIZE = 20.0f;
  33. static const unsigned VO_NONE = 0x0;
  34. static const unsigned VO_LOW_MATERIAL_QUALITY = 0x1;
  35. static const unsigned VO_DISABLE_SHADOWS = 0x2;
  36. static const unsigned VO_DISABLE_OCCLUSION = 0x4;
  37. /// %Camera component.
  38. class ATOMIC_API Camera : public Component
  39. {
  40. ATOMIC_OBJECT(Camera, Component);
  41. public:
  42. /// Construct.
  43. Camera(Context* context);
  44. /// Destruct.
  45. virtual ~Camera();
  46. /// Register object factory.
  47. static void RegisterObject(Context* context);
  48. /// Visualize the component as debug geometry.
  49. virtual void DrawDebugGeometry(DebugRenderer* debug, bool depthTest);
  50. /// Set near clip distance.
  51. void SetNearClip(float nearClip);
  52. /// Set far clip distance.
  53. void SetFarClip(float farClip);
  54. /// Set vertical field of view in degrees.
  55. void SetFov(float fov);
  56. /// Set orthographic mode view uniform size.
  57. void SetOrthoSize(float orthoSize);
  58. /// Set orthographic mode view non-uniform size. Disables the auto aspect ratio -mode.
  59. void SetOrthoSize(const Vector2& orthoSize);
  60. /// Set aspect ratio manually. Disables the auto aspect ratio -mode.
  61. void SetAspectRatio(float aspectRatio);
  62. /// Set polygon fill mode to use when rendering a scene.
  63. void SetFillMode(FillMode mode);
  64. /// Set zoom.
  65. void SetZoom(float zoom);
  66. /// Set LOD bias.
  67. void SetLodBias(float bias);
  68. /// Set view mask. Will be and'ed with object's view mask to see if the object should be rendered.
  69. void SetViewMask(unsigned mask);
  70. /// Set view override flags.
  71. void SetViewOverrideFlags(unsigned flags);
  72. /// Set orthographic mode enabled/disabled.
  73. void SetOrthographic(bool enable);
  74. /// Set automatic aspect ratio based on viewport dimensions. Enabled by default.
  75. void SetAutoAspectRatio(bool enable);
  76. /// Set projection offset. It needs to be calculated as (offset in pixels) / (viewport dimensions.)
  77. void SetProjectionOffset(const Vector2& offset);
  78. /// Set reflection mode.
  79. void SetUseReflection(bool enable);
  80. /// Set reflection plane in world space for reflection mode.
  81. void SetReflectionPlane(const Plane& plane);
  82. /// Set whether to use a custom clip plane.
  83. void SetUseClipping(bool enable);
  84. /// Set custom clipping plane in world space.
  85. void SetClipPlane(const Plane& plane);
  86. /// Set vertical flipping mode. Called internally by View to resolve OpenGL / Direct3D9 rendertarget sampling differences.
  87. void SetFlipVertical(bool enable);
  88. /// Set custom projection matrix, which should be specified in D3D convention with depth range 0 - 1. Disables auto aspect ratio.
  89. /** Change any of the standard view parameters (FOV, far clip, zoom etc.) to revert to the standard projection.
  90. Note that the custom projection is not serialized or replicated through the network.
  91. */
  92. void SetProjection(const Matrix4& projection);
  93. /// Return far clip distance. If a custom projection matrix is in use, is calculated from it instead of the value assigned with SetFarClip().
  94. float GetFarClip() const;
  95. /// Return near clip distance. If a custom projection matrix is in use, is calculated from it instead of the value assigned with SetNearClip().
  96. float GetNearClip() const;
  97. /// Return vertical field of view in degrees.
  98. float GetFov() const { return fov_; }
  99. /// Return orthographic mode size.
  100. float GetOrthoSize() const { return orthoSize_; }
  101. /// Return aspect ratio.
  102. float GetAspectRatio() const { return aspectRatio_; }
  103. /// Return zoom.
  104. float GetZoom() const { return zoom_; }
  105. /// Return LOD bias.
  106. float GetLodBias() const { return lodBias_; }
  107. /// Return view mask.
  108. unsigned GetViewMask() const { return viewMask_; }
  109. /// Return view override flags.
  110. unsigned GetViewOverrideFlags() const { return viewOverrideFlags_; }
  111. /// Return fill mode.
  112. FillMode GetFillMode() const { return fillMode_; }
  113. /// Return orthographic flag.
  114. bool IsOrthographic() const { return orthographic_; }
  115. /// Return auto aspect ratio flag.
  116. bool GetAutoAspectRatio() const { return autoAspectRatio_; }
  117. /// Return frustum in world space.
  118. const Frustum& GetFrustum() const;
  119. /// Return projection matrix. It's in D3D convention with depth range 0 - 1.
  120. Matrix4 GetProjection() const;
  121. /// Return projection matrix converted to API-specific format for use as a shader parameter.
  122. Matrix4 GetGPUProjection() const;
  123. /// Return view matrix.
  124. const Matrix3x4& GetView() const;
  125. /// Return frustum near and far sizes.
  126. void GetFrustumSize(Vector3& near, Vector3& far) const;
  127. /// Return half view size.
  128. float GetHalfViewSize() const;
  129. /// Return frustum split by custom near and far clip distances.
  130. Frustum GetSplitFrustum(float nearClip, float farClip) const;
  131. /// Return frustum in view space.
  132. Frustum GetViewSpaceFrustum() const;
  133. /// Return split frustum in view space.
  134. Frustum GetViewSpaceSplitFrustum(float nearClip, float farClip) const;
  135. /// Return ray corresponding to normalized screen coordinates (0 - 1), with origin on the near clip plane.
  136. Ray GetScreenRay(float x, float y) const;
  137. /// Convert a world space point to normalized screen coordinates (0 - 1).
  138. Vector2 WorldToScreenPoint(const Vector3& worldPos) const;
  139. /// Convert normalized screen coordinates (0 - 1) and distance along view Z axis (in Z coordinate) to a world space point. The distance can not be closer than the near clip plane.
  140. /** Note that a HitDistance() from the camera screen ray is not the same as distance along the view Z axis, as under a perspective projection the ray is likely to not be Z-aligned.
  141. */
  142. Vector3 ScreenToWorldPoint(const Vector3& screenPos) const;
  143. /// Return projection offset.
  144. const Vector2& GetProjectionOffset() const { return projectionOffset_; }
  145. /// Return whether is using reflection.
  146. bool GetUseReflection() const { return useReflection_; }
  147. /// Return the reflection plane.
  148. const Plane& GetReflectionPlane() const { return reflectionPlane_; }
  149. /// Return whether is using a custom clipping plane.
  150. bool GetUseClipping() const { return useClipping_; }
  151. /// Return the custom clipping plane.
  152. const Plane& GetClipPlane() const { return clipPlane_; }
  153. /// Return vertical flipping mode.
  154. bool GetFlipVertical() const { return flipVertical_; }
  155. /// Return whether to reverse culling; affected by vertical flipping and reflection.
  156. bool GetReverseCulling() const { return flipVertical_ ^ useReflection_; }
  157. /// Return distance to position. In orthographic mode uses only Z coordinate.
  158. float GetDistance(const Vector3& worldPos) const;
  159. /// Return squared distance to position. In orthographic mode uses only Z coordinate.
  160. float GetDistanceSquared(const Vector3& worldPos) const;
  161. /// Return a scene node's LOD scaled distance.
  162. float GetLodDistance(float distance, float scale, float bias) const;
  163. /// Return a world rotation for facing a camera on certain axes based on the existing world rotation.
  164. Quaternion GetFaceCameraRotation(const Vector3& position, const Quaternion& rotation, FaceCameraMode mode, float minAngle = 0.0f);
  165. /// Get effective world transform for matrix and frustum calculations including reflection but excluding node scaling.
  166. Matrix3x4 GetEffectiveWorldTransform() const;
  167. /// Return if projection parameters are valid for rendering and raycasting.
  168. bool IsProjectionValid() const;
  169. /// Set aspect ratio without disabling the "auto aspect ratio" mode. Called internally by View.
  170. void SetAspectRatioInternal(float aspectRatio);
  171. /// Set orthographic size attribute without forcing the aspect ratio.
  172. void SetOrthoSizeAttr(float orthoSize);
  173. /// Set reflection plane attribute.
  174. void SetReflectionPlaneAttr(const Vector4& value);
  175. /// Return reflection plane attribute.
  176. Vector4 GetReflectionPlaneAttr() const;
  177. /// Set clipping plane attribute.
  178. void SetClipPlaneAttr(const Vector4& value);
  179. /// Return clipping plane attribute.
  180. Vector4 GetClipPlaneAttr() const;
  181. protected:
  182. /// Handle node being assigned.
  183. virtual void OnNodeSet(Node* node);
  184. /// Handle node transform being dirtied.
  185. virtual void OnMarkedDirty(Node* node);
  186. private:
  187. /// Recalculate projection matrix.
  188. void UpdateProjection() const;
  189. /// Cached view matrix.
  190. mutable Matrix3x4 view_;
  191. /// Cached projection matrix.
  192. mutable Matrix4 projection_;
  193. /// Cached world space frustum.
  194. mutable Frustum frustum_;
  195. /// View matrix dirty flag.
  196. mutable bool viewDirty_;
  197. /// Projection matrix dirty flag.
  198. mutable bool projectionDirty_;
  199. /// Frustum dirty flag.
  200. mutable bool frustumDirty_;
  201. /// Orthographic mode flag.
  202. bool orthographic_;
  203. /// Cached actual near clip distance.
  204. mutable float projNearClip_;
  205. /// Cached actual far clip distance.
  206. mutable float projFarClip_;
  207. /// Near clip distance.
  208. float nearClip_;
  209. /// Far clip distance.
  210. float farClip_;
  211. /// Field of view.
  212. float fov_;
  213. /// Orthographic view size.
  214. float orthoSize_;
  215. /// Aspect ratio.
  216. float aspectRatio_;
  217. /// Zoom.
  218. float zoom_;
  219. /// LOD bias.
  220. float lodBias_;
  221. /// View mask.
  222. unsigned viewMask_;
  223. /// View override flags.
  224. unsigned viewOverrideFlags_;
  225. /// Fill mode.
  226. FillMode fillMode_;
  227. /// Projection offset.
  228. Vector2 projectionOffset_;
  229. /// Reflection plane.
  230. Plane reflectionPlane_;
  231. /// Clipping plane.
  232. Plane clipPlane_;
  233. /// Reflection matrix calculated from the plane.
  234. Matrix3x4 reflectionMatrix_;
  235. /// Auto aspect ratio flag.
  236. bool autoAspectRatio_;
  237. /// Flip vertical flag.
  238. bool flipVertical_;
  239. /// Reflection mode enabled flag.
  240. bool useReflection_;
  241. /// Use custom clip plane flag.
  242. bool useClipping_;
  243. /// Use custom projection matrix flag. Used internally.
  244. mutable bool customProjection_;
  245. };
  246. }