Camera.h 6.8 KB

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