SleakEngine 1.0.0
C++23 multi-backend game engine
Loading...
Searching...
No Matches
Sleak::Camera Class Reference

#include <Camera.hpp>

Inheritance diagram for Sleak::Camera:

Public Member Functions

 Camera (std::string name="Camera", Math::Vector3D Position=Math::Vector3D(0, 0, -3.5), float fov=60, float near=1.0f, float far=1000.0f)
void AddDirection (const Math::Vector3D &direction)
void AddLookTarget (const Math::Vector3D &target)
void AddPosition (const Math::Vector3D &position)
Math::Vector3D GetDirection () const
float GetFarPlane () const
float GetFieldOfView () const
Math::Vector3D GetLookTarget () const
float GetNearPlane () const
Math::Vector3D GetPosition () const
ProjectionType GetProjectionType () const
Math::Vector3D GetUp () const
void Initialize () override
 Initializes the object and its components; called once before the first Update.
void OnResize (uint32_t width, uint32_t height)
 Updates the viewport dimensions and recomputes the projection matrix.
void SetDirection (const Math::Vector3D &direction)
void SetFarPlane (float farPlaneValue)
void SetFieldOfView (float fov)
void SetLookTarget (const Math::Vector3D &target)
void SetNearPlane (float nearPlaneValue)
void SetPosition (const Math::Vector3D &position)
void SetProjectionType (ProjectionType type)
void SetUp (const Math::Vector3D &up)
void Update (float DeltaTime) override
 Recalculates the view/projection matrices and frustum while active.
Public Member Functions inherited from Sleak::GameObject
 GameObject (const std::string &name="GameObject")
 ~GameObject () override
void AddChild (GameObject *child)
template<typename T, typename... Args>
void AddComponent (Args &&... args)
 Constructs and attaches a component of type T. Warns and no-ops if one already exists.
virtual void FixedUpdate (float fixedDeltaTime)
const List< GameObject * > & GetChildren () const
template<typename T>
T * GetComponent ()
 Finds the first attached component of type T, or nullptr.
GameObjectGetParent () const
const std::string & GetTag () const
bool HasChildren () const
template<typename T>
bool HasComponent ()
 True if a component of type T is attached.
bool HasParent () const
bool IsActive () const
virtual bool IsLight () const
bool IsPendingDestroy () const
virtual void LateUpdate (float deltaTime)
void MarkForDestroy ()
 Flags the object for deferred destruction on the next scene pass.
void RemoveChild (GameObject *child)
template<typename T>
void RemoveComponent ()
 Destroys and detaches the first component of type T, if present.
void SetActive (bool active)
 Enables or disables the object, firing OnEnable/OnDisable on its components.
void SetParent (GameObject *parent)
 Reparents this object, updating both the old and new parent's child lists.
void SetTag (const std::string &tag)
Public Member Functions inherited from Sleak::Object
 Object (const std::string &name="Object")
virtual ~Object ()=default
const std::string & GetName () const
uint64_t GetUniqueID () const
void SetName (const std::string &value)

Static Public Member Functions

static const Math::Vector3DGetMainCameraPosition ()
static const Math::Matrix4GetMainProjectionMatrix ()
static const ViewFrustumGetMainViewFrustum ()
static const Math::Matrix4GetMainViewMatrix ()
Static Public Member Functions inherited from Sleak::GameObject
static GameObjectCreateCapsule (Math::Vector3D position, int segments=16, int rings=8, float height=1, float radius=0.5)
static GameObjectCreateCube (Math::Vector3D position)
static GameObjectCreateCylinder (Math::Vector3D position, int segments=16, float height=1, float radius=0.5)
static GameObjectCreatePlane (Math::Vector3D position, int width=100, int height=100)
 Built-in primitive factories, mainly for prototyping and debug scenes.
static GameObjectCreateSphere (Math::Vector3D position, int stack=16, int slices=16)
static GameObjectCreateTorus (Math::Vector3D position, int segments=16, int rings=8, float innerRadius=8, float outerRadius=9)

Protected Member Functions

void RecalculateProjectionMatrix ()
 Rebuilds the static projection matrix from FOV/aspect/near/far or the orthographic extents.
void RecalculateViewMatrix ()
 Rebuilds the static view matrix and view frustum from position/target/up, and begins culling for the frame.

Protected Attributes

float farPlane
float fieldOfView
float height
Math::Vector3D LookTarget
float nearPlane
Math::Vector3D Position
ProjectionType type = ProjectionType::Perspective
Math::Vector3D Up = Math::Vector3D::Up()
float width
Protected Attributes inherited from Sleak::GameObject
bool bIsInitialized

Static Protected Attributes

static Math::Vector3D MainPosition = Math::Vector3D(0, 0, 0)
static Math::Matrix4 Projection = Math::Matrix4::Identity()
static ViewFrustum s_frustum
static Math::Matrix4 View = Math::Matrix4::Identity()

Detailed Description

GameObject that owns the engine's single active view/projection matrices and view frustum. Position/orientation come from CameraController subclasses.

Create one, add it to the scene, and call SceneBase::SetActiveCamera() so the renderer knows which view to draw from. Because Camera is a GameObject, it also carries components: attach a FreeLookCameraController for a debug or spectator view, or a FirstPersonController plus a ColliderComponent and RigidbodyComponent for a player character that collides with the world.

The view and projection matrices are static, shared state. The active camera rewrites them each Update() through GetMainViewMatrix(), GetMainProjectionMatrix(), GetMainCameraPosition(), and GetMainViewFrustum(), which is how the renderer and CullingSystem read the current view. Only one camera should be active at a time.

Orientation is expressed as a look target rather than a rotation. SetLookTarget() aims at a world point and SetDirection() aims along a vector; GetDirection() returns the normalized facing.

auto* cam = new Sleak::Camera(
"MainCamera",
Sleak::Math::Vector3D(-5.0f, 3.0f, -5.0f),
/*fov=*/60.0f, /*near=*/0.01f, /*far=*/200.0f);
cam->SetLookTarget(Sleak::Math::Vector3D(0.0f, 0.0f, 0.0f));
cam->AddComponent<Sleak::FreeLookCameraController>();
cam->Initialize();
if (auto* ctrl =
cam->GetComponent<Sleak::FreeLookCameraController>()) {
ctrl->SetEnabled(true);
}
AddObject(cam);
SetActiveCamera(cam);
bool Initialize() override
Derives initial yaw/pitch from the camera's current facing direction.
See also
CameraController, FreeLookCameraController, FirstPersonController, ViewFrustum, CullingSystem

Definition at line 59 of file Camera.hpp.

Constructor & Destructor Documentation

◆ Camera()

Sleak::Camera::Camera ( std::string name = "Camera",
Math::Vector3D Position = Math::Vector3D(0, 0, -3.5),
float fov = 60,
float near = 1.0f,
float far = 1000.0f )

Definition at line 14 of file Camera.cpp.

Member Function Documentation

◆ AddDirection()

void Sleak::Camera::AddDirection ( const Math::Vector3D & direction)
inline

Definition at line 86 of file Camera.hpp.

◆ AddLookTarget()

void Sleak::Camera::AddLookTarget ( const Math::Vector3D & target)
inline

Definition at line 91 of file Camera.hpp.

◆ AddPosition()

void Sleak::Camera::AddPosition ( const Math::Vector3D & position)
inline

Definition at line 79 of file Camera.hpp.

◆ GetDirection()

Math::Vector3D Sleak::Camera::GetDirection ( ) const
inline

Definition at line 82 of file Camera.hpp.

◆ GetFarPlane()

float Sleak::Camera::GetFarPlane ( ) const
inline

Definition at line 76 of file Camera.hpp.

◆ GetFieldOfView()

float Sleak::Camera::GetFieldOfView ( ) const
inline

Definition at line 70 of file Camera.hpp.

◆ GetLookTarget()

Math::Vector3D Sleak::Camera::GetLookTarget ( ) const
inline

Definition at line 92 of file Camera.hpp.

◆ GetMainCameraPosition()

const Math::Vector3D & Sleak::Camera::GetMainCameraPosition ( )
inlinestatic

Definition at line 111 of file Camera.hpp.

◆ GetMainProjectionMatrix()

const Math::Matrix4 & Sleak::Camera::GetMainProjectionMatrix ( )
inlinestatic

Definition at line 107 of file Camera.hpp.

◆ GetMainViewFrustum()

const ViewFrustum & Sleak::Camera::GetMainViewFrustum ( )
inlinestatic

Definition at line 115 of file Camera.hpp.

◆ GetMainViewMatrix()

const Math::Matrix4 & Sleak::Camera::GetMainViewMatrix ( )
inlinestatic

Definition at line 103 of file Camera.hpp.

◆ GetNearPlane()

float Sleak::Camera::GetNearPlane ( ) const
inline

Definition at line 73 of file Camera.hpp.

◆ GetPosition()

Math::Vector3D Sleak::Camera::GetPosition ( ) const
inline

Definition at line 80 of file Camera.hpp.

◆ GetProjectionType()

ProjectionType Sleak::Camera::GetProjectionType ( ) const
inline

Definition at line 98 of file Camera.hpp.

◆ GetUp()

Math::Vector3D Sleak::Camera::GetUp ( ) const
inline

Definition at line 95 of file Camera.hpp.

◆ Initialize()

void Sleak::Camera::Initialize ( )
overridevirtual

Initializes the object and its components; called once before the first Update.

Reimplemented from Sleak::GameObject.

Definition at line 24 of file Camera.cpp.

◆ OnResize()

void Sleak::Camera::OnResize ( uint32_t width,
uint32_t height )

Updates the viewport dimensions and recomputes the projection matrix.

Definition at line 45 of file Camera.cpp.

◆ RecalculateProjectionMatrix()

void Sleak::Camera::RecalculateProjectionMatrix ( )
protected

Rebuilds the static projection matrix from FOV/aspect/near/far or the orthographic extents.

Definition at line 66 of file Camera.cpp.

◆ RecalculateViewMatrix()

void Sleak::Camera::RecalculateViewMatrix ( )
protected

Rebuilds the static view matrix and view frustum from position/target/up, and begins culling for the frame.

Definition at line 52 of file Camera.cpp.

◆ SetDirection()

void Sleak::Camera::SetDirection ( const Math::Vector3D & direction)
inline

Definition at line 83 of file Camera.hpp.

◆ SetFarPlane()

void Sleak::Camera::SetFarPlane ( float farPlaneValue)
inline

Definition at line 75 of file Camera.hpp.

◆ SetFieldOfView()

void Sleak::Camera::SetFieldOfView ( float fov)
inline

Definition at line 69 of file Camera.hpp.

◆ SetLookTarget()

void Sleak::Camera::SetLookTarget ( const Math::Vector3D & target)
inline

Definition at line 90 of file Camera.hpp.

◆ SetNearPlane()

void Sleak::Camera::SetNearPlane ( float nearPlaneValue)
inline

Definition at line 72 of file Camera.hpp.

◆ SetPosition()

void Sleak::Camera::SetPosition ( const Math::Vector3D & position)
inline

Definition at line 78 of file Camera.hpp.

◆ SetProjectionType()

void Sleak::Camera::SetProjectionType ( ProjectionType type)
inline

Definition at line 97 of file Camera.hpp.

◆ SetUp()

void Sleak::Camera::SetUp ( const Math::Vector3D & up)
inline

Definition at line 94 of file Camera.hpp.

◆ Update()

void Sleak::Camera::Update ( float DeltaTime)
overridevirtual

Recalculates the view/projection matrices and frustum while active.

Reimplemented from Sleak::GameObject.

Definition at line 31 of file Camera.cpp.

Member Data Documentation

◆ farPlane

float Sleak::Camera::farPlane
protected

Definition at line 127 of file Camera.hpp.

◆ fieldOfView

float Sleak::Camera::fieldOfView
protected

Definition at line 125 of file Camera.hpp.

◆ height

float Sleak::Camera::height
protected

Definition at line 129 of file Camera.hpp.

◆ LookTarget

Math::Vector3D Sleak::Camera::LookTarget
protected

Definition at line 134 of file Camera.hpp.

◆ MainPosition

Math::Vector3D Sleak::Camera::MainPosition = Math::Vector3D(0, 0, 0)
staticprotected

Definition at line 139 of file Camera.hpp.

◆ nearPlane

float Sleak::Camera::nearPlane
protected

Definition at line 126 of file Camera.hpp.

◆ Position

Math::Vector3D Sleak::Camera::Position
protected

Definition at line 133 of file Camera.hpp.

◆ Projection

Math::Matrix4 Sleak::Camera::Projection = Math::Matrix4::Identity()
staticprotected

Definition at line 138 of file Camera.hpp.

◆ s_frustum

ViewFrustum Sleak::Camera::s_frustum
staticprotected

Definition at line 140 of file Camera.hpp.

◆ type

ProjectionType Sleak::Camera::type = ProjectionType::Perspective
protected

Definition at line 131 of file Camera.hpp.

◆ Up

Math::Vector3D Sleak::Camera::Up = Math::Vector3D::Up()
protected

Definition at line 135 of file Camera.hpp.

◆ View

Math::Matrix4 Sleak::Camera::View = Math::Matrix4::Identity()
staticprotected

Definition at line 137 of file Camera.hpp.

◆ width

float Sleak::Camera::width
protected

Definition at line 128 of file Camera.hpp.