Camera.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. #ifndef CAMERA_H_
  2. #define CAMERA_H_
  3. #include "Ref.h"
  4. #include "Transform.h"
  5. #include "Frustum.h"
  6. #include "Rectangle.h"
  7. namespace gameplay
  8. {
  9. class Node;
  10. class NodeCloneContext;
  11. /**
  12. * Defines a camera which acts as a view of a scene to be rendered.
  13. */
  14. class Camera : public Ref, public Transform::Listener
  15. {
  16. friend class Node;
  17. public:
  18. /**
  19. * The type of camera.
  20. */
  21. enum Type
  22. {
  23. PERSPECTIVE = 1,
  24. ORTHOGRAPHIC = 2
  25. };
  26. /**
  27. * Creates a perspective camera.
  28. *
  29. * @param fieldOfView The field of view for the perspective camera (normally in the range of 40-60 degrees).
  30. * @param aspectRatio The aspect ratio of the camera (normally the width of the viewport divided by the height of the viewport).
  31. * @param nearPlane The near plane distance.
  32. * @param farPlane The far plane distance.
  33. */
  34. static Camera* createPerspective(float fieldOfView, float aspectRatio, float nearPlane, float farPlane);
  35. /**
  36. * Creates an orthographic camera.
  37. *
  38. * @param zoomX The zoom factor along the X-axis of the orthographic projection (the width of the ortho projection).
  39. * @param zoomY The zoom factor along the Y-axis of the orthographic projection (the height of the ortho projection).
  40. * @param aspectRatio The aspect ratio of the orthographic projection.
  41. * @param nearPlane The near plane distance.
  42. * @param farPlane The far plane distance.
  43. */
  44. static Camera* createOrthographic(float zoomX, float zoomY, float aspectRatio, float nearPlane, float farPlane);
  45. /**
  46. * Gets the type of camera.
  47. *
  48. * @return The camera type.
  49. */
  50. Camera::Type getCameraType() const;
  51. /**
  52. * Gets the field of view for a perspective camera.
  53. *
  54. * @return The field of view.
  55. */
  56. float getFieldOfView() const;
  57. /**
  58. * Sets the field of view.
  59. *
  60. * @param fieldOfView The field of view.
  61. */
  62. void setFieldOfView(float fieldOfView);
  63. /**
  64. * Gets the x-zoom (magnification) for an orthographic camera.
  65. * Default is 1.0f.
  66. *
  67. * @return The magnification (zoom) for x.
  68. */
  69. float getZoomX() const;
  70. /**
  71. * Sets the x-zoom (magnification) for a orthographic camera.
  72. * Default is 1.0f.
  73. *
  74. * @param zoomX The magnification (zoom) for x.
  75. */
  76. void setZoomX(float zoomX);
  77. /**
  78. * Gets the y-zoom (magnification) for a orthographic camera.
  79. * Default is 1.0f.
  80. *
  81. * @return The magnification (zoom) for y.
  82. */
  83. float getZoomY() const;
  84. /**
  85. * Sets the y-zoom (magnification) for a orthographic camera.
  86. *
  87. * @param zoomY The magnification (zoom) for y.
  88. */
  89. void setZoomY(float zoomY);
  90. /**
  91. * Gets the aspect ratio.
  92. *
  93. * @return The aspect ratio.
  94. */
  95. float getAspectRatio() const;
  96. /**
  97. * Sets the aspect ratio.
  98. *
  99. * @param aspectRatio The aspect ratio.
  100. */
  101. void setAspectRatio(float aspectRatio);
  102. /**
  103. * Gets the near z clipping plane distance.
  104. *
  105. * @return The near z clipping plane distance.
  106. */
  107. float getNearPlane() const;
  108. /**
  109. * Sets the near z clipping plane distance.
  110. *
  111. * @param nearPlane The near z clipping plane distance.
  112. */
  113. void setNearPlane(float nearPlane);
  114. /**
  115. * Gets the far z clipping plane distance.
  116. *
  117. * @return The far z clipping plane distance.
  118. */
  119. float getFarPlane() const;
  120. /**
  121. * Sets the far z clipping plane distance.
  122. *
  123. * @param farPlane The far z clipping plane distance.
  124. */
  125. void setFarPlane(float farPlane);
  126. /**
  127. * Gets the node that this camera is attached to.
  128. *
  129. * @return The node that this camera is attached to.
  130. */
  131. Node* getNode() const;
  132. /**
  133. * Gets the camera's view matrix.
  134. *
  135. * @return The camera view matrix.
  136. */
  137. const Matrix& getViewMatrix() const;
  138. /**
  139. * Gets the camera's inverse view matrix.
  140. *
  141. * @return The camera inverse view matrix.
  142. */
  143. const Matrix& getInverseViewMatrix() const;
  144. /**
  145. * Gets the camera's projection matrix.
  146. *
  147. * @return The camera projection matrix.
  148. */
  149. const Matrix& getProjectionMatrix() const;
  150. /**
  151. * Gets the camera's view * projection matrix.
  152. *
  153. * @return The camera view * projection matrix.
  154. */
  155. const Matrix& getViewProjectionMatrix() const;
  156. /**
  157. * Gets the camera's inverse view * projection matrix.
  158. *
  159. * @return The camera inverse view * projection matrix.
  160. */
  161. const Matrix& getInverseViewProjectionMatrix() const;
  162. /**
  163. * Gets the view bounding frustum.
  164. *
  165. * @return The viewing bounding frustum.
  166. */
  167. const Frustum& getFrustum() const;
  168. /**
  169. * Projects the specified world position into the viewport coordinates.
  170. *
  171. * @param viewport The viewport rectangle to use.
  172. * @param position The world space position.
  173. * @param x The returned viewport x coordinate.
  174. * @param y The returned viewport y coordinate.
  175. * @param depth The returned pixel depth (can be NULL).
  176. */
  177. void project(const Rectangle& viewport, const Vector3& position, float* x, float* y, float* depth = NULL) const;
  178. /**
  179. * Converts a viewport-space coordinate to a world-space position for the given depth value.
  180. *
  181. * The depth parameter is a value ranging between 0 and 1, where 0 returns a point on the
  182. * near clipping plane and 1 returns a point on the far clipping plane.
  183. *
  184. * @param viewport The viewport rectangle to use.
  185. * @param x The viewport-space x coordinate.
  186. * @param y The viewport-space y coordinate.
  187. * @param depth The depth range.
  188. * @param dst The world space position.
  189. */
  190. void unproject(const Rectangle& viewport, float x, float y, float depth, Vector3* dst) const;
  191. /**
  192. * Picks a ray that can be used for picking given the specified viewport-space coordinates.
  193. *
  194. * @param viewport The viewport rectangle to use.
  195. * @param x The viewport x-coordinate.
  196. * @param y The viewport y-coordinate.
  197. * @param dst The computed pick ray.
  198. */
  199. void pickRay(const Rectangle& viewport, float x, float y, Ray* dst) const;
  200. private:
  201. /**
  202. * Constructor.
  203. */
  204. Camera(float fieldOfView, float aspectRatio, float nearPlane, float farPlane);
  205. /**
  206. * Constructor.
  207. */
  208. Camera(float zoomX, float zoomY, float aspectRatio, float nearPlane, float farPlane);
  209. /**
  210. * Destructor.
  211. */
  212. virtual ~Camera();
  213. /**
  214. * Hidden copy assignment operator.
  215. */
  216. Camera& operator=(const Camera&);
  217. /**
  218. * Clones the camera and returns a new camera.
  219. *
  220. * @param context The clone context.
  221. * @return The newly created camera.
  222. */
  223. Camera* clone(NodeCloneContext &context) const;
  224. /**
  225. * @see Transform::Listener::transformChanged
  226. */
  227. void transformChanged(Transform* transform, long cookie);
  228. /**
  229. * Sets the node associated with this camera.
  230. */
  231. void setNode(Node* node);
  232. Camera::Type _type;
  233. float _fieldOfView;
  234. float _zoom[2];
  235. float _aspectRatio;
  236. float _nearPlane;
  237. float _farPlane;
  238. mutable Matrix _view;
  239. mutable Matrix _projection;
  240. mutable Matrix _viewProjection;
  241. mutable Matrix _inverseView;
  242. mutable Matrix _inverseViewProjection;
  243. mutable Frustum _bounds;
  244. mutable int _dirtyBits;
  245. Node* _node;
  246. };
  247. }
  248. #endif