Camera.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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() * 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. #ifdef USE_OPENGL
  212. Vector3 near(x, y, -1.0f);
  213. Vector3 far(x, y, 1.0f);
  214. #else
  215. Vector3 near(x, y, 0.0f);
  216. Vector3 far(x, y, 1.0f);
  217. #endif
  218. ret.origin_ = viewProjInverse * near;
  219. ret.direction_ = ((viewProjInverse * far) - ret.origin_).Normalized();
  220. return ret;
  221. }
  222. const Frustum& Camera::GetFrustum()
  223. {
  224. if (frustumDirty_)
  225. {
  226. if (!orthographic_)
  227. frustum_.Define(fov_, aspectRatio_, zoom_, GetNearClip(), farClip_, GetWorldTransform());
  228. else
  229. frustum_.DefineOrtho(orthoSize_, aspectRatio_, zoom_, GetNearClip(), farClip_, GetWorldTransform());
  230. frustumDirty_ = false;
  231. }
  232. return frustum_;
  233. }
  234. const Matrix4& Camera::GetProjection()
  235. {
  236. if (projectionDirty_)
  237. {
  238. projection_ = Matrix4::ZERO;
  239. if (!orthographic_)
  240. {
  241. float nearClip = GetNearClip();
  242. float h = (1.0f / tanf(fov_ * M_DEGTORAD * 0.5f)) * zoom_;
  243. float w = h / aspectRatio_;
  244. #ifdef USE_OPENGL
  245. float q = (farClip_ + nearClip) / (farClip_ - nearClip);
  246. float r = -2.0f * farClip_ * nearClip / (farClip_ - nearClip);
  247. #else
  248. float q = farClip_ / (farClip_ - nearClip);
  249. float r = -q * nearClip;
  250. #endif
  251. projection_.m00_ = w;
  252. projection_.m02_ = projectionOffset_.x_ * 2.0f;
  253. projection_.m11_ = h;
  254. projection_.m12_ = projectionOffset_.y_ * 2.0f;
  255. projection_.m22_ = q;
  256. projection_.m23_ = r;
  257. projection_.m32_ = 1.0f;
  258. }
  259. else
  260. {
  261. // Disregard near clip, because it does not affect depth precision as with perspective projection
  262. float h = (1.0f / (orthoSize_ * 0.5f)) * zoom_;
  263. float w = h / aspectRatio_;
  264. #ifdef USE_OPENGL
  265. float q = 2.0f / farClip_;
  266. float r = -1.0f;
  267. #else
  268. float q = 1.0f / farClip_;
  269. float r = 0.0f;
  270. #endif
  271. projection_.m00_ = w;
  272. projection_.m03_ = projectionOffset_.x_ * 2.0f;
  273. projection_.m11_ = h;
  274. projection_.m13_ = projectionOffset_.y_ * 2.0f;
  275. projection_.m22_ = q;
  276. projection_.m23_ = r;
  277. projection_.m33_ = 1.0f;
  278. }
  279. if (flipVertical_)
  280. projection_ = flipMatrix * projection_;
  281. projectionDirty_ = false;
  282. }
  283. return projection_;
  284. }
  285. void Camera::GetFrustumSize(Vector3& near, Vector3& far) const
  286. {
  287. near.z_ = GetNearClip();
  288. far.z_ = farClip_;
  289. if (!orthographic_)
  290. {
  291. float halfViewSize = tanf(fov_ * M_DEGTORAD * 0.5f) / zoom_;
  292. near.y_ = near.z_ * halfViewSize;
  293. near.x_ = near.y_ * aspectRatio_;
  294. far.y_ = far.z_ * halfViewSize;
  295. far.x_ = far.y_ * aspectRatio_;
  296. }
  297. else
  298. {
  299. float halfViewSize = orthoSize_ * 0.5f / zoom_;
  300. near.y_ = far.y_ = halfViewSize;
  301. near.x_ = far.x_ = near.y_ * aspectRatio_;
  302. }
  303. if (flipVertical_)
  304. {
  305. near.y_ = -near.y_;
  306. far.y_ = -far.y_;
  307. }
  308. }
  309. float Camera::GetHalfViewSize() const
  310. {
  311. if (!orthographic_)
  312. return tanf(fov_ * M_DEGTORAD * 0.5f) / zoom_;
  313. else
  314. return orthoSize_ * 0.5f / zoom_;
  315. }
  316. Vector3 Camera::GetForwardVector()
  317. {
  318. return GetWorldTransform().RotationMatrix() * Vector3::FORWARD;
  319. }
  320. Vector3 Camera::GetRightVector()
  321. {
  322. return GetWorldTransform().RotationMatrix() * Vector3::RIGHT;
  323. }
  324. Vector3 Camera::GetUpVector()
  325. {
  326. return GetWorldTransform().RotationMatrix() * Vector3::UP;
  327. }
  328. float Camera::GetDistance(const Vector3& worldPos)
  329. {
  330. if (!orthographic_)
  331. return (worldPos - GetWorldPosition()).Length();
  332. else
  333. return fabsf((GetInverseWorldTransform() * worldPos).z_);
  334. }
  335. float Camera::GetDistanceSquared(const Vector3& worldPos)
  336. {
  337. if (!orthographic_)
  338. return (worldPos - GetWorldPosition()).LengthSquared();
  339. else
  340. {
  341. float distance = (GetInverseWorldTransform() * worldPos).z_;
  342. return distance * distance;
  343. }
  344. }
  345. float Camera::GetLodDistance(float distance, float scale, float bias) const
  346. {
  347. float d = Max(lodBias_ * bias * scale * zoom_, M_EPSILON);
  348. if (!orthographic_)
  349. return distance / d;
  350. else
  351. return orthoSize_ / d;
  352. }
  353. bool Camera::IsProjectionValid() const
  354. {
  355. return farClip_ > GetNearClip();
  356. }
  357. void Camera::OnMarkedDirty(Node* node)
  358. {
  359. if (node == node_)
  360. frustumDirty_ = true;
  361. }