Camera.h 8.2 KB

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