Camera.cpp 14 KB

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