Camera.cpp 16 KB

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