Camera.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. #ifndef ANKI_SCENE_CAMERA_H
  2. #define ANKI_SCENE_CAMERA_H
  3. #include "anki/scene/SceneNode.h"
  4. #include "anki/scene/SpatialComponent.h"
  5. #include "anki/scene/MoveComponent.h"
  6. #include "anki/scene/FrustumComponent.h"
  7. namespace anki {
  8. /// @addtogroup Scene
  9. /// @{
  10. /// Camera SceneNode interface class
  11. class Camera: public SceneNode, public MoveComponent, public SpatialComponent,
  12. public FrustumComponent
  13. {
  14. public:
  15. /// @note Don't EVER change the order
  16. enum CameraType
  17. {
  18. CT_PERSPECTIVE,
  19. CT_ORTHOGRAPHIC,
  20. CT_COUNT
  21. };
  22. /// @name Constructors/Destructor
  23. /// @{
  24. Camera(
  25. const char* name, SceneGraph* scene, // SceneNode
  26. Frustum* frustum, // Spatial & Frustumable
  27. CameraType type); // Self
  28. virtual ~Camera();
  29. /// @}
  30. /// @name Accessors
  31. /// @{
  32. CameraType getCameraType() const
  33. {
  34. return type;
  35. }
  36. const Mat4& getInverseProjectionMatrix() const
  37. {
  38. return invProjectionMat;
  39. }
  40. /// Needed by the renderer
  41. virtual F32 getNear() const = 0;
  42. /// Needed by the renderer
  43. virtual F32 getFar() const = 0;
  44. /// @}
  45. /// @name Frustumable virtuals
  46. /// @{
  47. /// Override Frustumable::getFrustumableOrigin()
  48. const Vec3& getFrustumOrigin() const
  49. {
  50. return getWorldTransform().getOrigin();
  51. }
  52. /// @}
  53. /// @name Spatial virtuals
  54. /// @{
  55. /// Override Spatial::getSpatialOrigin
  56. const Vec3& getSpatialOrigin() const
  57. {
  58. return getWorldTransform().getOrigin();
  59. }
  60. /// @}
  61. void lookAtPoint(const Vec3& point);
  62. protected:
  63. /// Used in deferred shading for the calculation of view vector (see
  64. /// CalcViewVector). The reason we store this matrix here is that we
  65. /// don't want it to be re-calculated all the time but only when the
  66. /// projection params (fovX, fovY, zNear, zFar) change. Fortunately
  67. /// the projection params change rarely. Note that the Camera as we all
  68. /// know re-calculates the matrices only when the parameters change!!
  69. Mat4 invProjectionMat = Mat4::getIdentity();
  70. /// Calculate the @a viewMat. The view matrix is the inverse world
  71. /// transformation
  72. void updateViewMatrix()
  73. {
  74. viewMat = Mat4(getWorldTransform().getInverse());
  75. }
  76. void updateViewProjectionMatrix()
  77. {
  78. viewProjectionMat = projectionMat * viewMat;
  79. }
  80. private:
  81. CameraType type;
  82. };
  83. /// Perspective camera
  84. class PerspectiveCamera: public Camera
  85. {
  86. public:
  87. /// @name Constructors
  88. /// @{
  89. PerspectiveCamera(const char* name, SceneGraph* scene);
  90. /// @}
  91. /// @name Accessors
  92. /// @{
  93. F32 getNear() const
  94. {
  95. return frustum.getNear();
  96. }
  97. F32 getFar() const
  98. {
  99. return frustum.getFar();
  100. }
  101. F32 getFovX() const
  102. {
  103. return frustum.getFovX();
  104. }
  105. void setFovX(F32 x)
  106. {
  107. frustum.setFovX(x);
  108. frustumUpdate();
  109. }
  110. F32 getFovY() const
  111. {
  112. return frustum.getFovY();
  113. }
  114. void setFovY(F32 x)
  115. {
  116. frustum.setFovY(x);
  117. frustumUpdate();
  118. }
  119. void setAll(F32 fovX_, F32 fovY_, F32 near_, F32 far_)
  120. {
  121. frustum.setAll(fovX_, fovY_, near_, far_);
  122. frustumUpdate();
  123. }
  124. /// @}
  125. /// @name Movable virtuals
  126. /// @{
  127. /// Overrides Movable::moveUpdate(). This does:
  128. /// @li Update view matrix
  129. /// @li Update view-projection matrix
  130. /// @li Move the frustum
  131. void moveUpdate()
  132. {
  133. updateViewMatrix();
  134. updateViewProjectionMatrix();
  135. frustum.setTransform(getWorldTransform());
  136. SpatialComponent::markForUpdate();
  137. }
  138. /// @}
  139. private:
  140. PerspectiveFrustum frustum;
  141. /// Called when something changes in the frustum
  142. void frustumUpdate()
  143. {
  144. projectionMat = frustum.calculateProjectionMatrix();
  145. invProjectionMat = projectionMat.getInverse();
  146. updateViewProjectionMatrix();
  147. SpatialComponent::markForUpdate();
  148. FrustumComponent::markForUpdate();
  149. }
  150. };
  151. /// Orthographic camera
  152. class OrthographicCamera: public Camera
  153. {
  154. public:
  155. /// @name Constructors
  156. /// @{
  157. OrthographicCamera(const char* name, SceneGraph* scene);
  158. /// @}
  159. /// @name Accessors
  160. /// @{
  161. F32 getNear() const
  162. {
  163. return frustum.getNear();
  164. }
  165. F32 getFar() const
  166. {
  167. return frustum.getFar();
  168. }
  169. F32 getLeft() const
  170. {
  171. return frustum.getLeft();
  172. }
  173. F32 getRight() const
  174. {
  175. return frustum.getRight();
  176. }
  177. F32 getBottom() const
  178. {
  179. return frustum.getBottom();
  180. }
  181. F32 getTop() const
  182. {
  183. return frustum.getTop();
  184. }
  185. void setAll(F32 left, F32 right, F32 near, F32 far, F32 top, F32 bottom)
  186. {
  187. frustum.setAll(left, right, near, far, top, bottom);
  188. frustumUpdate();
  189. }
  190. /// @}
  191. /// @name Movable virtuals
  192. /// @{
  193. /// Overrides Movable::moveUpdate(). This does:
  194. /// @li Update view matrix
  195. /// @li Update view-projection matrix
  196. /// @li Update frustum
  197. void moveUpdate()
  198. {
  199. ANKI_ASSERT(0 && "TODO");
  200. }
  201. /// @}
  202. private:
  203. OrthographicFrustum frustum;
  204. /// Called when something changes in the frustum
  205. void frustumUpdate()
  206. {
  207. ANKI_ASSERT(0 && "TODO");
  208. }
  209. };
  210. /// @}
  211. } // end namespace anki
  212. #endif