Camera.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. /// \file
  4. #pragma once
  5. #include "../GraphicsAPI/GraphicsDefs.h"
  6. #include "../Math/Frustum.h"
  7. #include "../Math/Ray.h"
  8. #include "../Scene/Component.h"
  9. namespace Urho3D
  10. {
  11. static const float DEFAULT_NEARCLIP = 0.1f;
  12. static const float DEFAULT_FARCLIP = 1000.0f;
  13. static const float DEFAULT_CAMERA_FOV = 45.0f;
  14. static const float DEFAULT_ORTHOSIZE = 20.0f;
  15. enum ViewOverride : unsigned
  16. {
  17. VO_NONE = 0x0,
  18. VO_LOW_MATERIAL_QUALITY = 0x1,
  19. VO_DISABLE_SHADOWS = 0x2,
  20. VO_DISABLE_OCCLUSION = 0x4,
  21. };
  22. URHO3D_FLAGSET(ViewOverride, ViewOverrideFlags);
  23. /// %Camera component.
  24. class URHO3D_API Camera : public Component
  25. {
  26. URHO3D_OBJECT(Camera, Component);
  27. public:
  28. /// Construct.
  29. explicit Camera(Context* context);
  30. /// Destruct.
  31. ~Camera() override;
  32. /// Register object factory.
  33. /// @nobind
  34. static void RegisterObject(Context* context);
  35. /// Visualize the component as debug geometry.
  36. void DrawDebugGeometry(DebugRenderer* debug, bool depthTest) override;
  37. /// Set near clip distance.
  38. /// @property
  39. void SetNearClip(float nearClip);
  40. /// Set far clip distance.
  41. /// @property
  42. void SetFarClip(float farClip);
  43. /// Set vertical field of view in degrees.
  44. /// @property
  45. void SetFov(float fov);
  46. /// Set orthographic mode view uniform size.
  47. /// @property
  48. void SetOrthoSize(float orthoSize);
  49. /// Set orthographic mode view non-uniform size. Disables the auto aspect ratio -mode.
  50. void SetOrthoSize(const Vector2& orthoSize);
  51. /// Set aspect ratio manually. Disables the auto aspect ratio -mode.
  52. /// @property
  53. void SetAspectRatio(float aspectRatio);
  54. /// Set polygon fill mode to use when rendering a scene.
  55. /// @property
  56. void SetFillMode(FillMode mode);
  57. /// Set zoom.
  58. /// @property
  59. void SetZoom(float zoom);
  60. /// Set LOD bias.
  61. /// @property
  62. void SetLodBias(float bias);
  63. /// Set view mask. Will be and'ed with object's view mask to see if the object should be rendered.
  64. /// @property
  65. void SetViewMask(unsigned mask);
  66. /// Set view override flags.
  67. /// @property
  68. void SetViewOverrideFlags(ViewOverrideFlags flags);
  69. /// Set orthographic mode enabled/disabled.
  70. /// @property
  71. void SetOrthographic(bool enable);
  72. /// Set automatic aspect ratio based on viewport dimensions. Enabled by default.
  73. /// @property
  74. void SetAutoAspectRatio(bool enable);
  75. /// Set projection offset. It needs to be calculated as (offset in pixels) / (viewport dimensions).
  76. /// @property
  77. void SetProjectionOffset(const Vector2& offset);
  78. /// Set reflection mode.
  79. /// @property
  80. void SetUseReflection(bool enable);
  81. /// Set reflection plane in world space for reflection mode.
  82. /// @property
  83. void SetReflectionPlane(const Plane& plane);
  84. /// Set whether to use a custom clip plane.
  85. /// @property
  86. void SetUseClipping(bool enable);
  87. /// Set custom clipping plane in world space.
  88. /// @property
  89. void SetClipPlane(const Plane& plane);
  90. /// Set vertical flipping mode. Called internally by View to resolve OpenGL / Direct3D9 rendertarget sampling differences.
  91. void SetFlipVertical(bool enable);
  92. /// Set custom projection matrix, which should be specified in D3D convention with depth range 0 - 1. Disables auto aspect ratio.
  93. /// @property
  94. /** Change any of the standard view parameters (FOV, far clip, zoom, etc.) to revert to the standard projection.
  95. Note that the custom projection is not serialized or replicated through the network.
  96. */
  97. void SetProjection(const Matrix4& projection);
  98. /// Return far clip distance. If a custom projection matrix is in use, is calculated from it instead of the value assigned with SetFarClip().
  99. /// @property
  100. float GetFarClip() const;
  101. /// Return near clip distance. If a custom projection matrix is in use, is calculated from it instead of the value assigned with SetNearClip().
  102. /// @property
  103. float GetNearClip() const;
  104. /// Return vertical field of view in degrees.
  105. /// @property
  106. float GetFov() const { return fov_; }
  107. /// Return orthographic mode size.
  108. /// @property
  109. float GetOrthoSize() const { return orthoSize_; }
  110. /// Return aspect ratio.
  111. /// @property
  112. float GetAspectRatio() const { return aspectRatio_; }
  113. /// Return zoom.
  114. /// @property
  115. float GetZoom() const { return zoom_; }
  116. /// Return LOD bias.
  117. /// @property
  118. float GetLodBias() const { return lodBias_; }
  119. /// Return view mask.
  120. /// @property
  121. unsigned GetViewMask() const { return viewMask_; }
  122. /// Return view override flags.
  123. /// @property
  124. ViewOverrideFlags GetViewOverrideFlags() const { return viewOverrideFlags_; }
  125. /// Return fill mode.
  126. /// @property
  127. FillMode GetFillMode() const { return fillMode_; }
  128. /// Return orthographic flag.
  129. /// @property
  130. bool IsOrthographic() const { return orthographic_; }
  131. /// Return auto aspect ratio flag.
  132. /// @property
  133. bool GetAutoAspectRatio() const { return autoAspectRatio_; }
  134. /// Return frustum in world space.
  135. /// @property
  136. const Frustum& GetFrustum() const;
  137. /// Return projection matrix. It's in D3D convention with depth range 0 - 1.
  138. /// @property
  139. Matrix4 GetProjection() const;
  140. /// Return projection matrix converted to API-specific format for use as a shader parameter.
  141. /// @property
  142. Matrix4 GetGPUProjection() const;
  143. /// Return view matrix.
  144. /// @property
  145. const Matrix3x4& GetView() const;
  146. /// Return frustum near and far sizes.
  147. void GetFrustumSize(Vector3& near, Vector3& far) const;
  148. /// Return half view size.
  149. /// @property
  150. float GetHalfViewSize() const;
  151. /// Return frustum split by custom near and far clip distances.
  152. Frustum GetSplitFrustum(float nearClip, float farClip) const;
  153. /// Return frustum in view space.
  154. /// @property
  155. Frustum GetViewSpaceFrustum() const;
  156. /// Return split frustum in view space.
  157. Frustum GetViewSpaceSplitFrustum(float nearClip, float farClip) const;
  158. /// Return ray corresponding to normalized screen coordinates (0 - 1), with origin on the near clip plane.
  159. Ray GetScreenRay(float x, float y) const;
  160. /// Convert a world space point to normalized screen coordinates (0 - 1).
  161. Vector2 WorldToScreenPoint(const Vector3& worldPos) const;
  162. /// 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.
  163. /** 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.
  164. */
  165. Vector3 ScreenToWorldPoint(const Vector3& screenPos) const;
  166. /// Return projection offset.
  167. /// @property
  168. const Vector2& GetProjectionOffset() const { return projectionOffset_; }
  169. /// Return whether is using reflection.
  170. /// @property
  171. bool GetUseReflection() const { return useReflection_; }
  172. /// Return the reflection plane.
  173. /// @property
  174. const Plane& GetReflectionPlane() const { return reflectionPlane_; }
  175. /// Return whether is using a custom clipping plane.
  176. /// @property
  177. bool GetUseClipping() const { return useClipping_; }
  178. /// Return the custom clipping plane.
  179. /// @property
  180. const Plane& GetClipPlane() const { return clipPlane_; }
  181. /// Return vertical flipping mode.
  182. bool GetFlipVertical() const { return flipVertical_; }
  183. /// Return whether to reverse culling; affected by vertical flipping and reflection.
  184. bool GetReverseCulling() const { return flipVertical_ ^ useReflection_; }
  185. /// Return distance to position. In orthographic mode uses only Z coordinate.
  186. float GetDistance(const Vector3& worldPos) const;
  187. /// Return squared distance to position. In orthographic mode uses only Z coordinate.
  188. float GetDistanceSquared(const Vector3& worldPos) const;
  189. /// Return a scene node's LOD scaled distance.
  190. float GetLodDistance(float distance, float scale, float bias) const;
  191. /// Return a world rotation for facing a camera on certain axes based on the existing world rotation.
  192. Quaternion GetFaceCameraRotation(const Vector3& position, const Quaternion& rotation, FaceCameraMode mode, float minAngle = 0.0f);
  193. /// Get effective world transform for matrix and frustum calculations including reflection but excluding node scaling.
  194. /// @property
  195. Matrix3x4 GetEffectiveWorldTransform() const;
  196. /// Return if projection parameters are valid for rendering and raycasting.
  197. bool IsProjectionValid() const;
  198. /// Set aspect ratio without disabling the "auto aspect ratio" mode. Called internally by View.
  199. void SetAspectRatioInternal(float aspectRatio);
  200. /// Set orthographic size attribute without forcing the aspect ratio.
  201. void SetOrthoSizeAttr(float orthoSize);
  202. /// Set reflection plane attribute.
  203. void SetReflectionPlaneAttr(const Vector4& value);
  204. /// Return reflection plane attribute.
  205. Vector4 GetReflectionPlaneAttr() const;
  206. /// Set clipping plane attribute.
  207. void SetClipPlaneAttr(const Vector4& value);
  208. /// Return clipping plane attribute.
  209. Vector4 GetClipPlaneAttr() const;
  210. protected:
  211. /// Handle node being assigned.
  212. void OnNodeSet(Node* node) override;
  213. /// Handle node transform being dirtied.
  214. void OnMarkedDirty(Node* node) override;
  215. private:
  216. /// Recalculate projection matrix.
  217. void UpdateProjection() const;
  218. /// Cached view matrix.
  219. mutable Matrix3x4 view_;
  220. /// Cached projection matrix.
  221. mutable Matrix4 projection_;
  222. /// Cached world space frustum.
  223. mutable Frustum frustum_;
  224. /// View matrix dirty flag.
  225. mutable bool viewDirty_;
  226. /// Projection matrix dirty flag.
  227. mutable bool projectionDirty_;
  228. /// Frustum dirty flag.
  229. mutable bool frustumDirty_;
  230. /// Orthographic mode flag.
  231. bool orthographic_;
  232. /// Cached actual near clip distance.
  233. mutable float projNearClip_{};
  234. /// Cached actual far clip distance.
  235. mutable float projFarClip_{};
  236. /// Near clip distance.
  237. float nearClip_;
  238. /// Far clip distance.
  239. float farClip_;
  240. /// Field of view.
  241. float fov_;
  242. /// Orthographic view size.
  243. float orthoSize_;
  244. /// Aspect ratio.
  245. float aspectRatio_;
  246. /// Zoom.
  247. float zoom_;
  248. /// LOD bias.
  249. float lodBias_;
  250. /// View mask.
  251. unsigned viewMask_;
  252. /// View override flags.
  253. ViewOverrideFlags viewOverrideFlags_;
  254. /// Fill mode.
  255. FillMode fillMode_;
  256. /// Projection offset.
  257. Vector2 projectionOffset_;
  258. /// Reflection plane.
  259. Plane reflectionPlane_;
  260. /// Clipping plane.
  261. Plane clipPlane_;
  262. /// Reflection matrix calculated from the plane.
  263. Matrix3x4 reflectionMatrix_;
  264. /// Auto aspect ratio flag.
  265. bool autoAspectRatio_;
  266. /// Flip vertical flag.
  267. bool flipVertical_;
  268. /// Reflection mode enabled flag.
  269. bool useReflection_;
  270. /// Use custom clip plane flag.
  271. bool useClipping_;
  272. /// Use custom projection matrix flag. Used internally.
  273. mutable bool customProjection_;
  274. };
  275. }