BsCamera.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. //__________________________ Banshee Project - A modern game development toolkit _________________________________//
  2. //_____________________________________ www.banshee-project.com __________________________________________________//
  3. //________________________ Copyright (c) 2014 Marko Pintera. All rights reserved. ________________________________//
  4. #include "BsCamera.h"
  5. #include "BsCameraRTTI.h"
  6. #include "BsMath.h"
  7. #include "BsMatrix3.h"
  8. #include "BsVector2.h"
  9. #include "BsAABox.h"
  10. #include "BsSphere.h"
  11. #include "BsHardwareBufferManager.h"
  12. #include "BsVertexBuffer.h"
  13. #include "BsIndexBuffer.h"
  14. #include "BsException.h"
  15. #include "BsRenderSystem.h"
  16. #include "BsSceneObject.h"
  17. namespace BansheeEngine
  18. {
  19. const float Camera::INFINITE_FAR_PLANE_ADJUST = 0.00001f;
  20. Camera::Camera(const HSceneObject& parent, RenderTargetPtr target, float left, float top, float width, float height)
  21. : Component(parent), mProjType(PT_PERSPECTIVE), mHorzFOV(Radian(Math::PI/4.0f)), mFarDist(100000.0f),
  22. mNearDist(100.0f), mAspect(1.33333333333333f), mOrthoHeight(1000), mRecalcFrustum(true), mRecalcFrustumPlanes(true),
  23. mCustomViewMatrix(false), mCustomProjMatrix(false), mFrustumExtentsManuallySet(false), mIgnoreSceneRenderables(false),
  24. mPriority(0), mLayers(0xFFFFFFFFFFFFFFFF), mCoreDirtyFlags(0xFFFFFFFF)
  25. {
  26. setName("Camera");
  27. mViewMatrix = Matrix4::ZERO;
  28. mProjMatrixRS = Matrix4::ZERO;
  29. invalidateFrustum();
  30. target->synchronize();
  31. mViewport = bs_shared_ptr<Viewport, PoolAlloc>(target, left, top, width, height);
  32. }
  33. Camera::~Camera()
  34. {
  35. }
  36. void Camera::setHorzFOV(const Radian& fov)
  37. {
  38. mHorzFOV = fov;
  39. invalidateFrustum();
  40. markCoreDirty();
  41. }
  42. const Radian& Camera::getHorzFOV() const
  43. {
  44. return mHorzFOV;
  45. }
  46. void Camera::setFarClipDistance(float farPlane)
  47. {
  48. mFarDist = farPlane;
  49. invalidateFrustum();
  50. markCoreDirty();
  51. }
  52. float Camera::getFarClipDistance() const
  53. {
  54. return mFarDist;
  55. }
  56. void Camera::setNearClipDistance(float nearPlane)
  57. {
  58. if (nearPlane <= 0)
  59. {
  60. BS_EXCEPT(InvalidParametersException, "Near clip distance must be greater than zero.");
  61. }
  62. mNearDist = nearPlane;
  63. invalidateFrustum();
  64. markCoreDirty();
  65. }
  66. float Camera::getNearClipDistance() const
  67. {
  68. return mNearDist;
  69. }
  70. const Matrix4& Camera::getProjectionMatrix() const
  71. {
  72. updateFrustum();
  73. return mProjMatrix;
  74. }
  75. const Matrix4& Camera::getProjectionMatrixRS() const
  76. {
  77. updateFrustum();
  78. return mProjMatrixRS;
  79. }
  80. const Matrix4& Camera::getViewMatrix() const
  81. {
  82. updateView();
  83. return mViewMatrix;
  84. }
  85. const ConvexVolume& Camera::getFrustum() const
  86. {
  87. // Make any pending updates to the calculated frustum planes
  88. updateFrustumPlanes();
  89. return mFrustum;
  90. }
  91. void Camera::calcProjectionParameters(float& left, float& right, float& bottom, float& top) const
  92. {
  93. if (mCustomProjMatrix)
  94. {
  95. // Convert clipspace corners to camera space
  96. Matrix4 invProj = mProjMatrix.inverse();
  97. Vector3 topLeft(-0.5f, 0.5f, 0.0f);
  98. Vector3 bottomRight(0.5f, -0.5f, 0.0f);
  99. topLeft = invProj.multiply(topLeft);
  100. bottomRight = invProj.multiply(bottomRight);
  101. left = topLeft.x;
  102. top = topLeft.y;
  103. right = bottomRight.x;
  104. bottom = bottomRight.y;
  105. }
  106. else
  107. {
  108. if (mFrustumExtentsManuallySet)
  109. {
  110. left = mLeft;
  111. right = mRight;
  112. top = mTop;
  113. bottom = mBottom;
  114. }
  115. else if (mProjType == PT_PERSPECTIVE)
  116. {
  117. Radian thetaY (mHorzFOV * 0.5f);
  118. float tanThetaY = Math::tan(thetaY);
  119. float tanThetaX = tanThetaY * mAspect;
  120. float half_w = tanThetaX * mNearDist;
  121. float half_h = tanThetaY * mNearDist;
  122. left = -half_w;
  123. right = half_w;
  124. bottom = -half_h;
  125. top = half_h;
  126. mLeft = left;
  127. mRight = right;
  128. mTop = top;
  129. mBottom = bottom;
  130. }
  131. else
  132. {
  133. float half_w = getOrthoWindowWidth() * 0.5f;
  134. float half_h = getOrthoWindowHeight() * 0.5f;
  135. left = -half_w;
  136. right = half_w;
  137. bottom = -half_h;
  138. top = half_h;
  139. mLeft = left;
  140. mRight = right;
  141. mTop = top;
  142. mBottom = bottom;
  143. }
  144. }
  145. }
  146. void Camera::updateFrustum() const
  147. {
  148. if (isFrustumOutOfDate())
  149. {
  150. float left, right, bottom, top;
  151. calcProjectionParameters(left, right, bottom, top);
  152. if (!mCustomProjMatrix)
  153. {
  154. float inv_w = 1 / (right - left);
  155. float inv_h = 1 / (top - bottom);
  156. float inv_d = 1 / (mFarDist - mNearDist);
  157. if (mProjType == PT_PERSPECTIVE)
  158. {
  159. float A = 2 * mNearDist * inv_w;
  160. float B = 2 * mNearDist * inv_h;
  161. float C = (right + left) * inv_w;
  162. float D = (top + bottom) * inv_h;
  163. float q, qn;
  164. if (mFarDist == 0)
  165. {
  166. // Infinite far plane
  167. q = Camera::INFINITE_FAR_PLANE_ADJUST - 1;
  168. qn = mNearDist * (Camera::INFINITE_FAR_PLANE_ADJUST - 2);
  169. }
  170. else
  171. {
  172. q = - (mFarDist + mNearDist) * inv_d;
  173. qn = -2 * (mFarDist * mNearDist) * inv_d;
  174. }
  175. mProjMatrix = Matrix4::ZERO;
  176. mProjMatrix[0][0] = A;
  177. mProjMatrix[0][2] = C;
  178. mProjMatrix[1][1] = B;
  179. mProjMatrix[1][2] = D;
  180. mProjMatrix[2][2] = q;
  181. mProjMatrix[2][3] = qn;
  182. mProjMatrix[3][2] = -1;
  183. }
  184. else if (mProjType == PT_ORTHOGRAPHIC)
  185. {
  186. float A = 2 * inv_w;
  187. float B = 2 * inv_h;
  188. float C = - (right + left) * inv_w;
  189. float D = - (top + bottom) * inv_h;
  190. float q, qn;
  191. if (mFarDist == 0)
  192. {
  193. // Can not do infinite far plane here, avoid divided zero only
  194. q = - Camera::INFINITE_FAR_PLANE_ADJUST / mNearDist;
  195. qn = - Camera::INFINITE_FAR_PLANE_ADJUST - 1;
  196. }
  197. else
  198. {
  199. q = - 2 * inv_d;
  200. qn = - (mFarDist + mNearDist) * inv_d;
  201. }
  202. mProjMatrix = Matrix4::ZERO;
  203. mProjMatrix[0][0] = A;
  204. mProjMatrix[0][3] = C;
  205. mProjMatrix[1][1] = B;
  206. mProjMatrix[1][3] = D;
  207. mProjMatrix[2][2] = q;
  208. mProjMatrix[2][3] = qn;
  209. mProjMatrix[3][3] = 1;
  210. }
  211. }
  212. RenderSystem* renderSystem = BansheeEngine::RenderSystem::instancePtr();
  213. renderSystem->convertProjectionMatrix(mProjMatrix, mProjMatrixRS);
  214. // Calculate bounding box (local)
  215. // Box is from 0, down -Z, max dimensions as determined from far plane
  216. // If infinite view frustum just pick a far value
  217. float farDist = (mFarDist == 0) ? 100000 : mFarDist;
  218. // Near plane bounds
  219. Vector3 min(left, bottom, -farDist);
  220. Vector3 max(right, top, 0);
  221. if (mCustomProjMatrix)
  222. {
  223. // Some custom projection matrices can have unusual inverted settings
  224. // So make sure the AABB is the right way around to start with
  225. Vector3 tmp = min;
  226. min.floor(max);
  227. max.ceil(tmp);
  228. }
  229. if (mProjType == PT_PERSPECTIVE)
  230. {
  231. // Merge with far plane bounds
  232. float radio = farDist / mNearDist;
  233. min.floor(Vector3(left * radio, bottom * radio, -farDist));
  234. max.ceil(Vector3(right * radio, top * radio, 0));
  235. }
  236. mBoundingBox.setExtents(min, max);
  237. mRecalcFrustum = false;
  238. mRecalcFrustumPlanes = true;
  239. }
  240. }
  241. bool Camera::isFrustumOutOfDate() const
  242. {
  243. return mRecalcFrustum;
  244. }
  245. void Camera::updateView() const
  246. {
  247. if (!mCustomViewMatrix)
  248. {
  249. Matrix3 rot;
  250. const Quaternion& orientation = sceneObject()->getWorldRotation();
  251. const Vector3& position = sceneObject()->getWorldPosition();
  252. mViewMatrix.makeView(position, orientation);
  253. }
  254. }
  255. void Camera::updateFrustumPlanes() const
  256. {
  257. updateFrustum();
  258. if (mRecalcFrustumPlanes)
  259. {
  260. Vector<Plane> frustumPlanes(6);
  261. Matrix4 combo = mProjMatrix;
  262. frustumPlanes[FRUSTUM_PLANE_LEFT].normal.x = combo[3][0] + combo[0][0];
  263. frustumPlanes[FRUSTUM_PLANE_LEFT].normal.y = combo[3][1] + combo[0][1];
  264. frustumPlanes[FRUSTUM_PLANE_LEFT].normal.z = combo[3][2] + combo[0][2];
  265. frustumPlanes[FRUSTUM_PLANE_LEFT].d = combo[3][3] + combo[0][3];
  266. frustumPlanes[FRUSTUM_PLANE_RIGHT].normal.x = combo[3][0] - combo[0][0];
  267. frustumPlanes[FRUSTUM_PLANE_RIGHT].normal.y = combo[3][1] - combo[0][1];
  268. frustumPlanes[FRUSTUM_PLANE_RIGHT].normal.z = combo[3][2] - combo[0][2];
  269. frustumPlanes[FRUSTUM_PLANE_RIGHT].d = combo[3][3] - combo[0][3];
  270. frustumPlanes[FRUSTUM_PLANE_TOP].normal.x = combo[3][0] - combo[1][0];
  271. frustumPlanes[FRUSTUM_PLANE_TOP].normal.y = combo[3][1] - combo[1][1];
  272. frustumPlanes[FRUSTUM_PLANE_TOP].normal.z = combo[3][2] - combo[1][2];
  273. frustumPlanes[FRUSTUM_PLANE_TOP].d = combo[3][3] - combo[1][3];
  274. frustumPlanes[FRUSTUM_PLANE_BOTTOM].normal.x = combo[3][0] + combo[1][0];
  275. frustumPlanes[FRUSTUM_PLANE_BOTTOM].normal.y = combo[3][1] + combo[1][1];
  276. frustumPlanes[FRUSTUM_PLANE_BOTTOM].normal.z = combo[3][2] + combo[1][2];
  277. frustumPlanes[FRUSTUM_PLANE_BOTTOM].d = combo[3][3] + combo[1][3];
  278. frustumPlanes[FRUSTUM_PLANE_NEAR].normal.x = combo[3][0] + combo[2][0];
  279. frustumPlanes[FRUSTUM_PLANE_NEAR].normal.y = combo[3][1] + combo[2][1];
  280. frustumPlanes[FRUSTUM_PLANE_NEAR].normal.z = combo[3][2] + combo[2][2];
  281. frustumPlanes[FRUSTUM_PLANE_NEAR].d = combo[3][3] + combo[2][3];
  282. frustumPlanes[FRUSTUM_PLANE_FAR].normal.x = combo[3][0] - combo[2][0];
  283. frustumPlanes[FRUSTUM_PLANE_FAR].normal.y = combo[3][1] - combo[2][1];
  284. frustumPlanes[FRUSTUM_PLANE_FAR].normal.z = combo[3][2] - combo[2][2];
  285. frustumPlanes[FRUSTUM_PLANE_FAR].d = combo[3][3] - combo[2][3];
  286. for(UINT32 i = 0; i < 6; i++)
  287. {
  288. float length = frustumPlanes[i].normal.normalize();
  289. frustumPlanes[i].d /= -length;
  290. }
  291. mFrustum = ConvexVolume(frustumPlanes);
  292. mRecalcFrustumPlanes = false;
  293. }
  294. }
  295. float Camera::getAspectRatio(void) const
  296. {
  297. return mAspect;
  298. }
  299. void Camera::setAspectRatio(float r)
  300. {
  301. mAspect = r;
  302. invalidateFrustum();
  303. markCoreDirty();
  304. }
  305. const AABox& Camera::getBoundingBox() const
  306. {
  307. updateFrustum();
  308. return mBoundingBox;
  309. }
  310. void Camera::setProjectionType(ProjectionType pt)
  311. {
  312. mProjType = pt;
  313. invalidateFrustum();
  314. markCoreDirty();
  315. }
  316. ProjectionType Camera::getProjectionType() const
  317. {
  318. return mProjType;
  319. }
  320. void Camera::setCustomViewMatrix(bool enable, const Matrix4& viewMatrix)
  321. {
  322. mCustomViewMatrix = enable;
  323. if (enable)
  324. {
  325. assert(viewMatrix.isAffine());
  326. mViewMatrix = viewMatrix;
  327. }
  328. markCoreDirty();
  329. }
  330. void Camera::setCustomProjectionMatrix(bool enable, const Matrix4& projMatrix)
  331. {
  332. mCustomProjMatrix = enable;
  333. if (enable)
  334. mProjMatrix = projMatrix;
  335. invalidateFrustum();
  336. markCoreDirty();
  337. }
  338. void Camera::setOrthoWindow(float w, float h)
  339. {
  340. mOrthoHeight = h;
  341. mAspect = w / h;
  342. invalidateFrustum();
  343. markCoreDirty();
  344. }
  345. void Camera::setOrthoWindowHeight(float h)
  346. {
  347. mOrthoHeight = h;
  348. invalidateFrustum();
  349. markCoreDirty();
  350. }
  351. void Camera::setOrthoWindowWidth(float w)
  352. {
  353. mOrthoHeight = w / mAspect;
  354. invalidateFrustum();
  355. markCoreDirty();
  356. }
  357. float Camera::getOrthoWindowHeight() const
  358. {
  359. return mOrthoHeight;
  360. }
  361. float Camera::getOrthoWindowWidth() const
  362. {
  363. return mOrthoHeight * mAspect;
  364. }
  365. void Camera::setFrustumExtents(float left, float right, float top, float bottom)
  366. {
  367. mFrustumExtentsManuallySet = true;
  368. mLeft = left;
  369. mRight = right;
  370. mTop = top;
  371. mBottom = bottom;
  372. invalidateFrustum();
  373. markCoreDirty();
  374. }
  375. void Camera::resetFrustumExtents()
  376. {
  377. mFrustumExtentsManuallySet = false;
  378. invalidateFrustum();
  379. markCoreDirty();
  380. }
  381. void Camera::getFrustumExtents(float& outleft, float& outright, float& outtop, float& outbottom) const
  382. {
  383. updateFrustum();
  384. outleft = mLeft;
  385. outright = mRight;
  386. outtop = mTop;
  387. outbottom = mBottom;
  388. }
  389. void Camera::invalidateFrustum() const
  390. {
  391. mRecalcFrustum = true;
  392. mRecalcFrustumPlanes = true;
  393. }
  394. CameraProxyPtr Camera::_createProxy() const
  395. {
  396. CameraProxyPtr proxy = bs_shared_ptr<CameraProxy>();
  397. proxy->layer = mLayers;
  398. proxy->priority = mPriority;
  399. proxy->projMatrix = getProjectionMatrix();
  400. proxy->viewMatrix = getViewMatrix();
  401. proxy->worldMatrix = SO()->getWorldTfrm();
  402. proxy->viewport = mViewport->clone();
  403. proxy->frustum = getFrustum();
  404. proxy->ignoreSceneRenderables = mIgnoreSceneRenderables;
  405. proxy->worldPosition = SO()->getWorldPosition();
  406. return proxy;
  407. }
  408. RTTITypeBase* Camera::getRTTIStatic()
  409. {
  410. return CameraRTTI::instance();
  411. }
  412. RTTITypeBase* Camera::getRTTI() const
  413. {
  414. return Camera::getRTTIStatic();
  415. }
  416. }