Camera.cpp 12 KB

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