Camera.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 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. /// Return far clip distance.
  70. float GetFarClip() const { return farClip_; }
  71. /// Return near clip distance.
  72. float GetNearClip() const;
  73. /// Return field of view.
  74. float GetFov() const { return fov_; }
  75. /// Return orthographic mode size.
  76. float GetOrthoSize() const { return orthoSize_; }
  77. /// Return aspect ratio.
  78. float GetAspectRatio() const { return aspectRatio_; }
  79. /// Return zoom.
  80. float GetZoom() const { return zoom_; }
  81. /// Return LOD bias.
  82. float GetLodBias() const { return lodBias_; }
  83. /// Return view mask.
  84. unsigned GetViewMask() const { return viewMask_; }
  85. /// Return view override flags.
  86. unsigned GetViewOverrideFlags() const { return viewOverrideFlags_; }
  87. /// Return orthographic flag.
  88. bool IsOrthographic() const { return orthographic_; }
  89. /// Return auto aspect ratio flag.
  90. bool GetAutoAspectRatio() const { return autoAspectRatio_; }
  91. /// Return frustum in world space.
  92. Frustum GetFrustum() const;
  93. /// Return projection matrix.
  94. Matrix4 GetProjection() const;
  95. /// Return frustum near and far sizes.
  96. void GetFrustumSize(Vector3& near, Vector3& far) const;
  97. /// Return half view size.
  98. float GetHalfViewSize() const;
  99. /// Return frustum split by custom near and far clip distances.
  100. Frustum GetSplitFrustum(float nearClip, float farClip);
  101. /// Return frustum in view space.
  102. Frustum GetViewSpaceFrustum() const;
  103. /// Return split frustum in view space.
  104. Frustum GetViewSpaceSplitFrustum(float nearClip, float farClip) const;
  105. /// Return ray corresponding to screen coordinates (0.0 to 1.0.)
  106. Ray GetScreenRay(float x, float y);
  107. /// Return forward vector.
  108. Vector3 GetForwardVector();
  109. /// Return right vector.
  110. Vector3 GetRightVector();
  111. /// Return up vector.
  112. Vector3 GetUpVector();
  113. /// Return projection offset.
  114. const Vector2& GetProjectionOffset() const { return projectionOffset_; }
  115. /// Return distance to position. In orthographic mode uses only Z coordinate.
  116. float GetDistance(const Vector3& worldPos);
  117. /// Return squared distance to position. In orthographic mode uses only Z coordinate.
  118. float GetDistanceSquared(const Vector3& worldPos);
  119. /// Return a scene node's LOD scaled distance.
  120. float GetLodDistance(float distance, float scale, float bias) const;
  121. /// Return if projection parameters are valid for rendering and raycasting
  122. bool IsProjectionValid() const;
  123. /// Return inverse world transform, also known as the view matrix.
  124. Matrix3x4 GetInverseWorldTransform() const { return GetWorldTransform().Inverse(); }
  125. private:
  126. /// Near clip distance.
  127. float nearClip_;
  128. /// Far clip distance.
  129. float farClip_;
  130. /// Field of view.
  131. float fov_;
  132. /// Orthographic view size.
  133. float orthoSize_;
  134. /// Aspect ratio.
  135. float aspectRatio_;
  136. /// Zoom.
  137. float zoom_;
  138. /// LOD bias.
  139. float lodBias_;
  140. /// View mask.
  141. unsigned viewMask_;
  142. /// View override flags.
  143. unsigned viewOverrideFlags_;
  144. /// Orthographic mode flag.
  145. bool orthographic_;
  146. /// Auto aspect ratio flag.
  147. bool autoAspectRatio_;
  148. /// Projection offset.
  149. Vector2 projectionOffset_;
  150. };