MXVK Vulkan Framework 0.35.0
C++20 Vulkan rendering framework for practical 2D and 3D application development with SDL3.
Loading...
Searching...
No Matches
mxvk::Camera Class Reference

Camera and projection data for the simple software 3D pipeline. More...

#include <mxvk/include/mxvk/mxvk_math.h>

Public Member Functions

void BuildEuler (int)
 Build the world-to-camera matrix from Euler direction angles.
void BuildEuler (int)
 Build the world-to-camera matrix from Euler direction angles.
void BuildUVN (int)
 Build the world-to-camera matrix from the camera position and look-at target.
void BuildUVN (int)
 Build the world-to-camera matrix from the camera position and look-at target.
 Camera ()
 Construct a camera with identity matrices.
 Camera ()
 Construct a camera with identity matrices.
void CameraToPerspective (mxObject &object) const
 Project an object's transformed vertices from camera space to perspective space.
void CameraToPerspective (mxObject &object) const
 Project an object's transformed vertices from camera space to perspective space.
void CameraToPerspective (RenderList &list) const
 Project a render list from camera space to perspective space.
void CameraToPerspective (RenderList &list) const
 Project a render list from camera space to perspective space.
void Init (int camera_attr, const vec4D &camera_pos, const vec4D &camera_dir, const vec4D *camera_target, float near_z, float far_z, float fov_degrees, float width, float height)
 Initialize camera projection, viewport, position, and direction parameters.
void Init (int camera_attr, const vec4D &camera_pos, const vec4D &camera_dir, const vec4D *camera_target, float near_z, float far_z, float fov_degrees, float width, float height)
 Initialize camera projection, viewport, position, and direction parameters.
void InitalizeForEuler ()
 Initialize camera fields for the Euler example path.
void InitalizeForEuler ()
 Initialize camera fields for the Euler example path.
void PerspectiveToScreen (mxObject &object) const
 Convert an object's transformed vertices from perspective coordinates to screen coordinates.
void PerspectiveToScreen (mxObject &object) const
 Convert an object's transformed vertices from perspective coordinates to screen coordinates.
void PerspectiveToScreen (RenderList &list) const
 Convert a render list from perspective coordinates to screen coordinates.
void PerspectiveToScreen (RenderList &list) const
 Convert a render list from perspective coordinates to screen coordinates.
void WorldToCamera (mxObject &object) const
 Transform an object's transformed vertices from world space to camera space.
void WorldToCamera (mxObject &object) const
 Transform an object's transformed vertices from world space to camera space.
void WorldToCamera (RenderList &list) const
 Transform a render list from world space to camera space.
void WorldToCamera (RenderList &list) const
 Transform a render list from world space to camera space.

Public Attributes

float aspect_ratio = 1.0f
 Viewport aspect ratio.
int attr = 0
 Camera attributes.
Plane3D bt_clip_plane
 Bottom clipping plane.
vec4D dir
 Euler camera direction in degrees.
float far_clip_z = 1000.0f
 Far clipping plane Z.
float fov = 90.0f
 Vertical field of view in degrees.
Plane3D lt_clip_plane
 Left clipping plane.
Mat4D mcam
 World-to-camera transform matrix.
Mat4D mper
 Perspective transform matrix placeholder.
Mat4D mscr
 Screen transform matrix placeholder.
vec4D n
 UVN camera N basis vector.
float near_clip_z = 1.0f
 Near clipping plane Z.
vec4D pos
 Camera position.
Plane3D rt_clip_plane
 Right clipping plane.
int state = 0
 Camera state flags.
vec4D target
 Camera look-at target.
Plane3D tp_clip_plane
 Top clipping plane.
vec4D u
 UVN camera U basis vector.
vec4D v
 UVN camera V basis vector.
float view_dist = 1.0f
 Distance from camera to view plane.
float viewplane_height = 2.0f
 View-plane height.
float viewplane_width = 2.0f
 View-plane width.
float viewport_center_x = 0.0f
 Viewport center X coordinate.
float viewport_center_y = 0.0f
 Viewport center Y coordinate.
float viewport_height = 1.0f
 Viewport height in pixels.
float viewport_width = 1.0f
 Viewport width in pixels.

Detailed Description

Camera and projection data for the simple software 3D pipeline.

Definition at line 1690 of file mxvk_math.h.

Constructor & Destructor Documentation

◆ Camera() [1/2]

mxvk::Camera::Camera ( )
inline

Construct a camera with identity matrices.

Definition at line 1771 of file mxvk_math.h.

1771 {
1775 }
Mat4D mcam
World-to-camera transform matrix.
Definition mxvk_math.h:1762
Mat4D mscr
Screen transform matrix placeholder.
Definition mxvk_math.h:1768
Mat4D mper
Perspective transform matrix placeholder.
Definition mxvk_math.h:1765
void LoadIdentity()
Set this matrix to the identity matrix.
Definition mxvk_math.h:738

◆ Camera() [2/2]

mxvk::Camera::Camera ( )
inline

Construct a camera with identity matrices.

Definition at line 1732 of file mxvk_math_eigen.hpp.

1732 {
1736 }

Member Function Documentation

◆ BuildEuler() [1/2]

void mxvk::Camera::BuildEuler ( int )
inline

Build the world-to-camera matrix from Euler direction angles.

Definition at line 1807 of file mxvk_math.h.

1807 {
1808 Mat4D translation(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, -pos.x, -pos.y, -pos.z, 1);
1809 Mat4D rotation;
1810 rotation.BuildXYZ(-dir.x, -dir.y, -dir.z);
1811 mcam = translation * rotation;
1812 }
vec4D dir
Euler camera direction in degrees.
Definition mxvk_math.h:1702
vec4D pos
Camera position.
Definition mxvk_math.h:1699
float y
Y coordinate.
Definition mxvk_math.h:369
float x
X coordinate.
Definition mxvk_math.h:366
float z
Z coordinate.
Definition mxvk_math.h:372

◆ BuildEuler() [2/2]

void mxvk::Camera::BuildEuler ( int )
inline

Build the world-to-camera matrix from Euler direction angles.

Definition at line 1768 of file mxvk_math_eigen.hpp.

1768 {
1769 Mat4D translation(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, -pos.x, -pos.y, -pos.z, 1);
1770 Mat4D rotation;
1771 rotation.BuildXYZ(-dir.x, -dir.y, -dir.z);
1772 mcam = translation * rotation;
1773 }

◆ BuildUVN() [1/2]

void mxvk::Camera::BuildUVN ( int )
inline

Build the world-to-camera matrix from the camera position and look-at target.

Definition at line 1815 of file mxvk_math.h.

1815 {
1816 n = vec4D().Build(pos, target);
1817 n.Normalize();
1818 v.Set(0.0f, 1.0f, 0.0f);
1819 u = v.CrossProduct(n);
1820 u.Normalize();
1821 v = n.CrossProduct(u);
1822 v.Normalize();
1823 Mat4D translation(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, -pos.x, -pos.y, -pos.z, 1);
1824 Mat4D uvn(u.x, v.x, n.x, 0, u.y, v.y, n.y, 0, u.z, v.z, n.z, 0, 0, 0, 0, 1);
1825 mcam = translation * uvn;
1826 }
vec4D target
Camera look-at target.
Definition mxvk_math.h:1714
vec4D u
UVN camera U basis vector.
Definition mxvk_math.h:1705
vec4D v
UVN camera V basis vector.
Definition mxvk_math.h:1708
vec4D n
UVN camera N basis vector.
Definition mxvk_math.h:1711
constexpr vec4D()
Construct the homogeneous origin.
Definition mxvk_math.h:378
void Normalize()
Normalize the 3D components in place and reset W to 1.
Definition mxvk_math.h:447
void Build(const vec4D &to)
Replace this vector with the direction from this point to to.
Definition mxvk_math.h:473
void Set(float x_value, float y_value, float z_value, float w_value=1.0f)
Set all vector coordinates.
Definition mxvk_math.h:384
constexpr vec4D CrossProduct(const vec4D &v) const
Compute the 3D cross product and return it with W set to 1.
Definition mxvk_math.h:441

◆ BuildUVN() [2/2]

void mxvk::Camera::BuildUVN ( int )
inline

Build the world-to-camera matrix from the camera position and look-at target.

Definition at line 1776 of file mxvk_math_eigen.hpp.

1776 {
1777 n = vec4D().Build(pos, target);
1778 n.Normalize();
1779 v.Set(0.0f, 1.0f, 0.0f);
1780 u = v.CrossProduct(n);
1781 u.Normalize();
1782 v = n.CrossProduct(u);
1783 v.Normalize();
1784 Mat4D translation(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, -pos.x, -pos.y, -pos.z, 1);
1785 Mat4D uvn(u.x, v.x, n.x, 0, u.y, v.y, n.y, 0, u.z, v.z, n.z, 0, 0, 0, 0, 1);
1786 mcam = translation * uvn;
1787 }

◆ CameraToPerspective() [1/4]

void mxvk::Camera::CameraToPerspective ( mxObject & object) const
inline

Project an object's transformed vertices from camera space to perspective space.

Definition at line 1864 of file mxvk_math.h.

1864 {
1865 for (auto &vertex : object.trans) {
1866 if (std::fabs(vertex.z) <= EPSILON) {
1867 continue;
1868 }
1869 vertex.x = view_dist * vertex.x / vertex.z;
1870 vertex.y = view_dist * vertex.y * aspect_ratio / vertex.z;
1871 }
1872 }
float aspect_ratio
Viewport aspect ratio.
Definition mxvk_math.h:1759
float view_dist
Distance from camera to view plane.
Definition mxvk_math.h:1717
constexpr float EPSILON
Default tolerance used for floating-point singularity and zero-length checks.
Definition mxvk_math.h:37

◆ CameraToPerspective() [2/4]

void mxvk::Camera::CameraToPerspective ( mxObject & object) const
inline

Project an object's transformed vertices from camera space to perspective space.

Definition at line 1826 of file mxvk_math_eigen.hpp.

1826 {
1827 for (auto &vertex : object.trans) {
1828 if (std::fabs(vertex.z) <= EPSILON) {
1829 continue;
1830 }
1831 const Eigen::Vector2f projected = Eigen::Vector2f(vertex.x, vertex.y).cwiseProduct(Eigen::Vector2f(view_dist, view_dist * aspect_ratio)) / vertex.z;
1832 vertex.x = projected.x();
1833 vertex.y = projected.y();
1834 }
1835 }

◆ CameraToPerspective() [3/4]

void mxvk::Camera::CameraToPerspective ( RenderList & list) const
inline

Project a render list from camera space to perspective space.

Definition at line 1848 of file mxvk_math.h.

1848 {
1849 for (auto &poly : list.polys) {
1850 if (poly.state == 0 || (poly.state & MX_BACKFACE) != 0) {
1851 continue;
1852 }
1853 for (auto &vertex : poly.tlist) {
1854 if (std::fabs(vertex.z) <= EPSILON) {
1855 continue;
1856 }
1857 vertex.x = view_dist * vertex.x / vertex.z;
1858 vertex.y = view_dist * vertex.y * aspect_ratio / vertex.z;
1859 }
1860 }
1861 }
@ MX_BACKFACE
Polygon is marked as a backface.
Definition mxvk_math.h:1225

◆ CameraToPerspective() [4/4]

void mxvk::Camera::CameraToPerspective ( RenderList & list) const
inline

Project a render list from camera space to perspective space.

Definition at line 1809 of file mxvk_math_eigen.hpp.

1809 {
1810 for (auto &poly : list.polys) {
1811 if (poly.state == 0 || (poly.state & MX_BACKFACE) != 0) {
1812 continue;
1813 }
1814 for (auto &vertex : poly.tlist) {
1815 if (std::fabs(vertex.z) <= EPSILON) {
1816 continue;
1817 }
1818 const Eigen::Vector2f projected = Eigen::Vector2f(vertex.x, vertex.y).cwiseProduct(Eigen::Vector2f(view_dist, view_dist * aspect_ratio)) / vertex.z;
1819 vertex.x = projected.x();
1820 vertex.y = projected.y();
1821 }
1822 }
1823 }

◆ Init() [1/2]

void mxvk::Camera::Init ( int camera_attr,
const vec4D & camera_pos,
const vec4D & camera_dir,
const vec4D * camera_target,
float near_z,
float far_z,
float fov_degrees,
float width,
float height )
inline

Initialize camera projection, viewport, position, and direction parameters.

Definition at line 1785 of file mxvk_math.h.

1785 {
1786 attr = camera_attr;
1787 pos = camera_pos;
1788 dir = camera_dir;
1789 target = camera_target != nullptr ? *camera_target : vec4D();
1790 near_clip_z = near_z;
1791 far_clip_z = far_z;
1792 fov = fov_degrees;
1793 viewport_width = std::max(1.0f, width);
1794 viewport_height = std::max(1.0f, height);
1795 viewport_center_x = (viewport_width - 1.0f) * 0.5f;
1796 viewport_center_y = (viewport_height - 1.0f) * 0.5f;
1798 viewplane_width = 2.0f;
1800 view_dist = (viewplane_width * 0.5f) / std::tan(deg2rad(fov * 0.5f));
1804 }
float viewport_width
Viewport width in pixels.
Definition mxvk_math.h:1747
float far_clip_z
Far clipping plane Z.
Definition mxvk_math.h:1726
int attr
Camera attributes.
Definition mxvk_math.h:1696
float near_clip_z
Near clipping plane Z.
Definition mxvk_math.h:1723
float viewport_height
Viewport height in pixels.
Definition mxvk_math.h:1750
float viewplane_width
View-plane width.
Definition mxvk_math.h:1744
float viewport_center_y
Viewport center Y coordinate.
Definition mxvk_math.h:1756
float viewplane_height
View-plane height.
Definition mxvk_math.h:1741
float fov
Vertical field of view in degrees.
Definition mxvk_math.h:1720
float viewport_center_x
Viewport center X coordinate.
Definition mxvk_math.h:1753
float deg2rad(float ang)
Convert degrees to radians.
Definition mxvk_math.h:109

◆ Init() [2/2]

void mxvk::Camera::Init ( int camera_attr,
const vec4D & camera_pos,
const vec4D & camera_dir,
const vec4D * camera_target,
float near_z,
float far_z,
float fov_degrees,
float width,
float height )
inline

Initialize camera projection, viewport, position, and direction parameters.

Definition at line 1746 of file mxvk_math_eigen.hpp.

1746 {
1747 attr = camera_attr;
1748 pos = camera_pos;
1749 dir = camera_dir;
1750 target = camera_target != nullptr ? *camera_target : vec4D();
1751 near_clip_z = near_z;
1752 far_clip_z = far_z;
1753 fov = fov_degrees;
1754 viewport_width = std::max(1.0f, width);
1755 viewport_height = std::max(1.0f, height);
1756 viewport_center_x = (viewport_width - 1.0f) * 0.5f;
1757 viewport_center_y = (viewport_height - 1.0f) * 0.5f;
1759 viewplane_width = 2.0f;
1761 view_dist = (viewplane_width * 0.5f) / std::tan(deg2rad(fov * 0.5f));
1765 }

◆ InitalizeForEuler() [1/2]

void mxvk::Camera::InitalizeForEuler ( )
inline

Initialize camera fields for the Euler example path.

Definition at line 1778 of file mxvk_math.h.

1778 {
1779 pos.Set(100.0f, 200.0f, 300.0f);
1780 dir.Set(-48.0f, 0.0f, 0.0f);
1781 BuildEuler(5);
1782 }
void BuildEuler(int)
Build the world-to-camera matrix from Euler direction angles.
Definition mxvk_math.h:1807

◆ InitalizeForEuler() [2/2]

void mxvk::Camera::InitalizeForEuler ( )
inline

Initialize camera fields for the Euler example path.

Definition at line 1739 of file mxvk_math_eigen.hpp.

1739 {
1740 pos.Set(100.0f, 200.0f, 300.0f);
1741 dir.Set(-48.0f, 0.0f, 0.0f);
1742 BuildEuler(5);
1743 }

◆ PerspectiveToScreen() [1/4]

void mxvk::Camera::PerspectiveToScreen ( mxObject & object) const
inline

Convert an object's transformed vertices from perspective coordinates to screen coordinates.

Definition at line 1890 of file mxvk_math.h.

1890 {
1891 const float alpha = viewport_center_x;
1892 const float beta = viewport_center_y;
1893 for (auto &vertex : object.trans) {
1894 vertex.x = alpha + alpha * vertex.x;
1895 vertex.y = beta - beta * vertex.y;
1896 }
1897 }

◆ PerspectiveToScreen() [2/4]

void mxvk::Camera::PerspectiveToScreen ( mxObject & object) const
inline

Convert an object's transformed vertices from perspective coordinates to screen coordinates.

Definition at line 1854 of file mxvk_math_eigen.hpp.

1854 {
1855 const float alpha = viewport_center_x;
1856 const float beta = viewport_center_y;
1857 for (auto &vertex : object.trans) {
1858 const Eigen::Vector2f screen = Eigen::Vector2f(alpha, beta) + Eigen::Vector2f(alpha, -beta).cwiseProduct(Eigen::Vector2f(vertex.x, vertex.y));
1859 vertex.x = screen.x();
1860 vertex.y = screen.y();
1861 }
1862 }

◆ PerspectiveToScreen() [3/4]

void mxvk::Camera::PerspectiveToScreen ( RenderList & list) const
inline

Convert a render list from perspective coordinates to screen coordinates.

Definition at line 1875 of file mxvk_math.h.

1875 {
1876 const float alpha = viewport_center_x;
1877 const float beta = viewport_center_y;
1878 for (auto &poly : list.polys) {
1879 if (poly.state == 0 || (poly.state & MX_BACKFACE) != 0) {
1880 continue;
1881 }
1882 for (auto &vertex : poly.tlist) {
1883 vertex.x = alpha + alpha * vertex.x;
1884 vertex.y = beta - beta * vertex.y;
1885 }
1886 }
1887 }

◆ PerspectiveToScreen() [4/4]

void mxvk::Camera::PerspectiveToScreen ( RenderList & list) const
inline

Convert a render list from perspective coordinates to screen coordinates.

Definition at line 1838 of file mxvk_math_eigen.hpp.

1838 {
1839 const float alpha = viewport_center_x;
1840 const float beta = viewport_center_y;
1841 for (auto &poly : list.polys) {
1842 if (poly.state == 0 || (poly.state & MX_BACKFACE) != 0) {
1843 continue;
1844 }
1845 for (auto &vertex : poly.tlist) {
1846 const Eigen::Vector2f screen = Eigen::Vector2f(alpha, beta) + Eigen::Vector2f(alpha, -beta).cwiseProduct(Eigen::Vector2f(vertex.x, vertex.y));
1847 vertex.x = screen.x();
1848 vertex.y = screen.y();
1849 }
1850 }
1851 }

◆ WorldToCamera() [1/4]

void mxvk::Camera::WorldToCamera ( mxObject & object) const
inline

Transform an object's transformed vertices from world space to camera space.

Definition at line 1841 of file mxvk_math.h.

1841 {
1842 for (auto &vertex : object.trans) {
1843 vertex = mcam.MulVec(vertex);
1844 }
1845 }
vec4D MulVec(const vec4D &in) const
Transform a homogeneous 4D vector by this matrix.
Definition mxvk_math.h:771

◆ WorldToCamera() [2/4]

void mxvk::Camera::WorldToCamera ( mxObject & object) const
inline

Transform an object's transformed vertices from world space to camera space.

Definition at line 1802 of file mxvk_math_eigen.hpp.

1802 {
1803 for (auto &vertex : object.trans) {
1804 vertex = mcam.MulVec(vertex);
1805 }
1806 }

◆ WorldToCamera() [3/4]

void mxvk::Camera::WorldToCamera ( RenderList & list) const
inline

Transform a render list from world space to camera space.

Definition at line 1829 of file mxvk_math.h.

1829 {
1830 for (auto &poly : list.polys) {
1831 if (poly.state == 0 || (poly.state & MX_BACKFACE) != 0) {
1832 continue;
1833 }
1834 for (auto &vertex : poly.tlist) {
1835 vertex = mcam.MulVec(vertex);
1836 }
1837 }
1838 }

◆ WorldToCamera() [4/4]

void mxvk::Camera::WorldToCamera ( RenderList & list) const
inline

Transform a render list from world space to camera space.

Definition at line 1790 of file mxvk_math_eigen.hpp.

1790 {
1791 for (auto &poly : list.polys) {
1792 if (poly.state == 0 || (poly.state & MX_BACKFACE) != 0) {
1793 continue;
1794 }
1795 for (auto &vertex : poly.tlist) {
1796 vertex = mcam.MulVec(vertex);
1797 }
1798 }
1799 }

Member Data Documentation

◆ aspect_ratio

float mxvk::Camera::aspect_ratio = 1.0f

Viewport aspect ratio.

Definition at line 1759 of file mxvk_math.h.

◆ attr

int mxvk::Camera::attr = 0

Camera attributes.

Definition at line 1696 of file mxvk_math.h.

◆ bt_clip_plane

Plane3D mxvk::Camera::bt_clip_plane

Bottom clipping plane.

Definition at line 1738 of file mxvk_math.h.

◆ dir

vec4D mxvk::Camera::dir

Euler camera direction in degrees.

Definition at line 1702 of file mxvk_math.h.

◆ far_clip_z

float mxvk::Camera::far_clip_z = 1000.0f

Far clipping plane Z.

Definition at line 1726 of file mxvk_math.h.

◆ fov

float mxvk::Camera::fov = 90.0f

Vertical field of view in degrees.

Definition at line 1720 of file mxvk_math.h.

◆ lt_clip_plane

Plane3D mxvk::Camera::lt_clip_plane

Left clipping plane.

Definition at line 1732 of file mxvk_math.h.

◆ mcam

Mat4D mxvk::Camera::mcam

World-to-camera transform matrix.

Definition at line 1762 of file mxvk_math.h.

◆ mper

Mat4D mxvk::Camera::mper

Perspective transform matrix placeholder.

Definition at line 1765 of file mxvk_math.h.

◆ mscr

Mat4D mxvk::Camera::mscr

Screen transform matrix placeholder.

Definition at line 1768 of file mxvk_math.h.

◆ n

vec4D mxvk::Camera::n

UVN camera N basis vector.

Definition at line 1711 of file mxvk_math.h.

◆ near_clip_z

float mxvk::Camera::near_clip_z = 1.0f

Near clipping plane Z.

Definition at line 1723 of file mxvk_math.h.

◆ pos

vec4D mxvk::Camera::pos

Camera position.

Definition at line 1699 of file mxvk_math.h.

◆ rt_clip_plane

Plane3D mxvk::Camera::rt_clip_plane

Right clipping plane.

Definition at line 1729 of file mxvk_math.h.

◆ state

int mxvk::Camera::state = 0

Camera state flags.

Definition at line 1693 of file mxvk_math.h.

◆ target

vec4D mxvk::Camera::target

Camera look-at target.

Definition at line 1714 of file mxvk_math.h.

◆ tp_clip_plane

Plane3D mxvk::Camera::tp_clip_plane

Top clipping plane.

Definition at line 1735 of file mxvk_math.h.

◆ u

vec4D mxvk::Camera::u

UVN camera U basis vector.

Definition at line 1705 of file mxvk_math.h.

◆ v

vec4D mxvk::Camera::v

UVN camera V basis vector.

Definition at line 1708 of file mxvk_math.h.

◆ view_dist

float mxvk::Camera::view_dist = 1.0f

Distance from camera to view plane.

Definition at line 1717 of file mxvk_math.h.

◆ viewplane_height

float mxvk::Camera::viewplane_height = 2.0f

View-plane height.

Definition at line 1741 of file mxvk_math.h.

◆ viewplane_width

float mxvk::Camera::viewplane_width = 2.0f

View-plane width.

Definition at line 1744 of file mxvk_math.h.

◆ viewport_center_x

float mxvk::Camera::viewport_center_x = 0.0f

Viewport center X coordinate.

Definition at line 1753 of file mxvk_math.h.

◆ viewport_center_y

float mxvk::Camera::viewport_center_y = 0.0f

Viewport center Y coordinate.

Definition at line 1756 of file mxvk_math.h.

◆ viewport_height

float mxvk::Camera::viewport_height = 1.0f

Viewport height in pixels.

Definition at line 1750 of file mxvk_math.h.

◆ viewport_width

float mxvk::Camera::viewport_width = 1.0f

Viewport width in pixels.

Definition at line 1747 of file mxvk_math.h.


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