Camera.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. #include "Base.h"
  2. #include "Camera.h"
  3. #include "Game.h"
  4. #include "Node.h"
  5. #include "Game.h"
  6. #include "PhysicsController.h"
  7. // Camera dirty bits
  8. #define CAMERA_DIRTY_VIEW 1
  9. #define CAMERA_DIRTY_PROJ 2
  10. #define CAMERA_DIRTY_VIEW_PROJ 4
  11. #define CAMERA_DIRTY_INV_VIEW 8
  12. #define CAMERA_DIRTY_INV_VIEW_PROJ 16
  13. #define CAMERA_DIRTY_BOUNDS 32
  14. #define CAMERA_DIRTY_ALL (CAMERA_DIRTY_VIEW | CAMERA_DIRTY_PROJ | CAMERA_DIRTY_VIEW_PROJ | CAMERA_DIRTY_INV_VIEW | CAMERA_DIRTY_INV_VIEW_PROJ | CAMERA_DIRTY_BOUNDS)
  15. namespace gameplay
  16. {
  17. Camera::Camera(float fieldOfView, float aspectRatio, float nearPlane, float farPlane)
  18. : _type(PERSPECTIVE), _fieldOfView(fieldOfView), _aspectRatio(aspectRatio), _nearPlane(nearPlane), _farPlane(farPlane),
  19. _dirtyBits(CAMERA_DIRTY_ALL), _node(NULL)
  20. {
  21. }
  22. Camera::Camera(float zoomX, float zoomY, float aspectRatio, float nearPlane, float farPlane)
  23. : _type(ORTHOGRAPHIC), _aspectRatio(aspectRatio), _nearPlane(nearPlane), _farPlane(farPlane),
  24. _dirtyBits(CAMERA_DIRTY_ALL), _node(NULL)
  25. {
  26. // Orthographic camera.
  27. _zoom[0] = zoomX;
  28. _zoom[1] = zoomY;
  29. }
  30. Camera::~Camera()
  31. {
  32. }
  33. Camera* Camera::createPerspective(float fieldOfView, float aspectRatio, float nearPlane, float farPlane)
  34. {
  35. return new Camera(fieldOfView, aspectRatio, nearPlane, farPlane);
  36. }
  37. Camera* Camera::createOrthographic(float zoomX, float zoomY, float aspectRatio, float nearPlane, float farPlane)
  38. {
  39. return new Camera(zoomX, zoomY, aspectRatio, nearPlane, farPlane);
  40. }
  41. Camera::Type Camera::getCameraType() const
  42. {
  43. return _type;
  44. }
  45. float Camera::getFieldOfView() const
  46. {
  47. assert(_type == Camera::PERSPECTIVE);
  48. return _fieldOfView;
  49. }
  50. void Camera::setFieldOfView(float fieldOfView)
  51. {
  52. assert(_type == Camera::PERSPECTIVE);
  53. _fieldOfView = fieldOfView;
  54. _dirtyBits |= CAMERA_DIRTY_PROJ | CAMERA_DIRTY_VIEW_PROJ | CAMERA_DIRTY_INV_VIEW_PROJ | CAMERA_DIRTY_BOUNDS;
  55. }
  56. float Camera::getZoomX() const
  57. {
  58. assert(_type == Camera::ORTHOGRAPHIC);
  59. return _zoom[0];
  60. }
  61. void Camera::setZoomX(float zoomX)
  62. {
  63. assert(_type == Camera::ORTHOGRAPHIC);
  64. _zoom[0] = zoomX;
  65. _dirtyBits |= CAMERA_DIRTY_PROJ | CAMERA_DIRTY_VIEW_PROJ | CAMERA_DIRTY_INV_VIEW_PROJ | CAMERA_DIRTY_BOUNDS;
  66. }
  67. float Camera::getZoomY() const
  68. {
  69. assert(_type == Camera::ORTHOGRAPHIC);
  70. return _zoom[1];
  71. }
  72. void Camera::setZoomY(float zoomY)
  73. {
  74. assert(_type == Camera::ORTHOGRAPHIC);
  75. _zoom[1] = zoomY;
  76. _dirtyBits |= CAMERA_DIRTY_PROJ | CAMERA_DIRTY_VIEW_PROJ | CAMERA_DIRTY_INV_VIEW_PROJ | CAMERA_DIRTY_BOUNDS;
  77. }
  78. float Camera::getAspectRatio() const
  79. {
  80. return _aspectRatio;
  81. }
  82. void Camera::setAspectRatio(float aspectRatio)
  83. {
  84. _aspectRatio = aspectRatio;
  85. _dirtyBits |= CAMERA_DIRTY_PROJ | CAMERA_DIRTY_VIEW_PROJ | CAMERA_DIRTY_INV_VIEW_PROJ | CAMERA_DIRTY_BOUNDS;
  86. }
  87. float Camera::getNearPlane() const
  88. {
  89. return _nearPlane;
  90. }
  91. void Camera::setNearPlane(float nearPlane)
  92. {
  93. _nearPlane = nearPlane;
  94. _dirtyBits |= CAMERA_DIRTY_PROJ | CAMERA_DIRTY_VIEW_PROJ | CAMERA_DIRTY_INV_VIEW_PROJ | CAMERA_DIRTY_BOUNDS;
  95. }
  96. float Camera::getFarPlane() const
  97. {
  98. return _farPlane;
  99. }
  100. void Camera::setFarPlane(float farPlane)
  101. {
  102. _farPlane = farPlane;
  103. _dirtyBits |= CAMERA_DIRTY_PROJ | CAMERA_DIRTY_VIEW_PROJ | CAMERA_DIRTY_INV_VIEW_PROJ | CAMERA_DIRTY_BOUNDS;
  104. }
  105. Node* Camera::getNode() const
  106. {
  107. return _node;
  108. }
  109. void Camera::setNode(Node* node)
  110. {
  111. if (_node != node)
  112. {
  113. if (_node)
  114. {
  115. _node->removeListener(this);
  116. }
  117. // Connect the new node.
  118. _node = node;
  119. if (_node)
  120. {
  121. _node->addListener(this);
  122. }
  123. _dirtyBits |= CAMERA_DIRTY_VIEW | CAMERA_DIRTY_VIEW_PROJ | CAMERA_DIRTY_INV_VIEW | CAMERA_DIRTY_INV_VIEW_PROJ | CAMERA_DIRTY_BOUNDS;
  124. }
  125. }
  126. const Matrix& Camera::getViewMatrix() const
  127. {
  128. if (_dirtyBits & CAMERA_DIRTY_VIEW)
  129. {
  130. if (_node)
  131. {
  132. // The view matrix is the inverse of our transform matrix.
  133. _node->getWorldMatrix().invert(&_view);
  134. }
  135. else
  136. {
  137. _view.setIdentity();
  138. }
  139. _dirtyBits &= ~CAMERA_DIRTY_VIEW;
  140. }
  141. return _view;
  142. }
  143. const Matrix& Camera::getInverseViewMatrix() const
  144. {
  145. if (_dirtyBits & CAMERA_DIRTY_INV_VIEW)
  146. {
  147. getViewMatrix().invert(&_inverseView);
  148. _dirtyBits &= ~CAMERA_DIRTY_INV_VIEW;
  149. }
  150. return _inverseView;
  151. }
  152. const Matrix& Camera::getProjectionMatrix() const
  153. {
  154. if (_dirtyBits & CAMERA_DIRTY_PROJ)
  155. {
  156. if (_type == PERSPECTIVE)
  157. {
  158. Matrix::createPerspective(_fieldOfView, _aspectRatio, _nearPlane, _farPlane, &_projection);
  159. }
  160. else
  161. {
  162. Matrix::createOrthographic(_zoom[0], _zoom[1], _nearPlane, _farPlane, &_projection);
  163. }
  164. _dirtyBits &= ~CAMERA_DIRTY_PROJ;
  165. }
  166. return _projection;
  167. }
  168. const Matrix& Camera::getViewProjectionMatrix() const
  169. {
  170. if (_dirtyBits & CAMERA_DIRTY_VIEW_PROJ)
  171. {
  172. Matrix::multiply(getProjectionMatrix(), getViewMatrix(), &_viewProjection);
  173. _dirtyBits &= ~CAMERA_DIRTY_VIEW_PROJ;
  174. }
  175. return _viewProjection;
  176. }
  177. const Matrix& Camera::getInverseViewProjectionMatrix() const
  178. {
  179. if (_dirtyBits & CAMERA_DIRTY_INV_VIEW_PROJ)
  180. {
  181. getViewProjectionMatrix().invert(&_inverseViewProjection);
  182. _dirtyBits &= ~CAMERA_DIRTY_INV_VIEW_PROJ;
  183. }
  184. return _inverseViewProjection;
  185. }
  186. const Frustum& Camera::getFrustum() const
  187. {
  188. if (_dirtyBits & CAMERA_DIRTY_BOUNDS)
  189. {
  190. // Update our bounding frustum from our view projection matrix.
  191. _bounds.set(getViewProjectionMatrix());
  192. _dirtyBits &= ~CAMERA_DIRTY_BOUNDS;
  193. }
  194. return _bounds;
  195. }
  196. void Camera::project(const Rectangle& viewport, const Vector3& position, float* x, float* y, float* depth)
  197. {
  198. // Determine viewport coords to use.
  199. float vpx = viewport.x;
  200. float vpy = viewport.y;
  201. float vpw = viewport.width;
  202. float vph = viewport.height;
  203. // Transform the point to clip-space.
  204. Vector4 clipPos;
  205. getViewProjectionMatrix().transformVector(Vector4(position.x, position.y, position.z, 1.0f), &clipPos);
  206. // Compute normalized device coordinates.
  207. float ndcX = clipPos.x / clipPos.w;
  208. float ndcY = clipPos.y / clipPos.w;
  209. // Compute screen coordinates by applying our viewport transformation.
  210. *x = vpx + (ndcX + 1.0f) * 0.5f * vpw;
  211. *y = vpy + (1.0f - (ndcY + 1.0f) * 0.5f) * vph;
  212. if (depth)
  213. {
  214. float ndcZ = clipPos.z / clipPos.w;
  215. *depth = ndcZ + 1.0f / 2.0f;
  216. }
  217. }
  218. void Camera::unproject(const Rectangle& viewport, float x, float y, float depth, Vector3* dst)
  219. {
  220. // Determine viewport coords to use.
  221. float vpx = viewport.x;
  222. float vpy = viewport.y;
  223. float vpw = viewport.width;
  224. float vph = viewport.height;
  225. // Create our screen space position in NDC.
  226. Vector4 screen(
  227. ((float)x - (float)vpx) / (float)vpw,
  228. ((float)(vph - y) - (float)vpy) / (float)vph,
  229. depth,
  230. 1.0f );
  231. // Map to range -1 to 1.
  232. screen.x = screen.x * 2.0f - 1.0f;
  233. screen.y = screen.y * 2.0f - 1.0f;
  234. screen.z = screen.z * 2.0f - 1.0f;
  235. // Transform the screen-space NDC by our inverse view projection matrix.
  236. getInverseViewProjectionMatrix().transformVector(screen, &screen);
  237. // Divide by our W coordinate.
  238. if (screen.w != 0.0f)
  239. {
  240. screen.x /= screen.w;
  241. screen.y /= screen.w;
  242. screen.z /= screen.w;
  243. }
  244. dst->set(screen.x, screen.y, screen.z);
  245. }
  246. void Camera::pickRay(const Rectangle& viewport, float x, float y, Ray* dst)
  247. {
  248. // Get the world-space position at the near clip plane.
  249. Vector3 nearPoint;
  250. unproject(viewport, x, y, 0.0f, &nearPoint);
  251. // Get the world-space position at the far clip plane.
  252. Vector3 farPoint;
  253. unproject(viewport, x, y, 1.0f, &farPoint);
  254. // Set the direction of the ray.
  255. Vector3 direction;
  256. Vector3::subtract(farPoint, nearPoint, &direction);
  257. direction.normalize();
  258. dst->set(nearPoint, direction);
  259. }
  260. Camera* Camera::clone(NodeCloneContext &context) const
  261. {
  262. Camera* cameraClone = NULL;
  263. if (getCameraType() == PERSPECTIVE)
  264. {
  265. cameraClone = createPerspective(_fieldOfView, _aspectRatio, _nearPlane, _farPlane);
  266. }
  267. else if (getCameraType() == ORTHOGRAPHIC)
  268. {
  269. cameraClone = createOrthographic(getZoomX(), getZoomY(), getAspectRatio(), _nearPlane, _farPlane);
  270. }
  271. assert(cameraClone);
  272. if (Node* node = context.findClonedNode(getNode()))
  273. {
  274. cameraClone->setNode(node);
  275. }
  276. return cameraClone;
  277. }
  278. void Camera::transformChanged(Transform* transform, long cookie)
  279. {
  280. _dirtyBits |= CAMERA_DIRTY_VIEW | CAMERA_DIRTY_INV_VIEW | CAMERA_DIRTY_INV_VIEW_PROJ | CAMERA_DIRTY_VIEW_PROJ | CAMERA_DIRTY_BOUNDS;
  281. }
  282. }