Camera.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. //
  2. // Copyright (c) 2008-2014 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 "Camera.h"
  24. #include "Context.h"
  25. #include "DebugRenderer.h"
  26. #include "Drawable.h"
  27. #include "Node.h"
  28. #include "DebugNew.h"
  29. namespace Urho3D
  30. {
  31. extern const char* SCENE_CATEGORY;
  32. static const float DEFAULT_NEARCLIP = 0.1f;
  33. static const float DEFAULT_FARCLIP = 1000.0f;
  34. static const float DEFAULT_FOV = 45.0f;
  35. static const float DEFAULT_ORTHOSIZE = 20.0f;
  36. static const char* fillModeNames[] =
  37. {
  38. "Solid",
  39. "Wireframe",
  40. "Point",
  41. 0
  42. };
  43. static const Matrix4 flipMatrix(
  44. 1.0f, 0.0f, 0.0f, 0.0f,
  45. 0.0f, -1.0f, 0.0f, 0.0f,
  46. 0.0f, 0.0f, 1.0f, 0.0f,
  47. 0.0f, 0.0f, 0.0f, 1.0f
  48. );
  49. Camera::Camera(Context* context) :
  50. Component(context),
  51. viewDirty_(true),
  52. projectionDirty_(true),
  53. frustumDirty_(true),
  54. orthographic_(false),
  55. nearClip_(DEFAULT_NEARCLIP),
  56. farClip_(DEFAULT_FARCLIP),
  57. fov_(DEFAULT_FOV),
  58. orthoSize_(DEFAULT_ORTHOSIZE),
  59. aspectRatio_(1.0f),
  60. zoom_(1.0f),
  61. lodBias_(1.0f),
  62. viewMask_(DEFAULT_VIEWMASK),
  63. viewOverrideFlags_(VO_NONE),
  64. fillMode_(FILL_SOLID),
  65. projectionOffset_(Vector2::ZERO),
  66. reflectionPlane_(Plane::UP),
  67. clipPlane_(Plane::UP),
  68. autoAspectRatio_(true),
  69. flipVertical_(false),
  70. useReflection_(false),
  71. useClipping_(false)
  72. {
  73. reflectionMatrix_ = reflectionPlane_.ReflectionMatrix();
  74. }
  75. Camera::~Camera()
  76. {
  77. }
  78. void Camera::RegisterObject(Context* context)
  79. {
  80. context->RegisterFactory<Camera>(SCENE_CATEGORY);
  81. ACCESSOR_ATTRIBUTE(Camera, VAR_BOOL, "Is Enabled", IsEnabled, SetEnabled, bool, true, AM_DEFAULT);
  82. ACCESSOR_ATTRIBUTE(Camera, VAR_FLOAT, "Near Clip", GetNearClip, SetNearClip, float, DEFAULT_NEARCLIP, AM_DEFAULT);
  83. ACCESSOR_ATTRIBUTE(Camera, VAR_FLOAT, "Far Clip", GetFarClip, SetFarClip, float, DEFAULT_FARCLIP, AM_DEFAULT);
  84. ACCESSOR_ATTRIBUTE(Camera, VAR_FLOAT, "FOV", GetFov, SetFov, float, DEFAULT_FOV, AM_DEFAULT);
  85. ACCESSOR_ATTRIBUTE(Camera, VAR_FLOAT, "Aspect Ratio", GetAspectRatio, SetAspectRatioInternal, float, 1.0f, AM_DEFAULT);
  86. ENUM_ATTRIBUTE(Camera, "Fill Mode", fillMode_, fillModeNames, FILL_SOLID, AM_DEFAULT);
  87. ATTRIBUTE(Camera, VAR_BOOL, "Auto Aspect Ratio", autoAspectRatio_, true, AM_DEFAULT);
  88. ACCESSOR_ATTRIBUTE(Camera, VAR_BOOL, "Orthographic", IsOrthographic, SetOrthographic, bool, false, AM_DEFAULT);
  89. ACCESSOR_ATTRIBUTE(Camera, VAR_FLOAT, "Orthographic Size", GetOrthoSize, SetOrthoSize, float, DEFAULT_ORTHOSIZE, AM_DEFAULT);
  90. ACCESSOR_ATTRIBUTE(Camera, VAR_FLOAT, "Zoom", GetZoom, SetZoom, float, 1.0f, AM_DEFAULT);
  91. ACCESSOR_ATTRIBUTE(Camera, VAR_FLOAT, "LOD Bias", GetLodBias, SetLodBias, float, 1.0f, AM_DEFAULT);
  92. ATTRIBUTE(Camera, VAR_INT, "View Mask", viewMask_, DEFAULT_VIEWMASK, AM_DEFAULT);
  93. ATTRIBUTE(Camera, VAR_INT, "View Override Flags", viewOverrideFlags_, VO_NONE, AM_DEFAULT);
  94. REF_ACCESSOR_ATTRIBUTE(Camera, VAR_VECTOR2, "Projection Offset", GetProjectionOffset, SetProjectionOffset, Vector2, Vector2::ZERO, AM_DEFAULT);
  95. ACCESSOR_ATTRIBUTE(Camera, VAR_VECTOR4, "Reflection Plane", GetReflectionPlaneAttr, SetReflectionPlaneAttr, Vector4, Vector4(0.0f, 1.0f, 0.0f, 0.0f), AM_DEFAULT);
  96. ACCESSOR_ATTRIBUTE(Camera, VAR_VECTOR4, "Clip Plane", GetClipPlaneAttr, SetClipPlaneAttr, Vector4, Vector4(0.0f, 1.0f, 0.0f, 0.0f), AM_DEFAULT);
  97. ACCESSOR_ATTRIBUTE(Camera, VAR_BOOL, "Use Reflection", GetUseReflection, SetUseReflection, bool, false, AM_DEFAULT);
  98. ACCESSOR_ATTRIBUTE(Camera, VAR_BOOL, "Use Clipping", GetUseClipping, SetUseClipping, bool, false, AM_DEFAULT);
  99. }
  100. void Camera::DrawDebugGeometry(DebugRenderer* debug, bool depthTest)
  101. {
  102. debug->AddFrustum(GetFrustum(), Color::WHITE, depthTest);
  103. }
  104. void Camera::SetNearClip(float nearClip)
  105. {
  106. nearClip_ = Max(nearClip, M_MIN_NEARCLIP);
  107. frustumDirty_ = true;
  108. projectionDirty_ = true;
  109. MarkNetworkUpdate();
  110. }
  111. void Camera::SetFarClip(float farClip)
  112. {
  113. farClip_ = Max(farClip, M_MIN_NEARCLIP);
  114. frustumDirty_ = true;
  115. projectionDirty_ = true;
  116. MarkNetworkUpdate();
  117. }
  118. void Camera::SetFov(float fov)
  119. {
  120. fov_ = Clamp(fov, 0.0f, M_MAX_FOV);
  121. frustumDirty_ = true;
  122. projectionDirty_ = true;
  123. MarkNetworkUpdate();
  124. }
  125. void Camera::SetOrthoSize(float orthoSize)
  126. {
  127. orthoSize_ = orthoSize;
  128. aspectRatio_ = 1.0f;
  129. frustumDirty_ = true;
  130. projectionDirty_ = true;
  131. MarkNetworkUpdate();
  132. }
  133. void Camera::SetOrthoSize(const Vector2& orthoSize)
  134. {
  135. autoAspectRatio_ = false;
  136. orthoSize_ = orthoSize.y_;
  137. aspectRatio_ = orthoSize.x_ / orthoSize.y_;
  138. frustumDirty_ = true;
  139. projectionDirty_ = true;
  140. MarkNetworkUpdate();
  141. }
  142. void Camera::SetAspectRatio(float aspectRatio)
  143. {
  144. autoAspectRatio_ = false;
  145. SetAspectRatioInternal(aspectRatio);
  146. }
  147. void Camera::SetZoom(float zoom)
  148. {
  149. zoom_ = Max(zoom, M_EPSILON);
  150. frustumDirty_ = true;
  151. projectionDirty_ = true;
  152. MarkNetworkUpdate();
  153. }
  154. void Camera::SetLodBias(float bias)
  155. {
  156. lodBias_ = Max(bias, M_EPSILON);
  157. MarkNetworkUpdate();
  158. }
  159. void Camera::SetViewMask(unsigned mask)
  160. {
  161. viewMask_ = mask;
  162. MarkNetworkUpdate();
  163. }
  164. void Camera::SetViewOverrideFlags(unsigned flags)
  165. {
  166. viewOverrideFlags_ = flags;
  167. MarkNetworkUpdate();
  168. }
  169. void Camera::SetFillMode(FillMode mode)
  170. {
  171. fillMode_ = mode;
  172. MarkNetworkUpdate();
  173. }
  174. void Camera::SetOrthographic(bool enable)
  175. {
  176. orthographic_ = enable;
  177. frustumDirty_ = true;
  178. projectionDirty_ = true;
  179. MarkNetworkUpdate();
  180. }
  181. void Camera::SetAutoAspectRatio(bool enable)
  182. {
  183. autoAspectRatio_ = enable;
  184. MarkNetworkUpdate();
  185. }
  186. void Camera::SetProjectionOffset(const Vector2& offset)
  187. {
  188. projectionOffset_ = offset;
  189. projectionDirty_ = true;
  190. MarkNetworkUpdate();
  191. }
  192. void Camera::SetUseReflection(bool enable)
  193. {
  194. useReflection_ = enable;
  195. viewDirty_ = true;
  196. frustumDirty_ = true;
  197. MarkNetworkUpdate();
  198. }
  199. void Camera::SetReflectionPlane(const Plane& plane)
  200. {
  201. reflectionPlane_ = plane;
  202. reflectionMatrix_ = reflectionPlane_.ReflectionMatrix();
  203. viewDirty_ = true;
  204. frustumDirty_ = true;
  205. MarkNetworkUpdate();
  206. }
  207. void Camera::SetUseClipping(bool enable)
  208. {
  209. useClipping_ = enable;
  210. projectionDirty_ = true;
  211. MarkNetworkUpdate();
  212. }
  213. void Camera::SetClipPlane(const Plane& plane)
  214. {
  215. clipPlane_ = plane;
  216. projectionDirty_ = true;
  217. MarkNetworkUpdate();
  218. }
  219. void Camera::SetFlipVertical(bool enable)
  220. {
  221. flipVertical_ = enable;
  222. projectionDirty_ = true;
  223. MarkNetworkUpdate();
  224. }
  225. float Camera::GetNearClip() const
  226. {
  227. // Orthographic camera has always near clip at 0 to avoid trouble with shader depth parameters,
  228. // and unlike in perspective mode there should be no depth buffer precision issue
  229. if (!orthographic_)
  230. return nearClip_;
  231. else
  232. return 0.0f;
  233. }
  234. Frustum Camera::GetSplitFrustum(float nearClip, float farClip) const
  235. {
  236. Frustum ret;
  237. Matrix3x4 worldTransform = GetEffectiveWorldTransform();
  238. nearClip = Max(nearClip, GetNearClip());
  239. farClip = Min(farClip, farClip_);
  240. if (farClip < nearClip)
  241. farClip = nearClip;
  242. if (!orthographic_)
  243. ret.Define(fov_, aspectRatio_, zoom_, nearClip, farClip, worldTransform);
  244. else
  245. ret.DefineOrtho(orthoSize_, aspectRatio_, zoom_, nearClip, farClip, worldTransform);
  246. return ret;
  247. }
  248. Frustum Camera::GetViewSpaceFrustum() const
  249. {
  250. Frustum ret;
  251. if (!orthographic_)
  252. ret.Define(fov_, aspectRatio_, zoom_, GetNearClip(), farClip_);
  253. else
  254. ret.DefineOrtho(orthoSize_, aspectRatio_, zoom_, GetNearClip(), farClip_);
  255. return ret;
  256. }
  257. Frustum Camera::GetViewSpaceSplitFrustum(float nearClip, float farClip) const
  258. {
  259. Frustum ret;
  260. nearClip = Max(nearClip, GetNearClip());
  261. farClip = Min(farClip, farClip_);
  262. if (farClip < nearClip)
  263. farClip = nearClip;
  264. if (!orthographic_)
  265. ret.Define(fov_, aspectRatio_, zoom_, nearClip, farClip);
  266. else
  267. ret.DefineOrtho(orthoSize_, aspectRatio_, zoom_, nearClip, farClip);
  268. return ret;
  269. }
  270. Ray Camera::GetScreenRay(float x, float y) const
  271. {
  272. Ray ret;
  273. // If projection is invalid, just return a ray pointing forward
  274. if (!IsProjectionValid())
  275. {
  276. ret.origin_ = node_ ? node_->GetWorldPosition() : Vector3::ZERO;
  277. ret.direction_ = node_ ? node_->GetWorldDirection() : Vector3::FORWARD;
  278. return ret;
  279. }
  280. Matrix4 viewProjInverse = (GetProjection(false) * GetView()).Inverse();
  281. // The parameters range from 0.0 to 1.0. Expand to normalized device coordinates (-1.0 to 1.0) & flip Y axis
  282. x = 2.0f * x - 1.0f;
  283. y = 1.0f - 2.0f * y;
  284. Vector3 near(x, y, 0.0f);
  285. Vector3 far(x, y, 1.0f);
  286. ret.origin_ = viewProjInverse * near;
  287. ret.direction_ = ((viewProjInverse * far) - ret.origin_).Normalized();
  288. return ret;
  289. }
  290. Vector2 Camera::WorldToScreenPoint(const Vector3& worldPos) const
  291. {
  292. Vector3 eyeSpacePos = GetView() * worldPos;
  293. Vector2 ret;
  294. if(eyeSpacePos.z_ > 0.0f)
  295. {
  296. Vector3 screenSpacePos = GetProjection(false) * eyeSpacePos;
  297. ret.x_ = screenSpacePos.x_;
  298. ret.y_ = screenSpacePos.y_;
  299. }
  300. else
  301. {
  302. ret.x_ = (-eyeSpacePos.x_ > 0.0f) ? -1.0f : 1.0f;
  303. ret.y_ = (-eyeSpacePos.y_ > 0.0f) ? -1.0f : 1.0f;
  304. }
  305. ret.x_ = (ret.x_ / 2.0f) + 0.5f;
  306. ret.y_ = 1.0f - ((ret.y_ / 2.0f) + 0.5f);
  307. return ret;
  308. }
  309. Vector3 Camera::ScreenToWorldPoint(const Vector3& screenPos) const
  310. {
  311. Ray ray = GetScreenRay(screenPos.x_, screenPos.y_);
  312. return ray.origin_ + ray.direction_ * screenPos.z_;
  313. }
  314. const Frustum& Camera::GetFrustum() const
  315. {
  316. if (frustumDirty_)
  317. {
  318. Matrix3x4 worldTransform = GetEffectiveWorldTransform();
  319. if (!orthographic_)
  320. frustum_.Define(fov_, aspectRatio_, zoom_, GetNearClip(), farClip_, worldTransform);
  321. else
  322. frustum_.DefineOrtho(orthoSize_, aspectRatio_, zoom_, GetNearClip(), farClip_, worldTransform);
  323. frustumDirty_ = false;
  324. }
  325. return frustum_;
  326. }
  327. const Matrix4& Camera::GetProjection() const
  328. {
  329. if (projectionDirty_)
  330. {
  331. projection_ = GetProjection(true);
  332. projectionDirty_ = false;
  333. }
  334. return projection_;
  335. }
  336. Matrix4 Camera::GetProjection(bool apiSpecific) const
  337. {
  338. Matrix4 ret(Matrix4::ZERO);
  339. // Whether to construct matrix using OpenGL or Direct3D clip space convention
  340. #ifdef USE_OPENGL
  341. bool openGLFormat = apiSpecific;
  342. #else
  343. bool openGLFormat = false;
  344. #endif
  345. if (!orthographic_)
  346. {
  347. float nearClip = GetNearClip();
  348. float h = (1.0f / tanf(fov_ * M_DEGTORAD * 0.5f)) * zoom_;
  349. float w = h / aspectRatio_;
  350. float q, r;
  351. if (openGLFormat)
  352. {
  353. q = (farClip_ + nearClip) / (farClip_ - nearClip);
  354. r = -2.0f * farClip_ * nearClip / (farClip_ - nearClip);
  355. }
  356. else
  357. {
  358. q = farClip_ / (farClip_ - nearClip);
  359. r = -q * nearClip;
  360. }
  361. ret.m00_ = w;
  362. ret.m02_ = projectionOffset_.x_ * 2.0f;
  363. ret.m11_ = h;
  364. ret.m12_ = projectionOffset_.y_ * 2.0f;
  365. ret.m22_ = q;
  366. ret.m23_ = r;
  367. ret.m32_ = 1.0f;
  368. }
  369. else
  370. {
  371. // Disregard near clip, because it does not affect depth precision as with perspective projection
  372. float h = (1.0f / (orthoSize_ * 0.5f)) * zoom_;
  373. float w = h / aspectRatio_;
  374. float q, r;
  375. if (openGLFormat)
  376. {
  377. q = 2.0f / farClip_;
  378. r = -1.0f;
  379. }
  380. else
  381. {
  382. q = 1.0f / farClip_;
  383. r = 0.0f;
  384. }
  385. ret.m00_ = w;
  386. ret.m03_ = projectionOffset_.x_ * 2.0f;
  387. ret.m11_ = h;
  388. ret.m13_ = projectionOffset_.y_ * 2.0f;
  389. ret.m22_ = q;
  390. ret.m23_ = r;
  391. ret.m33_ = 1.0f;
  392. }
  393. if (flipVertical_)
  394. ret = flipMatrix * ret;
  395. return ret;
  396. }
  397. void Camera::GetFrustumSize(Vector3& near, Vector3& far) const
  398. {
  399. near.z_ = GetNearClip();
  400. far.z_ = farClip_;
  401. if (!orthographic_)
  402. {
  403. float halfViewSize = tanf(fov_ * M_DEGTORAD * 0.5f) / zoom_;
  404. near.y_ = near.z_ * halfViewSize;
  405. near.x_ = near.y_ * aspectRatio_;
  406. far.y_ = far.z_ * halfViewSize;
  407. far.x_ = far.y_ * aspectRatio_;
  408. }
  409. else
  410. {
  411. float halfViewSize = orthoSize_ * 0.5f / zoom_;
  412. near.y_ = far.y_ = halfViewSize;
  413. near.x_ = far.x_ = near.y_ * aspectRatio_;
  414. }
  415. if (flipVertical_)
  416. {
  417. near.y_ = -near.y_;
  418. far.y_ = -far.y_;
  419. }
  420. }
  421. float Camera::GetHalfViewSize() const
  422. {
  423. if (!orthographic_)
  424. return tanf(fov_ * M_DEGTORAD * 0.5f) / zoom_;
  425. else
  426. return orthoSize_ * 0.5f / zoom_;
  427. }
  428. float Camera::GetDistance(const Vector3& worldPos) const
  429. {
  430. if (!orthographic_)
  431. {
  432. const Vector3& cameraPos = node_ ? node_->GetWorldPosition() : Vector3::ZERO;
  433. return (worldPos - cameraPos).Length();
  434. }
  435. else
  436. return Abs((GetView() * worldPos).z_);
  437. }
  438. float Camera::GetDistanceSquared(const Vector3& worldPos) const
  439. {
  440. if (!orthographic_)
  441. {
  442. const Vector3& cameraPos = node_ ? node_->GetWorldPosition() : Vector3::ZERO;
  443. return (worldPos - cameraPos).LengthSquared();
  444. }
  445. else
  446. {
  447. float distance = (GetView() * worldPos).z_;
  448. return distance * distance;
  449. }
  450. }
  451. float Camera::GetLodDistance(float distance, float scale, float bias) const
  452. {
  453. float d = Max(lodBias_ * bias * scale * zoom_, M_EPSILON);
  454. if (!orthographic_)
  455. return distance / d;
  456. else
  457. return orthoSize_ / d;
  458. }
  459. Matrix3x4 Camera::GetEffectiveWorldTransform() const
  460. {
  461. Matrix3x4 worldTransform = node_ ? Matrix3x4(node_->GetWorldPosition(), node_->GetWorldRotation(), 1.0f) : Matrix3x4::IDENTITY;
  462. return useReflection_ ? reflectionMatrix_ * worldTransform : worldTransform;
  463. }
  464. bool Camera::IsProjectionValid() const
  465. {
  466. return farClip_ > GetNearClip();
  467. }
  468. const Matrix3x4& Camera::GetView() const
  469. {
  470. if (viewDirty_)
  471. {
  472. // Note: view matrix is unaffected by node or parent scale
  473. view_ = GetEffectiveWorldTransform().Inverse();
  474. viewDirty_ = false;
  475. }
  476. return view_;
  477. }
  478. void Camera::SetAspectRatioInternal(float aspectRatio)
  479. {
  480. aspectRatio_ = aspectRatio;
  481. frustumDirty_ = true;
  482. projectionDirty_ = true;
  483. MarkNetworkUpdate();
  484. }
  485. void Camera::SetReflectionPlaneAttr(Vector4 value)
  486. {
  487. SetReflectionPlane(Plane(value));
  488. }
  489. void Camera::SetClipPlaneAttr(Vector4 value)
  490. {
  491. SetClipPlane(Plane(value));
  492. }
  493. Vector4 Camera::GetReflectionPlaneAttr() const
  494. {
  495. return reflectionPlane_.ToVector4();
  496. }
  497. Vector4 Camera::GetClipPlaneAttr() const
  498. {
  499. return clipPlane_.ToVector4();
  500. }
  501. void Camera::OnNodeSet(Node* node)
  502. {
  503. if (node)
  504. node->AddListener(this);
  505. }
  506. void Camera::OnMarkedDirty(Node* node)
  507. {
  508. frustumDirty_ = true;
  509. viewDirty_ = true;
  510. }
  511. }