Camera.cpp 14 KB

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