Radium Engine  1.5.20
Loading...
Searching...
No Matches
Camera.hpp
1#pragma once
2
3#include <Core/Math/Math.hpp>
4#include <Core/Types.hpp>
5#include <Core/Utils/Observable.hpp>
6namespace Ra {
7namespace Core {
8namespace Asset {
9
15class RA_CORE_API Camera
16{
17 public:
19 enum class ProjType { ORTHOGRAPHIC, PERSPECTIVE };
20
21 Camera( Scalar height = 1_ra, Scalar width = 1_ra );
22
23 ~Camera() = default;
24
25 Camera& operator=( const Camera& rhs );
26
29 inline Core::Transform getFrame() const;
30
32 inline void setFrame( const Core::Transform& frame );
33
35 inline Core::Vector3 getPosition() const;
36
38 inline void setPosition( const Core::Vector3& position );
39
41 inline Core::Vector3 getDirection() const;
42
45 void setDirection( const Core::Vector3& direction );
46
48 inline Core::Vector3 getUpVector() const;
49
52 inline void setUpVector( const Core::Vector3& upVector );
53
54 inline Core::Vector3 getRightVector() const;
55
56 //
57 // Utility functions
58 //
59
60 // Note : in all screen pixels coordinates function, Y is taken to be in standard UI-libs style
61 // coordinates, i.e. Y=0 is the top line and Y=height is the bottom line.
62
65 Core::Ray getRayFromScreen( const Core::Vector2& pix ) const;
66
68 Core::Vector3 projectToScreen( const Core::Vector3& p ) const;
69
71 Core::Vector3 projectToNDC( const Core::Vector3& p ) const;
72
76 Core::Vector3 unProjectFromScreen( const Core::Vector2& pix ) const;
77
82 Core::Vector3 unProjectFromScreen( const Core::Vector3& pix ) const;
83
85 Core::Vector3 unProjectFromNDC( const Core::Vector3& pix ) const;
86
87 //
88 // Getters and setters for projection matrix parameters.
89 //
90
92 inline ProjType getType() const;
93
95 inline void setType( const ProjType& projectionType );
96
98 inline Scalar getZoomFactor() const;
99
101 inline void setZoomFactor( const Scalar& zoomFactor );
102
105 inline Scalar getFOV() const;
106
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
236inline Core::Transform Camera::getFrame() const {
237 return m_frame;
238}
239
240inline void Camera::setFrame( const Core::Transform& frame ) {
241 m_frame = frame;
242}
243
244inline Core::Vector3 Camera::getPosition() const {
245 return ( m_frame.translation() );
246}
247
248inline 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
254inline Core::Vector3 Camera::getDirection() const {
255 return ( -m_frame.linear().block<3, 1>( 0, 2 ) ).normalized();
256}
257
258inline Core::Vector3 Camera::getUpVector() const {
259 return ( m_frame.affine().block<3, 1>( 0, 1 ) );
260}
261
262inline 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
268inline Core::Vector3 Camera::getRightVector() const {
269 return ( m_frame.affine().block<3, 1>( 0, 0 ) );
270}
271
272inline Scalar Camera::getFOV() const {
273 return m_fov;
274}
275
276inline 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
284inline Scalar Camera::getZNear() const {
285 return m_zNear;
286}
287
288inline void Camera::setZNear( Scalar zNear ) {
289 m_zNear = zNear;
291}
292
293inline Scalar Camera::getZFar() const {
294 return m_zFar;
295}
296
297inline void Camera::setZFar( Scalar zFar ) {
298 m_zFar = zFar;
300}
301
302inline Scalar Camera::getZoomFactor() const {
303 return m_zoomFactor;
304}
305
306inline void Camera::setZoomFactor( const Scalar& zoomFactor ) {
307 m_zoomFactor = zoomFactor;
309}
310
311inline Scalar Camera::getWidth() const {
312 return m_width;
313}
314
315inline Scalar Camera::getHeight() const {
316 return m_height;
317}
318
319inline Scalar Camera::getAspect() const {
320 return m_aspect;
321}
322
324 return m_projType;
325}
326
327inline void Camera::setType( const ProjType& projectionType ) {
328 m_projType = projectionType;
329}
330
331inline Core::Matrix4 Camera::getViewMatrix() const {
332 return getFrame().inverse().matrix();
333}
334
335inline Core::Matrix4 Camera::getProjMatrix() const {
336 return m_projMatrix;
337}
338
339inline Scalar Camera::getMinZNear() const {
340 return m_minZNear;
341}
342
343inline Scalar Camera::getMinZRange() const {
344 return m_minZRange;
345}
346
347inline void Camera::setProjMatrix( Core::Matrix4 projMatrix ) {
348 m_projMatrix = projMatrix;
349}
350
351inline 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
360 return { m_xmag, m_ymag };
361}
362
363} // namespace Asset
364} // namespace Core
365} // namespace Ra
T atan2(T... args)
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:19
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
hepler function to manage enum as underlying types in VariableSet
Definition Cage.cpp:3
T tan(T... args)