lmgl 1.0.0
A lightweight OpenGL graphics engine library written in C++
Loading...
Searching...
No Matches
lmgl::scene::Camera Class Reference

Represents a camera for viewing the 3D scene. More...

#include <camera.hpp>

Public Types

enum class  ProjectionMode { Perspective , Orthographic }
 Projection mode for the camera.
 

Public Member Functions

 Camera (float fov=45.0f, float aspect=16.0f/9.0f, float near=0.1f, float far=100.0f)
 Constructor for the Camera class.
 
void set_perspective (float fov, float aspect, float near, float far)
 Set the camera to perspective projection mode.
 
void set_orthographic (float left, float right, float bottom, float top, float near, float far)
 Set the camera to orthographic projection mode.
 
void set_position (const glm::vec3 &position)
 Set the camera position.
 
void set_target (const glm::vec3 &target)
 Set the camera target/look-at point.
 
void set_up (const glm::vec3 &up)
 Set the camera's up vector.
 
const glm::vec3 & get_position () const
 Get the camera position.
 
const glm::vec3 & get_target () const
 Get the camera target.
 
const glm::vec3 & get_up () const
 Get the camera up vector.
 
const glm::mat4 & get_view_matrix () const
 Get the view matrix.
 
const glm::mat4 & get_projection_matrix () const
 Get the projection matrix.
 
glm::mat4 get_view_projection_matrix () const
 Get the combined view-projection matrix.
 
glm::vec3 unproject (float screen_x, float screen_y, float screen_width, float screen_height) const
 Unproject screen coordinates to world space.
 
ProjectionMode get_projection_mode () const
 Get the current projection mode.
 
float get_aspect () const
 Get the current aspect ratio.
 
void set_aspect (float aspect)
 Set a new aspect ratio.
 

Detailed Description

Represents a camera for viewing the 3D scene.

The Camera class provides methods to configure perspective or orthographic projection, compute view matrices, and perform screen-to-world unprojection for tasks like mouse picking.

Constructor & Destructor Documentation

◆ Camera()

lmgl::scene::Camera::Camera ( float fov = 45.0f,
float aspect = 16.0f / 9.0f,
float near = 0.1f,
float far = 100.0f )

Constructor for the Camera class.

Initializes the camera with default perspective settings.

Parameters
fovField of view in degrees (default: 45.0).
aspectAspect ratio (width/height) (default: 16.0/9.0).
nearNear clipping plane (default: 0.1).
farFar clipping plane (default: 100.0).

Member Function Documentation

◆ get_aspect()

float lmgl::scene::Camera::get_aspect ( ) const
inline

Get the current aspect ratio.

Returns
The current aspect ratio.

◆ get_position()

const glm::vec3 & lmgl::scene::Camera::get_position ( ) const
inline

Get the camera position.

Returns
Current position of the camera.

◆ get_projection_matrix()

const glm::mat4 & lmgl::scene::Camera::get_projection_matrix ( ) const
inline

Get the projection matrix.

Returns the current projection matrix (perspective or orthographic).

Returns
Projection matrix.

◆ get_projection_mode()

ProjectionMode lmgl::scene::Camera::get_projection_mode ( ) const
inline

Get the current projection mode.

Returns
Current projection mode (Perspective or Orthographic).

◆ get_target()

const glm::vec3 & lmgl::scene::Camera::get_target ( ) const
inline

Get the camera target.

Returns
Current target/look-at point.

◆ get_up()

const glm::vec3 & lmgl::scene::Camera::get_up ( ) const
inline

Get the camera up vector.

Returns
Current up direction vector.

◆ get_view_matrix()

const glm::mat4 & lmgl::scene::Camera::get_view_matrix ( ) const

Get the view matrix.

Returns the view matrix computed using glm::lookAt. The matrix is cached and only recomputed when necessary.

Returns
View matrix.

◆ get_view_projection_matrix()

glm::mat4 lmgl::scene::Camera::get_view_projection_matrix ( ) const

Get the combined view-projection matrix.

Returns the product of projection and view matrices.

Returns
View-projection matrix.

◆ set_aspect()

void lmgl::scene::Camera::set_aspect ( float aspect)

Set a new aspect ratio.

Updates the aspect ratio and recalculates the projection matrix if the camera is in perspective mode.

Parameters
aspectThe new aspect ratio

◆ set_orthographic()

void lmgl::scene::Camera::set_orthographic ( float left,
float right,
float bottom,
float top,
float near,
float far )

Set the camera to orthographic projection mode.

Configures the camera to use orthographic projection with the specified bounds.

Parameters
leftLeft boundary.
rightRight boundary.
bottomBottom boundary.
topTop boundary.
nearNear clipping plane.
farFar clipping plane.

◆ set_perspective()

void lmgl::scene::Camera::set_perspective ( float fov,
float aspect,
float near,
float far )

Set the camera to perspective projection mode.

Configures the camera to use perspective projection with the specified parameters.

Parameters
fovField of view in degrees.
aspectAspect ratio (width/height).
nearNear clipping plane.
farFar clipping plane.

◆ set_position()

void lmgl::scene::Camera::set_position ( const glm::vec3 & position)
inline

Set the camera position.

Updates the position of the camera in world space.

Parameters
positionNew position vector.

◆ set_target()

void lmgl::scene::Camera::set_target ( const glm::vec3 & target)
inline

Set the camera target/look-at point.

Updates the point the camera is looking at.

Parameters
targetTarget position vector.

◆ set_up()

void lmgl::scene::Camera::set_up ( const glm::vec3 & up)
inline

Set the camera's up vector.

Updates the up direction for the camera orientation.

Parameters
upUp direction vector (default: (0, 1, 0)).

◆ unproject()

glm::vec3 lmgl::scene::Camera::unproject ( float screen_x,
float screen_y,
float screen_width,
float screen_height ) const

Unproject screen coordinates to world space.

Converts 2D screen coordinates to a 3D ray in world space, useful for mouse picking and raycasting.

Parameters
screen_xScreen X coordinate.
screen_yScreen Y coordinate.
screen_widthWidth of the viewport.
screen_heightHeight of the viewport.
Returns
Ray direction in world space (normalized).

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