Camera.cpp 16 KB

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