Camera.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. //
  2. // Copyright (c) 2008-2014 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 "Frustum.h"
  24. #include "Component.h"
  25. #include "GraphicsDefs.h"
  26. #include "Ray.h"
  27. namespace Urho3D
  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 URHO3D_API Camera : public Component
  39. {
  40. OBJECT(Camera);
  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. /// Return far clip distance.
  89. float GetFarClip() const { return farClip_; }
  90. /// Return near clip distance.
  91. float GetNearClip() const;
  92. /// Return vertical field of view in degrees.
  93. float GetFov() const { return fov_; }
  94. /// Return orthographic mode size.
  95. float GetOrthoSize() const { return orthoSize_; }
  96. /// Return aspect ratio.
  97. float GetAspectRatio() const { return aspectRatio_; }
  98. /// Return zoom.
  99. float GetZoom() const { return zoom_; }
  100. /// Return LOD bias.
  101. float GetLodBias() const { return lodBias_; }
  102. /// Return view mask.
  103. unsigned GetViewMask() const { return viewMask_; }
  104. /// Return view override flags.
  105. unsigned GetViewOverrideFlags() const { return viewOverrideFlags_; }
  106. /// Return fill mode.
  107. FillMode GetFillMode() const { return fillMode_; }
  108. /// Return orthographic flag.
  109. bool IsOrthographic() const { return orthographic_; }
  110. /// Return auto aspect ratio flag.
  111. bool GetAutoAspectRatio() const { return autoAspectRatio_; }
  112. /// Return frustum in world space.
  113. const Frustum& GetFrustum() const;
  114. /// Return API-specific projection matrix.
  115. const Matrix4& GetProjection() const;
  116. /// Return either API-specific or API-independent (D3D convention) projection matrix.
  117. Matrix4 GetProjection(bool apiSpecific) const;
  118. /// Return view matrix.
  119. const Matrix3x4& GetView() const;
  120. /// Return frustum near and far sizes.
  121. void GetFrustumSize(Vector3& near, Vector3& far) const;
  122. /// Return half view size.
  123. float GetHalfViewSize() const;
  124. /// Return frustum split by custom near and far clip distances.
  125. Frustum GetSplitFrustum(float nearClip, float farClip) const;
  126. /// Return frustum in view space.
  127. Frustum GetViewSpaceFrustum() const;
  128. /// Return split frustum in view space.
  129. Frustum GetViewSpaceSplitFrustum(float nearClip, float farClip) const;
  130. /// Return ray corresponding to normalized screen coordinates (0.0 - 1.0).
  131. Ray GetScreenRay(float x, float y) const;
  132. // Convert a world space point to normalized screen coordinates (0.0 - 1.0).
  133. Vector2 WorldToScreenPoint(const Vector3& worldPos) const;
  134. // Convert normalized screen coordinates (0.0 - 1.0) and depth to a world space point.
  135. Vector3 ScreenToWorldPoint(const Vector3& screenPos) const;
  136. /// Return projection offset.
  137. const Vector2& GetProjectionOffset() const { return projectionOffset_; }
  138. /// Return whether is using reflection.
  139. bool GetUseReflection() const { return useReflection_; }
  140. /// Return the reflection plane.
  141. const Plane& GetReflectionPlane() const { return reflectionPlane_; }
  142. /// Return whether is using a custom clipping plane.
  143. bool GetUseClipping() const { return useClipping_; }
  144. /// Return the custom clipping plane.
  145. const Plane& GetClipPlane() const { return clipPlane_; }
  146. /// Return vertical flipping mode.
  147. bool GetFlipVertical() const { return flipVertical_; }
  148. /// Return whether to reverse culling; affected by vertical flipping and reflection.
  149. bool GetReverseCulling() const { return flipVertical_ ^ useReflection_; }
  150. /// Return distance to position. In orthographic mode uses only Z coordinate.
  151. float GetDistance(const Vector3& worldPos) const;
  152. /// Return squared distance to position. In orthographic mode uses only Z coordinate.
  153. float GetDistanceSquared(const Vector3& worldPos) const;
  154. /// Return a scene node's LOD scaled distance.
  155. float GetLodDistance(float distance, float scale, float bias) const;
  156. /// Return a world rotation for facing a camera on certain axes based on the existing world rotation.
  157. Quaternion GetFaceCameraRotation(const Vector3& position, const Quaternion& rotation, FaceCameraMode mode);
  158. /// Get effective world transform for matrix and frustum calculations including reflection but excluding node scaling.
  159. Matrix3x4 GetEffectiveWorldTransform() const;
  160. /// Return if projection parameters are valid for rendering and raycasting.
  161. bool IsProjectionValid() const;
  162. /// Set aspect ratio without disabling the "auto aspect ratio" mode. Called internally by View.
  163. void SetAspectRatioInternal(float aspectRatio);
  164. /// Set reflection plane attribute.
  165. void SetReflectionPlaneAttr(const Vector4& value);
  166. /// Return reflection plane attribute.
  167. Vector4 GetReflectionPlaneAttr() const;
  168. /// Set clipping plane attribute.
  169. void SetClipPlaneAttr(const Vector4& value);
  170. /// Return clipping plane attribute.
  171. Vector4 GetClipPlaneAttr() const;
  172. protected:
  173. /// Handle node being assigned.
  174. virtual void OnNodeSet(Node* node);
  175. /// Handle node transform being dirtied.
  176. virtual void OnMarkedDirty(Node* node);
  177. private:
  178. /// Cached view matrix.
  179. mutable Matrix3x4 view_;
  180. /// Cached projection matrix.
  181. mutable Matrix4 projection_;
  182. /// Cached frustum.
  183. mutable Frustum frustum_;
  184. /// View matrix dirty flag.
  185. mutable bool viewDirty_;
  186. /// Projection matrix dirty flag.
  187. mutable bool projectionDirty_;
  188. /// Frustum dirty flag.
  189. mutable bool frustumDirty_;
  190. /// Orthographic mode flag.
  191. bool orthographic_;
  192. /// Near clip distance.
  193. float nearClip_;
  194. /// Far clip distance.
  195. float farClip_;
  196. /// Field of view.
  197. float fov_;
  198. /// Orthographic view size.
  199. float orthoSize_;
  200. /// Aspect ratio.
  201. float aspectRatio_;
  202. /// Zoom.
  203. float zoom_;
  204. /// LOD bias.
  205. float lodBias_;
  206. /// View mask.
  207. unsigned viewMask_;
  208. /// View override flags.
  209. unsigned viewOverrideFlags_;
  210. /// Fill mode.
  211. FillMode fillMode_;
  212. /// Projection offset.
  213. Vector2 projectionOffset_;
  214. /// Reflection plane.
  215. Plane reflectionPlane_;
  216. /// Clipping plane.
  217. Plane clipPlane_;
  218. /// Reflection matrix calculated from the plane.
  219. Matrix3x4 reflectionMatrix_;
  220. /// Auto aspect ratio flag.
  221. bool autoAspectRatio_;
  222. /// Flip vertical flag.
  223. bool flipVertical_;
  224. /// Reflection mode enabled flag.
  225. bool useReflection_;
  226. /// Use custom clip plane flag.
  227. bool useClipping_;
  228. };
  229. }