Camera.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #ifndef CAMERA_H
  2. #define CAMERA_H
  3. #include <boost/array.hpp>
  4. #include <deque>
  5. #include "Util/Vec.h"
  6. #include "Collision/Collision.h"
  7. #include "SceneNode.h"
  8. #include "Util/Accessors.h"
  9. #include "VisibilityInfo.h"
  10. class RenderableNode;
  11. class SpotLight;
  12. class PointLight;
  13. /// Camera SceneNode
  14. class Camera: public SceneNode, public VisibilityInfo
  15. {
  16. public:
  17. enum CameraType
  18. {
  19. CT_PERSPECTIVE,
  20. CT_ORTHOGRAPHIC,
  21. CT_NUM
  22. };
  23. enum FrustrumPlanes
  24. {
  25. FP_LEFT = 0,
  26. FP_RIGHT,
  27. FP_NEAR,
  28. FP_TOP,
  29. FP_BOTTOM,
  30. FP_FAR,
  31. FP_NUM
  32. };
  33. Camera(CameraType camType, bool inheritParentTrfFlag,
  34. SceneNode* parent);
  35. /// @name Accessors
  36. /// @{
  37. GETTER_R(CameraType, type, getType)
  38. float getZNear() const {return zNear;}
  39. void setZNear(float znear);
  40. float getZFar() const {return zFar;}
  41. void setZFar(float zfar);
  42. const Mat4& getProjectionMatrix() const {return projectionMat;}
  43. const Mat4& getViewMatrix() const {return viewMat;}
  44. /// See the declaration of invProjectionMat for info
  45. const Mat4& getInvProjectionMatrix() const {return invProjectionMat;}
  46. const Col::Plane& getWSpaceFrustumPlane(FrustrumPlanes id) const;
  47. /// @}
  48. void lookAtPoint(const Vec3& point);
  49. /// This does:
  50. /// - Update view matrix
  51. /// - Update frustum planes
  52. void moveUpdate();
  53. /// Do nothing
  54. void frameUpdate(float /*prevUpdateTime*/, float /*crntTime*/) {}
  55. /// Do nothing
  56. void init(const char*) {}
  57. /// @name Frustum checks
  58. /// @{
  59. /// Check if the given camera is inside the frustum clipping planes.
  60. /// This is used mainly to test if the projected lights are visible
  61. bool insideFrustum(const Col::CollisionShape& vol) const;
  62. /// Check if another camera is inside our view (used for projected
  63. /// lights)
  64. bool insideFrustum(const Camera& cam) const;
  65. /// @}
  66. protected:
  67. CameraType type;
  68. float zNear, zFar;
  69. /// @name The frustum planes in local and world space
  70. /// @{
  71. boost::array<Col::Plane, FP_NUM> lspaceFrustumPlanes;
  72. boost::array<Col::Plane, FP_NUM> wspaceFrustumPlanes;
  73. /// @}
  74. /// @name Matrices
  75. /// @{
  76. Mat4 projectionMat;
  77. Mat4 viewMat;
  78. /// Used in deferred shading for the calculation of view vector (see
  79. /// CalcViewVector). The reason we store this matrix here is that we
  80. /// don't want it to be re-calculated all the time but only when the
  81. /// projection params (fovX, fovY, zNear, zFar) change. Fortunately
  82. /// the projection params change rarely. Note that the Camera as we all
  83. /// know re-calculates the matrices only when the parameters change!!
  84. Mat4 invProjectionMat;
  85. /// @}
  86. /// Calculate projectionMat and invProjectionMat
  87. virtual void calcProjectionMatrix() = 0;
  88. virtual void calcLSpaceFrustumPlanes() = 0;
  89. void updateViewMatrix();
  90. void updateWSpaceFrustumPlanes();
  91. /// Get the edge points of the camera
  92. virtual void getExtremePoints(Vec3* pointsArr,
  93. uint& pointsNum) const = 0;
  94. };
  95. inline Camera::Camera(CameraType camType, bool inheritParentTrfFlag,
  96. SceneNode* parent)
  97. : SceneNode(SNT_CAMERA, inheritParentTrfFlag, parent),
  98. type(camType)
  99. {
  100. name = "Camera:" + name;
  101. }
  102. inline const Col::Plane& Camera::getWSpaceFrustumPlane(FrustrumPlanes id) const
  103. {
  104. return wspaceFrustumPlanes[id];
  105. }
  106. inline void Camera::setZNear(float znear_)
  107. {
  108. zNear = znear_;
  109. calcProjectionMatrix();
  110. calcLSpaceFrustumPlanes();
  111. }
  112. inline void Camera::setZFar(float zfar_)
  113. {
  114. zFar = zfar_;
  115. calcProjectionMatrix();
  116. calcLSpaceFrustumPlanes();
  117. }
  118. #endif