3 #include <Core/Math/Math.hpp>
5 #include <Core/Utils/Observable.hpp>
18 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
21 enum class ProjType { ORTHOGRAPHIC, PERSPECTIVE };
23 Camera( Scalar height = 1_ra, Scalar width = 1_ra );
31 inline Core::Transform getFrame()
const;
34 inline void setFrame(
const Core::Transform& frame );
37 inline Core::Vector3 getPosition()
const;
40 inline void setPosition(
const Core::Vector3& position );
43 inline Core::Vector3 getDirection()
const;
47 void setDirection(
const Core::Vector3& direction );
50 inline Core::Vector3 getUpVector()
const;
54 inline void setUpVector(
const Core::Vector3& upVector );
56 inline Core::Vector3 getRightVector()
const;
67 Core::Ray getRayFromScreen(
const Core::Vector2& pix )
const;
70 Core::Vector3 projectToScreen(
const Core::Vector3& p )
const;
73 Core::Vector3 projectToNDC(
const Core::Vector3& p )
const;
78 Core::Vector3 unProjectFromScreen(
const Core::Vector2& pix )
const;
84 Core::Vector3 unProjectFromScreen(
const Core::Vector3& pix )
const;
87 Core::Vector3 unProjectFromNDC(
const Core::Vector3& pix )
const;
94 inline ProjType getType()
const;
97 inline void setType(
const ProjType& projectionType );
100 inline Scalar getZoomFactor()
const;
103 inline void setZoomFactor(
const Scalar& zoomFactor );
107 inline Scalar getFOV()
const;
115 inline void setFOV( Scalar fov );
117 inline Scalar getMinZNear()
const;
118 inline Scalar getMinZRange()
const;
121 inline Core::Matrix4 getProjMatrix()
const;
122 inline Core::Matrix4 getViewMatrix()
const;
125 void updateProjMatrix();
129 inline void setProjMatrix( Core::Matrix4 projMatrix );
132 inline Scalar getZNear()
const;
135 inline void setZNear( Scalar zNear );
138 inline Scalar getZFar()
const;
141 inline void setZFar( Scalar zFar );
144 inline Scalar getWidth()
const;
147 inline Scalar getHeight()
const;
150 inline Scalar getAspect()
const;
153 inline void setXYmag( Scalar xmag, Scalar ymag );
156 inline std::pair<Scalar, Scalar> getXYmag()
const;
159 void setViewport( Scalar width, Scalar height );
161 void applyTransform(
const Core::Transform& T );
164 void fitZRange(
const Core::Aabb& aabb );
166 const Scalar m_minZNear { 0.01_ra };
167 const Scalar m_minZRange { 0.01_ra };
178 static Core::Matrix4 perspective( Scalar a, Scalar y, Scalar n, Scalar f );
192 static Core::Matrix4 frustum( Scalar l, Scalar r, Scalar b, Scalar t, Scalar n, Scalar f );
205 static Core::Matrix4 ortho( Scalar l, Scalar r, Scalar b, Scalar t, Scalar n, Scalar f );
210 Core::Transform m_frame { Core::Transform::Identity() };
212 Core::Matrix4 m_projMatrix { Core::Matrix4::Identity() };
214 Scalar m_width { 1_ra };
215 Scalar m_height { 1_ra };
219 ProjType m_projType { ProjType::PERSPECTIVE };
220 Scalar m_zoomFactor { 1_ra };
221 Scalar m_zNear { 0.1_ra };
222 Scalar m_zFar { 1000_ra };
226 Scalar m_fov { Core::Math::PiDiv4 };
231 Scalar m_xmag { 1_ra };
232 Scalar m_ymag { 1_ra };
245 return ( m_frame.translation() );
249 Core::Transform T = Core::Transform::Identity();
250 T.translation() = position - m_frame.translation();
255 return ( -m_frame.linear().block<3, 1>( 0, 2 ) ).normalized();
259 return ( m_frame.affine().block<3, 1>( 0, 1 ) );
263 Core::Transform T = Core::Transform::Identity();
264 T.rotate( Core::Quaternion::FromTwoVectors(
getUpVector(), upVector ) );
268 inline Core::Vector3 Camera::getRightVector()
const {
269 return ( m_frame.affine().block<3, 1>( 0, 0 ) );
279 m_xmag = 2_ra * std::tan( m_fov / 2_ra );
307 m_zoomFactor = zoomFactor;
328 m_projType = projectionType;
331 inline Core::Matrix4 Camera::getViewMatrix()
const {
332 return getFrame().inverse().matrix();
339 inline Scalar Camera::getMinZNear()
const {
343 inline Scalar Camera::getMinZRange()
const {
348 m_projMatrix = projMatrix;
355 m_fov = std::atan2( m_xmag * 2, 1_ra );
360 return { m_xmag, m_ymag };
Camera class storing the Camera frame and the projection properties The view direction is -z in camer...
Scalar getZFar() const
Return the Z Far plane distance from the camera.
void updateProjMatrix()
Update the projection matrix according to the current parameters.
ProjType
Define the projection type.
Scalar getWidth() const
Return the width of the viewport.
void setPosition(const Core::Vector3 &position)
Set the position of the camera to position.
Scalar getHeight() const
Return the height of the viewport.
void setZNear(Scalar zNear)
Set the Z Near plane distance to zNear.
void setZFar(Scalar zFar)
Set the Z Far plane distance to zFar.
Core::Vector3 getPosition() const
Return the position.
void setZoomFactor(const Scalar &zoomFactor)
Set the zoom factor to zoomFactor.
Core::Vector3 getUpVector() const
Return the up vector.
Scalar getZNear() const
Return the Z Near plane distance from the camera.
void setProjMatrix(Core::Matrix4 projMatrix)
void setUpVector(const Core::Vector3 &upVector)
Core::Matrix4 getProjMatrix() const
Return the projection matrix.
void setType(const ProjType &projectionType)
Set the projection type to projectionType.
Scalar getZoomFactor() const
Return the zoom factor.
void setFrame(const Core::Transform &frame)
Set the frame of the camera to frame.
ProjType getType() const
Return the projection type.
Scalar getAspect() const
Return the aspect ratio of the viewport.
Core::Transform getFrame() const
void setXYmag(Scalar xmag, Scalar ymag)
Set xmag and ymag for orthographic camera.
Core::Vector3 getDirection() const
Return the direction the camera is looking at.
std::pair< Scalar, Scalar > getXYmag() const
Get xmag and ymag for orthographic camera.