Camera.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 unsigned VO_NONE = 0x0;
  30. static const unsigned VO_LOW_MATERIAL_QUALITY = 0x1;
  31. static const unsigned VO_DISABLE_SHADOWS = 0x2;
  32. static const unsigned VO_DISABLE_OCCLUSION = 0x4;
  33. /// %Camera component.
  34. class URHO3D_API Camera : public Component
  35. {
  36. OBJECT(Camera);
  37. public:
  38. /// Construct.
  39. Camera(Context* context);
  40. /// Destruct.
  41. virtual ~Camera();
  42. /// Register object factory.
  43. static void RegisterObject(Context* context);
  44. /// Visualize the component as debug geometry.
  45. virtual void DrawDebugGeometry(DebugRenderer* debug, bool depthTest);
  46. /// Set near clip distance.
  47. void SetNearClip(float nearClip);
  48. /// Set far clip distance.
  49. void SetFarClip(float farClip);
  50. /// Set field of view.
  51. void SetFov(float fov);
  52. /// Set orthographic mode view uniform size.
  53. void SetOrthoSize(float orthoSize);
  54. /// Set orthographic mode view size.
  55. void SetOrthoSize(const Vector2& orthoSize);
  56. /// Set aspect ratio.
  57. void SetAspectRatio(float aspectRatio);
  58. /// Set polygon fill mode to use when rendering a scene.
  59. void SetFillMode(FillMode mode);
  60. /// Set zoom.
  61. void SetZoom(float zoom);
  62. /// Set LOD bias.
  63. void SetLodBias(float bias);
  64. /// Set view mask. Will be and'ed with object's view mask to see if the object should be rendered.
  65. void SetViewMask(unsigned mask);
  66. /// Set view override flags.
  67. void SetViewOverrideFlags(unsigned flags);
  68. /// Set orthographic mode enabled/disabled.
  69. void SetOrthographic(bool enable);
  70. /// Set automatic aspect ratio based on viewport dimensions.
  71. void SetAutoAspectRatio(bool enable);
  72. /// Set projection offset. It needs to be calculated as (offset in pixels) / (viewport dimensions.)
  73. void SetProjectionOffset(const Vector2& offset);
  74. /// Set reflection mode.
  75. void SetUseReflection(bool enable);
  76. /// Set reflection plane in world space for reflection mode.
  77. void SetReflectionPlane(const Plane& plane);
  78. /// Set whether to use a custom clip plane.
  79. void SetUseClipping(bool enable);
  80. /// Set custom clipping plane in world space.
  81. void SetClipPlane(const Plane& plane);
  82. /// Set vertical flipping mode. Called internally by View to resolve OpenGL / Direct3D9 rendertarget sampling differences.
  83. void SetFlipVertical(bool enable);
  84. /// Return far clip distance.
  85. float GetFarClip() const { return farClip_; }
  86. /// Return near clip distance.
  87. float GetNearClip() const;
  88. /// Return field of view.
  89. float GetFov() const { return fov_; }
  90. /// Return orthographic mode size.
  91. float GetOrthoSize() const { return orthoSize_; }
  92. /// Return aspect ratio.
  93. float GetAspectRatio() const { return aspectRatio_; }
  94. /// Return zoom.
  95. float GetZoom() const { return zoom_; }
  96. /// Return LOD bias.
  97. float GetLodBias() const { return lodBias_; }
  98. /// Return view mask.
  99. unsigned GetViewMask() const { return viewMask_; }
  100. /// Return view override flags.
  101. unsigned GetViewOverrideFlags() const { return viewOverrideFlags_; }
  102. /// Return fill mode.
  103. FillMode GetFillMode() const { return fillMode_; }
  104. /// Return orthographic flag.
  105. bool IsOrthographic() const { return orthographic_; }
  106. /// Return auto aspect ratio flag.
  107. bool GetAutoAspectRatio() const { return autoAspectRatio_; }
  108. /// Return frustum in world space.
  109. const Frustum& GetFrustum() const;
  110. /// Return API-specific projection matrix.
  111. const Matrix4& GetProjection() const;
  112. /// Return either API-specific or API-independent (D3D convention) projection matrix.
  113. Matrix4 GetProjection(bool apiSpecific) const;
  114. /// Return view matrix.
  115. const Matrix3x4& GetView() const;
  116. /// Return frustum near and far sizes.
  117. void GetFrustumSize(Vector3& near, Vector3& far) const;
  118. /// Return half view size.
  119. float GetHalfViewSize() const;
  120. /// Return frustum split by custom near and far clip distances.
  121. Frustum GetSplitFrustum(float nearClip, float farClip) const;
  122. /// Return frustum in view space.
  123. Frustum GetViewSpaceFrustum() const;
  124. /// Return split frustum in view space.
  125. Frustum GetViewSpaceSplitFrustum(float nearClip, float farClip) const;
  126. /// Return ray corresponding to normalized screen coordinates (0.0 to 1.0.)
  127. Ray GetScreenRay(float x, float y) const;
  128. // Convert a world space point to normalized screen coordinates (0.0 - 1.0).
  129. Vector2 WorldToScreenPoint(const Vector3& worldPos) const;
  130. // Convert normalized screen coordinates (0.0 - 1.0) and depth to a world space point.
  131. Vector3 ScreenToWorldPoint(const Vector3& screenPos) const;
  132. /// Return projection offset.
  133. const Vector2& GetProjectionOffset() const { return projectionOffset_; }
  134. /// Return whether is using reflection.
  135. bool GetUseReflection() const { return useReflection_; }
  136. /// Return the reflection plane.
  137. const Plane& GetReflectionPlane() const { return reflectionPlane_; }
  138. /// Return whether is using a custom clipping plane.
  139. bool GetUseClipping() const { return useClipping_; }
  140. /// Return the custom clipping plane.
  141. const Plane& GetClipPlane() const { return clipPlane_; }
  142. /// Return vertical flipping mode.
  143. bool GetFlipVertical() const { return flipVertical_; }
  144. /// Return whether to reverse culling; affected by vertical flipping and reflection.
  145. bool GetReverseCulling() const { return flipVertical_ ^ useReflection_; }
  146. /// Return distance to position. In orthographic mode uses only Z coordinate.
  147. float GetDistance(const Vector3& worldPos) const;
  148. /// Return squared distance to position. In orthographic mode uses only Z coordinate.
  149. float GetDistanceSquared(const Vector3& worldPos) const;
  150. /// Return a scene node's LOD scaled distance.
  151. float GetLodDistance(float distance, float scale, float bias) const;
  152. /// Get effective world transform for matrix and frustum calculations including reflection but excluding node scaling.
  153. Matrix3x4 GetEffectiveWorldTransform() const;
  154. /// Return if projection parameters are valid for rendering and raycasting.
  155. bool IsProjectionValid() const;
  156. /// Set reflection plane attribute.
  157. void SetReflectionPlaneAttr(Vector4 value);
  158. /// Return reflection plane attribute.
  159. Vector4 GetReflectionPlaneAttr() const;
  160. /// Set clipping plane attribute.
  161. void SetClipPlaneAttr(Vector4 value);
  162. /// Return clipping plane attribute.
  163. Vector4 GetClipPlaneAttr() const;
  164. protected:
  165. /// Handle node being assigned.
  166. virtual void OnNodeSet(Node* node);
  167. /// Handle node transform being dirtied.
  168. virtual void OnMarkedDirty(Node* node);
  169. private:
  170. /// Cached view matrix.
  171. mutable Matrix3x4 view_;
  172. /// Cached projection matrix.
  173. mutable Matrix4 projection_;
  174. /// Cached frustum.
  175. mutable Frustum frustum_;
  176. /// View matrix dirty flag.
  177. mutable bool viewDirty_;
  178. /// Projection matrix dirty flag.
  179. mutable bool projectionDirty_;
  180. /// Frustum dirty flag.
  181. mutable bool frustumDirty_;
  182. /// Orthographic mode flag.
  183. bool orthographic_;
  184. /// Near clip distance.
  185. float nearClip_;
  186. /// Far clip distance.
  187. float farClip_;
  188. /// Field of view.
  189. float fov_;
  190. /// Orthographic view size.
  191. float orthoSize_;
  192. /// Aspect ratio.
  193. float aspectRatio_;
  194. /// Zoom.
  195. float zoom_;
  196. /// LOD bias.
  197. float lodBias_;
  198. /// View mask.
  199. unsigned viewMask_;
  200. /// View override flags.
  201. unsigned viewOverrideFlags_;
  202. /// Fill mode.
  203. FillMode fillMode_;
  204. /// Projection offset.
  205. Vector2 projectionOffset_;
  206. /// Reflection plane.
  207. Plane reflectionPlane_;
  208. /// Clipping plane.
  209. Plane clipPlane_;
  210. /// Reflection matrix calculated from the plane.
  211. Matrix3x4 reflectionMatrix_;
  212. /// Auto aspect ratio flag.
  213. bool autoAspectRatio_;
  214. /// Flip vertical flag.
  215. bool flipVertical_;
  216. /// Reflection mode enabled flag.
  217. bool useReflection_;
  218. /// Use custom clip plane flag.
  219. bool useClipping_;
  220. };
  221. }