Camera.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Öörni
  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 "Ray.h"
  27. static const unsigned VO_NONE = 0x0;
  28. static const unsigned VO_LOW_MATERIAL_QUALITY = 0x1;
  29. static const unsigned VO_DISABLE_SHADOWS = 0x2;
  30. static const unsigned VO_DISABLE_OCCLUSION = 0x4;
  31. /// %Camera component.
  32. class Camera : public Component
  33. {
  34. OBJECT(Camera);
  35. public:
  36. /// Construct.
  37. Camera(Context* context);
  38. /// Destruct.
  39. virtual ~Camera();
  40. /// Register object factory.
  41. static void RegisterObject(Context* context);
  42. /// %Set near clip distance.
  43. void SetNearClip(float nearClip);
  44. /// %Set far clip distance.
  45. void SetFarClip(float farClip);
  46. /// %Set field of view.
  47. void SetFov(float fov);
  48. /// %Set orthographic mode view uniform size.
  49. void SetOrthoSize(float orthoSize);
  50. /// %Set orthographic mode view size.
  51. void SetOrthoSize(const Vector2& orthoSize);
  52. /// %Set aspect ratio.
  53. void SetAspectRatio(float aspectRatio);
  54. /// %Set zoom.
  55. void SetZoom(float zoom);
  56. /// %Set LOD bias.
  57. void SetLodBias(float bias);
  58. /// %Set view mask. Will be and'ed with object's view mask to see if the object should be rendered.
  59. void SetViewMask(unsigned mask);
  60. /// %Set view override flags.
  61. void SetViewOverrideFlags(unsigned flags);
  62. /// %Set orthographic mode enabled/disabled.
  63. void SetOrthographic(bool enable);
  64. /// %Set automatic aspect ratio based on viewport dimensions.
  65. void SetAutoAspectRatio(bool enable);
  66. /// %Set projection offset. It needs to be calculated as (offset in pixels) / (viewport dimensions.)
  67. void SetProjectionOffset(const Vector2& offset);
  68. /// %Set vertical flipping mode.
  69. void SetFlipVertical(bool enable);
  70. /// Return far clip distance.
  71. float GetFarClip() const { return farClip_; }
  72. /// Return near clip distance.
  73. float GetNearClip() const;
  74. /// Return field of view.
  75. float GetFov() const { return fov_; }
  76. /// Return orthographic mode size.
  77. float GetOrthoSize() const { return orthoSize_; }
  78. /// Return aspect ratio.
  79. float GetAspectRatio() const { return aspectRatio_; }
  80. /// Return zoom.
  81. float GetZoom() const { return zoom_; }
  82. /// Return LOD bias.
  83. float GetLodBias() const { return lodBias_; }
  84. /// Return view mask.
  85. unsigned GetViewMask() const { return viewMask_; }
  86. /// Return view override flags.
  87. unsigned GetViewOverrideFlags() const { return viewOverrideFlags_; }
  88. /// Return orthographic flag.
  89. bool IsOrthographic() const { return orthographic_; }
  90. /// Return auto aspect ratio flag.
  91. bool GetAutoAspectRatio() const { return autoAspectRatio_; }
  92. /// Return frustum in world space.
  93. const Frustum& GetFrustum() const;
  94. /// Return API-specific projection matrix.
  95. const Matrix4& GetProjection() const;
  96. /// Return either API-specific or API-independent (D3D convention) projection matrix.
  97. Matrix4 GetProjection(bool apiSpecific) const;
  98. /// Return frustum near and far sizes.
  99. void GetFrustumSize(Vector3& near, Vector3& far) const;
  100. /// Return half view size.
  101. float GetHalfViewSize() const;
  102. /// Return frustum split by custom near and far clip distances.
  103. Frustum GetSplitFrustum(float nearClip, float farClip) const;
  104. /// Return frustum in view space.
  105. Frustum GetViewSpaceFrustum() const;
  106. /// Return split frustum in view space.
  107. Frustum GetViewSpaceSplitFrustum(float nearClip, float farClip) const;
  108. /// Return ray corresponding to screen coordinates (0.0 to 1.0.)
  109. Ray GetScreenRay(float x, float y);
  110. /// Return forward vector.
  111. Vector3 GetForwardVector();
  112. /// Return right vector.
  113. Vector3 GetRightVector();
  114. /// Return up vector.
  115. Vector3 GetUpVector();
  116. /// Return projection offset.
  117. const Vector2& GetProjectionOffset() const { return projectionOffset_; }
  118. /// Return vertical flipping mode.
  119. bool GetFlipVertical() const { return flipVertical_; }
  120. /// Return distance to position. In orthographic mode uses only Z coordinate.
  121. float GetDistance(const Vector3& worldPos) const;
  122. /// Return squared distance to position. In orthographic mode uses only Z coordinate.
  123. float GetDistanceSquared(const Vector3& worldPos) const;
  124. /// Return a scene node's LOD scaled distance.
  125. float GetLodDistance(float distance, float scale, float bias) const;
  126. /// Return if projection parameters are valid for rendering and raycasting.
  127. bool IsProjectionValid() const;
  128. /// Return inverse world transform, also known as the view matrix.
  129. const Matrix3x4& GetInverseWorldTransform() const;
  130. protected:
  131. /// Handle node being assigned.
  132. virtual void OnNodeSet(Node* node);
  133. /// Handle node transform being dirtied.
  134. virtual void OnMarkedDirty(Node* node);
  135. private:
  136. /// Cached inverse world transform matrix.
  137. mutable Matrix3x4 inverseWorld_;
  138. /// Cached projection matrix.
  139. mutable Matrix4 projection_;
  140. /// Cached frustum.
  141. mutable Frustum frustum_;
  142. /// Inverse world transform dirty flag.
  143. mutable bool inverseWorldDirty_;
  144. /// Projection matrix dirty flag.
  145. mutable bool projectionDirty_;
  146. /// Frustum dirty flag.
  147. mutable bool frustumDirty_;
  148. /// Orthographic mode flag.
  149. bool orthographic_;
  150. /// Near clip distance.
  151. float nearClip_;
  152. /// Far clip distance.
  153. float farClip_;
  154. /// Field of view.
  155. float fov_;
  156. /// Orthographic view size.
  157. float orthoSize_;
  158. /// Aspect ratio.
  159. float aspectRatio_;
  160. /// Zoom.
  161. float zoom_;
  162. /// LOD bias.
  163. float lodBias_;
  164. /// View mask.
  165. unsigned viewMask_;
  166. /// View override flags.
  167. unsigned viewOverrideFlags_;
  168. /// Projection offset.
  169. Vector2 projectionOffset_;
  170. /// Auto aspect ratio flag.
  171. bool autoAspectRatio_;
  172. /// Flip vertical flag.
  173. bool flipVertical_;
  174. };