Camera.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. //
  2. // Copyright (c) 2008-2013 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. /// Set near clip distance.
  45. void SetNearClip(float nearClip);
  46. /// Set far clip distance.
  47. void SetFarClip(float farClip);
  48. /// Set field of view.
  49. void SetFov(float fov);
  50. /// Set orthographic mode view uniform size.
  51. void SetOrthoSize(float orthoSize);
  52. /// Set orthographic mode view size.
  53. void SetOrthoSize(const Vector2& orthoSize);
  54. /// Set aspect ratio.
  55. void SetAspectRatio(float aspectRatio);
  56. /// Set polygon fill mode to use when rendering a scene.
  57. void SetFillMode(FillMode mode);
  58. /// Set zoom.
  59. void SetZoom(float zoom);
  60. /// Set LOD bias.
  61. void SetLodBias(float bias);
  62. /// Set view mask. Will be and'ed with object's view mask to see if the object should be rendered.
  63. void SetViewMask(unsigned mask);
  64. /// Set view override flags.
  65. void SetViewOverrideFlags(unsigned flags);
  66. /// Set orthographic mode enabled/disabled.
  67. void SetOrthographic(bool enable);
  68. /// Set automatic aspect ratio based on viewport dimensions.
  69. void SetAutoAspectRatio(bool enable);
  70. /// Set projection offset. It needs to be calculated as (offset in pixels) / (viewport dimensions.)
  71. void SetProjectionOffset(const Vector2& offset);
  72. /// Set vertical flipping mode. Called internally by View to resolve OpenGL / Direct3D9 rendertarget sampling differences.
  73. void SetFlipVertical(bool enable);
  74. /// Return far clip distance.
  75. float GetFarClip() const { return farClip_; }
  76. /// Return near clip distance.
  77. float GetNearClip() const;
  78. /// Return field of view.
  79. float GetFov() const { return fov_; }
  80. /// Return orthographic mode size.
  81. float GetOrthoSize() const { return orthoSize_; }
  82. /// Return aspect ratio.
  83. float GetAspectRatio() const { return aspectRatio_; }
  84. /// Return zoom.
  85. float GetZoom() const { return zoom_; }
  86. /// Return LOD bias.
  87. float GetLodBias() const { return lodBias_; }
  88. /// Return view mask.
  89. unsigned GetViewMask() const { return viewMask_; }
  90. /// Return view override flags.
  91. unsigned GetViewOverrideFlags() const { return viewOverrideFlags_; }
  92. /// Return fill mode.
  93. FillMode GetFillMode() const { return fillMode_; }
  94. /// Return orthographic flag.
  95. bool IsOrthographic() const { return orthographic_; }
  96. /// Return auto aspect ratio flag.
  97. bool GetAutoAspectRatio() const { return autoAspectRatio_; }
  98. /// Return frustum in world space.
  99. const Frustum& GetFrustum() const;
  100. /// Return API-specific projection matrix.
  101. const Matrix4& GetProjection() const;
  102. /// Return either API-specific or API-independent (D3D convention) projection matrix.
  103. Matrix4 GetProjection(bool apiSpecific) const;
  104. /// Return view matrix.
  105. const Matrix3x4& GetView() const;
  106. /// Return frustum near and far sizes.
  107. void GetFrustumSize(Vector3& near, Vector3& far) const;
  108. /// Return half view size.
  109. float GetHalfViewSize() const;
  110. /// Return frustum split by custom near and far clip distances.
  111. Frustum GetSplitFrustum(float nearClip, float farClip) const;
  112. /// Return frustum in view space.
  113. Frustum GetViewSpaceFrustum() const;
  114. /// Return split frustum in view space.
  115. Frustum GetViewSpaceSplitFrustum(float nearClip, float farClip) const;
  116. /// Return ray corresponding to normalized screen coordinates (0.0 to 1.0.)
  117. Ray GetScreenRay(float x, float y) const;
  118. // Convert a world space point to normalized screen coordinates (0.0 - 1.0).
  119. Vector2 WorldToScreenPoint(const Vector3& worldPos) const;
  120. // Convert normalized screen coordinates (0.0 - 1.0) and depth to a world space point.
  121. Vector3 ScreenToWorldPoint(const Vector3& screenPos) const;
  122. /// Return forward vector.
  123. Vector3 GetForwardVector() const;
  124. /// Return right vector.
  125. Vector3 GetRightVector() const;
  126. /// Return up vector.
  127. Vector3 GetUpVector() const;
  128. /// Return projection offset.
  129. const Vector2& GetProjectionOffset() const { return projectionOffset_; }
  130. /// Return vertical flipping mode.
  131. bool GetFlipVertical() const { return flipVertical_; }
  132. /// Return distance to position. In orthographic mode uses only Z coordinate.
  133. float GetDistance(const Vector3& worldPos) const;
  134. /// Return squared distance to position. In orthographic mode uses only Z coordinate.
  135. float GetDistanceSquared(const Vector3& worldPos) const;
  136. /// Return a scene node's LOD scaled distance.
  137. float GetLodDistance(float distance, float scale, float bias) const;
  138. /// Return if projection parameters are valid for rendering and raycasting.
  139. bool IsProjectionValid() const;
  140. protected:
  141. /// Handle node being assigned.
  142. virtual void OnNodeSet(Node* node);
  143. /// Handle node transform being dirtied.
  144. virtual void OnMarkedDirty(Node* node);
  145. private:
  146. /// Cached view matrix.
  147. mutable Matrix3x4 view_;
  148. /// Cached projection matrix.
  149. mutable Matrix4 projection_;
  150. /// Cached frustum.
  151. mutable Frustum frustum_;
  152. /// View matrix dirty flag.
  153. mutable bool viewDirty_;
  154. /// Projection matrix dirty flag.
  155. mutable bool projectionDirty_;
  156. /// Frustum dirty flag.
  157. mutable bool frustumDirty_;
  158. /// Orthographic mode flag.
  159. bool orthographic_;
  160. /// Near clip distance.
  161. float nearClip_;
  162. /// Far clip distance.
  163. float farClip_;
  164. /// Field of view.
  165. float fov_;
  166. /// Orthographic view size.
  167. float orthoSize_;
  168. /// Aspect ratio.
  169. float aspectRatio_;
  170. /// Zoom.
  171. float zoom_;
  172. /// LOD bias.
  173. float lodBias_;
  174. /// View mask.
  175. unsigned viewMask_;
  176. /// View override flags.
  177. unsigned viewOverrideFlags_;
  178. /// Fill mode.
  179. FillMode fillMode_;
  180. /// Projection offset.
  181. Vector2 projectionOffset_;
  182. /// Auto aspect ratio flag.
  183. bool autoAspectRatio_;
  184. /// Flip vertical flag.
  185. bool flipVertical_;
  186. };
  187. }