Camera.cpp 16 KB

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