Camera.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  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, SetAspectRatio, 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. 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. aspectRatio_ = aspectRatio;
  144. frustumDirty_ = true;
  145. projectionDirty_ = true;
  146. MarkNetworkUpdate();
  147. }
  148. void Camera::SetZoom(float zoom)
  149. {
  150. zoom_ = Max(zoom, M_EPSILON);
  151. frustumDirty_ = true;
  152. projectionDirty_ = true;
  153. MarkNetworkUpdate();
  154. }
  155. void Camera::SetLodBias(float bias)
  156. {
  157. lodBias_ = Max(bias, M_EPSILON);
  158. MarkNetworkUpdate();
  159. }
  160. void Camera::SetViewMask(unsigned mask)
  161. {
  162. viewMask_ = mask;
  163. MarkNetworkUpdate();
  164. }
  165. void Camera::SetViewOverrideFlags(unsigned flags)
  166. {
  167. viewOverrideFlags_ = flags;
  168. MarkNetworkUpdate();
  169. }
  170. void Camera::SetFillMode(FillMode mode)
  171. {
  172. fillMode_ = mode;
  173. MarkNetworkUpdate();
  174. }
  175. void Camera::SetOrthographic(bool enable)
  176. {
  177. orthographic_ = enable;
  178. frustumDirty_ = true;
  179. projectionDirty_ = true;
  180. MarkNetworkUpdate();
  181. }
  182. void Camera::SetAutoAspectRatio(bool enable)
  183. {
  184. autoAspectRatio_ = enable;
  185. MarkNetworkUpdate();
  186. }
  187. void Camera::SetProjectionOffset(const Vector2& offset)
  188. {
  189. projectionOffset_ = offset;
  190. projectionDirty_ = true;
  191. MarkNetworkUpdate();
  192. }
  193. void Camera::SetUseReflection(bool enable)
  194. {
  195. useReflection_ = enable;
  196. viewDirty_ = true;
  197. frustumDirty_ = true;
  198. MarkNetworkUpdate();
  199. }
  200. void Camera::SetReflectionPlane(const Plane& plane)
  201. {
  202. reflectionPlane_ = plane;
  203. reflectionMatrix_ = reflectionPlane_.ReflectionMatrix();
  204. viewDirty_ = true;
  205. frustumDirty_ = true;
  206. MarkNetworkUpdate();
  207. }
  208. void Camera::SetUseClipping(bool enable)
  209. {
  210. useClipping_ = enable;
  211. projectionDirty_ = true;
  212. MarkNetworkUpdate();
  213. }
  214. void Camera::SetClipPlane(const Plane& plane)
  215. {
  216. clipPlane_ = plane;
  217. projectionDirty_ = true;
  218. MarkNetworkUpdate();
  219. }
  220. void Camera::SetFlipVertical(bool enable)
  221. {
  222. flipVertical_ = enable;
  223. projectionDirty_ = true;
  224. MarkNetworkUpdate();
  225. }
  226. float Camera::GetNearClip() const
  227. {
  228. // Orthographic camera has always near clip at 0 to avoid trouble with shader depth parameters,
  229. // and unlike in perspective mode there should be no depth buffer precision issue
  230. if (!orthographic_)
  231. return nearClip_;
  232. else
  233. return 0.0f;
  234. }
  235. Frustum Camera::GetSplitFrustum(float nearClip, float farClip) const
  236. {
  237. Frustum ret;
  238. Matrix3x4 worldTransform = GetEffectiveWorldTransform();
  239. nearClip = Max(nearClip, GetNearClip());
  240. farClip = Min(farClip, farClip_);
  241. if (farClip < nearClip)
  242. farClip = nearClip;
  243. if (!orthographic_)
  244. ret.Define(fov_, aspectRatio_, zoom_, nearClip, farClip, worldTransform);
  245. else
  246. ret.DefineOrtho(orthoSize_, aspectRatio_, zoom_, nearClip, farClip, worldTransform);
  247. return ret;
  248. }
  249. Frustum Camera::GetViewSpaceFrustum() const
  250. {
  251. Frustum ret;
  252. if (!orthographic_)
  253. ret.Define(fov_, aspectRatio_, zoom_, GetNearClip(), farClip_);
  254. else
  255. ret.DefineOrtho(orthoSize_, aspectRatio_, zoom_, GetNearClip(), farClip_);
  256. return ret;
  257. }
  258. Frustum Camera::GetViewSpaceSplitFrustum(float nearClip, float farClip) const
  259. {
  260. Frustum ret;
  261. nearClip = Max(nearClip, GetNearClip());
  262. farClip = Min(farClip, farClip_);
  263. if (farClip < nearClip)
  264. farClip = nearClip;
  265. if (!orthographic_)
  266. ret.Define(fov_, aspectRatio_, zoom_, nearClip, farClip);
  267. else
  268. ret.DefineOrtho(orthoSize_, aspectRatio_, zoom_, nearClip, farClip);
  269. return ret;
  270. }
  271. Ray Camera::GetScreenRay(float x, float y) const
  272. {
  273. Ray ret;
  274. // If projection is invalid, just return a ray pointing forward
  275. if (!IsProjectionValid())
  276. {
  277. ret.origin_ = node_ ? node_->GetWorldPosition() : Vector3::ZERO;
  278. ret.direction_ = node_ ? node_->GetWorldDirection() : Vector3::FORWARD;
  279. return ret;
  280. }
  281. Matrix4 viewProjInverse = (GetProjection(false) * GetView()).Inverse();
  282. // The parameters range from 0.0 to 1.0. Expand to normalized device coordinates (-1.0 to 1.0) & flip Y axis
  283. x = 2.0f * x - 1.0f;
  284. y = 1.0f - 2.0f * y;
  285. Vector3 near(x, y, 0.0f);
  286. Vector3 far(x, y, 1.0f);
  287. ret.origin_ = viewProjInverse * near;
  288. ret.direction_ = ((viewProjInverse * far) - ret.origin_).Normalized();
  289. return ret;
  290. }
  291. Vector2 Camera::WorldToScreenPoint(const Vector3& worldPos) const
  292. {
  293. Vector3 eyeSpacePos = GetView() * worldPos;
  294. Vector2 ret;
  295. if(eyeSpacePos.z_ > 0.0f)
  296. {
  297. Vector3 screenSpacePos = GetProjection(false) * eyeSpacePos;
  298. ret.x_ = screenSpacePos.x_;
  299. ret.y_ = screenSpacePos.y_;
  300. }
  301. else
  302. {
  303. ret.x_ = (-eyeSpacePos.x_ > 0.0f) ? -1.0f : 1.0f;
  304. ret.y_ = (-eyeSpacePos.y_ > 0.0f) ? -1.0f : 1.0f;
  305. }
  306. ret.x_ = (ret.x_ / 2.0f) + 0.5f;
  307. ret.y_ = 1.0f - ((ret.y_ / 2.0f) + 0.5f);
  308. return ret;
  309. }
  310. Vector3 Camera::ScreenToWorldPoint(const Vector3& screenPos) const
  311. {
  312. Ray ray = GetScreenRay(screenPos.x_, screenPos.y_);
  313. return ray.origin_ + ray.direction_ * screenPos.z_;
  314. }
  315. const Frustum& Camera::GetFrustum() const
  316. {
  317. if (frustumDirty_)
  318. {
  319. Matrix3x4 worldTransform = GetEffectiveWorldTransform();
  320. if (!orthographic_)
  321. frustum_.Define(fov_, aspectRatio_, zoom_, GetNearClip(), farClip_, worldTransform);
  322. else
  323. frustum_.DefineOrtho(orthoSize_, aspectRatio_, zoom_, GetNearClip(), farClip_, worldTransform);
  324. frustumDirty_ = false;
  325. }
  326. return frustum_;
  327. }
  328. const Matrix4& Camera::GetProjection() const
  329. {
  330. if (projectionDirty_)
  331. {
  332. projection_ = GetProjection(true);
  333. projectionDirty_ = false;
  334. }
  335. return projection_;
  336. }
  337. Matrix4 Camera::GetProjection(bool apiSpecific) const
  338. {
  339. Matrix4 ret(Matrix4::ZERO);
  340. // Whether to construct matrix using OpenGL or Direct3D clip space convention
  341. #ifdef USE_OPENGL
  342. bool openGLFormat = apiSpecific;
  343. #else
  344. bool openGLFormat = false;
  345. #endif
  346. if (!orthographic_)
  347. {
  348. float nearClip = GetNearClip();
  349. float h = (1.0f / tanf(fov_ * M_DEGTORAD * 0.5f)) * zoom_;
  350. float w = h / aspectRatio_;
  351. float q, r;
  352. if (openGLFormat)
  353. {
  354. q = (farClip_ + nearClip) / (farClip_ - nearClip);
  355. r = -2.0f * farClip_ * nearClip / (farClip_ - nearClip);
  356. }
  357. else
  358. {
  359. q = farClip_ / (farClip_ - nearClip);
  360. r = -q * nearClip;
  361. }
  362. ret.m00_ = w;
  363. ret.m02_ = projectionOffset_.x_ * 2.0f;
  364. ret.m11_ = h;
  365. ret.m12_ = projectionOffset_.y_ * 2.0f;
  366. ret.m22_ = q;
  367. ret.m23_ = r;
  368. ret.m32_ = 1.0f;
  369. }
  370. else
  371. {
  372. // Disregard near clip, because it does not affect depth precision as with perspective projection
  373. float h = (1.0f / (orthoSize_ * 0.5f)) * zoom_;
  374. float w = h / aspectRatio_;
  375. float q, r;
  376. if (openGLFormat)
  377. {
  378. q = 2.0f / farClip_;
  379. r = -1.0f;
  380. }
  381. else
  382. {
  383. q = 1.0f / farClip_;
  384. r = 0.0f;
  385. }
  386. ret.m00_ = w;
  387. ret.m03_ = projectionOffset_.x_ * 2.0f;
  388. ret.m11_ = h;
  389. ret.m13_ = projectionOffset_.y_ * 2.0f;
  390. ret.m22_ = q;
  391. ret.m23_ = r;
  392. ret.m33_ = 1.0f;
  393. }
  394. if (flipVertical_)
  395. ret = flipMatrix * ret;
  396. return ret;
  397. }
  398. void Camera::GetFrustumSize(Vector3& near, Vector3& far) const
  399. {
  400. near.z_ = GetNearClip();
  401. far.z_ = farClip_;
  402. if (!orthographic_)
  403. {
  404. float halfViewSize = tanf(fov_ * M_DEGTORAD * 0.5f) / zoom_;
  405. near.y_ = near.z_ * halfViewSize;
  406. near.x_ = near.y_ * aspectRatio_;
  407. far.y_ = far.z_ * halfViewSize;
  408. far.x_ = far.y_ * aspectRatio_;
  409. }
  410. else
  411. {
  412. float halfViewSize = orthoSize_ * 0.5f / zoom_;
  413. near.y_ = far.y_ = halfViewSize;
  414. near.x_ = far.x_ = near.y_ * aspectRatio_;
  415. }
  416. if (flipVertical_)
  417. {
  418. near.y_ = -near.y_;
  419. far.y_ = -far.y_;
  420. }
  421. }
  422. float Camera::GetHalfViewSize() const
  423. {
  424. if (!orthographic_)
  425. return tanf(fov_ * M_DEGTORAD * 0.5f) / zoom_;
  426. else
  427. return orthoSize_ * 0.5f / zoom_;
  428. }
  429. float Camera::GetDistance(const Vector3& worldPos) const
  430. {
  431. if (!orthographic_)
  432. {
  433. const Vector3& cameraPos = node_ ? node_->GetWorldPosition() : Vector3::ZERO;
  434. return (worldPos - cameraPos).Length();
  435. }
  436. else
  437. return Abs((GetView() * worldPos).z_);
  438. }
  439. float Camera::GetDistanceSquared(const Vector3& worldPos) const
  440. {
  441. if (!orthographic_)
  442. {
  443. const Vector3& cameraPos = node_ ? node_->GetWorldPosition() : Vector3::ZERO;
  444. return (worldPos - cameraPos).LengthSquared();
  445. }
  446. else
  447. {
  448. float distance = (GetView() * worldPos).z_;
  449. return distance * distance;
  450. }
  451. }
  452. float Camera::GetLodDistance(float distance, float scale, float bias) const
  453. {
  454. float d = Max(lodBias_ * bias * scale * zoom_, M_EPSILON);
  455. if (!orthographic_)
  456. return distance / d;
  457. else
  458. return orthoSize_ / d;
  459. }
  460. Matrix3x4 Camera::GetEffectiveWorldTransform() const
  461. {
  462. Matrix3x4 worldTransform = node_ ? Matrix3x4(node_->GetWorldPosition(), node_->GetWorldRotation(), 1.0f) : Matrix3x4::IDENTITY;
  463. return useReflection_ ? reflectionMatrix_ * worldTransform : worldTransform;
  464. }
  465. bool Camera::IsProjectionValid() const
  466. {
  467. return farClip_ > GetNearClip();
  468. }
  469. const Matrix3x4& Camera::GetView() const
  470. {
  471. if (viewDirty_)
  472. {
  473. // Note: view matrix is unaffected by node or parent scale
  474. view_ = GetEffectiveWorldTransform().Inverse();
  475. viewDirty_ = false;
  476. }
  477. return view_;
  478. }
  479. void Camera::SetReflectionPlaneAttr(Vector4 value)
  480. {
  481. SetReflectionPlane(Plane(value));
  482. }
  483. void Camera::SetClipPlaneAttr(Vector4 value)
  484. {
  485. SetClipPlane(Plane(value));
  486. }
  487. Vector4 Camera::GetReflectionPlaneAttr() const
  488. {
  489. return reflectionPlane_.ToVector4();
  490. }
  491. Vector4 Camera::GetClipPlaneAttr() const
  492. {
  493. return clipPlane_.ToVector4();
  494. }
  495. void Camera::OnNodeSet(Node* node)
  496. {
  497. if (node)
  498. node->AddListener(this);
  499. }
  500. void Camera::OnMarkedDirty(Node* node)
  501. {
  502. frustumDirty_ = true;
  503. viewDirty_ = true;
  504. }
  505. }