Camera.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Precompiled.h"
  24. #include "Camera.h"
  25. #include "Context.h"
  26. #include "Drawable.h"
  27. #include "DebugNew.h"
  28. static const float DEFAULT_NEARCLIP = 0.1f;
  29. static const float DEFAULT_FARCLIP = 1000.0f;
  30. static const float DEFAULT_FOV = 45.0f;
  31. static const float DEFAULT_ORTHOSIZE = 20.0f;
  32. static const Matrix4 flipMatrix(
  33. 1.0f, 0.0f, 0.0f, 0.0f,
  34. 0.0f, -1.0f, 0.0f, 0.0f,
  35. 0.0f, 0.0f, 1.0f, 0.0f,
  36. 0.0f, 0.0f, 0.0f, 1.0f
  37. );
  38. OBJECTTYPESTATIC(Camera);
  39. Camera::Camera(Context* context) :
  40. Component(context),
  41. inverseWorldDirty_(true),
  42. projectionDirty_(true),
  43. frustumDirty_(true),
  44. orthographic_(false),
  45. nearClip_(DEFAULT_NEARCLIP),
  46. farClip_(DEFAULT_FARCLIP),
  47. fov_(DEFAULT_FOV),
  48. orthoSize_(DEFAULT_ORTHOSIZE),
  49. aspectRatio_(1.0f),
  50. zoom_(1.0f),
  51. lodBias_(1.0f),
  52. viewMask_(DEFAULT_VIEWMASK),
  53. viewOverrideFlags_(VO_NONE),
  54. projectionOffset_(Vector2::ZERO),
  55. autoAspectRatio_(true),
  56. flipVertical_(false)
  57. {
  58. }
  59. Camera::~Camera()
  60. {
  61. }
  62. void Camera::RegisterObject(Context* context)
  63. {
  64. context->RegisterFactory<Camera>();
  65. ACCESSOR_ATTRIBUTE(Camera, VAR_FLOAT, "Near Clip", GetNearClip, SetNearClip, float, DEFAULT_NEARCLIP, AM_DEFAULT);
  66. ACCESSOR_ATTRIBUTE(Camera, VAR_FLOAT, "Far Clip", GetFarClip, SetFarClip, float, DEFAULT_FARCLIP, AM_DEFAULT);
  67. ACCESSOR_ATTRIBUTE(Camera, VAR_FLOAT, "FOV", GetFov, SetFov, float, DEFAULT_FOV, AM_DEFAULT);
  68. ACCESSOR_ATTRIBUTE(Camera, VAR_FLOAT, "Aspect Ratio", GetAspectRatio, SetAspectRatio, float, 1.0f, AM_DEFAULT);
  69. ATTRIBUTE(Camera, VAR_BOOL, "Auto Aspect Ratio", autoAspectRatio_, true, AM_DEFAULT);
  70. ACCESSOR_ATTRIBUTE(Camera, VAR_BOOL, "Orthographic", IsOrthographic, SetOrthographic, bool, false, AM_DEFAULT);
  71. ACCESSOR_ATTRIBUTE(Camera, VAR_FLOAT, "Orthographic Size", GetOrthoSize, SetOrthoSize, float, DEFAULT_ORTHOSIZE, AM_DEFAULT);
  72. ACCESSOR_ATTRIBUTE(Camera, VAR_FLOAT, "Zoom", GetZoom, SetZoom, float, 1.0f, AM_DEFAULT);
  73. ACCESSOR_ATTRIBUTE(Camera, VAR_FLOAT, "LOD Bias", GetLodBias, SetLodBias, float, 1.0f, AM_DEFAULT);
  74. ATTRIBUTE(Camera, VAR_INT, "View Mask", viewMask_, DEFAULT_VIEWMASK, AM_DEFAULT);
  75. ATTRIBUTE(Camera, VAR_INT, "View Override Flags", viewOverrideFlags_, VO_NONE, AM_DEFAULT);
  76. REF_ACCESSOR_ATTRIBUTE(Camera, VAR_VECTOR2, "Projection Offset", GetProjectionOffset, SetProjectionOffset, Vector2, Vector2::ZERO, AM_DEFAULT);
  77. }
  78. void Camera::SetNearClip(float nearClip)
  79. {
  80. nearClip_ = Max(nearClip, M_MIN_NEARCLIP);
  81. frustumDirty_ = true;
  82. projectionDirty_ = true;
  83. MarkNetworkUpdate();
  84. }
  85. void Camera::SetFarClip(float farClip)
  86. {
  87. farClip_ = Max(farClip, M_MIN_NEARCLIP);
  88. frustumDirty_ = true;
  89. projectionDirty_ = true;
  90. MarkNetworkUpdate();
  91. }
  92. void Camera::SetFov(float fov)
  93. {
  94. fov_ = Clamp(fov, 0.0f, M_MAX_FOV);
  95. frustumDirty_ = true;
  96. projectionDirty_ = true;
  97. MarkNetworkUpdate();
  98. }
  99. void Camera::SetOrthoSize(float orthoSize)
  100. {
  101. orthoSize_ = orthoSize;
  102. aspectRatio_ = 1.0f;
  103. frustumDirty_ = true;
  104. projectionDirty_ = true;
  105. MarkNetworkUpdate();
  106. }
  107. void Camera::SetOrthoSize(const Vector2& orthoSize)
  108. {
  109. orthoSize_ = orthoSize.y_;
  110. aspectRatio_ = orthoSize.x_ / orthoSize.y_;
  111. frustumDirty_ = true;
  112. projectionDirty_ = true;
  113. MarkNetworkUpdate();
  114. }
  115. void Camera::SetAspectRatio(float aspectRatio)
  116. {
  117. aspectRatio_ = aspectRatio;
  118. frustumDirty_ = true;
  119. projectionDirty_ = true;
  120. MarkNetworkUpdate();
  121. }
  122. void Camera::SetZoom(float zoom)
  123. {
  124. zoom_ = Max(zoom, M_EPSILON);
  125. frustumDirty_ = true;
  126. projectionDirty_ = true;
  127. MarkNetworkUpdate();
  128. }
  129. void Camera::SetLodBias(float bias)
  130. {
  131. lodBias_ = Max(bias, M_EPSILON);
  132. MarkNetworkUpdate();
  133. }
  134. void Camera::SetViewMask(unsigned mask)
  135. {
  136. viewMask_ = mask;
  137. MarkNetworkUpdate();
  138. }
  139. void Camera::SetViewOverrideFlags(unsigned flags)
  140. {
  141. viewOverrideFlags_ = flags;
  142. MarkNetworkUpdate();
  143. }
  144. void Camera::SetOrthographic(bool enable)
  145. {
  146. orthographic_ = enable;
  147. frustumDirty_ = true;
  148. projectionDirty_ = true;
  149. MarkNetworkUpdate();
  150. }
  151. void Camera::SetAutoAspectRatio(bool enable)
  152. {
  153. autoAspectRatio_ = enable;
  154. MarkNetworkUpdate();
  155. }
  156. void Camera::SetProjectionOffset(const Vector2& offset)
  157. {
  158. projectionOffset_ = offset;
  159. projectionDirty_ = true;
  160. MarkNetworkUpdate();
  161. }
  162. void Camera::SetFlipVertical(bool enable)
  163. {
  164. flipVertical_ = enable;
  165. projectionDirty_ = true;
  166. MarkNetworkUpdate();
  167. }
  168. float Camera::GetNearClip() const
  169. {
  170. // Orthographic camera has always near clip at 0 to avoid trouble with shader depth parameters,
  171. // and unlike in perspective mode there should be no depth buffer precision issue
  172. if (!orthographic_)
  173. return nearClip_;
  174. else
  175. return 0.0f;
  176. }
  177. Frustum Camera::GetSplitFrustum(float nearClip, float farClip) const
  178. {
  179. Frustum ret;
  180. const Matrix3x4& worldTransform = node_ ? node_->GetWorldTransform() : Matrix3x4::IDENTITY;
  181. nearClip = Max(nearClip, GetNearClip());
  182. farClip = Min(farClip, farClip_);
  183. if (farClip < nearClip)
  184. farClip = nearClip;
  185. if (!orthographic_)
  186. ret.Define(fov_, aspectRatio_, zoom_, nearClip, farClip, worldTransform);
  187. else
  188. ret.DefineOrtho(orthoSize_, aspectRatio_, zoom_, nearClip, farClip, worldTransform);
  189. return ret;
  190. }
  191. Frustum Camera::GetViewSpaceFrustum() const
  192. {
  193. Frustum ret;
  194. if (!orthographic_)
  195. ret.Define(fov_, aspectRatio_, zoom_, GetNearClip(), farClip_);
  196. else
  197. ret.DefineOrtho(orthoSize_, aspectRatio_, zoom_, GetNearClip(), farClip_);
  198. return ret;
  199. }
  200. Frustum Camera::GetViewSpaceSplitFrustum(float nearClip, float farClip) const
  201. {
  202. Frustum ret;
  203. nearClip = Max(nearClip, GetNearClip());
  204. farClip = Min(farClip, farClip_);
  205. if (farClip < nearClip)
  206. farClip = nearClip;
  207. if (!orthographic_)
  208. ret.Define(fov_, aspectRatio_, zoom_, nearClip, farClip);
  209. else
  210. ret.DefineOrtho(orthoSize_, aspectRatio_, zoom_, nearClip, farClip);
  211. return ret;
  212. }
  213. Ray Camera::GetScreenRay(float x, float y)
  214. {
  215. Ray ret;
  216. // If projection is invalid, just return a ray pointing forward
  217. if (!IsProjectionValid())
  218. {
  219. ret.origin_ = node_ ? node_->GetWorldPosition() : Vector3::ZERO;
  220. ret.direction_ = GetForwardVector();
  221. return ret;
  222. }
  223. Matrix4 viewProjInverse = (GetProjection(false) * GetInverseWorldTransform()).Inverse();
  224. // The parameters range from 0.0 to 1.0. Expand to normalized device coordinates (-1.0 to 1.0) & flip Y axis
  225. x = 2.0f * x - 1.0f;
  226. y = 1.0f - 2.0f * y;
  227. Vector3 near(x, y, 0.0f);
  228. Vector3 far(x, y, 1.0f);
  229. ret.origin_ = viewProjInverse * near;
  230. ret.direction_ = ((viewProjInverse * far) - ret.origin_).Normalized();
  231. return ret;
  232. }
  233. const Frustum& Camera::GetFrustum() const
  234. {
  235. if (frustumDirty_)
  236. {
  237. const Matrix3x4& worldTransform = node_ ? node_->GetWorldTransform() : Matrix3x4::IDENTITY;
  238. if (!orthographic_)
  239. frustum_.Define(fov_, aspectRatio_, zoom_, GetNearClip(), farClip_, worldTransform);
  240. else
  241. frustum_.DefineOrtho(orthoSize_, aspectRatio_, zoom_, GetNearClip(), farClip_, worldTransform);
  242. frustumDirty_ = false;
  243. }
  244. return frustum_;
  245. }
  246. const Matrix4& Camera::GetProjection() const
  247. {
  248. if (projectionDirty_)
  249. {
  250. projection_ = GetProjection(true);
  251. projectionDirty_ = false;
  252. }
  253. return projection_;
  254. }
  255. Matrix4 Camera::GetProjection(bool apiSpecific) const
  256. {
  257. Matrix4 ret(Matrix4::ZERO);
  258. if (!orthographic_)
  259. {
  260. float nearClip = GetNearClip();
  261. float h = (1.0f / tanf(fov_ * M_DEGTORAD * 0.5f)) * zoom_;
  262. float w = h / aspectRatio_;
  263. float q, r;
  264. if (apiSpecific)
  265. {
  266. #ifdef USE_OPENGL
  267. q = (farClip_ + nearClip) / (farClip_ - nearClip);
  268. r = -2.0f * farClip_ * nearClip / (farClip_ - nearClip);
  269. #else
  270. q = farClip_ / (farClip_ - nearClip);
  271. r = -q * nearClip;
  272. #endif
  273. }
  274. else
  275. {
  276. q = farClip_ / (farClip_ - nearClip);
  277. r = -q * nearClip;
  278. }
  279. ret.m00_ = w;
  280. ret.m02_ = projectionOffset_.x_ * 2.0f;
  281. ret.m11_ = h;
  282. ret.m12_ = projectionOffset_.y_ * 2.0f;
  283. ret.m22_ = q;
  284. ret.m23_ = r;
  285. ret.m32_ = 1.0f;
  286. }
  287. else
  288. {
  289. // Disregard near clip, because it does not affect depth precision as with perspective projection
  290. float h = (1.0f / (orthoSize_ * 0.5f)) * zoom_;
  291. float w = h / aspectRatio_;
  292. float q, r;
  293. if (apiSpecific)
  294. {
  295. #ifdef USE_OPENGL
  296. q = 2.0f / farClip_;
  297. r = -1.0f;
  298. #else
  299. q = 1.0f / farClip_;
  300. r = 0.0f;
  301. #endif
  302. }
  303. else
  304. {
  305. q = 1.0f / farClip_;
  306. r = 0.0f;
  307. }
  308. ret.m00_ = w;
  309. ret.m03_ = projectionOffset_.x_ * 2.0f;
  310. ret.m11_ = h;
  311. ret.m13_ = projectionOffset_.y_ * 2.0f;
  312. ret.m22_ = q;
  313. ret.m23_ = r;
  314. ret.m33_ = 1.0f;
  315. }
  316. if (flipVertical_)
  317. ret = flipMatrix * ret;
  318. return ret;
  319. }
  320. void Camera::GetFrustumSize(Vector3& near, Vector3& far) const
  321. {
  322. near.z_ = GetNearClip();
  323. far.z_ = farClip_;
  324. if (!orthographic_)
  325. {
  326. float halfViewSize = tanf(fov_ * M_DEGTORAD * 0.5f) / zoom_;
  327. near.y_ = near.z_ * halfViewSize;
  328. near.x_ = near.y_ * aspectRatio_;
  329. far.y_ = far.z_ * halfViewSize;
  330. far.x_ = far.y_ * aspectRatio_;
  331. }
  332. else
  333. {
  334. float halfViewSize = orthoSize_ * 0.5f / zoom_;
  335. near.y_ = far.y_ = halfViewSize;
  336. near.x_ = far.x_ = near.y_ * aspectRatio_;
  337. }
  338. if (flipVertical_)
  339. {
  340. near.y_ = -near.y_;
  341. far.y_ = -far.y_;
  342. }
  343. }
  344. float Camera::GetHalfViewSize() const
  345. {
  346. if (!orthographic_)
  347. return tanf(fov_ * M_DEGTORAD * 0.5f) / zoom_;
  348. else
  349. return orthoSize_ * 0.5f / zoom_;
  350. }
  351. Vector3 Camera::GetForwardVector()
  352. {
  353. Quaternion worldRotation = node_ ? node_->GetWorldRotation() : Quaternion::IDENTITY;
  354. return worldRotation * Vector3::FORWARD;
  355. }
  356. Vector3 Camera::GetRightVector()
  357. {
  358. Quaternion worldRotation = node_ ? node_->GetWorldRotation() : Quaternion::IDENTITY;
  359. return worldRotation * Vector3::RIGHT;
  360. }
  361. Vector3 Camera::GetUpVector()
  362. {
  363. Quaternion worldRotation = node_ ? node_->GetWorldRotation() : Quaternion::IDENTITY;
  364. return worldRotation * Vector3::UP;
  365. }
  366. float Camera::GetDistance(const Vector3& worldPos) const
  367. {
  368. if (!orthographic_)
  369. {
  370. const Vector3& cameraPos = node_ ? node_->GetWorldPosition() : Vector3::ZERO;
  371. return (worldPos - cameraPos).Length();
  372. }
  373. else
  374. return fabsf((GetInverseWorldTransform() * worldPos).z_);
  375. }
  376. float Camera::GetDistanceSquared(const Vector3& worldPos) const
  377. {
  378. if (!orthographic_)
  379. {
  380. const Vector3& cameraPos = node_ ? node_->GetWorldPosition() : Vector3::ZERO;
  381. return (worldPos - cameraPos).LengthSquared();
  382. }
  383. else
  384. {
  385. float distance = (GetInverseWorldTransform() * worldPos).z_;
  386. return distance * distance;
  387. }
  388. }
  389. float Camera::GetLodDistance(float distance, float scale, float bias) const
  390. {
  391. float d = Max(lodBias_ * bias * scale * zoom_, M_EPSILON);
  392. if (!orthographic_)
  393. return distance / d;
  394. else
  395. return orthoSize_ / d;
  396. }
  397. bool Camera::IsProjectionValid() const
  398. {
  399. return farClip_ > GetNearClip();
  400. }
  401. const Matrix3x4& Camera::GetInverseWorldTransform() const
  402. {
  403. if (inverseWorldDirty_)
  404. {
  405. const Matrix3x4& worldTransform = node_ ? node_->GetWorldTransform() : Matrix3x4::IDENTITY;
  406. inverseWorld_ = worldTransform.Inverse();
  407. inverseWorldDirty_ = false;
  408. }
  409. return inverseWorld_;
  410. }
  411. void Camera::OnNodeSet(Node* node)
  412. {
  413. if (node)
  414. node->AddListener(this);
  415. }
  416. void Camera::OnMarkedDirty(Node* node)
  417. {
  418. frustumDirty_ = true;
  419. inverseWorldDirty_ = true;
  420. }