Camera.cpp 14 KB

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