Camera.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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. #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. static const Matrix4 flipMatrix(
  33. 1.0f, 0.0f, 0.0f, 0.0f,
  34. 0.0f, -1.0f, 0.0f, 0.0f,
  35. 0.0f, 0.0f, 1.0f, 0.0f,
  36. 0.0f, 0.0f, 0.0f, 1.0f
  37. );
  38. OBJECTTYPESTATIC(Camera);
  39. Camera::Camera(Context* context) :
  40. Component(context),
  41. inverseWorldDirty_(true),
  42. projectionDirty_(true),
  43. frustumDirty_(true),
  44. orthographic_(false),
  45. nearClip_(DEFAULT_NEARCLIP),
  46. farClip_(DEFAULT_FARCLIP),
  47. fov_(DEFAULT_FOV),
  48. orthoSize_(DEFAULT_ORTHOSIZE),
  49. aspectRatio_(1.0f),
  50. zoom_(1.0f),
  51. lodBias_(1.0f),
  52. viewMask_(DEFAULT_VIEWMASK),
  53. viewOverrideFlags_(VO_NONE),
  54. projectionOffset_(Vector2::ZERO),
  55. autoAspectRatio_(true),
  56. flipVertical_(false)
  57. {
  58. }
  59. Camera::~Camera()
  60. {
  61. }
  62. void Camera::RegisterObject(Context* context)
  63. {
  64. context->RegisterFactory<Camera>();
  65. ACCESSOR_ATTRIBUTE(Camera, VAR_FLOAT, "Near Clip", GetNearClip, SetNearClip, float, DEFAULT_NEARCLIP, AM_DEFAULT);
  66. ACCESSOR_ATTRIBUTE(Camera, VAR_FLOAT, "Far Clip", GetFarClip, SetFarClip, float, DEFAULT_FARCLIP, AM_DEFAULT);
  67. ACCESSOR_ATTRIBUTE(Camera, VAR_FLOAT, "FOV", GetFov, SetFov, float, DEFAULT_FOV, AM_DEFAULT);
  68. ACCESSOR_ATTRIBUTE(Camera, VAR_FLOAT, "Aspect Ratio", GetAspectRatio, SetAspectRatio, float, 1.0f, AM_DEFAULT);
  69. ATTRIBUTE(Camera, VAR_BOOL, "Auto Aspect Ratio", autoAspectRatio_, true, AM_DEFAULT);
  70. ACCESSOR_ATTRIBUTE(Camera, VAR_BOOL, "Orthographic", IsOrthographic, SetOrthographic, bool, false, AM_DEFAULT);
  71. ACCESSOR_ATTRIBUTE(Camera, VAR_FLOAT, "Orthographic Size", GetOrthoSize, SetOrthoSize, float, DEFAULT_ORTHOSIZE, AM_DEFAULT);
  72. ACCESSOR_ATTRIBUTE(Camera, VAR_FLOAT, "Zoom", GetZoom, SetZoom, float, 1.0f, AM_DEFAULT);
  73. ACCESSOR_ATTRIBUTE(Camera, VAR_FLOAT, "LOD Bias", GetLodBias, SetLodBias, float, 1.0f, AM_DEFAULT);
  74. ATTRIBUTE(Camera, VAR_INT, "View Mask", viewMask_, DEFAULT_VIEWMASK, AM_DEFAULT);
  75. ATTRIBUTE(Camera, VAR_INT, "View Override Flags", viewOverrideFlags_, VO_NONE, AM_DEFAULT);
  76. REF_ACCESSOR_ATTRIBUTE(Camera, VAR_VECTOR2, "Projection Offset", GetProjectionOffset, SetProjectionOffset, Vector2, Vector2::ZERO, AM_DEFAULT);
  77. }
  78. void Camera::SetNearClip(float nearClip)
  79. {
  80. nearClip_ = Max(nearClip, M_MIN_NEARCLIP);
  81. frustumDirty_ = true;
  82. projectionDirty_ = true;
  83. MarkNetworkUpdate();
  84. }
  85. void Camera::SetFarClip(float farClip)
  86. {
  87. farClip_ = Max(farClip, M_MIN_NEARCLIP);
  88. frustumDirty_ = true;
  89. projectionDirty_ = true;
  90. MarkNetworkUpdate();
  91. }
  92. void Camera::SetFov(float fov)
  93. {
  94. fov_ = Clamp(fov, 0.0f, M_MAX_FOV);
  95. frustumDirty_ = true;
  96. projectionDirty_ = true;
  97. MarkNetworkUpdate();
  98. }
  99. void Camera::SetOrthoSize(float orthoSize)
  100. {
  101. orthoSize_ = orthoSize;
  102. aspectRatio_ = 1.0f;
  103. frustumDirty_ = true;
  104. projectionDirty_ = true;
  105. MarkNetworkUpdate();
  106. }
  107. void Camera::SetOrthoSize(const Vector2& orthoSize)
  108. {
  109. orthoSize_ = orthoSize.y_;
  110. aspectRatio_ = orthoSize.x_ / orthoSize.y_;
  111. frustumDirty_ = true;
  112. projectionDirty_ = true;
  113. MarkNetworkUpdate();
  114. }
  115. void Camera::SetAspectRatio(float aspectRatio)
  116. {
  117. aspectRatio_ = aspectRatio;
  118. frustumDirty_ = true;
  119. projectionDirty_ = true;
  120. MarkNetworkUpdate();
  121. }
  122. void Camera::SetZoom(float zoom)
  123. {
  124. zoom_ = Max(zoom, M_EPSILON);
  125. frustumDirty_ = true;
  126. projectionDirty_ = true;
  127. MarkNetworkUpdate();
  128. }
  129. void Camera::SetLodBias(float bias)
  130. {
  131. lodBias_ = Max(bias, M_EPSILON);
  132. MarkNetworkUpdate();
  133. }
  134. void Camera::SetViewMask(unsigned mask)
  135. {
  136. viewMask_ = mask;
  137. MarkNetworkUpdate();
  138. }
  139. void Camera::SetViewOverrideFlags(unsigned flags)
  140. {
  141. viewOverrideFlags_ = flags;
  142. MarkNetworkUpdate();
  143. }
  144. void Camera::SetOrthographic(bool enable)
  145. {
  146. orthographic_ = enable;
  147. frustumDirty_ = true;
  148. projectionDirty_ = true;
  149. MarkNetworkUpdate();
  150. }
  151. void Camera::SetAutoAspectRatio(bool enable)
  152. {
  153. autoAspectRatio_ = enable;
  154. MarkNetworkUpdate();
  155. }
  156. void Camera::SetProjectionOffset(const Vector2& offset)
  157. {
  158. projectionOffset_ = offset;
  159. projectionDirty_ = true;
  160. MarkNetworkUpdate();
  161. }
  162. void Camera::SetFlipVertical(bool enable)
  163. {
  164. flipVertical_ = enable;
  165. projectionDirty_ = true;
  166. MarkNetworkUpdate();
  167. }
  168. float Camera::GetNearClip() const
  169. {
  170. // Orthographic camera has always near clip at 0 to avoid trouble with shader depth parameters,
  171. // and unlike in perspective mode there should be no depth buffer precision issue
  172. if (!orthographic_)
  173. return nearClip_;
  174. else
  175. return 0.0f;
  176. }
  177. Frustum Camera::GetSplitFrustum(float nearClip, float farClip) const
  178. {
  179. Frustum ret;
  180. nearClip = Max(nearClip, GetNearClip());
  181. farClip = Min(farClip, farClip_);
  182. if (farClip < nearClip)
  183. farClip = nearClip;
  184. if (!orthographic_)
  185. ret.Define(fov_, aspectRatio_, zoom_, nearClip, farClip, GetWorldTransform());
  186. else
  187. ret.DefineOrtho(orthoSize_, aspectRatio_, zoom_, nearClip, farClip, GetWorldTransform());
  188. return ret;
  189. }
  190. Frustum Camera::GetViewSpaceFrustum() const
  191. {
  192. Frustum ret;
  193. if (!orthographic_)
  194. ret.Define(fov_, aspectRatio_, zoom_, GetNearClip(), farClip_);
  195. else
  196. ret.DefineOrtho(orthoSize_, aspectRatio_, zoom_, GetNearClip(), farClip_);
  197. return ret;
  198. }
  199. Frustum Camera::GetViewSpaceSplitFrustum(float nearClip, float farClip) const
  200. {
  201. Frustum ret;
  202. nearClip = Max(nearClip, GetNearClip());
  203. farClip = Min(farClip, farClip_);
  204. if (farClip < nearClip)
  205. farClip = nearClip;
  206. if (!orthographic_)
  207. ret.Define(fov_, aspectRatio_, zoom_, nearClip, farClip);
  208. else
  209. ret.DefineOrtho(orthoSize_, aspectRatio_, zoom_, nearClip, farClip);
  210. return ret;
  211. }
  212. Ray Camera::GetScreenRay(float x, float y)
  213. {
  214. Ray ret;
  215. // If projection is invalid, just return a ray pointing forward
  216. if (!IsProjectionValid())
  217. {
  218. ret.origin_ = GetWorldPosition();
  219. ret.direction_ = GetForwardVector();
  220. return ret;
  221. }
  222. Matrix4 viewProjInverse = (GetProjection(false) * GetInverseWorldTransform()).Inverse();
  223. // The parameters range from 0.0 to 1.0. Expand to normalized device coordinates (-1.0 to 1.0) & flip Y axis
  224. x = 2.0f * x - 1.0f;
  225. y = 1.0f - 2.0f * y;
  226. Vector3 near(x, y, 0.0f);
  227. Vector3 far(x, y, 1.0f);
  228. ret.origin_ = viewProjInverse * near;
  229. ret.direction_ = ((viewProjInverse * far) - ret.origin_).Normalized();
  230. return ret;
  231. }
  232. const Frustum& Camera::GetFrustum() const
  233. {
  234. if (frustumDirty_)
  235. {
  236. if (!orthographic_)
  237. frustum_.Define(fov_, aspectRatio_, zoom_, GetNearClip(), farClip_, GetWorldTransform());
  238. else
  239. frustum_.DefineOrtho(orthoSize_, aspectRatio_, zoom_, GetNearClip(), farClip_, GetWorldTransform());
  240. frustumDirty_ = false;
  241. }
  242. return frustum_;
  243. }
  244. const Matrix4& Camera::GetProjection() const
  245. {
  246. if (projectionDirty_)
  247. {
  248. projection_ = GetProjection(true);
  249. projectionDirty_ = false;
  250. }
  251. return projection_;
  252. }
  253. Matrix4 Camera::GetProjection(bool apiSpecific) const
  254. {
  255. Matrix4 ret(Matrix4::ZERO);
  256. if (!orthographic_)
  257. {
  258. float nearClip = GetNearClip();
  259. float h = (1.0f / tanf(fov_ * M_DEGTORAD * 0.5f)) * zoom_;
  260. float w = h / aspectRatio_;
  261. float q, r;
  262. if (apiSpecific)
  263. {
  264. #ifdef USE_OPENGL
  265. q = (farClip_ + nearClip) / (farClip_ - nearClip);
  266. r = -2.0f * farClip_ * nearClip / (farClip_ - nearClip);
  267. #else
  268. q = farClip_ / (farClip_ - nearClip);
  269. r = -q * nearClip;
  270. #endif
  271. }
  272. else
  273. {
  274. q = farClip_ / (farClip_ - nearClip);
  275. r = -q * nearClip;
  276. }
  277. ret.m00_ = w;
  278. ret.m02_ = projectionOffset_.x_ * 2.0f;
  279. ret.m11_ = h;
  280. ret.m12_ = projectionOffset_.y_ * 2.0f;
  281. ret.m22_ = q;
  282. ret.m23_ = r;
  283. ret.m32_ = 1.0f;
  284. }
  285. else
  286. {
  287. // Disregard near clip, because it does not affect depth precision as with perspective projection
  288. float h = (1.0f / (orthoSize_ * 0.5f)) * zoom_;
  289. float w = h / aspectRatio_;
  290. float q, r;
  291. if (apiSpecific)
  292. {
  293. #ifdef USE_OPENGL
  294. q = 2.0f / farClip_;
  295. r = -1.0f;
  296. #else
  297. q = 1.0f / farClip_;
  298. r = 0.0f;
  299. #endif
  300. }
  301. else
  302. {
  303. q = 1.0f / farClip_;
  304. r = 0.0f;
  305. }
  306. ret.m00_ = w;
  307. ret.m03_ = projectionOffset_.x_ * 2.0f;
  308. ret.m11_ = h;
  309. ret.m13_ = projectionOffset_.y_ * 2.0f;
  310. ret.m22_ = q;
  311. ret.m23_ = r;
  312. ret.m33_ = 1.0f;
  313. }
  314. if (flipVertical_)
  315. ret = flipMatrix * ret;
  316. return ret;
  317. }
  318. void Camera::GetFrustumSize(Vector3& near, Vector3& far) const
  319. {
  320. near.z_ = GetNearClip();
  321. far.z_ = farClip_;
  322. if (!orthographic_)
  323. {
  324. float halfViewSize = tanf(fov_ * M_DEGTORAD * 0.5f) / zoom_;
  325. near.y_ = near.z_ * halfViewSize;
  326. near.x_ = near.y_ * aspectRatio_;
  327. far.y_ = far.z_ * halfViewSize;
  328. far.x_ = far.y_ * aspectRatio_;
  329. }
  330. else
  331. {
  332. float halfViewSize = orthoSize_ * 0.5f / zoom_;
  333. near.y_ = far.y_ = halfViewSize;
  334. near.x_ = far.x_ = near.y_ * aspectRatio_;
  335. }
  336. if (flipVertical_)
  337. {
  338. near.y_ = -near.y_;
  339. far.y_ = -far.y_;
  340. }
  341. }
  342. float Camera::GetHalfViewSize() const
  343. {
  344. if (!orthographic_)
  345. return tanf(fov_ * M_DEGTORAD * 0.5f) / zoom_;
  346. else
  347. return orthoSize_ * 0.5f / zoom_;
  348. }
  349. Vector3 Camera::GetForwardVector()
  350. {
  351. return GetWorldTransform().RotationMatrix() * Vector3::FORWARD;
  352. }
  353. Vector3 Camera::GetRightVector()
  354. {
  355. return GetWorldTransform().RotationMatrix() * Vector3::RIGHT;
  356. }
  357. Vector3 Camera::GetUpVector()
  358. {
  359. return GetWorldTransform().RotationMatrix() * Vector3::UP;
  360. }
  361. float Camera::GetDistance(const Vector3& worldPos) const
  362. {
  363. if (!orthographic_)
  364. {
  365. const Vector3& cameraPos = node_ ? node_->GetWorldPosition() : Vector3::ZERO;
  366. return (worldPos - cameraPos).Length();
  367. }
  368. else
  369. return fabsf((GetInverseWorldTransform() * worldPos).z_);
  370. }
  371. float Camera::GetDistanceSquared(const Vector3& worldPos) const
  372. {
  373. if (!orthographic_)
  374. {
  375. const Vector3& cameraPos = node_ ? node_->GetWorldPosition() : Vector3::ZERO;
  376. return (worldPos - cameraPos).LengthSquared();
  377. }
  378. else
  379. {
  380. float distance = (GetInverseWorldTransform() * worldPos).z_;
  381. return distance * distance;
  382. }
  383. }
  384. float Camera::GetLodDistance(float distance, float scale, float bias) const
  385. {
  386. float d = Max(lodBias_ * bias * scale * zoom_, M_EPSILON);
  387. if (!orthographic_)
  388. return distance / d;
  389. else
  390. return orthoSize_ / d;
  391. }
  392. bool Camera::IsProjectionValid() const
  393. {
  394. return farClip_ > GetNearClip();
  395. }
  396. const Matrix3x4& Camera::GetInverseWorldTransform() const
  397. {
  398. if (inverseWorldDirty_)
  399. {
  400. inverseWorld_ = GetWorldTransform().Inverse();
  401. inverseWorldDirty_ = false;
  402. }
  403. return inverseWorld_;
  404. }
  405. void Camera::OnNodeSet(Node* node)
  406. {
  407. if (node)
  408. node->AddListener(this);
  409. }
  410. void Camera::OnMarkedDirty(Node* node)
  411. {
  412. frustumDirty_ = true;
  413. inverseWorldDirty_ = true;
  414. }