PageRenderTime 178ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/hsp3dish/gameplay/src/Camera.cpp

https://github.com/zakki/openhsp
C++ | 445 lines | 342 code | 82 blank | 21 comment | 44 complexity | 544bfbf371ebcd2f95b5501ba392edda MD5 | raw file
Possible License(s): LGPL-3.0
  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)
  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)
  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. }
  117. float Camera::getZoomX() const
  118. {
  119. GP_ASSERT(_type == Camera::ORTHOGRAPHIC);
  120. return _zoom[0];
  121. }
  122. void Camera::setZoomX(float zoomX)
  123. {
  124. GP_ASSERT(_type == Camera::ORTHOGRAPHIC);
  125. _zoom[0] = zoomX;
  126. _bits |= CAMERA_DIRTY_PROJ | CAMERA_DIRTY_VIEW_PROJ | CAMERA_DIRTY_INV_VIEW_PROJ | CAMERA_DIRTY_BOUNDS;
  127. }
  128. float Camera::getZoomY() const
  129. {
  130. GP_ASSERT(_type == Camera::ORTHOGRAPHIC);
  131. return _zoom[1];
  132. }
  133. void Camera::setZoomY(float zoomY)
  134. {
  135. GP_ASSERT(_type == Camera::ORTHOGRAPHIC);
  136. _zoom[1] = zoomY;
  137. _bits |= CAMERA_DIRTY_PROJ | CAMERA_DIRTY_VIEW_PROJ | CAMERA_DIRTY_INV_VIEW_PROJ | CAMERA_DIRTY_BOUNDS;
  138. }
  139. float Camera::getAspectRatio() const
  140. {
  141. return _aspectRatio;
  142. }
  143. void Camera::setAspectRatio(float aspectRatio)
  144. {
  145. _aspectRatio = aspectRatio;
  146. _bits |= CAMERA_DIRTY_PROJ | CAMERA_DIRTY_VIEW_PROJ | CAMERA_DIRTY_INV_VIEW_PROJ | CAMERA_DIRTY_BOUNDS;
  147. }
  148. float Camera::getNearPlane() const
  149. {
  150. return _nearPlane;
  151. }
  152. void Camera::setNearPlane(float nearPlane)
  153. {
  154. _nearPlane = nearPlane;
  155. _bits |= CAMERA_DIRTY_PROJ | CAMERA_DIRTY_VIEW_PROJ | CAMERA_DIRTY_INV_VIEW_PROJ | CAMERA_DIRTY_BOUNDS;
  156. }
  157. float Camera::getFarPlane() const
  158. {
  159. return _farPlane;
  160. }
  161. void Camera::setFarPlane(float farPlane)
  162. {
  163. _farPlane = farPlane;
  164. _bits |= CAMERA_DIRTY_PROJ | CAMERA_DIRTY_VIEW_PROJ | CAMERA_DIRTY_INV_VIEW_PROJ | CAMERA_DIRTY_BOUNDS;
  165. }
  166. Node* Camera::getNode() const
  167. {
  168. return _node;
  169. }
  170. void Camera::setNode(Node* node)
  171. {
  172. if (_node != node)
  173. {
  174. if (_node)
  175. {
  176. _node->removeListener(this);
  177. }
  178. // Connect the new node.
  179. _node = node;
  180. if (_node)
  181. {
  182. _node->addListener(this);
  183. }
  184. _bits |= CAMERA_DIRTY_VIEW | CAMERA_DIRTY_VIEW_PROJ | CAMERA_DIRTY_INV_VIEW | CAMERA_DIRTY_INV_VIEW_PROJ | CAMERA_DIRTY_BOUNDS;
  185. }
  186. }
  187. const Matrix& Camera::getViewMatrix() const
  188. {
  189. if (_bits & CAMERA_DIRTY_VIEW)
  190. {
  191. if (_node)
  192. {
  193. // The view matrix is the inverse of our transform matrix.
  194. _node->getWorldMatrix().invert(&_view);
  195. }
  196. else
  197. {
  198. _view.setIdentity();
  199. }
  200. _bits &= ~CAMERA_DIRTY_VIEW;
  201. }
  202. return _view;
  203. }
  204. const Matrix& Camera::getInverseViewMatrix() const
  205. {
  206. if (_bits & CAMERA_DIRTY_INV_VIEW)
  207. {
  208. getViewMatrix().invert(&_inverseView);
  209. _bits &= ~CAMERA_DIRTY_INV_VIEW;
  210. }
  211. return _inverseView;
  212. }
  213. const Matrix& Camera::getProjectionMatrix() const
  214. {
  215. if (!(_bits & CAMERA_CUSTOM_PROJECTION) && (_bits & CAMERA_DIRTY_PROJ))
  216. {
  217. if (_type == PERSPECTIVE)
  218. {
  219. Matrix::createPerspective(_fieldOfView, _aspectRatio, _nearPlane, _farPlane, &_projection);
  220. }
  221. else
  222. {
  223. Matrix::createOrthographic(_zoom[0], _zoom[1], _nearPlane, _farPlane, &_projection);
  224. }
  225. _bits &= ~CAMERA_DIRTY_PROJ;
  226. }
  227. return _projection;
  228. }
  229. void Camera::setProjectionMatrix(const Matrix& matrix)
  230. {
  231. _projection = matrix;
  232. _bits |= CAMERA_CUSTOM_PROJECTION;
  233. _bits |= CAMERA_DIRTY_PROJ | CAMERA_DIRTY_VIEW_PROJ | CAMERA_DIRTY_INV_VIEW_PROJ | CAMERA_DIRTY_BOUNDS;
  234. }
  235. void Camera::resetProjectionMatrix()
  236. {
  237. if (_bits & CAMERA_CUSTOM_PROJECTION)
  238. {
  239. _bits &= ~CAMERA_CUSTOM_PROJECTION;
  240. _bits |= CAMERA_DIRTY_PROJ | CAMERA_DIRTY_VIEW_PROJ | CAMERA_DIRTY_INV_VIEW_PROJ | CAMERA_DIRTY_BOUNDS;
  241. }
  242. }
  243. const Matrix& Camera::getViewProjectionMatrix() const
  244. {
  245. if (_bits & CAMERA_DIRTY_VIEW_PROJ)
  246. {
  247. Matrix::multiply(getProjectionMatrix(), getViewMatrix(), &_viewProjection);
  248. _bits &= ~CAMERA_DIRTY_VIEW_PROJ;
  249. }
  250. return _viewProjection;
  251. }
  252. const Matrix& Camera::getInverseViewProjectionMatrix() const
  253. {
  254. if (_bits & CAMERA_DIRTY_INV_VIEW_PROJ)
  255. {
  256. getViewProjectionMatrix().invert(&_inverseViewProjection);
  257. _bits &= ~CAMERA_DIRTY_INV_VIEW_PROJ;
  258. }
  259. return _inverseViewProjection;
  260. }
  261. const Frustum& Camera::getFrustum() const
  262. {
  263. if (_bits & CAMERA_DIRTY_BOUNDS)
  264. {
  265. // Update our bounding frustum from our view projection matrix.
  266. _bounds.set(getViewProjectionMatrix());
  267. _bits &= ~CAMERA_DIRTY_BOUNDS;
  268. }
  269. return _bounds;
  270. }
  271. void Camera::project(const Rectangle& viewport, const Vector3& position, float* x, float* y, float* depth) const
  272. {
  273. GP_ASSERT(x);
  274. GP_ASSERT(y);
  275. // Transform the point to clip-space.
  276. Vector4 clipPos;
  277. getViewProjectionMatrix().transformVector(Vector4(position.x, position.y, position.z, 1.0f), &clipPos);
  278. // Compute normalized device coordinates.
  279. GP_ASSERT(clipPos.w != 0.0f);
  280. float ndcX = clipPos.x / clipPos.w;
  281. float ndcY = clipPos.y / clipPos.w;
  282. // Compute screen coordinates by applying our viewport transformation.
  283. *x = viewport.x + (ndcX + 1.0f) * 0.5f * viewport.width;
  284. *y = viewport.y + (1.0f - (ndcY + 1.0f) * 0.5f) * viewport.height;
  285. if (depth)
  286. {
  287. float ndcZ = clipPos.z / clipPos.w;
  288. *depth = ndcZ + 1.0f / 2.0f;
  289. }
  290. }
  291. void Camera::project(const Rectangle& viewport, const Vector3& position, Vector2* out) const
  292. {
  293. GP_ASSERT(out);
  294. float x, y;
  295. project(viewport, position, &x, &y);
  296. out->set(x, y);
  297. }
  298. void Camera::project(const Rectangle& viewport, const Vector3& position, Vector3* out) const
  299. {
  300. GP_ASSERT(out);
  301. float x, y, depth;
  302. project(viewport, position, &x, &y, &depth);
  303. out->set(x, y, depth);
  304. }
  305. void Camera::unproject(const Rectangle& viewport, float x, float y, float depth, Vector3* dst) const
  306. {
  307. GP_ASSERT(dst);
  308. // Create our screen space position in NDC.
  309. GP_ASSERT(viewport.width != 0.0f && viewport.height != 0.0f);
  310. Vector4 screen((x - viewport.x) / viewport.width, ((viewport.height - y) - viewport.y) / viewport.height, depth, 1.0f);
  311. // Map to range -1 to 1.
  312. screen.x = screen.x * 2.0f - 1.0f;
  313. screen.y = screen.y * 2.0f - 1.0f;
  314. screen.z = screen.z * 2.0f - 1.0f;
  315. // Transform the screen-space NDC by our inverse view projection matrix.
  316. getInverseViewProjectionMatrix().transformVector(screen, &screen);
  317. // Divide by our W coordinate.
  318. if (screen.w != 0.0f)
  319. {
  320. screen.x /= screen.w;
  321. screen.y /= screen.w;
  322. screen.z /= screen.w;
  323. }
  324. dst->set(screen.x, screen.y, screen.z);
  325. }
  326. void Camera::pickRay(const Rectangle& viewport, float x, float y, Ray* dst) const
  327. {
  328. GP_ASSERT(dst);
  329. // Get the world-space position at the near clip plane.
  330. Vector3 nearPoint;
  331. unproject(viewport, x, y, 0.0f, &nearPoint);
  332. // Get the world-space position at the far clip plane.
  333. Vector3 farPoint;
  334. unproject(viewport, x, y, 1.0f, &farPoint);
  335. // Set the direction of the ray.
  336. Vector3 direction;
  337. Vector3::subtract(farPoint, nearPoint, &direction);
  338. direction.normalize();
  339. dst->set(nearPoint, direction);
  340. }
  341. Camera* Camera::clone(NodeCloneContext &context) const
  342. {
  343. Camera* cameraClone = NULL;
  344. if (getCameraType() == PERSPECTIVE)
  345. {
  346. cameraClone = createPerspective(_fieldOfView, _aspectRatio, _nearPlane, _farPlane);
  347. }
  348. else if (getCameraType() == ORTHOGRAPHIC)
  349. {
  350. cameraClone = createOrthographic(getZoomX(), getZoomY(), getAspectRatio(), _nearPlane, _farPlane);
  351. }
  352. GP_ASSERT(cameraClone);
  353. if (Node* node = context.findClonedNode(getNode()))
  354. {
  355. cameraClone->setNode(node);
  356. }
  357. return cameraClone;
  358. }
  359. void Camera::transformChanged(Transform* transform, long cookie)
  360. {
  361. _bits |= CAMERA_DIRTY_VIEW | CAMERA_DIRTY_INV_VIEW | CAMERA_DIRTY_INV_VIEW_PROJ | CAMERA_DIRTY_VIEW_PROJ | CAMERA_DIRTY_BOUNDS;
  362. }
  363. }