Camera.cpp 20 KB

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