Camera.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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_(VO_NONE),
  46. projectionOffset_(Vector2::ZERO)
  47. {
  48. }
  49. Camera::~Camera()
  50. {
  51. }
  52. void Camera::RegisterObject(Context* context)
  53. {
  54. context->RegisterFactory<Camera>();
  55. ACCESSOR_ATTRIBUTE(Camera, VAR_FLOAT, "Near Clip", GetNearClip, SetNearClip, float, DEFAULT_NEARCLIP, AM_DEFAULT);
  56. ACCESSOR_ATTRIBUTE(Camera, VAR_FLOAT, "Far Clip", GetFarClip, SetFarClip, float, DEFAULT_FARCLIP, AM_DEFAULT);
  57. ACCESSOR_ATTRIBUTE(Camera, VAR_FLOAT, "FOV", GetFov, SetFov, float, DEFAULT_FOV, AM_DEFAULT);
  58. ACCESSOR_ATTRIBUTE(Camera, VAR_FLOAT, "Aspect Ratio", GetAspectRatio, SetAspectRatio, float, 1.0f, AM_DEFAULT);
  59. ATTRIBUTE(Camera, VAR_BOOL, "Auto Aspect Ratio", autoAspectRatio_, true, AM_DEFAULT);
  60. ATTRIBUTE(Camera, VAR_BOOL, "Orthographic", orthographic_, false, AM_DEFAULT);
  61. ATTRIBUTE(Camera, VAR_FLOAT, "Orthographic Size", orthoSize_, DEFAULT_ORTHOSIZE, AM_DEFAULT);
  62. ACCESSOR_ATTRIBUTE(Camera, VAR_FLOAT, "Zoom", GetZoom, SetZoom, float, 1.0f, AM_DEFAULT);
  63. ACCESSOR_ATTRIBUTE(Camera, VAR_FLOAT, "LOD Bias", GetLodBias, SetLodBias, float, 1.0f, AM_DEFAULT);
  64. ATTRIBUTE(Camera, VAR_INT, "View Mask", viewMask_, DEFAULT_VIEWMASK, AM_DEFAULT);
  65. ATTRIBUTE(Camera, VAR_INT, "View Override Flags", viewOverrideFlags_, VO_NONE, AM_DEFAULT);
  66. ATTRIBUTE(Camera, VAR_VECTOR2, "Projection Offset", projectionOffset_, Vector2::ZERO, AM_DEFAULT);
  67. }
  68. void Camera::SetNearClip(float nearClip)
  69. {
  70. nearClip_ = Max(nearClip, M_MIN_NEARCLIP);
  71. }
  72. void Camera::SetFarClip(float farClip)
  73. {
  74. farClip_ = Max(farClip, M_MIN_NEARCLIP);
  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::GetSplitFrustum(float nearClip, float farClip)
  132. {
  133. Frustum ret;
  134. nearClip = Max(nearClip, GetNearClip());
  135. farClip = Min(farClip, farClip_);
  136. if (farClip < nearClip)
  137. farClip = nearClip;
  138. if (!orthographic_)
  139. ret.Define(fov_, aspectRatio_, zoom_, nearClip, farClip, GetWorldTransform());
  140. else
  141. ret.DefineOrtho(orthoSize_, aspectRatio_, zoom_, nearClip, farClip, GetWorldTransform());
  142. return ret;
  143. }
  144. Frustum Camera::GetViewSpaceFrustum() const
  145. {
  146. Frustum ret;
  147. if (!orthographic_)
  148. ret.Define(fov_, aspectRatio_, zoom_, GetNearClip(), farClip_);
  149. else
  150. ret.DefineOrtho(orthoSize_, aspectRatio_, zoom_, GetNearClip(), farClip_);
  151. return ret;
  152. }
  153. Frustum Camera::GetViewSpaceSplitFrustum(float nearClip, float farClip) const
  154. {
  155. Frustum ret;
  156. nearClip = Max(nearClip, GetNearClip());
  157. farClip = Min(farClip, farClip_);
  158. if (farClip < nearClip)
  159. farClip = nearClip;
  160. if (!orthographic_)
  161. ret.Define(fov_, aspectRatio_, zoom_, nearClip, farClip);
  162. else
  163. ret.DefineOrtho(orthoSize_, aspectRatio_, zoom_, nearClip, farClip);
  164. return ret;
  165. }
  166. Ray Camera::GetScreenRay(float x, float y)
  167. {
  168. Ray ret;
  169. // If projection is invalid, just return a ray pointing forward
  170. if (!IsProjectionValid())
  171. {
  172. ret.origin_ = GetWorldPosition();
  173. ret.direction_ = GetForwardVector();
  174. return ret;
  175. }
  176. Matrix4 viewProjInverse = (GetProjection() * GetInverseWorldTransform()).Inverse();
  177. // The parameters range from 0.0 to 1.0. Expand to normalized device coordinates (-1.0 to 1.0) & flip Y axis
  178. x = 2.0f * x - 1.0f;
  179. y = 1.0f - 2.0f * y;
  180. #ifdef USE_OPENGL
  181. Vector3 near(x, y, -1.0f);
  182. Vector3 far(x, y, 1.0f);
  183. #else
  184. Vector3 near(x, y, 0.0f);
  185. Vector3 far(x, y, 1.0f);
  186. #endif
  187. ret.origin_ = viewProjInverse * near;
  188. ret.direction_ = ((viewProjInverse * far) - ret.origin_).Normalized();
  189. return ret;
  190. }
  191. Frustum Camera::GetFrustum() const
  192. {
  193. Frustum ret;
  194. if (!orthographic_)
  195. ret.Define(fov_, aspectRatio_, zoom_, GetNearClip(), farClip_, GetWorldTransform());
  196. else
  197. ret.DefineOrtho(orthoSize_, aspectRatio_, zoom_, GetNearClip(), farClip_, GetWorldTransform());
  198. return ret;
  199. }
  200. Matrix4 Camera::GetProjection() const
  201. {
  202. Matrix4 ret(Matrix4::ZERO);
  203. if (!orthographic_)
  204. {
  205. float nearClip = GetNearClip();
  206. float h = (1.0f / tanf(fov_ * M_DEGTORAD * 0.5f)) * zoom_;
  207. float w = h / aspectRatio_;
  208. #ifdef USE_OPENGL
  209. float q = (farClip_ + nearClip) / (farClip_ - nearClip);
  210. float r = -2.0f * farClip_ * nearClip / (farClip_ - nearClip);
  211. #else
  212. float q = farClip_ / (farClip_ - nearClip);
  213. float r = -q * nearClip;
  214. #endif
  215. ret.m00_ = w;
  216. ret.m02_ = projectionOffset_.x_ * 2.0f;
  217. ret.m11_ = h;
  218. ret.m12_ = projectionOffset_.y_ * 2.0f;
  219. ret.m22_ = q;
  220. ret.m23_ = r;
  221. ret.m32_ = 1.0f;
  222. }
  223. else
  224. {
  225. // Disregard near clip, because it does not affect depth precision as with perspective projection
  226. float h = (1.0f / (orthoSize_ * 0.5f)) * zoom_;
  227. float w = h / aspectRatio_;
  228. #ifdef USE_OPENGL
  229. float q = 2.0f / farClip_;
  230. float r = -1.0f;
  231. #else
  232. float q = 1.0f / farClip_;
  233. float r = 0.0f;
  234. #endif
  235. ret.m00_ = w;
  236. ret.m03_ = projectionOffset_.x_ * 2.0f;
  237. ret.m11_ = h;
  238. ret.m13_ = projectionOffset_.y_ * 2.0f;
  239. ret.m22_ = q;
  240. ret.m23_ = r;
  241. ret.m33_ = 1.0f;
  242. }
  243. return ret;
  244. }
  245. void Camera::GetFrustumSize(Vector3& near, Vector3& far) const
  246. {
  247. near.z_ = GetNearClip();
  248. far.z_ = farClip_;
  249. if (!orthographic_)
  250. {
  251. float halfViewSize = tanf(fov_ * M_DEGTORAD * 0.5f) / zoom_;
  252. near.y_ = near.z_ * halfViewSize;
  253. near.x_ = near.y_ * aspectRatio_;
  254. far.y_ = far.z_ * halfViewSize;
  255. far.x_ = far.y_ * aspectRatio_;
  256. }
  257. else
  258. {
  259. float halfViewSize = orthoSize_ * 0.5f / zoom_;
  260. near.y_ = far.y_ = halfViewSize;
  261. near.x_ = far.x_ = near.y_ * aspectRatio_;
  262. }
  263. }
  264. float Camera::GetHalfViewSize() const
  265. {
  266. if (!orthographic_)
  267. return tanf(fov_ * M_DEGTORAD * 0.5f) / zoom_;
  268. else
  269. return orthoSize_ * 0.5f / zoom_;
  270. }
  271. Vector3 Camera::GetForwardVector()
  272. {
  273. return GetWorldTransform().RotationMatrix() * Vector3::FORWARD;
  274. }
  275. Vector3 Camera::GetRightVector()
  276. {
  277. return GetWorldTransform().RotationMatrix() * Vector3::RIGHT;
  278. }
  279. Vector3 Camera::GetUpVector()
  280. {
  281. return GetWorldTransform().RotationMatrix() * Vector3::UP;
  282. }
  283. float Camera::GetDistance(const Vector3& worldPos)
  284. {
  285. if (!orthographic_)
  286. return (worldPos - GetWorldPosition()).LengthFast();
  287. else
  288. return fabsf((GetInverseWorldTransform() * worldPos).z_);
  289. }
  290. float Camera::GetDistanceSquared(const Vector3& worldPos)
  291. {
  292. if (!orthographic_)
  293. return (worldPos - GetWorldPosition()).LengthSquared();
  294. else
  295. {
  296. float distance = (GetInverseWorldTransform() * worldPos).z_;
  297. return distance * distance;
  298. }
  299. }
  300. float Camera::GetLodDistance(float distance, float scale, float bias) const
  301. {
  302. float d = Max(lodBias_ * bias * scale * zoom_, M_EPSILON);
  303. if (!orthographic_)
  304. return distance / d;
  305. else
  306. return orthoSize_ / d;
  307. }
  308. bool Camera::IsProjectionValid() const
  309. {
  310. return farClip_ > GetNearClip();
  311. }