Camera.cpp 14 KB

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