Radium Engine  1.5.0
Camera.hpp
1 #pragma once
2 
3 #include <Core/Math/Math.hpp>
4 #include <Core/Types.hpp>
5 #include <Core/Utils/Observable.hpp>
6 namespace Ra {
7 namespace Core {
8 namespace Asset {
9 
15 class RA_CORE_API Camera
16 {
17  public:
18  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
19 
21  enum class ProjType { ORTHOGRAPHIC, PERSPECTIVE };
22 
23  Camera( Scalar height = 1_ra, Scalar width = 1_ra );
24 
25  ~Camera() = default;
26 
27  Camera& operator=( const Camera& rhs );
28 
31  inline Core::Transform getFrame() const;
32 
34  inline void setFrame( const Core::Transform& frame );
35 
37  inline Core::Vector3 getPosition() const;
38 
40  inline void setPosition( const Core::Vector3& position );
41 
43  inline Core::Vector3 getDirection() const;
44 
47  void setDirection( const Core::Vector3& direction );
48 
50  inline Core::Vector3 getUpVector() const;
51 
54  inline void setUpVector( const Core::Vector3& upVector );
55 
56  inline Core::Vector3 getRightVector() const;
57 
58  //
59  // Utility functions
60  //
61 
62  // Note : in all screen pixels coordinates function, Y is taken to be in standard UI-libs style
63  // coordinates, i.e. Y=0 is the top line and Y=height is the bottom line.
64 
67  Core::Ray getRayFromScreen( const Core::Vector2& pix ) const;
68 
70  Core::Vector3 projectToScreen( const Core::Vector3& p ) const;
71 
73  Core::Vector3 projectToNDC( const Core::Vector3& p ) const;
74 
78  Core::Vector3 unProjectFromScreen( const Core::Vector2& pix ) const;
79 
84  Core::Vector3 unProjectFromScreen( const Core::Vector3& pix ) const;
85 
87  Core::Vector3 unProjectFromNDC( const Core::Vector3& pix ) const;
88 
89  //
90  // Getters and setters for projection matrix parameters.
91  //
92 
94  inline ProjType getType() const;
95 
97  inline void setType( const ProjType& projectionType );
98 
100  inline Scalar getZoomFactor() const;
101 
103  inline void setZoomFactor( const Scalar& zoomFactor );
104 
107  inline Scalar getFOV() const;
108 
112  // if ( fovxDiv2 < 0_ra ) { fovxDiv2 = Ra::Core::Math::PiDiv2; }
115  inline void setFOV( Scalar fov );
116 
117  inline Scalar getMinZNear() const;
118  inline Scalar getMinZRange() const;
119 
121  inline Core::Matrix4 getProjMatrix() const;
122  inline Core::Matrix4 getViewMatrix() const;
123 
125  void updateProjMatrix();
126 
129  inline void setProjMatrix( Core::Matrix4 projMatrix );
130 
132  inline Scalar getZNear() const;
133 
135  inline void setZNear( Scalar zNear );
136 
138  inline Scalar getZFar() const;
139 
141  inline void setZFar( Scalar zFar );
142 
144  inline Scalar getWidth() const;
145 
147  inline Scalar getHeight() const;
148 
150  inline Scalar getAspect() const;
151 
153  inline void setXYmag( Scalar xmag, Scalar ymag );
154 
156  inline std::pair<Scalar, Scalar> getXYmag() const;
157 
159  void setViewport( Scalar width, Scalar height );
160 
161  void applyTransform( const Core::Transform& T );
162 
164  void fitZRange( const Core::Aabb& aabb );
165 
166  const Scalar m_minZNear { 0.01_ra };
167  const Scalar m_minZRange { 0.01_ra };
168 
178  static Core::Matrix4 perspective( Scalar a, Scalar y, Scalar n, Scalar f );
179 
192  static Core::Matrix4 frustum( Scalar l, Scalar r, Scalar b, Scalar t, Scalar n, Scalar f );
193 
205  static Core::Matrix4 ortho( Scalar l, Scalar r, Scalar b, Scalar t, Scalar n, Scalar f );
206 
207  private:
210  Core::Transform m_frame { Core::Transform::Identity() };
211 
212  Core::Matrix4 m_projMatrix { Core::Matrix4::Identity() };
213 
214  Scalar m_width { 1_ra };
215  Scalar m_height { 1_ra };
216  Scalar m_aspect {
217  1_ra };
218 
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 };
223 
226  Scalar m_fov { Core::Math::PiDiv4 };
228 
231  Scalar m_xmag { 1_ra };
232  Scalar m_ymag { 1_ra };
234 };
235 
236 inline Core::Transform Camera::getFrame() const {
237  return m_frame;
238 }
239 
240 inline void Camera::setFrame( const Core::Transform& frame ) {
241  m_frame = frame;
242 }
243 
244 inline Core::Vector3 Camera::getPosition() const {
245  return ( m_frame.translation() );
246 }
247 
248 inline void Camera::setPosition( const Core::Vector3& position ) {
249  Core::Transform T = Core::Transform::Identity();
250  T.translation() = position - m_frame.translation();
251  applyTransform( T );
252 }
253 
254 inline Core::Vector3 Camera::getDirection() const {
255  return ( -m_frame.linear().block<3, 1>( 0, 2 ) ).normalized();
256 }
257 
258 inline Core::Vector3 Camera::getUpVector() const {
259  return ( m_frame.affine().block<3, 1>( 0, 1 ) );
260 }
261 
262 inline void Camera::setUpVector( const Core::Vector3& upVector ) {
263  Core::Transform T = Core::Transform::Identity();
264  T.rotate( Core::Quaternion::FromTwoVectors( getUpVector(), upVector ) );
265  applyTransform( T );
266 }
267 
268 inline Core::Vector3 Camera::getRightVector() const {
269  return ( m_frame.affine().block<3, 1>( 0, 0 ) );
270 }
271 
272 inline Scalar Camera::getFOV() const {
273  return m_fov;
274 }
275 
276 inline void Camera::setFOV( Scalar fov ) {
277  m_fov = fov;
278  // update m_xmag and m_ymag according to the perspective parameters (heuristic)
279  m_xmag = 2_ra * std::tan( m_fov / 2_ra );
280  m_ymag = m_xmag;
282 }
283 
284 inline Scalar Camera::getZNear() const {
285  return m_zNear;
286 }
287 
288 inline void Camera::setZNear( Scalar zNear ) {
289  m_zNear = zNear;
291 }
292 
293 inline Scalar Camera::getZFar() const {
294  return m_zFar;
295 }
296 
297 inline void Camera::setZFar( Scalar zFar ) {
298  m_zFar = zFar;
300 }
301 
302 inline Scalar Camera::getZoomFactor() const {
303  return m_zoomFactor;
304 }
305 
306 inline void Camera::setZoomFactor( const Scalar& zoomFactor ) {
307  m_zoomFactor = zoomFactor;
309 }
310 
311 inline Scalar Camera::getWidth() const {
312  return m_width;
313 }
314 
315 inline Scalar Camera::getHeight() const {
316  return m_height;
317 }
318 
319 inline Scalar Camera::getAspect() const {
320  return m_aspect;
321 }
322 
324  return m_projType;
325 }
326 
327 inline void Camera::setType( const ProjType& projectionType ) {
328  m_projType = projectionType;
329 }
330 
331 inline Core::Matrix4 Camera::getViewMatrix() const {
332  return getFrame().inverse().matrix();
333 }
334 
335 inline Core::Matrix4 Camera::getProjMatrix() const {
336  return m_projMatrix;
337 }
338 
339 inline Scalar Camera::getMinZNear() const {
340  return m_minZNear;
341 }
342 
343 inline Scalar Camera::getMinZRange() const {
344  return m_minZRange;
345 }
346 
347 inline void Camera::setProjMatrix( Core::Matrix4 projMatrix ) {
348  m_projMatrix = projMatrix;
349 }
350 
351 inline void Camera::setXYmag( Scalar xmag, Scalar ymag ) {
352  m_xmag = xmag;
353  m_ymag = ymag;
354  // update m_fov according to orthographic parameters (heuristic)
355  m_fov = std::atan2( m_xmag * 2, 1_ra );
357 }
358 
359 inline std::pair<Scalar, Scalar> Camera::getXYmag() const {
360  return { m_xmag, m_ymag };
361 }
362 
363 } // namespace Asset
364 } // namespace Core
365 } // namespace Ra
Camera class storing the Camera frame and the projection properties The view direction is -z in camer...
Definition: Camera.hpp:16
Scalar getZFar() const
Return the Z Far plane distance from the camera.
Definition: Camera.hpp:293
void updateProjMatrix()
Update the projection matrix according to the current parameters.
Definition: Camera.cpp:66
ProjType
Define the projection type.
Definition: Camera.hpp:21
Scalar getWidth() const
Return the width of the viewport.
Definition: Camera.hpp:311
void setPosition(const Core::Vector3 &position)
Set the position of the camera to position.
Definition: Camera.hpp:248
Scalar getHeight() const
Return the height of the viewport.
Definition: Camera.hpp:315
void setZNear(Scalar zNear)
Set the Z Near plane distance to zNear.
Definition: Camera.hpp:288
void setZFar(Scalar zFar)
Set the Z Far plane distance to zFar.
Definition: Camera.hpp:297
Core::Vector3 getPosition() const
Return the position.
Definition: Camera.hpp:244
void setZoomFactor(const Scalar &zoomFactor)
Set the zoom factor to zoomFactor.
Definition: Camera.hpp:306
Core::Vector3 getUpVector() const
Return the up vector.
Definition: Camera.hpp:258
Scalar getZNear() const
Return the Z Near plane distance from the camera.
Definition: Camera.hpp:284
void setProjMatrix(Core::Matrix4 projMatrix)
Definition: Camera.hpp:347
void setUpVector(const Core::Vector3 &upVector)
Definition: Camera.hpp:262
Core::Matrix4 getProjMatrix() const
Return the projection matrix.
Definition: Camera.hpp:335
void setType(const ProjType &projectionType)
Set the projection type to projectionType.
Definition: Camera.hpp:327
Scalar getZoomFactor() const
Return the zoom factor.
Definition: Camera.hpp:302
void setFrame(const Core::Transform &frame)
Set the frame of the camera to frame.
Definition: Camera.hpp:240
Scalar getFOV() const
Definition: Camera.hpp:272
ProjType getType() const
Return the projection type.
Definition: Camera.hpp:323
Scalar getAspect() const
Return the aspect ratio of the viewport.
Definition: Camera.hpp:319
void setFOV(Scalar fov)
Definition: Camera.hpp:276
Core::Transform getFrame() const
Definition: Camera.hpp:236
void setXYmag(Scalar xmag, Scalar ymag)
Set xmag and ymag for orthographic camera.
Definition: Camera.hpp:351
Core::Vector3 getDirection() const
Return the direction the camera is looking at.
Definition: Camera.hpp:254
std::pair< Scalar, Scalar > getXYmag() const
Get xmag and ymag for orthographic camera.
Definition: Camera.hpp:359
Definition: Cage.cpp:3