Camera.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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. namespace Urho3D
  30. {
  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. OBJECTTYPESTATIC(Camera);
  49. Camera::Camera(Context* context) :
  50. Component(context),
  51. inverseWorldDirty_(true),
  52. projectionDirty_(true),
  53. frustumDirty_(true),
  54. orthographic_(false),
  55. nearClip_(DEFAULT_NEARCLIP),
  56. farClip_(DEFAULT_FARCLIP),
  57. fov_(DEFAULT_FOV),
  58. orthoSize_(DEFAULT_ORTHOSIZE),
  59. aspectRatio_(1.0f),
  60. zoom_(1.0f),
  61. lodBias_(1.0f),
  62. viewMask_(DEFAULT_VIEWMASK),
  63. viewOverrideFlags_(VO_NONE),
  64. fillMode_(FILL_SOLID),
  65. projectionOffset_(Vector2::ZERO),
  66. autoAspectRatio_(true),
  67. flipVertical_(false)
  68. {
  69. }
  70. Camera::~Camera()
  71. {
  72. }
  73. void Camera::RegisterObject(Context* context)
  74. {
  75. context->RegisterFactory<Camera>();
  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)
  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) * GetInverseWorldTransform()).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. const Frustum& Camera::GetFrustum() const
  251. {
  252. if (frustumDirty_)
  253. {
  254. const Matrix3x4& worldTransform = node_ ? node_->GetWorldTransform() : Matrix3x4::IDENTITY;
  255. if (!orthographic_)
  256. frustum_.Define(fov_, aspectRatio_, zoom_, GetNearClip(), farClip_, worldTransform);
  257. else
  258. frustum_.DefineOrtho(orthoSize_, aspectRatio_, zoom_, GetNearClip(), farClip_, worldTransform);
  259. frustumDirty_ = false;
  260. }
  261. return frustum_;
  262. }
  263. const Matrix4& Camera::GetProjection() const
  264. {
  265. if (projectionDirty_)
  266. {
  267. projection_ = GetProjection(true);
  268. projectionDirty_ = false;
  269. }
  270. return projection_;
  271. }
  272. Matrix4 Camera::GetProjection(bool apiSpecific) const
  273. {
  274. Matrix4 ret(Matrix4::ZERO);
  275. if (!orthographic_)
  276. {
  277. float nearClip = GetNearClip();
  278. float h = (1.0f / tanf(fov_ * M_DEGTORAD * 0.5f)) * zoom_;
  279. float w = h / aspectRatio_;
  280. float q, r;
  281. if (apiSpecific)
  282. {
  283. #ifdef USE_OPENGL
  284. q = (farClip_ + nearClip) / (farClip_ - nearClip);
  285. r = -2.0f * farClip_ * nearClip / (farClip_ - nearClip);
  286. #else
  287. q = farClip_ / (farClip_ - nearClip);
  288. r = -q * nearClip;
  289. #endif
  290. }
  291. else
  292. {
  293. q = farClip_ / (farClip_ - nearClip);
  294. r = -q * nearClip;
  295. }
  296. ret.m00_ = w;
  297. ret.m02_ = projectionOffset_.x_ * 2.0f;
  298. ret.m11_ = h;
  299. ret.m12_ = projectionOffset_.y_ * 2.0f;
  300. ret.m22_ = q;
  301. ret.m23_ = r;
  302. ret.m32_ = 1.0f;
  303. }
  304. else
  305. {
  306. // Disregard near clip, because it does not affect depth precision as with perspective projection
  307. float h = (1.0f / (orthoSize_ * 0.5f)) * zoom_;
  308. float w = h / aspectRatio_;
  309. float q, r;
  310. if (apiSpecific)
  311. {
  312. #ifdef USE_OPENGL
  313. q = 2.0f / farClip_;
  314. r = -1.0f;
  315. #else
  316. q = 1.0f / farClip_;
  317. r = 0.0f;
  318. #endif
  319. }
  320. else
  321. {
  322. q = 1.0f / farClip_;
  323. r = 0.0f;
  324. }
  325. ret.m00_ = w;
  326. ret.m03_ = projectionOffset_.x_ * 2.0f;
  327. ret.m11_ = h;
  328. ret.m13_ = projectionOffset_.y_ * 2.0f;
  329. ret.m22_ = q;
  330. ret.m23_ = r;
  331. ret.m33_ = 1.0f;
  332. }
  333. if (flipVertical_)
  334. ret = flipMatrix * ret;
  335. return ret;
  336. }
  337. void Camera::GetFrustumSize(Vector3& near, Vector3& far) const
  338. {
  339. near.z_ = GetNearClip();
  340. far.z_ = farClip_;
  341. if (!orthographic_)
  342. {
  343. float halfViewSize = tanf(fov_ * M_DEGTORAD * 0.5f) / zoom_;
  344. near.y_ = near.z_ * halfViewSize;
  345. near.x_ = near.y_ * aspectRatio_;
  346. far.y_ = far.z_ * halfViewSize;
  347. far.x_ = far.y_ * aspectRatio_;
  348. }
  349. else
  350. {
  351. float halfViewSize = orthoSize_ * 0.5f / zoom_;
  352. near.y_ = far.y_ = halfViewSize;
  353. near.x_ = far.x_ = near.y_ * aspectRatio_;
  354. }
  355. if (flipVertical_)
  356. {
  357. near.y_ = -near.y_;
  358. far.y_ = -far.y_;
  359. }
  360. }
  361. float Camera::GetHalfViewSize() const
  362. {
  363. if (!orthographic_)
  364. return tanf(fov_ * M_DEGTORAD * 0.5f) / zoom_;
  365. else
  366. return orthoSize_ * 0.5f / zoom_;
  367. }
  368. Vector3 Camera::GetForwardVector()
  369. {
  370. Matrix3 worldRotation = node_ ? node_->GetWorldTransform().RotationMatrix() : Matrix3::IDENTITY;
  371. return worldRotation * Vector3::FORWARD;
  372. }
  373. Vector3 Camera::GetRightVector()
  374. {
  375. Matrix3 worldRotation = node_ ? node_->GetWorldTransform().RotationMatrix() : Matrix3::IDENTITY;
  376. return worldRotation * Vector3::RIGHT;
  377. }
  378. Vector3 Camera::GetUpVector()
  379. {
  380. Matrix3 worldRotation = node_ ? node_->GetWorldTransform().RotationMatrix() : Matrix3::IDENTITY;
  381. return worldRotation * Vector3::UP;
  382. }
  383. float Camera::GetDistance(const Vector3& worldPos) const
  384. {
  385. if (!orthographic_)
  386. {
  387. const Vector3& cameraPos = node_ ? node_->GetWorldPosition() : Vector3::ZERO;
  388. return (worldPos - cameraPos).Length();
  389. }
  390. else
  391. return Abs((GetInverseWorldTransform() * worldPos).z_);
  392. }
  393. float Camera::GetDistanceSquared(const Vector3& worldPos) const
  394. {
  395. if (!orthographic_)
  396. {
  397. const Vector3& cameraPos = node_ ? node_->GetWorldPosition() : Vector3::ZERO;
  398. return (worldPos - cameraPos).LengthSquared();
  399. }
  400. else
  401. {
  402. float distance = (GetInverseWorldTransform() * worldPos).z_;
  403. return distance * distance;
  404. }
  405. }
  406. float Camera::GetLodDistance(float distance, float scale, float bias) const
  407. {
  408. float d = Max(lodBias_ * bias * scale * zoom_, M_EPSILON);
  409. if (!orthographic_)
  410. return distance / d;
  411. else
  412. return orthoSize_ / d;
  413. }
  414. bool Camera::IsProjectionValid() const
  415. {
  416. return farClip_ > GetNearClip();
  417. }
  418. const Matrix3x4& Camera::GetInverseWorldTransform() const
  419. {
  420. if (inverseWorldDirty_)
  421. {
  422. const Matrix3x4& worldTransform = node_ ? node_->GetWorldTransform() : Matrix3x4::IDENTITY;
  423. inverseWorld_ = worldTransform.Inverse();
  424. inverseWorldDirty_ = false;
  425. }
  426. return inverseWorld_;
  427. }
  428. void Camera::OnNodeSet(Node* node)
  429. {
  430. if (node)
  431. node->AddListener(this);
  432. }
  433. void Camera::OnMarkedDirty(Node* node)
  434. {
  435. frustumDirty_ = true;
  436. inverseWorldDirty_ = true;
  437. }
  438. }