Skip to main content

Camera Class

Represents a camera for perspective or orthographic projection. More...

Declaration

class helios::scene::Camera { ... }

Public Constructors Index

Camera () noexcept

Constructs a camera with identity projection and view matrices. More...

Public Member Functions Index

const helios::math::mat4f &projectionMatrix () const noexcept

Gets the current projection matrix. More...

const helios::math::mat4f &viewMatrix () const noexcept

Gets the current view matrix. More...

Camera &setViewMatrix (const helios::math::mat4f &viewMatrix) noexcept

Sets the view matrix for this camera. More...

Camera &setAspectRatio (float aspectRatio) noexcept

Sets the aspect ratio used by the camera. More...

voidonResize (const float width, const float height) noexcept

Handles viewport resize events. More...

Camera &setPerspective (float fovY, float aspectRatio, float zNear, float zFar) noexcept

Sets the perspective projection parameters. More...

Camera &setOrtho (const float left, const float right, const float bottom, const float top, const float zNear=-1.0f, const float zFar=100.0f) noexcept

Sets the orthographic projection parameters. More...

Camera &setZNear (float zNear) noexcept

Sets the near clipping plane distance. More...

Camera &setZFar (float zFar) noexcept

Sets the far clipping plane distance. More...

Camera &setFovY (float fovY) noexcept

Sets the vertical field of view. More...

floatfovY () const noexcept

Gets the current vertical field of view. More...

floataspectRatio () const noexcept

Gets the current aspect ratio. More...

floatzNear () const noexcept

Gets the current near clipping plane distance. More...

floatzFar () const noexcept

Gets the current far clipping plane distance. More...

Protected Member Functions Index

voidupdate () const

Updates the view and projection matrices if needed. More...

Protected Member Attributes Index

helios::math::mat4fperspectiveMatrix_

The projection matrix of this camera. More...

helios::math::mat4forthographicMatrix_

The orthographic projection matrix of this camera. More...

helios::math::mat4fviewMatrix_

The view matrix of this camera. More...

floataspectRatio_ = 1.0f

The aspect ratio of the camera (width/height). More...

floatzNear_ = 0.1f

The near clipping plane distance. More...

floatzFar_ = 1000.0f

The far clipping plane distance. More...

floatfovY_ = helios::math::radians(90)

The vertical field of view in radians. More...

boolneedsUpdate_ = true

Flag indicating whether the matrices need to be updated. More...

floatleft_

Left boundary of the orthographic frustum. More...

floatright_

Right boundary of the orthographic frustum. More...

floatbottom_

Bottom boundary of the orthographic frustum. More...

floattop_

Top boundary of the orthographic frustum. More...

boolusePerspective_ = true

Flag indicating the projection mode. More...

Description

Represents a camera for perspective or orthographic projection.

The camera provides the view and projection matrices, whereas the view matrix is computed as the inverse of the camera's world transform.

The camera supports two projection modes:

  • **Perspective projection:** Simulates how the human eye perceives depth, where distant objects appear smaller. Use `setPerspective()` to configure.
  • **Orthographic projection:** Maintains parallel lines and uniform scaling regardless of distance, useful for 2D rendering, UI elements, or CAD-style views. Use `setOrtho()` to configure.

By default, the camera uses perspective projection.

Example usage: ```cpp helios::scene::Camera camera;

// Perspective projection camera.setPerspective(helios::math::radians(60.0f), 16.0f / 9.0f, 0.1f, 1000.0f);

// Orthographic projection camera.setOrtho(0.0f, 800.0f, 0.0f, 600.0f, -1.0f, 100.0f); ```

Definition at line 44 of file Camera.ixx.

Public Constructors

Camera()

helios::scene::Camera::Camera ()
inline noexcept

Constructs a camera with identity projection and view matrices.

Definition at line 150 of file Camera.ixx.

150 Camera() noexcept :
151 perspectiveMatrix_(helios::math::mat4f::identity()),
152 orthographicMatrix_(helios::math::mat4f::identity()),
153 viewMatrix_(helios::math::mat4f::identity())
154 {}

References orthographicMatrix_, perspectiveMatrix_ and viewMatrix_.

Referenced by setAspectRatio, setFovY, setOrtho, setPerspective, setViewMatrix, setZFar and setZNear.

Public Member Functions

aspectRatio()

float helios::scene::Camera::aspectRatio ()
inline nodiscard noexcept

Gets the current aspect ratio.

Returns

The aspect ratio (width/height).

Definition at line 362 of file Camera.ixx.

362 [[nodiscard]] float aspectRatio() const noexcept {
363 return aspectRatio_;
364 }

Reference aspectRatio_.

Referenced by setAspectRatio and setPerspective.

fovY()

float helios::scene::Camera::fovY ()
inline nodiscard noexcept

Gets the current vertical field of view.

Returns

The vertical field of view in radians.

Definition at line 353 of file Camera.ixx.

353 [[nodiscard]] float fovY() const noexcept {
354 return fovY_;
355 }

Reference fovY_.

Referenced by setFovY and setPerspective.

onResize()

void helios::scene::Camera::onResize (const float width, const float height)
inline noexcept

Handles viewport resize events.

Adjusts the camera projection parameters when the viewport dimensions change.

  • For perspective projection: Updates the aspect ratio.
  • For orthographic projection: Adjusts the right and top boundaries to maintain the correct projection volume.
Parameters
width

The new viewport width in pixels.

height

The new viewport height in pixels.

Definition at line 225 of file Camera.ixx.

225 void onResize(const float width, const float height) noexcept {
226
227 if (usePerspective_) {
228 setAspectRatio(width / height);
229 return;
230 }
231
232 if (left_ == 0.0f && bottom_ == 0.0f) {
233 right_ = width;
234 top_ = height;
235 } else {
236
237 left_ = -width/2.0f;
238 right_ = width/2.0f;
239 bottom_ = -height/2.0f;
240 top_ = height/2.0f;
241 }
242
243 needsUpdate_ = true;
244 }

References bottom_, left_, needsUpdate_, right_, setAspectRatio, top_ and usePerspective_.

projectionMatrix()

const helios::math::mat4f & helios::scene::Camera::projectionMatrix ()
inline nodiscard noexcept

Gets the current projection matrix.

Typically set to a perspective or orthographic projection.

Returns

A const reference to this camera's projection matrix.

Definition at line 163 of file Camera.ixx.

163 [[nodiscard]] const helios::math::mat4f& projectionMatrix() const noexcept {
164 update();
165
166 if (usePerspective_) {
167 return perspectiveMatrix_;
168 }
170 }

References orthographicMatrix_, perspectiveMatrix_, update and usePerspective_.

setAspectRatio()

Camera & helios::scene::Camera::setAspectRatio (float aspectRatio)
inline noexcept

Sets the aspect ratio used by the camera.

Parameters
aspectRatio

The new aspect ratio (width/height).

Returns

A reference to this camera instance.

Definition at line 208 of file Camera.ixx.

209 needsUpdate_ = true;
211 return *this;
212 }

References aspectRatio, aspectRatio_, Camera and needsUpdate_.

Referenced by onResize.

setFovY()

Camera & helios::scene::Camera::setFovY (float fovY)
inline noexcept

Sets the vertical field of view.

Parameters
fovY

The new vertical field of view in radians.

Returns

A reference to this camera instance.

Definition at line 342 of file Camera.ixx.

342 Camera& setFovY(float fovY) noexcept {
343 fovY_ = fovY;
344 needsUpdate_ = true;
345 return *this;
346 }

References Camera, fovY, fovY_ and needsUpdate_.

setOrtho()

Camera & helios::scene::Camera::setOrtho (const float left, const float right, const float bottom, const float top, const float zNear=-1.0f, const float zFar=100.0f)
inline noexcept

Sets the orthographic projection parameters.

Configures the camera for orthographic projection, which is useful for 2D rendering, UI overlays, or scenes where parallel lines should remain parallel (no perspective distortion).

Parameters
left

The left boundary of the view volume.

right

The right boundary of the view volume.

bottom

The bottom boundary of the view volume.

top

The top boundary of the view volume.

zNear

The near clipping plane distance. Defaults to -1.0.

zFar

The far clipping plane distance. Defaults to 100.0.

Returns

A reference to this camera instance.

info

Calling this method switches the camera to orthographic projection mode.

Definition at line 286 of file Camera.ixx.

287 const float left , const float right,
288 const float bottom, const float top,
289 const float zNear = -1.0f, const float zFar = 100.0f) noexcept {
290
291 assert(zFar > zNear && "zFar must be greater than zNear");
292
293 zNear_ = zNear;
294 zFar_ = zFar;
295
296 left_ = left;
297 right_ = right;
298 bottom_ = bottom;
299 top_ = top;
300
301 aspectRatio_ = (right - left) / (top - bottom); // w/h
302
303 usePerspective_ = false;
304
305 needsUpdate_ = true;
306 return *this;
307 }

References aspectRatio_, bottom_, Camera, left_, needsUpdate_, right_, top_, usePerspective_, zFar, zFar_, zNear and zNear_.

setPerspective()

Camera & helios::scene::Camera::setPerspective (float fovY, float aspectRatio, float zNear, float zFar)
inline noexcept

Sets the perspective projection parameters.

Parameters
fovY

The vertical field of view in radians.

aspectRatio

The aspect ratio (width/height).

zNear

The near clipping plane distance.

zFar

The far clipping plane distance.

Returns

A reference to this camera instance.

Definition at line 256 of file Camera.ixx.

256 Camera& setPerspective(float fovY, float aspectRatio, float zNear, float zFar) noexcept {
257 assert(zNear > 0 && "zNear must be positive");
258 assert(zFar > 0 && zFar > zNear && "zFar must be positive and greater than zNear");
259 fovY_ = fovY;
261 zNear_ = zNear;
262 zFar_ = zFar;
263 needsUpdate_ = true;
264 usePerspective_ = true;
265 return *this;
266 }

References aspectRatio, aspectRatio_, Camera, fovY, fovY_, needsUpdate_, usePerspective_, zFar, zFar_, zNear and zNear_.

setViewMatrix()

Camera & helios::scene::Camera::setViewMatrix (const helios::math::mat4f & viewMatrix)
inline noexcept

Sets the view matrix for this camera.

This method is typically called by `CameraSceneNode::worldTransform()` to update the view matrix based on the camera node's position and orientation in the scene graph.

Parameters
viewMatrix

The new view matrix to assign.

Returns

A const reference to this camera instance.

See Also

helios::scene::CameraSceneNode::worldTransform()

Definition at line 196 of file Camera.ixx.

198 return *this;
199 }

References Camera, viewMatrix and viewMatrix_.

setZFar()

Camera & helios::scene::Camera::setZFar (float zFar)
inline noexcept

Sets the far clipping plane distance.

Parameters
zFar

The new far clipping plane distance.

Returns

A reference to this camera instance.

Definition at line 329 of file Camera.ixx.

329 Camera& setZFar(float zFar) noexcept {
330 zFar_ = zFar;
331 needsUpdate_ = true;
332 return *this;
333 }

References Camera, needsUpdate_, zFar and zFar_.

setZNear()

Camera & helios::scene::Camera::setZNear (float zNear)
inline noexcept

Sets the near clipping plane distance.

Parameters
zNear

The new near clipping plane distance.

Returns

A reference to this camera instance.

Definition at line 316 of file Camera.ixx.

316 Camera& setZNear(float zNear) noexcept {
317 zNear_ = zNear;
318 needsUpdate_ = true;
319 return *this;
320 }

References Camera, needsUpdate_, zNear and zNear_.

viewMatrix()

const helios::math::mat4f & helios::scene::Camera::viewMatrix ()
inline nodiscard noexcept

Gets the current view matrix.

The view matrix represents the inverse of the camera's world transform.

Returns

A const reference to this camera's view matrix.

Definition at line 180 of file Camera.ixx.

180 [[nodiscard]] const helios::math::mat4f& viewMatrix() const noexcept {
181 return viewMatrix_;
182 }

Reference viewMatrix_.

Referenced by setViewMatrix.

zFar()

float helios::scene::Camera::zFar ()
inline nodiscard noexcept

Gets the current far clipping plane distance.

Returns

The far clipping plane distance.

Definition at line 380 of file Camera.ixx.

380 [[nodiscard]] float zFar() const noexcept {
381 return zFar_;
382 }

Reference zFar_.

Referenced by setOrtho, setPerspective and setZFar.

zNear()

float helios::scene::Camera::zNear ()
inline nodiscard noexcept

Gets the current near clipping plane distance.

Returns

The near clipping plane distance.

Definition at line 371 of file Camera.ixx.

371 [[nodiscard]] float zNear() const noexcept {
372 return zNear_;
373 }

Reference zNear_.

Referenced by setOrtho, setPerspective and setZNear.

Protected Member Functions

update()

void helios::scene::Camera::update ()
inline protected

Updates the view and projection matrices if needed.

This function recalculates the view and projection matrices based on the current camera parameters.

Definition at line 131 of file Camera.ixx.

131 void update() const {
132
133 if (!needsUpdate_) {
134 return;
135 }
136
137 if (usePerspective_) {
139 } else {
141 }
142
143 needsUpdate_ = false;
144 }

References aspectRatio_, bottom_, fovY_, left_, needsUpdate_, helios::math::ortho, orthographicMatrix_, helios::math::perspective, perspectiveMatrix_, right_, top_, usePerspective_, zFar_ and zNear_.

Referenced by projectionMatrix.

Protected Member Attributes

aspectRatio_

float helios::scene::Camera::aspectRatio_ = 1.0f
protected

The aspect ratio of the camera (width/height).

Defaults to 1.0.

Definition at line 69 of file Camera.ixx.

69 float aspectRatio_ = 1.0f;

Referenced by aspectRatio, setAspectRatio, setOrtho, setPerspective and update.

bottom_

float helios::scene::Camera::bottom_
protected

Bottom boundary of the orthographic frustum.

Definition at line 110 of file Camera.ixx.

110 float bottom_;

Referenced by onResize, setOrtho and update.

fovY_

float helios::scene::Camera::fovY_ = helios::math::radians(90)
protected

The vertical field of view in radians.

Defaults to 90 degrees (converted to radians).

Definition at line 90 of file Camera.ixx.

Referenced by fovY, setFovY, setPerspective and update.

left_

float helios::scene::Camera::left_
protected

Left boundary of the orthographic frustum.

Definition at line 100 of file Camera.ixx.

100 float left_;

Referenced by onResize, setOrtho and update.

needsUpdate_

bool helios::scene::Camera::needsUpdate_ = true
protected mutable

Flag indicating whether the matrices need to be updated.

Definition at line 95 of file Camera.ixx.

95 mutable bool needsUpdate_ = true;

Referenced by onResize, setAspectRatio, setFovY, setOrtho, setPerspective, setZFar, setZNear and update.

orthographicMatrix_

helios::math::mat4f helios::scene::Camera::orthographicMatrix_
protected mutable

The orthographic projection matrix of this camera.

Used when the camera is in orthographic mode (`usePerspective_ == false`).

Definition at line 57 of file Camera.ixx.

Referenced by Camera, projectionMatrix and update.

perspectiveMatrix_

helios::math::mat4f helios::scene::Camera::perspectiveMatrix_
protected mutable

The projection matrix of this camera.

Definition at line 50 of file Camera.ixx.

Referenced by Camera, projectionMatrix and update.

right_

float helios::scene::Camera::right_
protected

Right boundary of the orthographic frustum.

Definition at line 105 of file Camera.ixx.

105 float right_;

Referenced by onResize, setOrtho and update.

top_

float helios::scene::Camera::top_
protected

Top boundary of the orthographic frustum.

Definition at line 115 of file Camera.ixx.

115 float top_;

Referenced by onResize, setOrtho and update.

usePerspective_

bool helios::scene::Camera::usePerspective_ = true
protected

Flag indicating the projection mode.

When `true`, perspective projection is used. When `false`, orthographic projection is used. Defaults to `true`.

Definition at line 123 of file Camera.ixx.

123 bool usePerspective_ = true;

Referenced by onResize, projectionMatrix, setOrtho, setPerspective and update.

viewMatrix_

helios::math::mat4f helios::scene::Camera::viewMatrix_
protected

The view matrix of this camera.

Definition at line 62 of file Camera.ixx.

Referenced by Camera, setViewMatrix and viewMatrix.

zFar_

float helios::scene::Camera::zFar_ = 1000.0f
protected

The far clipping plane distance.

Defaults to 1000.0.

Definition at line 83 of file Camera.ixx.

83 float zFar_ = 1000.0f;

Referenced by setOrtho, setPerspective, setZFar, update and zFar.

zNear_

float helios::scene::Camera::zNear_ = 0.1f
protected

The near clipping plane distance.

Defaults to 0.1.

Definition at line 76 of file Camera.ixx.

76 float zNear_ = 0.1f;

Referenced by setOrtho, setPerspective, setZNear, update and zNear.


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


Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.15.0.