Camera3D requires CNA link
Easy3D / Cameras / Camera3D
Easy3D::Camera3D is a small, explicit perspective camera. It stores a position/target/up and the usual perspective parameters, and produces CNA (Microsoft::Xna::Framework::Matrix) view and projection matrices. CNA types are used directly and on purpose: Easy3D does not hide CNA.
#include <Easy3D/Camera3D.hpp>
namespace Easy3D { class Camera3D; }
GetViewMatrix() / GetProjectionMatrix() call into CNA's compiled math (Matrix::CreateLookAt, Matrix::CreatePerspectiveFieldOfView), so executables that call them must link the CNA library. See Building & CMake.Type aliases
| Alias | Refers to |
|---|---|
Camera3D::Vector3 | Microsoft::Xna::Framework::Vector3 |
Camera3D::Matrix | Microsoft::Xna::Framework::Matrix |
Defaults
A default-constructed Camera3D is immediately usable — it looks at the origin from slightly above and behind:
| Property | Default | Notes |
|---|---|---|
| Position | (0, 2, 5) | |
| Target | (0, 0, 0) | Vector3::Zero |
| Up | (0, 1, 0) | Vector3::Up |
| Field of view | 0.785398163 rad | 45°, vertical |
| Aspect ratio | 16/9 | |
| Near plane | 0.1 | |
| Far plane | 1000 |
Eye / target / up
[[nodiscard]] const Vector3& GetPosition() const noexcept
void SetPosition(const Vector3& value) noexcept
void SetPosition(const Vector3& value) noexcept
The eye position of the camera in world space.
[[nodiscard]] const Vector3& GetTarget() const noexcept
void SetTarget(const Vector3& value) noexcept
void SetTarget(const Vector3& value) noexcept
The world-space point the camera looks at.
[[nodiscard]] const Vector3& GetUp() const noexcept
void SetUp(const Vector3& value) noexcept
void SetUp(const Vector3& value) noexcept
The camera's up vector (default
Vector3::Up).Perspective parameters
[[nodiscard]] float GetFieldOfView() const noexcept
void SetFieldOfView(float radians) noexcept
void SetFieldOfView(float radians) noexcept
Vertical field of view, in radians.
[[nodiscard]] float GetAspectRatio() const noexcept
void SetAspectRatio(float aspect) noexcept
void SetAspectRatio(float aspect) noexcept
Viewport width divided by height. Update this when the window resizes.
[[nodiscard]] float GetNearPlane() const noexcept
void SetNearPlane(float value) noexcept
void SetNearPlane(float value) noexcept
Distance to the near clipping plane.
[[nodiscard]] float GetFarPlane() const noexcept
void SetFarPlane(float value) noexcept
void SetFarPlane(float value) noexcept
Distance to the far clipping plane.
Derived matrices requires CNA link
[[nodiscard]] Matrix GetViewMatrix() const
Returns
Matrix::CreateLookAt(position, target, up) — a right-handed CNA look-at view matrix.[[nodiscard]] Matrix GetProjectionMatrix() const
Returns
Matrix::CreatePerspectiveFieldOfView(fieldOfView, aspectRatio, nearPlane, farPlane) — a CNA perspective projection matrix.Example
#include <Easy3D/Camera3D.hpp>
using Vector3 = Easy3D::Camera3D::Vector3;
Easy3D::Camera3D camera;
camera.SetPosition(Vector3(0.0f, 3.0f, 8.0f));
camera.SetTarget(Vector3::Zero);
camera.SetAspectRatio(static_cast<float>(viewportWidth) / viewportHeight);
// Hand the CNA matrices straight to your CNA rendering code.
const auto view = camera.GetViewMatrix();
const auto projection = camera.GetProjectionMatrix();
Working with the other cameras
Camera3D is the single "real" camera; OrbitCamera and FollowCamera are positioning helpers that do not own one. Both provide an ApplyTo() that pushes their computed position/target into a Camera3D:
Easy3D::OrbitCamera orbit;
orbit.SetDistance(12.0f);
orbit.SetYaw(0.6f);
orbit.ApplyTo(camera); // sets camera position + target
Keep one
Camera3D as the source of truth for view/projection, and drive it from whichever rig (orbit, follow, or your own code) fits the current game state.