Camera.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Oorni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #pragma once
  24. #include "Frustum.h"
  25. #include "Component.h"
  26. #include "GraphicsDefs.h"
  27. #include "Ray.h"
  28. namespace Urho3D
  29. {
  30. static const unsigned VO_NONE = 0x0;
  31. static const unsigned VO_LOW_MATERIAL_QUALITY = 0x1;
  32. static const unsigned VO_DISABLE_SHADOWS = 0x2;
  33. static const unsigned VO_DISABLE_OCCLUSION = 0x4;
  34. /// %Camera component.
  35. class Camera : public Component
  36. {
  37. OBJECT(Camera);
  38. public:
  39. /// Construct.
  40. Camera(Context* context);
  41. /// Destruct.
  42. virtual ~Camera();
  43. /// Register object factory.
  44. static void RegisterObject(Context* context);
  45. /// Set near clip distance.
  46. void SetNearClip(float nearClip);
  47. /// Set far clip distance.
  48. void SetFarClip(float farClip);
  49. /// Set field of view.
  50. void SetFov(float fov);
  51. /// Set orthographic mode view uniform size.
  52. void SetOrthoSize(float orthoSize);
  53. /// Set orthographic mode view size.
  54. void SetOrthoSize(const Vector2& orthoSize);
  55. /// Set aspect ratio.
  56. void SetAspectRatio(float aspectRatio);
  57. /// Set polygon fill mode to use when rendering a scene.
  58. void SetFillMode(FillMode mode);
  59. /// Set zoom.
  60. void SetZoom(float zoom);
  61. /// Set LOD bias.
  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. void SetViewMask(unsigned mask);
  65. /// Set view override flags.
  66. void SetViewOverrideFlags(unsigned flags);
  67. /// Set orthographic mode enabled/disabled.
  68. void SetOrthographic(bool enable);
  69. /// Set automatic aspect ratio based on viewport dimensions.
  70. void SetAutoAspectRatio(bool enable);
  71. /// Set projection offset. It needs to be calculated as (offset in pixels) / (viewport dimensions.)
  72. void SetProjectionOffset(const Vector2& offset);
  73. /// Set vertical flipping mode.
  74. void SetFlipVertical(bool enable);
  75. /// Return far clip distance.
  76. float GetFarClip() const { return farClip_; }
  77. /// Return near clip distance.
  78. float GetNearClip() const;
  79. /// Return field of view.
  80. float GetFov() const { return fov_; }
  81. /// Return orthographic mode size.
  82. float GetOrthoSize() const { return orthoSize_; }
  83. /// Return aspect ratio.
  84. float GetAspectRatio() const { return aspectRatio_; }
  85. /// Return zoom.
  86. float GetZoom() const { return zoom_; }
  87. /// Return LOD bias.
  88. float GetLodBias() const { return lodBias_; }
  89. /// Return view mask.
  90. unsigned GetViewMask() const { return viewMask_; }
  91. /// Return view override flags.
  92. unsigned GetViewOverrideFlags() const { return viewOverrideFlags_; }
  93. /// Return fill mode.
  94. FillMode GetFillMode() const { return fillMode_; }
  95. /// Return orthographic flag.
  96. bool IsOrthographic() const { return orthographic_; }
  97. /// Return auto aspect ratio flag.
  98. bool GetAutoAspectRatio() const { return autoAspectRatio_; }
  99. /// Return frustum in world space.
  100. const Frustum& GetFrustum() const;
  101. /// Return API-specific projection matrix.
  102. const Matrix4& GetProjection() const;
  103. /// Return either API-specific or API-independent (D3D convention) projection matrix.
  104. Matrix4 GetProjection(bool apiSpecific) const;
  105. /// Return frustum near and far sizes.
  106. void GetFrustumSize(Vector3& near, Vector3& far) const;
  107. /// Return half view size.
  108. float GetHalfViewSize() const;
  109. /// Return frustum split by custom near and far clip distances.
  110. Frustum GetSplitFrustum(float nearClip, float farClip) const;
  111. /// Return frustum in view space.
  112. Frustum GetViewSpaceFrustum() const;
  113. /// Return split frustum in view space.
  114. Frustum GetViewSpaceSplitFrustum(float nearClip, float farClip) const;
  115. /// Return ray corresponding to screen coordinates (0.0 to 1.0.)
  116. Ray GetScreenRay(float x, float y);
  117. /// Return forward vector.
  118. Vector3 GetForwardVector();
  119. /// Return right vector.
  120. Vector3 GetRightVector();
  121. /// Return up vector.
  122. Vector3 GetUpVector();
  123. /// Return projection offset.
  124. const Vector2& GetProjectionOffset() const { return projectionOffset_; }
  125. /// Return vertical flipping mode.
  126. bool GetFlipVertical() const { return flipVertical_; }
  127. /// Return distance to position. In orthographic mode uses only Z coordinate.
  128. float GetDistance(const Vector3& worldPos) const;
  129. /// Return squared distance to position. In orthographic mode uses only Z coordinate.
  130. float GetDistanceSquared(const Vector3& worldPos) const;
  131. /// Return a scene node's LOD scaled distance.
  132. float GetLodDistance(float distance, float scale, float bias) const;
  133. /// Return if projection parameters are valid for rendering and raycasting.
  134. bool IsProjectionValid() const;
  135. /// Return inverse world transform, also known as the view matrix.
  136. const Matrix3x4& GetInverseWorldTransform() const;
  137. protected:
  138. /// Handle node being assigned.
  139. virtual void OnNodeSet(Node* node);
  140. /// Handle node transform being dirtied.
  141. virtual void OnMarkedDirty(Node* node);
  142. private:
  143. /// Cached inverse world transform matrix.
  144. mutable Matrix3x4 inverseWorld_;
  145. /// Cached projection matrix.
  146. mutable Matrix4 projection_;
  147. /// Cached frustum.
  148. mutable Frustum frustum_;
  149. /// Inverse world transform dirty flag.
  150. mutable bool inverseWorldDirty_;
  151. /// Projection matrix dirty flag.
  152. mutable bool projectionDirty_;
  153. /// Frustum dirty flag.
  154. mutable bool frustumDirty_;
  155. /// Orthographic mode flag.
  156. bool orthographic_;
  157. /// Near clip distance.
  158. float nearClip_;
  159. /// Far clip distance.
  160. float farClip_;
  161. /// Field of view.
  162. float fov_;
  163. /// Orthographic view size.
  164. float orthoSize_;
  165. /// Aspect ratio.
  166. float aspectRatio_;
  167. /// Zoom.
  168. float zoom_;
  169. /// LOD bias.
  170. float lodBias_;
  171. /// View mask.
  172. unsigned viewMask_;
  173. /// View override flags.
  174. unsigned viewOverrideFlags_;
  175. /// Fill mode.
  176. FillMode fillMode_;
  177. /// Projection offset.
  178. Vector2 projectionOffset_;
  179. /// Auto aspect ratio flag.
  180. bool autoAspectRatio_;
  181. /// Flip vertical flag.
  182. bool flipVertical_;
  183. };
  184. }