Camera.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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. // Other misc camera bits
  16. #define CAMERA_CUSTOM_PROJECTION 64
  17. namespace gameplay
  18. {
  19. Camera::Camera(float fieldOfView, float aspectRatio, float nearPlane, float farPlane)
  20. : _type(PERSPECTIVE), _fieldOfView(fieldOfView), _aspectRatio(aspectRatio), _nearPlane(nearPlane), _farPlane(farPlane),
  21. _bits(CAMERA_DIRTY_ALL), _node(NULL), _listeners(NULL)
  22. {
  23. }
  24. Camera::Camera(float zoomX, float zoomY, float aspectRatio, float nearPlane, float farPlane)
  25. : _type(ORTHOGRAPHIC), _aspectRatio(aspectRatio), _nearPlane(nearPlane), _farPlane(farPlane),
  26. _bits(CAMERA_DIRTY_ALL), _node(NULL), _listeners(NULL)
  27. {
  28. // Orthographic camera.
  29. _zoom[0] = zoomX;
  30. _zoom[1] = zoomY;
  31. }
  32. Camera::~Camera()
  33. {
  34. }
  35. Camera* Camera::createPerspective(float fieldOfView, float aspectRatio, float nearPlane, float farPlane)
  36. {
  37. return new Camera(fieldOfView, aspectRatio, nearPlane, farPlane);
  38. }
  39. Camera* Camera::createOrthographic(float zoomX, float zoomY, float aspectRatio, float nearPlane, float farPlane)
  40. {
  41. return new Camera(zoomX, zoomY, aspectRatio, nearPlane, farPlane);
  42. }
  43. Camera* Camera::create(Properties* properties)
  44. {
  45. GP_ASSERT(properties);
  46. // Read camera type
  47. std::string typeStr;
  48. if (properties->exists("type"))
  49. typeStr = properties->getString("type");
  50. Camera::Type type;
  51. if (typeStr == "PERSPECTIVE")
  52. {
  53. type = Camera::PERSPECTIVE;
  54. }
  55. else if (typeStr == "ORTHOGRAPHIC")
  56. {
  57. type = Camera::ORTHOGRAPHIC;
  58. }
  59. else
  60. {
  61. GP_ERROR("Invalid 'type' parameter for camera definition.");
  62. return NULL;
  63. }
  64. // Read common parameters
  65. float aspectRatio, nearPlane, farPlane;
  66. if (properties->exists("aspectRatio"))
  67. {
  68. aspectRatio = properties->getFloat("aspectRatio");
  69. }
  70. else
  71. {
  72. // Use default aspect ratio
  73. aspectRatio = (float)Game::getInstance()->getWidth() / Game::getInstance()->getHeight();
  74. }
  75. if (properties->exists("nearPlane"))
  76. nearPlane = properties->getFloat("nearPlane");
  77. else
  78. nearPlane = 0.2f; // use some reasonable default value
  79. if (properties->exists("farPlane"))
  80. farPlane = properties->getFloat("farPlane");
  81. else
  82. farPlane = 100; // use some reasonable default value
  83. Camera* camera = NULL;
  84. switch (type)
  85. {
  86. case Camera::PERSPECTIVE:
  87. // If field of view is not specified, use a default of 60 degrees
  88. camera = createPerspective(
  89. properties->exists("fieldOfView") ? properties->getFloat("fieldOfView") : 60.0f,
  90. aspectRatio, nearPlane, farPlane);
  91. break;
  92. case Camera::ORTHOGRAPHIC:
  93. // If zoomX and zoomY are not specified, use screen width/height
  94. camera = createOrthographic(
  95. properties->exists("zoomX") ? properties->getFloat("zoomX") : Game::getInstance()->getWidth(),
  96. properties->exists("zoomY") ? properties->getFloat("zoomY") : Game::getInstance()->getHeight(),
  97. aspectRatio, nearPlane, farPlane);
  98. break;
  99. }
  100. return camera;
  101. }
  102. Camera::Type Camera::getCameraType() const
  103. {
  104. return _type;
  105. }
  106. float Camera::getFieldOfView() const
  107. {
  108. GP_ASSERT(_type == Camera::PERSPECTIVE);
  109. return _fieldOfView;
  110. }
  111. void Camera::setFieldOfView(float fieldOfView)
  112. {
  113. GP_ASSERT(_type == Camera::PERSPECTIVE);
  114. _fieldOfView = fieldOfView;
  115. _bits |= CAMERA_DIRTY_PROJ | CAMERA_DIRTY_VIEW_PROJ | CAMERA_DIRTY_INV_VIEW_PROJ | CAMERA_DIRTY_BOUNDS;
  116. cameraChanged();
  117. }
  118. float Camera::getZoomX() const
  119. {
  120. GP_ASSERT(_type == Camera::ORTHOGRAPHIC);
  121. return _zoom[0];
  122. }
  123. void Camera::setZoomX(float zoomX)
  124. {
  125. GP_ASSERT(_type == Camera::ORTHOGRAPHIC);
  126. _zoom[0] = zoomX;
  127. _bits |= CAMERA_DIRTY_PROJ | CAMERA_DIRTY_VIEW_PROJ | CAMERA_DIRTY_INV_VIEW_PROJ | CAMERA_DIRTY_BOUNDS;
  128. cameraChanged();
  129. }
  130. float Camera::getZoomY() const
  131. {
  132. GP_ASSERT(_type == Camera::ORTHOGRAPHIC);
  133. return _zoom[1];
  134. }
  135. void Camera::setZoomY(float zoomY)
  136. {
  137. GP_ASSERT(_type == Camera::ORTHOGRAPHIC);
  138. _zoom[1] = zoomY;
  139. _bits |= CAMERA_DIRTY_PROJ | CAMERA_DIRTY_VIEW_PROJ | CAMERA_DIRTY_INV_VIEW_PROJ | CAMERA_DIRTY_BOUNDS;
  140. cameraChanged();
  141. }
  142. float Camera::getAspectRatio() const
  143. {
  144. return _aspectRatio;
  145. }
  146. void Camera::setAspectRatio(float aspectRatio)
  147. {
  148. _aspectRatio = aspectRatio;
  149. _bits |= CAMERA_DIRTY_PROJ | CAMERA_DIRTY_VIEW_PROJ | CAMERA_DIRTY_INV_VIEW_PROJ | CAMERA_DIRTY_BOUNDS;
  150. cameraChanged();
  151. }
  152. float Camera::getNearPlane() const
  153. {
  154. return _nearPlane;
  155. }
  156. void Camera::setNearPlane(float nearPlane)
  157. {
  158. _nearPlane = nearPlane;
  159. _bits |= CAMERA_DIRTY_PROJ | CAMERA_DIRTY_VIEW_PROJ | CAMERA_DIRTY_INV_VIEW_PROJ | CAMERA_DIRTY_BOUNDS;
  160. cameraChanged();
  161. }
  162. float Camera::getFarPlane() const
  163. {
  164. return _farPlane;
  165. }
  166. void Camera::setFarPlane(float farPlane)
  167. {
  168. _farPlane = farPlane;
  169. _bits |= CAMERA_DIRTY_PROJ | CAMERA_DIRTY_VIEW_PROJ | CAMERA_DIRTY_INV_VIEW_PROJ | CAMERA_DIRTY_BOUNDS;
  170. cameraChanged();
  171. }
  172. Node* Camera::getNode() const
  173. {
  174. return _node;
  175. }
  176. void Camera::setNode(Node* node)
  177. {
  178. if (_node != node)
  179. {
  180. if (_node)
  181. {
  182. _node->removeListener(this);
  183. }
  184. // Connect the new node.
  185. _node = node;
  186. if (_node)
  187. {
  188. _node->addListener(this);
  189. }
  190. _bits |= CAMERA_DIRTY_VIEW | CAMERA_DIRTY_VIEW_PROJ | CAMERA_DIRTY_INV_VIEW | CAMERA_DIRTY_INV_VIEW_PROJ | CAMERA_DIRTY_BOUNDS;
  191. cameraChanged();
  192. }
  193. }
  194. const Matrix& Camera::getViewMatrix() const
  195. {
  196. if (_bits & CAMERA_DIRTY_VIEW)
  197. {
  198. if (_node)
  199. {
  200. // The view matrix is the inverse of our transform matrix.
  201. _node->getWorldMatrix().invert(&_view);
  202. }
  203. else
  204. {
  205. _view.setIdentity();
  206. }
  207. _bits &= ~CAMERA_DIRTY_VIEW;
  208. }
  209. return _view;
  210. }
  211. const Matrix& Camera::getInverseViewMatrix() const
  212. {
  213. if (_bits & CAMERA_DIRTY_INV_VIEW)
  214. {
  215. getViewMatrix().invert(&_inverseView);
  216. _bits &= ~CAMERA_DIRTY_INV_VIEW;
  217. }
  218. return _inverseView;
  219. }
  220. const Matrix& Camera::getProjectionMatrix() const
  221. {
  222. if (!(_bits & CAMERA_CUSTOM_PROJECTION) && (_bits & CAMERA_DIRTY_PROJ))
  223. {
  224. if (_type == PERSPECTIVE)
  225. {
  226. Matrix::createPerspective(_fieldOfView, _aspectRatio, _nearPlane, _farPlane, &_projection);
  227. }
  228. else
  229. {
  230. // Create an ortho projection with the origin at the bottom left of the viewport, +X to the right and +Y up.
  231. Matrix::createOrthographic(_zoom[0], _zoom[1], _nearPlane, _farPlane, &_projection);
  232. }
  233. _bits &= ~CAMERA_DIRTY_PROJ;
  234. }
  235. return _projection;
  236. }
  237. void Camera::setProjectionMatrix(const Matrix& matrix)
  238. {
  239. _projection = matrix;
  240. _bits |= CAMERA_CUSTOM_PROJECTION;
  241. _bits |= CAMERA_DIRTY_PROJ | CAMERA_DIRTY_VIEW_PROJ | CAMERA_DIRTY_INV_VIEW_PROJ | CAMERA_DIRTY_BOUNDS;
  242. cameraChanged();
  243. }
  244. void Camera::resetProjectionMatrix()
  245. {
  246. if (_bits & CAMERA_CUSTOM_PROJECTION)
  247. {
  248. _bits &= ~CAMERA_CUSTOM_PROJECTION;
  249. _bits |= CAMERA_DIRTY_PROJ | CAMERA_DIRTY_VIEW_PROJ | CAMERA_DIRTY_INV_VIEW_PROJ | CAMERA_DIRTY_BOUNDS;
  250. cameraChanged();
  251. }
  252. }
  253. const Matrix& Camera::getViewProjectionMatrix() const
  254. {
  255. if (_bits & CAMERA_DIRTY_VIEW_PROJ)
  256. {
  257. Matrix::multiply(getProjectionMatrix(), getViewMatrix(), &_viewProjection);
  258. _bits &= ~CAMERA_DIRTY_VIEW_PROJ;
  259. }
  260. return _viewProjection;
  261. }
  262. const Matrix& Camera::getInverseViewProjectionMatrix() const
  263. {
  264. if (_bits & CAMERA_DIRTY_INV_VIEW_PROJ)
  265. {
  266. getViewProjectionMatrix().invert(&_inverseViewProjection);
  267. _bits &= ~CAMERA_DIRTY_INV_VIEW_PROJ;
  268. }
  269. return _inverseViewProjection;
  270. }
  271. const Frustum& Camera::getFrustum() const
  272. {
  273. if (_bits & CAMERA_DIRTY_BOUNDS)
  274. {
  275. // Update our bounding frustum from our view projection matrix.
  276. _bounds.set(getViewProjectionMatrix());
  277. _bits &= ~CAMERA_DIRTY_BOUNDS;
  278. }
  279. return _bounds;
  280. }
  281. void Camera::project(const Rectangle& viewport, const Vector3& position, float* x, float* y, float* depth) const
  282. {
  283. GP_ASSERT(x);
  284. GP_ASSERT(y);
  285. // Transform the point to clip-space.
  286. Vector4 clipPos;
  287. getViewProjectionMatrix().transformVector(Vector4(position.x, position.y, position.z, 1.0f), &clipPos);
  288. // Compute normalized device coordinates.
  289. GP_ASSERT(clipPos.w != 0.0f);
  290. float ndcX = clipPos.x / clipPos.w;
  291. float ndcY = clipPos.y / clipPos.w;
  292. // Compute screen coordinates by applying our viewport transformation.
  293. *x = viewport.x + (ndcX + 1.0f) * 0.5f * viewport.width;
  294. *y = viewport.y + (1.0f - (ndcY + 1.0f) * 0.5f) * viewport.height;
  295. if (depth)
  296. {
  297. float ndcZ = clipPos.z / clipPos.w;
  298. *depth = (ndcZ + 1.0f) / 2.0f;
  299. }
  300. }
  301. void Camera::project(const Rectangle& viewport, const Vector3& position, Vector2* out) const
  302. {
  303. GP_ASSERT(out);
  304. float x, y;
  305. project(viewport, position, &x, &y);
  306. out->set(x, y);
  307. }
  308. void Camera::project(const Rectangle& viewport, const Vector3& position, Vector3* out) const
  309. {
  310. GP_ASSERT(out);
  311. float x, y, depth;
  312. project(viewport, position, &x, &y, &depth);
  313. out->set(x, y, depth);
  314. }
  315. void Camera::unproject(const Rectangle& viewport, float x, float y, float depth, Vector3* dst) const
  316. {
  317. GP_ASSERT(dst);
  318. // Create our screen space position in NDC.
  319. GP_ASSERT(viewport.width != 0.0f && viewport.height != 0.0f);
  320. Vector4 screen((x - viewport.x) / viewport.width, ((viewport.height - y) - viewport.y) / viewport.height, depth, 1.0f);
  321. // Map to range -1 to 1.
  322. screen.x = screen.x * 2.0f - 1.0f;
  323. screen.y = screen.y * 2.0f - 1.0f;
  324. screen.z = screen.z * 2.0f - 1.0f;
  325. // Transform the screen-space NDC by our inverse view projection matrix.
  326. getInverseViewProjectionMatrix().transformVector(screen, &screen);
  327. // Divide by our W coordinate.
  328. if (screen.w != 0.0f)
  329. {
  330. screen.x /= screen.w;
  331. screen.y /= screen.w;
  332. screen.z /= screen.w;
  333. }
  334. dst->set(screen.x, screen.y, screen.z);
  335. }
  336. void Camera::pickRay(const Rectangle& viewport, float x, float y, Ray* dst) const
  337. {
  338. GP_ASSERT(dst);
  339. // Get the world-space position at the near clip plane.
  340. Vector3 nearPoint;
  341. unproject(viewport, x, y, 0.0f, &nearPoint);
  342. // Get the world-space position at the far clip plane.
  343. Vector3 farPoint;
  344. unproject(viewport, x, y, 1.0f, &farPoint);
  345. // Set the direction of the ray.
  346. Vector3 direction;
  347. Vector3::subtract(farPoint, nearPoint, &direction);
  348. direction.normalize();
  349. dst->set(nearPoint, direction);
  350. }
  351. Camera* Camera::clone(NodeCloneContext &context) const
  352. {
  353. Camera* cameraClone = NULL;
  354. if (getCameraType() == PERSPECTIVE)
  355. {
  356. cameraClone = createPerspective(_fieldOfView, _aspectRatio, _nearPlane, _farPlane);
  357. }
  358. else if (getCameraType() == ORTHOGRAPHIC)
  359. {
  360. cameraClone = createOrthographic(getZoomX(), getZoomY(), getAspectRatio(), _nearPlane, _farPlane);
  361. }
  362. GP_ASSERT(cameraClone);
  363. if (Node* node = context.findClonedNode(getNode()))
  364. {
  365. cameraClone->setNode(node);
  366. }
  367. return cameraClone;
  368. }
  369. void Camera::transformChanged(Transform* transform, long cookie)
  370. {
  371. _bits |= CAMERA_DIRTY_VIEW | CAMERA_DIRTY_INV_VIEW | CAMERA_DIRTY_INV_VIEW_PROJ | CAMERA_DIRTY_VIEW_PROJ | CAMERA_DIRTY_BOUNDS;
  372. cameraChanged();
  373. }
  374. void Camera::cameraChanged()
  375. {
  376. if (_listeners == NULL)
  377. return;
  378. for (std::list<Camera::Listener*>::iterator itr = _listeners->begin(); itr != _listeners->end(); ++itr)
  379. {
  380. Camera::Listener* listener = (*itr);
  381. listener->cameraChanged(this);
  382. }
  383. }
  384. void Camera::addListener(Camera::Listener* listener)
  385. {
  386. GP_ASSERT(listener);
  387. if (_listeners == NULL)
  388. _listeners = new std::list<Camera::Listener*>();
  389. _listeners->push_back(listener);
  390. }
  391. void Camera::removeListener(Camera::Listener* listener)
  392. {
  393. GP_ASSERT(listener);
  394. if (_listeners)
  395. {
  396. for (std::list<Camera::Listener*>::iterator itr = _listeners->begin(); itr != _listeners->end(); ++itr)
  397. {
  398. if ((*itr) == listener)
  399. {
  400. _listeners->erase(itr);
  401. break;
  402. }
  403. }
  404. }
  405. }
  406. }