Camera.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  1. //
  2. // Copyright (c) 2008-2017 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "../Precompiled.h"
  23. #include "../Core/Context.h"
  24. #include "../Graphics/Camera.h"
  25. #include "../Graphics/DebugRenderer.h"
  26. #include "../Graphics/Drawable.h"
  27. #include "../Scene/Node.h"
  28. #include "../DebugNew.h"
  29. namespace Atomic
  30. {
  31. extern const char* SCENE_CATEGORY;
  32. static const char* fillModeNames[] =
  33. {
  34. "Solid",
  35. "Wireframe",
  36. "Point",
  37. 0
  38. };
  39. static const Matrix4 flipMatrix(
  40. 1.0f, 0.0f, 0.0f, 0.0f,
  41. 0.0f, -1.0f, 0.0f, 0.0f,
  42. 0.0f, 0.0f, 1.0f, 0.0f,
  43. 0.0f, 0.0f, 0.0f, 1.0f
  44. );
  45. Camera::Camera(Context* context) :
  46. Component(context),
  47. viewDirty_(true),
  48. projectionDirty_(true),
  49. frustumDirty_(true),
  50. orthographic_(false),
  51. nearClip_(DEFAULT_NEARCLIP),
  52. farClip_(DEFAULT_FARCLIP),
  53. fov_(DEFAULT_CAMERA_FOV),
  54. orthoSize_(DEFAULT_ORTHOSIZE),
  55. aspectRatio_(1.0f),
  56. zoom_(1.0f),
  57. lodBias_(1.0f),
  58. viewMask_(DEFAULT_VIEWMASK),
  59. viewOverrideFlags_(VO_NONE),
  60. fillMode_(FILL_SOLID),
  61. projectionOffset_(Vector2::ZERO),
  62. reflectionPlane_(Plane::UP),
  63. clipPlane_(Plane::UP),
  64. autoAspectRatio_(true),
  65. flipVertical_(false),
  66. useReflection_(false),
  67. useClipping_(false),
  68. customProjection_(false)
  69. {
  70. reflectionMatrix_ = reflectionPlane_.ReflectionMatrix();
  71. }
  72. Camera::~Camera()
  73. {
  74. }
  75. void Camera::RegisterObject(Context* context)
  76. {
  77. context->RegisterFactory<Camera>(SCENE_CATEGORY);
  78. ATOMIC_ACCESSOR_ATTRIBUTE("Is Enabled", IsEnabled, SetEnabled, bool, true, AM_DEFAULT);
  79. ATOMIC_ACCESSOR_ATTRIBUTE("Near Clip", GetNearClip, SetNearClip, float, DEFAULT_NEARCLIP, AM_DEFAULT);
  80. ATOMIC_ACCESSOR_ATTRIBUTE("Far Clip", GetFarClip, SetFarClip, float, DEFAULT_FARCLIP, AM_DEFAULT);
  81. ATOMIC_ACCESSOR_ATTRIBUTE("FOV", GetFov, SetFov, float, DEFAULT_CAMERA_FOV, AM_DEFAULT);
  82. ATOMIC_ACCESSOR_ATTRIBUTE("Aspect Ratio", GetAspectRatio, SetAspectRatioInternal, float, 1.0f, AM_DEFAULT);
  83. ATOMIC_ENUM_ATTRIBUTE("Fill Mode", fillMode_, fillModeNames, FILL_SOLID, AM_DEFAULT);
  84. ATOMIC_ATTRIBUTE("Auto Aspect Ratio", bool, autoAspectRatio_, true, AM_DEFAULT);
  85. ATOMIC_ACCESSOR_ATTRIBUTE("Orthographic", IsOrthographic, SetOrthographic, bool, false, AM_DEFAULT);
  86. ATOMIC_ACCESSOR_ATTRIBUTE("Orthographic Size", GetOrthoSize, SetOrthoSizeAttr, float, DEFAULT_ORTHOSIZE, AM_DEFAULT);
  87. ATOMIC_ACCESSOR_ATTRIBUTE("Zoom", GetZoom, SetZoom, float, 1.0f, AM_DEFAULT);
  88. ATOMIC_ACCESSOR_ATTRIBUTE("LOD Bias", GetLodBias, SetLodBias, float, 1.0f, AM_DEFAULT);
  89. ATOMIC_ATTRIBUTE("View Mask", int, viewMask_, DEFAULT_VIEWMASK, AM_DEFAULT);
  90. ATOMIC_ATTRIBUTE("View Override Flags", int, viewOverrideFlags_, VO_NONE, AM_DEFAULT);
  91. ATOMIC_ACCESSOR_ATTRIBUTE("Projection Offset", GetProjectionOffset, SetProjectionOffset, Vector2, Vector2::ZERO, AM_DEFAULT);
  92. ATOMIC_MIXED_ACCESSOR_ATTRIBUTE("Reflection Plane", GetReflectionPlaneAttr, SetReflectionPlaneAttr, Vector4,
  93. Vector4(0.0f, 1.0f, 0.0f, 0.0f), AM_DEFAULT);
  94. ATOMIC_MIXED_ACCESSOR_ATTRIBUTE("Clip Plane", GetClipPlaneAttr, SetClipPlaneAttr, Vector4, Vector4(0.0f, 1.0f, 0.0f, 0.0f),
  95. AM_DEFAULT);
  96. ATOMIC_ACCESSOR_ATTRIBUTE("Use Reflection", GetUseReflection, SetUseReflection, bool, false, AM_DEFAULT);
  97. ATOMIC_ACCESSOR_ATTRIBUTE("Use Clipping", GetUseClipping, SetUseClipping, bool, false, AM_DEFAULT);
  98. }
  99. void Camera::DrawDebugGeometry(DebugRenderer* debug, bool depthTest)
  100. {
  101. debug->AddFrustum(GetFrustum(), Color::WHITE, depthTest);
  102. }
  103. void Camera::SetNearClip(float nearClip)
  104. {
  105. nearClip_ = Max(nearClip, M_MIN_NEARCLIP);
  106. frustumDirty_ = true;
  107. projectionDirty_ = true;
  108. MarkNetworkUpdate();
  109. }
  110. void Camera::SetFarClip(float farClip)
  111. {
  112. farClip_ = Max(farClip, M_MIN_NEARCLIP);
  113. frustumDirty_ = true;
  114. projectionDirty_ = true;
  115. MarkNetworkUpdate();
  116. }
  117. void Camera::SetFov(float fov)
  118. {
  119. fov_ = Clamp(fov, 0.0f, M_MAX_FOV);
  120. frustumDirty_ = true;
  121. projectionDirty_ = true;
  122. MarkNetworkUpdate();
  123. }
  124. void Camera::SetOrthoSize(float orthoSize)
  125. {
  126. orthoSize_ = orthoSize;
  127. aspectRatio_ = 1.0f;
  128. frustumDirty_ = true;
  129. projectionDirty_ = true;
  130. MarkNetworkUpdate();
  131. }
  132. void Camera::SetOrthoSize(const Vector2& orthoSize)
  133. {
  134. autoAspectRatio_ = false;
  135. orthoSize_ = orthoSize.y_;
  136. aspectRatio_ = orthoSize.x_ / orthoSize.y_;
  137. frustumDirty_ = true;
  138. projectionDirty_ = true;
  139. MarkNetworkUpdate();
  140. }
  141. void Camera::SetAspectRatio(float aspectRatio)
  142. {
  143. autoAspectRatio_ = false;
  144. SetAspectRatioInternal(aspectRatio);
  145. }
  146. void Camera::SetZoom(float zoom)
  147. {
  148. zoom_ = Max(zoom, M_EPSILON);
  149. frustumDirty_ = true;
  150. projectionDirty_ = true;
  151. MarkNetworkUpdate();
  152. }
  153. void Camera::SetLodBias(float bias)
  154. {
  155. lodBias_ = Max(bias, M_EPSILON);
  156. MarkNetworkUpdate();
  157. }
  158. void Camera::SetViewMask(unsigned mask)
  159. {
  160. viewMask_ = mask;
  161. MarkNetworkUpdate();
  162. }
  163. void Camera::SetViewOverrideFlags(unsigned flags)
  164. {
  165. viewOverrideFlags_ = flags;
  166. MarkNetworkUpdate();
  167. }
  168. void Camera::SetFillMode(FillMode mode)
  169. {
  170. fillMode_ = mode;
  171. MarkNetworkUpdate();
  172. }
  173. void Camera::SetOrthographic(bool enable)
  174. {
  175. orthographic_ = enable;
  176. frustumDirty_ = true;
  177. projectionDirty_ = true;
  178. MarkNetworkUpdate();
  179. }
  180. void Camera::SetAutoAspectRatio(bool enable)
  181. {
  182. autoAspectRatio_ = enable;
  183. MarkNetworkUpdate();
  184. }
  185. void Camera::SetProjectionOffset(const Vector2& offset)
  186. {
  187. projectionOffset_ = offset;
  188. projectionDirty_ = true;
  189. MarkNetworkUpdate();
  190. }
  191. void Camera::SetUseReflection(bool enable)
  192. {
  193. useReflection_ = enable;
  194. viewDirty_ = true;
  195. frustumDirty_ = true;
  196. MarkNetworkUpdate();
  197. }
  198. void Camera::SetReflectionPlane(const Plane& plane)
  199. {
  200. reflectionPlane_ = plane;
  201. reflectionMatrix_ = reflectionPlane_.ReflectionMatrix();
  202. viewDirty_ = true;
  203. frustumDirty_ = true;
  204. MarkNetworkUpdate();
  205. }
  206. void Camera::SetUseClipping(bool enable)
  207. {
  208. useClipping_ = enable;
  209. projectionDirty_ = true;
  210. MarkNetworkUpdate();
  211. }
  212. void Camera::SetClipPlane(const Plane& plane)
  213. {
  214. clipPlane_ = plane;
  215. MarkNetworkUpdate();
  216. }
  217. void Camera::SetFlipVertical(bool enable)
  218. {
  219. flipVertical_ = enable;
  220. MarkNetworkUpdate();
  221. }
  222. void Camera::SetProjection(const Matrix4& projection)
  223. {
  224. projection_ = projection;
  225. Matrix4 projInverse = projection_.Inverse();
  226. // Calculate the actual near & far clip from the custom matrix
  227. projNearClip_ = (projInverse * Vector3(0.0f, 0.0f, 0.0f)).z_;
  228. projFarClip_ = (projInverse * Vector3(0.0f, 0.0f, 1.0f)).z_;
  229. projectionDirty_ = false;
  230. autoAspectRatio_ = false;
  231. frustumDirty_ = true;
  232. customProjection_ = true;
  233. // Called due to autoAspectRatio changing state, the projection itself is not serialized
  234. MarkNetworkUpdate();
  235. }
  236. float Camera::GetNearClip() const
  237. {
  238. if (projectionDirty_)
  239. UpdateProjection();
  240. return projNearClip_;
  241. }
  242. float Camera::GetFarClip() const
  243. {
  244. if (projectionDirty_)
  245. UpdateProjection();
  246. return projFarClip_;
  247. }
  248. const Frustum& Camera::GetFrustum() const
  249. {
  250. // Use projection_ instead of GetProjection() so that Y-flip has no effect. Update first if necessary
  251. if (projectionDirty_)
  252. UpdateProjection();
  253. if (frustumDirty_)
  254. {
  255. if (customProjection_)
  256. frustum_.Define(projection_ * GetView());
  257. else
  258. {
  259. // If not using a custom projection, prefer calculating frustum from projection parameters instead of matrix
  260. // for better accuracy
  261. if (!orthographic_)
  262. frustum_.Define(fov_, aspectRatio_, zoom_, GetNearClip(), GetFarClip(), GetEffectiveWorldTransform());
  263. else
  264. frustum_.DefineOrtho(orthoSize_, aspectRatio_, zoom_, GetNearClip(), GetFarClip(), GetEffectiveWorldTransform());
  265. }
  266. frustumDirty_ = false;
  267. }
  268. return frustum_;
  269. }
  270. Frustum Camera::GetSplitFrustum(float nearClip, float farClip) const
  271. {
  272. if (projectionDirty_)
  273. UpdateProjection();
  274. nearClip = Max(nearClip, projNearClip_);
  275. farClip = Min(farClip, projFarClip_);
  276. if (farClip < nearClip)
  277. farClip = nearClip;
  278. Frustum ret;
  279. if (customProjection_)
  280. {
  281. // DefineSplit() needs to project the near & far distances, so can not use a combined view-projection matrix.
  282. // Transform to world space afterward instead
  283. ret.DefineSplit(projection_, nearClip, farClip);
  284. ret.Transform(GetEffectiveWorldTransform());
  285. }
  286. else
  287. {
  288. if (!orthographic_)
  289. ret.Define(fov_, aspectRatio_, zoom_, nearClip, farClip, GetEffectiveWorldTransform());
  290. else
  291. ret.DefineOrtho(orthoSize_, aspectRatio_, zoom_, nearClip, farClip, GetEffectiveWorldTransform());
  292. }
  293. return ret;
  294. }
  295. Frustum Camera::GetViewSpaceFrustum() const
  296. {
  297. if (projectionDirty_)
  298. UpdateProjection();
  299. Frustum ret;
  300. if (customProjection_)
  301. ret.Define(projection_);
  302. else
  303. {
  304. if (!orthographic_)
  305. ret.Define(fov_, aspectRatio_, zoom_, GetNearClip(), GetFarClip());
  306. else
  307. ret.DefineOrtho(orthoSize_, aspectRatio_, zoom_, GetNearClip(), GetFarClip());
  308. }
  309. return ret;
  310. }
  311. Frustum Camera::GetViewSpaceSplitFrustum(float nearClip, float farClip) const
  312. {
  313. if (projectionDirty_)
  314. UpdateProjection();
  315. nearClip = Max(nearClip, projNearClip_);
  316. farClip = Min(farClip, projFarClip_);
  317. if (farClip < nearClip)
  318. farClip = nearClip;
  319. Frustum ret;
  320. if (customProjection_)
  321. ret.DefineSplit(projection_, nearClip, farClip);
  322. else
  323. {
  324. if (!orthographic_)
  325. ret.Define(fov_, aspectRatio_, zoom_, nearClip, farClip);
  326. else
  327. ret.DefineOrtho(orthoSize_, aspectRatio_, zoom_, nearClip, farClip);
  328. }
  329. return ret;
  330. }
  331. Ray Camera::GetScreenRay(float x, float y) const
  332. {
  333. Ray ret;
  334. // If projection is invalid, just return a ray pointing forward
  335. if (!IsProjectionValid())
  336. {
  337. ret.origin_ = node_ ? node_->GetWorldPosition() : Vector3::ZERO;
  338. ret.direction_ = node_ ? node_->GetWorldDirection() : Vector3::FORWARD;
  339. return ret;
  340. }
  341. Matrix4 viewProjInverse = (GetProjection() * GetView()).Inverse();
  342. // The parameters range from 0.0 to 1.0. Expand to normalized device coordinates (-1.0 to 1.0) & flip Y axis
  343. x = 2.0f * x - 1.0f;
  344. y = 1.0f - 2.0f * y;
  345. Vector3 near(x, y, 0.0f);
  346. Vector3 far(x, y, 1.0f);
  347. ret.origin_ = viewProjInverse * near;
  348. ret.direction_ = ((viewProjInverse * far) - ret.origin_).Normalized();
  349. return ret;
  350. }
  351. Vector2 Camera::WorldToScreenPoint(const Vector3& worldPos) const
  352. {
  353. Vector3 eyeSpacePos = GetView() * worldPos;
  354. Vector2 ret;
  355. if (eyeSpacePos.z_ > 0.0f)
  356. {
  357. Vector3 screenSpacePos = GetProjection() * eyeSpacePos;
  358. ret.x_ = screenSpacePos.x_;
  359. ret.y_ = screenSpacePos.y_;
  360. }
  361. else
  362. {
  363. ret.x_ = (-eyeSpacePos.x_ > 0.0f) ? -1.0f : 1.0f;
  364. ret.y_ = (-eyeSpacePos.y_ > 0.0f) ? -1.0f : 1.0f;
  365. }
  366. ret.x_ = (ret.x_ / 2.0f) + 0.5f;
  367. ret.y_ = 1.0f - ((ret.y_ / 2.0f) + 0.5f);
  368. return ret;
  369. }
  370. Vector3 Camera::ScreenToWorldPoint(const Vector3& screenPos) const
  371. {
  372. Ray ray = GetScreenRay(screenPos.x_, screenPos.y_);
  373. Vector3 viewSpaceDir = (GetView() * Vector4(ray.direction_, 0.0f));
  374. float rayDistance = (Max(screenPos.z_ - GetNearClip(), 0.0f) / viewSpaceDir.z_);
  375. return ray.origin_ + ray.direction_ * rayDistance;
  376. }
  377. Matrix4 Camera::GetProjection() const
  378. {
  379. if (projectionDirty_)
  380. UpdateProjection();
  381. return flipVertical_ ? flipMatrix * projection_ : projection_;
  382. }
  383. Matrix4 Camera::GetGPUProjection() const
  384. {
  385. #ifndef ATOMIC_OPENGL
  386. return GetProjection(); // Already matches API-specific format
  387. #else
  388. // See formulation for depth range conversion at http://www.ogre3d.org/forums/viewtopic.php?f=4&t=13357
  389. Matrix4 ret = GetProjection();
  390. ret.m20_ = 2.0f * ret.m20_ - ret.m30_;
  391. ret.m21_ = 2.0f * ret.m21_ - ret.m31_;
  392. ret.m22_ = 2.0f * ret.m22_ - ret.m32_;
  393. ret.m23_ = 2.0f * ret.m23_ - ret.m33_;
  394. return ret;
  395. #endif
  396. }
  397. void Camera::GetFrustumSize(Vector3& near, Vector3& far) const
  398. {
  399. Frustum viewSpaceFrustum = GetViewSpaceFrustum();
  400. near = viewSpaceFrustum.vertices_[0];
  401. far = viewSpaceFrustum.vertices_[4];
  402. /// \todo Necessary? Explain this
  403. if (flipVertical_)
  404. {
  405. near.y_ = -near.y_;
  406. far.y_ = -far.y_;
  407. }
  408. }
  409. float Camera::GetHalfViewSize() const
  410. {
  411. if (!orthographic_)
  412. return tanf(fov_ * M_DEGTORAD * 0.5f) / zoom_;
  413. else
  414. return orthoSize_ * 0.5f / zoom_;
  415. }
  416. float Camera::GetDistance(const Vector3& worldPos) const
  417. {
  418. if (!orthographic_)
  419. {
  420. const Vector3& cameraPos = node_ ? node_->GetWorldPosition() : Vector3::ZERO;
  421. return (worldPos - cameraPos).Length();
  422. }
  423. else
  424. return Abs((GetView() * worldPos).z_);
  425. }
  426. float Camera::GetDistanceSquared(const Vector3& worldPos) const
  427. {
  428. if (!orthographic_)
  429. {
  430. const Vector3& cameraPos = node_ ? node_->GetWorldPosition() : Vector3::ZERO;
  431. return (worldPos - cameraPos).LengthSquared();
  432. }
  433. else
  434. {
  435. float distance = (GetView() * worldPos).z_;
  436. return distance * distance;
  437. }
  438. }
  439. float Camera::GetLodDistance(float distance, float scale, float bias) const
  440. {
  441. float d = Max(lodBias_ * bias * scale * zoom_, M_EPSILON);
  442. if (!orthographic_)
  443. return distance / d;
  444. else
  445. return orthoSize_ / d;
  446. }
  447. Quaternion Camera::GetFaceCameraRotation(const Vector3& position, const Quaternion& rotation, FaceCameraMode mode, float minAngle)
  448. {
  449. if (!node_)
  450. return rotation;
  451. switch (mode)
  452. {
  453. case FC_ROTATE_XYZ:
  454. return node_->GetWorldRotation();
  455. case FC_ROTATE_Y:
  456. {
  457. Vector3 euler = rotation.EulerAngles();
  458. euler.y_ = node_->GetWorldRotation().EulerAngles().y_;
  459. return Quaternion(euler.x_, euler.y_, euler.z_);
  460. }
  461. case FC_LOOKAT_XYZ:
  462. {
  463. Quaternion lookAt;
  464. lookAt.FromLookRotation(position - node_->GetWorldPosition());
  465. return lookAt;
  466. }
  467. case FC_LOOKAT_Y:
  468. case FC_LOOKAT_MIXED:
  469. {
  470. // Mixed mode needs true look-at vector
  471. const Vector3 lookAtVec(position - node_->GetWorldPosition());
  472. // While Y-only lookat happens on an XZ plane to make sure there are no unwanted transitions or singularities
  473. const Vector3 lookAtVecXZ(lookAtVec.x_, 0.0f, lookAtVec.z_);
  474. Quaternion lookAt;
  475. lookAt.FromLookRotation(lookAtVecXZ);
  476. Vector3 euler = rotation.EulerAngles();
  477. if (mode == FC_LOOKAT_MIXED)
  478. {
  479. const float angle = lookAtVec.Angle(rotation * Vector3::UP);
  480. if (angle > 180 - minAngle)
  481. euler.x_ += minAngle - (180 - angle);
  482. else if (angle < minAngle)
  483. euler.x_ -= minAngle - angle;
  484. }
  485. euler.y_ = lookAt.EulerAngles().y_;
  486. return Quaternion(euler.x_, euler.y_, euler.z_);
  487. }
  488. default:
  489. return rotation;
  490. }
  491. }
  492. Matrix3x4 Camera::GetEffectiveWorldTransform() const
  493. {
  494. Matrix3x4 worldTransform = node_ ? Matrix3x4(node_->GetWorldPosition(), node_->GetWorldRotation(), 1.0f) : Matrix3x4::IDENTITY;
  495. return useReflection_ ? reflectionMatrix_ * worldTransform : worldTransform;
  496. }
  497. bool Camera::IsProjectionValid() const
  498. {
  499. return GetFarClip() > GetNearClip();
  500. }
  501. const Matrix3x4& Camera::GetView() const
  502. {
  503. if (viewDirty_)
  504. {
  505. // Note: view matrix is unaffected by node or parent scale
  506. view_ = GetEffectiveWorldTransform().Inverse();
  507. viewDirty_ = false;
  508. }
  509. return view_;
  510. }
  511. void Camera::SetAspectRatioInternal(float aspectRatio)
  512. {
  513. if (aspectRatio != aspectRatio_)
  514. {
  515. aspectRatio_ = aspectRatio;
  516. frustumDirty_ = true;
  517. projectionDirty_ = true;
  518. }
  519. MarkNetworkUpdate();
  520. }
  521. void Camera::SetOrthoSizeAttr(float orthoSize)
  522. {
  523. orthoSize_ = orthoSize;
  524. frustumDirty_ = true;
  525. projectionDirty_ = true;
  526. MarkNetworkUpdate();
  527. }
  528. void Camera::SetReflectionPlaneAttr(const Vector4& value)
  529. {
  530. SetReflectionPlane(Plane(value));
  531. }
  532. void Camera::SetClipPlaneAttr(const Vector4& value)
  533. {
  534. SetClipPlane(Plane(value));
  535. }
  536. Vector4 Camera::GetReflectionPlaneAttr() const
  537. {
  538. return reflectionPlane_.ToVector4();
  539. }
  540. Vector4 Camera::GetClipPlaneAttr() const
  541. {
  542. return clipPlane_.ToVector4();
  543. }
  544. void Camera::OnNodeSet(Node* node)
  545. {
  546. if (node)
  547. node->AddListener(this);
  548. }
  549. void Camera::OnMarkedDirty(Node* node)
  550. {
  551. frustumDirty_ = true;
  552. viewDirty_ = true;
  553. }
  554. void Camera::UpdateProjection() const
  555. {
  556. // Start from a zero matrix in case it was custom previously
  557. projection_ = Matrix4::ZERO;
  558. if (!orthographic_)
  559. {
  560. float h = (1.0f / tanf(fov_ * M_DEGTORAD * 0.5f)) * zoom_;
  561. float w = h / aspectRatio_;
  562. float q = farClip_ / (farClip_ - nearClip_);
  563. float r = -q * nearClip_;
  564. projection_.m00_ = w;
  565. projection_.m02_ = projectionOffset_.x_ * 2.0f;
  566. projection_.m11_ = h;
  567. projection_.m12_ = projectionOffset_.y_ * 2.0f;
  568. projection_.m22_ = q;
  569. projection_.m23_ = r;
  570. projection_.m32_ = 1.0f;
  571. projNearClip_ = nearClip_;
  572. projFarClip_ = farClip_;
  573. }
  574. else
  575. {
  576. float h = (1.0f / (orthoSize_ * 0.5f)) * zoom_;
  577. float w = h / aspectRatio_;
  578. float q = 1.0f / farClip_;
  579. float r = 0.0f;
  580. projection_.m00_ = w;
  581. projection_.m03_ = projectionOffset_.x_ * 2.0f;
  582. projection_.m11_ = h;
  583. projection_.m13_ = projectionOffset_.y_ * 2.0f;
  584. projection_.m22_ = q;
  585. projection_.m23_ = r;
  586. projection_.m33_ = 1.0f;
  587. // Near clip does not affect depth accuracy in ortho projection, so let it stay 0 to avoid problems with shader depth parameters
  588. projNearClip_ = 0.0f;
  589. projFarClip_ = farClip_;
  590. }
  591. projectionDirty_ = false;
  592. customProjection_ = false;
  593. }
  594. }