Camera.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. #include "Precompiled.h"
  24. #include "Camera.h"
  25. #include "Context.h"
  26. #include "Drawable.h"
  27. #include "DebugNew.h"
  28. static const float DEFAULT_NEARCLIP = 0.1f;
  29. static const float DEFAULT_FARCLIP = 1000.0f;
  30. static const float DEFAULT_FOV = 45.0f;
  31. static const float DEFAULT_ORTHOSIZE = 20.0f;
  32. OBJECTTYPESTATIC(Camera);
  33. Camera::Camera(Context* context) :
  34. Component(context),
  35. nearClip_(DEFAULT_NEARCLIP),
  36. farClip_(DEFAULT_FARCLIP),
  37. fov_(DEFAULT_FOV),
  38. orthoSize_(DEFAULT_ORTHOSIZE),
  39. aspectRatio_(1.0f),
  40. zoom_(1.0f),
  41. lodBias_(1.0f),
  42. orthographic_(false),
  43. autoAspectRatio_(true),
  44. viewMask_(DEFAULT_VIEWMASK),
  45. viewOverrideFlags_(VOF_NONE),
  46. projectionOffset_(Vector2::ZERO)
  47. {
  48. }
  49. Camera::~Camera()
  50. {
  51. }
  52. void Camera::RegisterObject(Context* context)
  53. {
  54. context->RegisterFactory<Camera>();
  55. ATTRIBUTE(Camera, VAR_FLOAT, "Near Clip", nearClip_, DEFAULT_NEARCLIP);
  56. ATTRIBUTE(Camera, VAR_FLOAT, "Far Clip", farClip_, DEFAULT_FARCLIP);
  57. ATTRIBUTE(Camera, VAR_FLOAT, "FOV", fov_, DEFAULT_FOV);
  58. ATTRIBUTE(Camera, VAR_FLOAT, "Aspect Ratio", aspectRatio_, 1.0f);
  59. ATTRIBUTE(Camera, VAR_BOOL, "Auto Aspect Ratio", autoAspectRatio_, true);
  60. ATTRIBUTE(Camera, VAR_BOOL, "Orthographic", orthographic_, false);
  61. ATTRIBUTE(Camera, VAR_FLOAT, "Orthographic Size", orthoSize_, DEFAULT_ORTHOSIZE);
  62. ATTRIBUTE(Camera, VAR_FLOAT, "Zoom", zoom_, 1.0f);
  63. ATTRIBUTE(Camera, VAR_FLOAT, "LOD Bias", lodBias_, 1.0f);
  64. ATTRIBUTE(Camera, VAR_INT, "View Mask", viewMask_, DEFAULT_VIEWMASK);
  65. ATTRIBUTE(Camera, VAR_INT, "View Override Flags", viewOverrideFlags_, VOF_NONE);
  66. ATTRIBUTE(Camera, VAR_VECTOR2, "Projection Offset", projectionOffset_, Vector2::ZERO);
  67. }
  68. void Camera::SetNearClip(float nearClip)
  69. {
  70. nearClip_ = Max(nearClip, 0.0f);
  71. }
  72. void Camera::SetFarClip(float farClip)
  73. {
  74. farClip_ = Max(farClip, 0.0f);
  75. }
  76. void Camera::SetFov(float fov)
  77. {
  78. fov_ = Clamp(fov, 0.0f, M_MAX_FOV);
  79. }
  80. void Camera::SetOrthoSize(float orthoSize)
  81. {
  82. orthoSize_ = orthoSize;
  83. aspectRatio_ = 1.0f;
  84. }
  85. void Camera::SetOrthoSize(const Vector2& orthoSize)
  86. {
  87. orthoSize_ = orthoSize.y_;
  88. aspectRatio_ = orthoSize.x_ / orthoSize.y_;
  89. }
  90. void Camera::SetAspectRatio(float aspectRatio)
  91. {
  92. aspectRatio_ = aspectRatio;
  93. }
  94. void Camera::SetZoom(float zoom)
  95. {
  96. zoom_ = Max(zoom, M_EPSILON);
  97. }
  98. void Camera::SetLodBias(float bias)
  99. {
  100. lodBias_ = Max(bias, M_EPSILON);
  101. }
  102. void Camera::SetViewMask(unsigned mask)
  103. {
  104. viewMask_ = mask;
  105. }
  106. void Camera::SetViewOverrideFlags(unsigned flags)
  107. {
  108. viewOverrideFlags_ = flags;
  109. }
  110. void Camera::SetOrthographic(bool enable)
  111. {
  112. orthographic_ = enable;
  113. }
  114. void Camera::SetAutoAspectRatio(bool enable)
  115. {
  116. autoAspectRatio_ = enable;
  117. }
  118. void Camera::SetProjectionOffset(const Vector2& offset)
  119. {
  120. projectionOffset_ = offset;
  121. }
  122. float Camera::GetNearClip() const
  123. {
  124. // Orthographic camera has always near clip at 0 to avoid trouble with shader depth parameters,
  125. // and unlike in perspective mode there should be no depth buffer precision issue
  126. if (!orthographic_)
  127. return nearClip_;
  128. else
  129. return 0.0f;
  130. }
  131. Frustum Camera::GetViewSpaceFrustum() const
  132. {
  133. Frustum ret;
  134. if (!orthographic_)
  135. ret.Define(fov_, aspectRatio_, zoom_, GetNearClip(), farClip_);
  136. else
  137. ret.DefineOrtho(orthoSize_, aspectRatio_, zoom_, GetNearClip(), farClip_);
  138. return ret;
  139. }
  140. Frustum Camera::GetSplitFrustum(float nearClip, float farClip)
  141. {
  142. Frustum ret;
  143. nearClip = Max(nearClip, GetNearClip());
  144. farClip = Min(farClip, farClip_);
  145. if (farClip < nearClip)
  146. farClip = nearClip;
  147. if (!orthographic_)
  148. ret.Define(fov_, aspectRatio_, zoom_, nearClip, farClip, GetWorldTransform());
  149. else
  150. ret.DefineOrtho(orthoSize_, aspectRatio_, zoom_, nearClip, farClip, GetWorldTransform());
  151. return ret;
  152. }
  153. Ray Camera::GetScreenRay(float x, float y)
  154. {
  155. Matrix4 viewProjInverse = (GetProjection() * InverseWorldTransform()).Inverse();
  156. // The parameters range from 0.0 to 1.0. Expand to Normalized device coordinates (-1.0 to 1.0) & flip Y axis
  157. x = 2.0f * x - 1.0f;
  158. y = 1.0f - 2.0f * y;
  159. Vector3 near(x, y, 0.0f);
  160. Vector3 far(x, y, 1.0f);
  161. Ray ray;
  162. ray.origin_ = viewProjInverse * near;
  163. ray.direction_ = ((viewProjInverse * far) - ray.origin_).Normalized();
  164. return ray;
  165. }
  166. Frustum Camera::GetFrustum() const
  167. {
  168. Frustum ret;
  169. if (!orthographic_)
  170. ret.Define(fov_, aspectRatio_, zoom_, GetNearClip(), farClip_, GetWorldTransform());
  171. else
  172. ret.DefineOrtho(orthoSize_, aspectRatio_, zoom_, GetNearClip(), farClip_, GetWorldTransform());
  173. return ret;
  174. }
  175. Matrix4 Camera::GetProjection(bool enableOffset) const
  176. {
  177. Matrix4 ret(Matrix4::ZERO);
  178. if (!orthographic_)
  179. {
  180. float nearClip = GetNearClip();
  181. float h = (1.0f / tanf(fov_ * M_DEGTORAD * 0.5f)) * zoom_;
  182. float w = h / aspectRatio_;
  183. float q = farClip_ / (farClip_ - nearClip);
  184. ret.m00_ = w;
  185. ret.m11_ = h;
  186. ret.m22_ = q;
  187. ret.m23_ = -q * nearClip;
  188. ret.m32_ = 1.0f;
  189. if (enableOffset)
  190. {
  191. ret.m02_ = projectionOffset_.x_ * 2.0f;
  192. ret.m12_ = projectionOffset_.y_ * 2.0f;
  193. }
  194. }
  195. else
  196. {
  197. // Disregard near clip, because it does not affect depth precision as with perspective projection
  198. float h = (1.0f / (orthoSize_ * 0.5f)) * zoom_;
  199. float w = h / aspectRatio_;
  200. float q = 1.0f / farClip_;
  201. ret.m00_ = w;
  202. ret.m11_ = h;
  203. ret.m22_ = q;
  204. ret.m33_ = 1.0f;
  205. if (enableOffset)
  206. {
  207. ret.m03_ = projectionOffset_.x_ * 2.0f;
  208. ret.m13_ = projectionOffset_.y_ * 2.0f;
  209. }
  210. }
  211. return ret;
  212. }
  213. void Camera::GetFrustumSize(Vector3& near, Vector3& far) const
  214. {
  215. near.z_ = GetNearClip();
  216. far.z_ = farClip_;
  217. if (!orthographic_)
  218. {
  219. float halfViewSize = tanf(fov_ * M_DEGTORAD * 0.5f) / zoom_;
  220. near.y_ = near.z_ * halfViewSize;
  221. near.x_ = near.y_ * aspectRatio_;
  222. far.y_ = far.z_ * halfViewSize;
  223. far.x_ = far.y_ * aspectRatio_;
  224. }
  225. else
  226. {
  227. float halfViewSize = orthoSize_ * 0.5f / zoom_;
  228. near.y_ = far.y_ = halfViewSize;
  229. near.x_ = far.x_ = near.y_ * aspectRatio_;
  230. }
  231. }
  232. float Camera::GetHalfViewSize() const
  233. {
  234. if (!orthographic_)
  235. return tanf(fov_ * M_DEGTORAD * 0.5f) / zoom_;
  236. else
  237. return orthoSize_ * 0.5f / zoom_;
  238. }
  239. Vector3 Camera::GetForwardVector()
  240. {
  241. return GetWorldTransform().RotationMatrix() * Vector3::FORWARD;
  242. }
  243. Vector3 Camera::GetRightVector()
  244. {
  245. return GetWorldTransform().RotationMatrix() * Vector3::RIGHT;
  246. }
  247. Vector3 Camera::GetUpVector()
  248. {
  249. return GetWorldTransform().RotationMatrix() * Vector3::UP;
  250. }
  251. float Camera::GetDistance(const Vector3& worldPos)
  252. {
  253. if (!orthographic_)
  254. return (worldPos - GetWorldPosition()).LengthFast();
  255. else
  256. return fabsf((InverseWorldTransform() * worldPos).z_);
  257. }
  258. float Camera::GetDistanceSquared(const Vector3& worldPos)
  259. {
  260. if (!orthographic_)
  261. return (worldPos - GetWorldPosition()).LengthSquared();
  262. else
  263. {
  264. float distance = (InverseWorldTransform() * worldPos).z_;
  265. return distance * distance;
  266. }
  267. }
  268. float Camera::GetLodDistance(float distance, float scale, float bias) const
  269. {
  270. float d = Max(lodBias_ * bias * scale * zoom_, M_EPSILON);
  271. if (!orthographic_)
  272. return distance / d;
  273. else
  274. return orthoSize_ / d;
  275. }