Camera.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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 Viewport* viewport, const Vector3& position, float* x, float* y, float* depth)
  197. {
  198. // Determine viewport coords to use.
  199. float vpx, vpy, vpw, vph;
  200. if (viewport)
  201. {
  202. vpx = viewport->getX();
  203. vpy = viewport->getY();
  204. vpw = viewport->getWidth();
  205. vph = viewport->getHeight();
  206. }
  207. else
  208. {
  209. vpx = 0;
  210. vpy = 0;
  211. vpw = Game::getInstance()->getWidth();
  212. vph = Game::getInstance()->getHeight();
  213. }
  214. // Transform the point to clip-space.
  215. Vector4 clipPos;
  216. getViewProjectionMatrix().transformVector(Vector4(position.x, position.y, position.z, 1.0f), &clipPos);
  217. // Compute normalized device coordinates.
  218. float ndcX = clipPos.x / clipPos.w;
  219. float ndcY = clipPos.y / clipPos.w;
  220. // Compute screen coordinates by applying our viewport transformation.
  221. *x = vpx + (ndcX + 1.0f) * 0.5f * vpw;
  222. *y = vpy + (1.0f - (ndcY + 1.0f) * 0.5f) * vph;
  223. if (depth)
  224. {
  225. float ndcZ = clipPos.z / clipPos.w;
  226. *depth = ndcZ + 1.0f / 2.0f;
  227. }
  228. }
  229. void Camera::unproject(const Viewport* viewport, float x, float y, float depth, Vector3* dst)
  230. {
  231. // Determine viewport coords to use.
  232. float vpx, vpy, vpw, vph;
  233. if (viewport)
  234. {
  235. vpx = viewport->getX();
  236. vpy = viewport->getY();
  237. vpw = viewport->getWidth();
  238. vph = viewport->getHeight();
  239. }
  240. else
  241. {
  242. vpx = 0;
  243. vpy = 0;
  244. vpw = Game::getInstance()->getWidth();
  245. vph = Game::getInstance()->getHeight();
  246. }
  247. // Create our screen space position in NDC.
  248. Vector4 screen(
  249. ((float)x - (float)vpx) / (float)vpw,
  250. ((float)(vph - y) - (float)vpy) / (float)vph,
  251. depth,
  252. 1.0f );
  253. // Map to range -1 to 1.
  254. screen.x = screen.x * 2.0f - 1.0f;
  255. screen.y = screen.y * 2.0f - 1.0f;
  256. screen.z = screen.z * 2.0f - 1.0f;
  257. // Transform the screen-space NDC by our inverse view projection matrix.
  258. getInverseViewProjectionMatrix().transformVector(screen, &screen);
  259. // Divide by our W coordinate.
  260. if (screen.w != 0.0f)
  261. {
  262. screen.x /= screen.w;
  263. screen.y /= screen.w;
  264. screen.z /= screen.w;
  265. }
  266. dst->set(screen.x, screen.y, screen.z);
  267. }
  268. void Camera::pickRay(const Viewport* viewport, float x, float y, Ray* dst)
  269. {
  270. // Get the world-space position at the near clip plane.
  271. Vector3 nearPoint;
  272. unproject(viewport, x, y, 0.0f, &nearPoint);
  273. // Get the world-space position at the far clip plane.
  274. Vector3 farPoint;
  275. unproject(viewport, x, y, 1.0f, &farPoint);
  276. // Set the direction of the ray.
  277. Vector3 direction;
  278. Vector3::subtract(farPoint, nearPoint, &direction);
  279. direction.normalize();
  280. dst->set(nearPoint, direction);
  281. }
  282. Camera* Camera::clone(CloneContext &context) const
  283. {
  284. Camera* cameraClone = NULL;
  285. if (getCameraType() == PERSPECTIVE)
  286. {
  287. cameraClone = createPerspective(_fieldOfView, _aspectRatio, _nearPlane, _farPlane);
  288. }
  289. else if (getCameraType() == ORTHOGRAPHIC)
  290. {
  291. cameraClone = createOrthographic(getZoomX(), getZoomY(), getAspectRatio(), _nearPlane, _farPlane);
  292. }
  293. assert(cameraClone);
  294. if (Node* node = context.findClonedNode(getNode()))
  295. {
  296. cameraClone->setNode(node);
  297. }
  298. return cameraClone;
  299. }
  300. void Camera::transformChanged(Transform* transform, long cookie)
  301. {
  302. _dirtyBits |= CAMERA_DIRTY_VIEW | CAMERA_DIRTY_INV_VIEW | CAMERA_DIRTY_INV_VIEW_PROJ | CAMERA_DIRTY_VIEW_PROJ | CAMERA_DIRTY_BOUNDS;
  303. }
  304. }