Radium Engine  1.5.20
Loading...
Searching...
No Matches
Ra::Core::Asset::Camera Class Reference

Camera class storing the Camera frame and the projection properties The view direction is -z in camera space. More...

#include <Core/Asset/Camera.hpp>

+ Collaboration diagram for Ra::Core::Asset::Camera:

Public Types

enum class  ProjType { ORTHOGRAPHIC , PERSPECTIVE }
 Define the projection type. More...
 

Public Member Functions

 Camera (Scalar height=1_ra, Scalar width=1_ra)
 
Cameraoperator= (const Camera &rhs)
 
Core::Transform getFrame () const
 
void setFrame (const Core::Transform &frame)
 Set the frame of the camera to frame.
 
Core::Vector3 getPosition () const
 Return the position.
 
void setPosition (const Core::Vector3 &position)
 Set the position of the camera to position.
 
Core::Vector3 getDirection () const
 Return the direction the camera is looking at.
 
void setDirection (const Core::Vector3 &direction)
 
Core::Vector3 getUpVector () const
 Return the up vector.
 
void setUpVector (const Core::Vector3 &upVector)
 
Core::Vector3 getRightVector () const
 
Core::Ray getRayFromScreen (const Core::Vector2 &pix) const
 
Core::Vector3 projectToScreen (const Core::Vector3 &p) const
 Return the screen coordinates + depth of the given point p (in world coordinates).
 
Core::Vector3 projectToNDC (const Core::Vector3 &p) const
 Return NDC cordinate in the view NDC cube \( [-1,1]^3 \).
 
Core::Vector3 unProjectFromScreen (const Core::Vector2 &pix) const
 
Core::Vector3 unProjectFromScreen (const Core::Vector3 &pix) const
 
Core::Vector3 unProjectFromNDC (const Core::Vector3 &pix) const
 Return the 3D point in world space corresponding to NDC cube \( [-1,1]^3 \) point pix.
 
ProjType getType () const
 Return the projection type.
 
void setType (const ProjType &projectionType)
 Set the projection type to projectionType.
 
Scalar getZoomFactor () const
 Return the zoom factor.
 
void setZoomFactor (const Scalar &zoomFactor)
 Set the zoom factor to zoomFactor.
 
Scalar getFOV () const
 
void setFOV (Scalar fov)
 
Scalar getMinZNear () const
 
Scalar getMinZRange () const
 
Core::Matrix4 getProjMatrix () const
 Return the projection matrix.
 
Core::Matrix4 getViewMatrix () const
 
void updateProjMatrix ()
 Update the projection matrix according to the current parameters.
 
void setProjMatrix (Core::Matrix4 projMatrix)
 
Scalar getZNear () const
 Return the Z Near plane distance from the camera.
 
void setZNear (Scalar zNear)
 Set the Z Near plane distance to zNear.
 
Scalar getZFar () const
 Return the Z Far plane distance from the camera.
 
void setZFar (Scalar zFar)
 Set the Z Far plane distance to zFar.
 
Scalar getWidth () const
 Return the width of the viewport.
 
Scalar getHeight () const
 Return the height of the viewport.
 
Scalar getAspect () const
 Return the aspect ratio of the viewport.
 
void setXYmag (Scalar xmag, Scalar ymag)
 Set xmag and ymag for orthographic camera.
 
std::pair< Scalar, Scalar > getXYmag () const
 Get xmag and ymag for orthographic camera.
 
void setViewport (Scalar width, Scalar height)
 Change the viewport size. Also compute aspectRatio.
 
void applyTransform (const Core::Transform &T)
 
void fitZRange (const Core::Aabb &aabb)
 Fit the Z-range of the camera to the scene's aabb. This will maximize z-buffer precision.
 

Static Public Member Functions

static Core::Matrix4 perspective (Scalar a, Scalar y, Scalar n, Scalar f)
 
static Core::Matrix4 frustum (Scalar l, Scalar r, Scalar b, Scalar t, Scalar n, Scalar f)
 Build a projection matrix from the parameters of the view volume.
 
static Core::Matrix4 ortho (Scalar l, Scalar r, Scalar b, Scalar t, Scalar n, Scalar f)
 Build a projection matrix from the parameters of the view volume Implements the algorithms described here https://www.scratchapixel.com/lessons/3d-basic-rendering/perspective-and-orthographic-projection-matrix/orthographic-projection-matrix https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glOrtho.xml adapted to our representation of camera.
 

Public Attributes

const Scalar m_minZNear { 0.01_ra }
 
const Scalar m_minZRange { 0.01_ra }
 

Detailed Description

Camera class storing the Camera frame and the projection properties The view direction is -z in camera space.

Note
Both orthonormal and non-orthonormal frames are supported. However, in the current implementation suspect a bug in fitZRange which does not behave as expected for non-orthogonal frames..

Definition at line 15 of file Camera.hpp.

Member Enumeration Documentation

◆ ProjType

Define the projection type.

Definition at line 19 of file Camera.hpp.

Constructor & Destructor Documentation

◆ Camera()

Ra::Core::Asset::Camera::Camera ( Scalar height = 1_ra,
Scalar width = 1_ra )

Definition at line 15 of file Camera.cpp.

Member Function Documentation

◆ applyTransform()

void Ra::Core::Asset::Camera::applyTransform ( const Core::Transform & T)

Definition at line 57 of file Camera.cpp.

◆ fitZRange()

void Ra::Core::Asset::Camera::fitZRange ( const Core::Aabb & aabb)

Fit the Z-range of the camera to the scene's aabb. This will maximize z-buffer precision.

Definition at line 132 of file Camera.cpp.

+ Here is the call graph for this function:

◆ frustum()

Core::Matrix4 Ra::Core::Asset::Camera::frustum ( Scalar l,
Scalar r,
Scalar b,
Scalar t,
Scalar n,
Scalar f )
static

Build a projection matrix from the parameters of the view volume.

See also
perspective() Implements the algorithm described here https://www.scratchapixel.com/lessons/3d-basic-rendering/perspective-and-orthographic-projection-matrix/opengl-perspective-projection-matrix https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glOrtho.xml adapted to our representation of camera
Parameters
l: left
r: right
b: bottom
t: top
n: z near
f: z far

Definition at line 98 of file Camera.cpp.

◆ getAspect()

Scalar Ra::Core::Asset::Camera::getAspect ( ) const
inline

Return the aspect ratio of the viewport.

Definition at line 319 of file Camera.hpp.

◆ getDirection()

Core::Vector3 Ra::Core::Asset::Camera::getDirection ( ) const
inline

Return the direction the camera is looking at.

Definition at line 254 of file Camera.hpp.

◆ getFOV()

Scalar Ra::Core::Asset::Camera::getFOV ( ) const
inline

Return the horizontal Field Of View.

Note
Meaningless for orthogonal projection.

Definition at line 272 of file Camera.hpp.

◆ getFrame()

Core::Transform Ra::Core::Asset::Camera::getFrame ( ) const
inline

Return the frame of the camera. Where Y is the up vector and -Z is the direction vector.

Definition at line 236 of file Camera.hpp.

◆ getHeight()

Scalar Ra::Core::Asset::Camera::getHeight ( ) const
inline

Return the height of the viewport.

Definition at line 315 of file Camera.hpp.

◆ getMinZNear()

Scalar Ra::Core::Asset::Camera::getMinZNear ( ) const
inline

Definition at line 339 of file Camera.hpp.

◆ getMinZRange()

Scalar Ra::Core::Asset::Camera::getMinZRange ( ) const
inline

Definition at line 343 of file Camera.hpp.

◆ getPosition()

Core::Vector3 Ra::Core::Asset::Camera::getPosition ( ) const
inline

Return the position.

Definition at line 244 of file Camera.hpp.

◆ getProjMatrix()

Core::Matrix4 Ra::Core::Asset::Camera::getProjMatrix ( ) const
inline

Return the projection matrix.

Definition at line 335 of file Camera.hpp.

◆ getRayFromScreen()

Core::Ray Ra::Core::Asset::Camera::getRayFromScreen ( const Core::Vector2 & pix) const

Return a ray emanating from the camera, passing by the point given by screen coordinates x and y.

Definition at line 178 of file Camera.cpp.

◆ getRightVector()

Core::Vector3 Ra::Core::Asset::Camera::getRightVector ( ) const
inline

Definition at line 268 of file Camera.hpp.

◆ getType()

Camera::ProjType Ra::Core::Asset::Camera::getType ( ) const
inline

Return the projection type.

Definition at line 323 of file Camera.hpp.

◆ getUpVector()

Core::Vector3 Ra::Core::Asset::Camera::getUpVector ( ) const
inline

Return the up vector.

Definition at line 258 of file Camera.hpp.

◆ getViewMatrix()

Core::Matrix4 Ra::Core::Asset::Camera::getViewMatrix ( ) const
inline

Definition at line 331 of file Camera.hpp.

◆ getWidth()

Scalar Ra::Core::Asset::Camera::getWidth ( ) const
inline

Return the width of the viewport.

Definition at line 311 of file Camera.hpp.

◆ getXYmag()

std::pair< Scalar, Scalar > Ra::Core::Asset::Camera::getXYmag ( ) const
inline

Get xmag and ymag for orthographic camera.

Definition at line 359 of file Camera.hpp.

◆ getZFar()

Scalar Ra::Core::Asset::Camera::getZFar ( ) const
inline

Return the Z Far plane distance from the camera.

Definition at line 293 of file Camera.hpp.

◆ getZNear()

Scalar Ra::Core::Asset::Camera::getZNear ( ) const
inline

Return the Z Near plane distance from the camera.

Definition at line 284 of file Camera.hpp.

◆ getZoomFactor()

Scalar Ra::Core::Asset::Camera::getZoomFactor ( ) const
inline

Return the zoom factor.

Definition at line 302 of file Camera.hpp.

◆ operator=()

Camera & Ra::Core::Asset::Camera::operator= ( const Camera & rhs)

Definition at line 18 of file Camera.cpp.

◆ ortho()

Core::Matrix4 Ra::Core::Asset::Camera::ortho ( Scalar l,
Scalar r,
Scalar b,
Scalar t,
Scalar n,
Scalar f )
static

Build a projection matrix from the parameters of the view volume Implements the algorithms described here https://www.scratchapixel.com/lessons/3d-basic-rendering/perspective-and-orthographic-projection-matrix/orthographic-projection-matrix https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glOrtho.xml adapted to our representation of camera.

Parameters
l: left
r: right
b: bottom
t: top
n: z near
f: z far

Definition at line 116 of file Camera.cpp.

◆ perspective()

Core::Matrix4 Ra::Core::Asset::Camera::perspective ( Scalar a,
Scalar y,
Scalar n,
Scalar f )
static

Compute a project projection matrix as describe here https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#cameras adapted to horizontal fov equivalent to scale = tan(m_fov*.5)*m_zNear frustum(-n*tan(f/2), n*tan(f/2), -n*tan(f/2)/aspect, n*tan(f/2)/aspect, n, f);

Parameters
a: aspect ratio as width/height, i.e. getAspect()
y: fov in the horizontal direction, i.e. getFOV()
n: z near, i.e. getZNear()
f: z far, i.e. getZFar()

Definition at line 84 of file Camera.cpp.

+ Here is the call graph for this function:

◆ projectToNDC()

Core::Vector3 Ra::Core::Asset::Camera::projectToNDC ( const Core::Vector3 & p) const

Return NDC cordinate in the view NDC cube \( [-1,1]^3 \).

Definition at line 183 of file Camera.cpp.

◆ projectToScreen()

Core::Vector3 Ra::Core::Asset::Camera::projectToScreen ( const Core::Vector3 & p) const

Return the screen coordinates + depth of the given point p (in world coordinates).

Definition at line 191 of file Camera.cpp.

◆ setDirection()

void Ra::Core::Asset::Camera::setDirection ( const Core::Vector3 & direction)

Set the direction of the camera to direction. The other vectors will be rotated accordingly.

Definition at line 32 of file Camera.cpp.

◆ setFOV()

void Ra::Core::Asset::Camera::setFOV ( Scalar fov)
inline

Set the Field Of View to 'fov' in the x (horizontal) direction. If you have an vertical field of view, you can convert it to horizontal as

Scalar fovx = 2_ra * std::atan( radiumCam->getAspect() * std::tan( cam.yfov / 2_ra ) );
if ( fovxDiv2 < 0_ra ) { fovxDiv2 = Ra::Core::Math::PiDiv2; }
T atan(T... args)
T tan(T... args)
Note
Meaningless for orthogonal projection.
Warning
Trigger a rebuild of the projection matrix.

Definition at line 276 of file Camera.hpp.

+ Here is the call graph for this function:

◆ setFrame()

void Ra::Core::Asset::Camera::setFrame ( const Core::Transform & frame)
inline

Set the frame of the camera to frame.

Definition at line 240 of file Camera.hpp.

◆ setPosition()

void Ra::Core::Asset::Camera::setPosition ( const Core::Vector3 & position)
inline

Set the position of the camera to position.

Definition at line 248 of file Camera.hpp.

◆ setProjMatrix()

void Ra::Core::Asset::Camera::setProjMatrix ( Core::Matrix4 projMatrix)
inline

set projection to projMatrix.

See also
perspective(), ortho().

Definition at line 347 of file Camera.hpp.

◆ setType()

void Ra::Core::Asset::Camera::setType ( const ProjType & projectionType)
inline

Set the projection type to projectionType.

Definition at line 327 of file Camera.hpp.

◆ setUpVector()

void Ra::Core::Asset::Camera::setUpVector ( const Core::Vector3 & upVector)
inline

Set the up vector of the camera to upVector. The other vectors will be rotated accordingly.

Definition at line 262 of file Camera.hpp.

+ Here is the call graph for this function:

◆ setViewport()

void Ra::Core::Asset::Camera::setViewport ( Scalar width,
Scalar height )

Change the viewport size. Also compute aspectRatio.

Definition at line 50 of file Camera.cpp.

◆ setXYmag()

void Ra::Core::Asset::Camera::setXYmag ( Scalar xmag,
Scalar ymag )
inline

Set xmag and ymag for orthographic camera.

Definition at line 351 of file Camera.hpp.

+ Here is the call graph for this function:

◆ setZFar()

void Ra::Core::Asset::Camera::setZFar ( Scalar zFar)
inline

Set the Z Far plane distance to zFar.

Definition at line 297 of file Camera.hpp.

+ Here is the call graph for this function:

◆ setZNear()

void Ra::Core::Asset::Camera::setZNear ( Scalar zNear)
inline

Set the Z Near plane distance to zNear.

Definition at line 288 of file Camera.hpp.

+ Here is the call graph for this function:

◆ setZoomFactor()

void Ra::Core::Asset::Camera::setZoomFactor ( const Scalar & zoomFactor)
inline

Set the zoom factor to zoomFactor.

Definition at line 306 of file Camera.hpp.

+ Here is the call graph for this function:

◆ unProjectFromNDC()

Core::Vector3 Ra::Core::Asset::Camera::unProjectFromNDC ( const Core::Vector3 & pix) const

Return the 3D point in world space corresponding to NDC cube \( [-1,1]^3 \) point pix.

Definition at line 209 of file Camera.cpp.

◆ unProjectFromScreen() [1/2]

Core::Vector3 Ra::Core::Asset::Camera::unProjectFromScreen ( const Core::Vector2 & pix) const

Return the 3D point in world space corresponding to screen coordinates pix. pix.x() and pix.y() are in pixel coordinates \( \in [0, getWidth()] \times [0, getHeight()] \). The returned point lies on z near plane.

Definition at line 198 of file Camera.cpp.

◆ unProjectFromScreen() [2/2]

Core::Vector3 Ra::Core::Asset::Camera::unProjectFromScreen ( const Core::Vector3 & pix) const

Return the 3D point in world space corresponding to screen pixels pix.x(), pix.y(), at depth pix.z() x and y are in pixel coordinates (from (0,0) to width,height, z is in 0, 1 (0 near, 1 far plane)

Definition at line 202 of file Camera.cpp.

◆ updateProjMatrix()

void Ra::Core::Asset::Camera::updateProjMatrix ( )

Update the projection matrix according to the current parameters.

Definition at line 66 of file Camera.cpp.

Member Data Documentation

◆ m_minZNear

const Scalar Ra::Core::Asset::Camera::m_minZNear { 0.01_ra }

Definition at line 166 of file Camera.hpp.

◆ m_minZRange

const Scalar Ra::Core::Asset::Camera::m_minZRange { 0.01_ra }

Definition at line 167 of file Camera.hpp.


The documentation for this class was generated from the following files: