MXVK Vulkan Framework 0.24.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 1840 of file mxvk_math.h.

Constructor & Destructor Documentation

◆ Camera() [1/2]

mxvk::Camera::Camera ( )
inline

Construct a camera with identity matrices.

Definition at line 1921 of file mxvk_math.h.

1921 {
1922 mcam.LoadIdentity();
1923 mper.LoadIdentity();
1924 mscr.LoadIdentity();
1925 }
Mat4D mcam
World-to-camera transform matrix.
Definition mxvk_math.h:1912
Mat4D mscr
Screen transform matrix placeholder.
Definition mxvk_math.h:1918
Mat4D mper
Perspective transform matrix placeholder.
Definition mxvk_math.h:1915

◆ Camera() [2/2]

mxvk::Camera::Camera ( )
inline

Construct a camera with identity matrices.

Definition at line 1918 of file mxvk_math_eigen.hpp.

1918 {
1919 mcam.LoadIdentity();
1920 mper.LoadIdentity();
1921 mscr.LoadIdentity();
1922 }

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 1957 of file mxvk_math.h.

1957 {
1958 Mat4D translation(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, -pos.x, -pos.y, -pos.z, 1);
1959 Mat4D rotation;
1960 rotation.BuildXYZ(-dir.x, -dir.y, -dir.z);
1961 mcam = translation * rotation;
1962 }
vec4D dir
Euler camera direction in degrees.
Definition mxvk_math.h:1852
vec4D pos
Camera position.
Definition mxvk_math.h:1849

◆ BuildEuler() [2/2]

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

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

Definition at line 1954 of file mxvk_math_eigen.hpp.

1954 {
1955 Mat4D translation(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, -pos.x, -pos.y, -pos.z, 1);
1956 Mat4D rotation;
1957 rotation.BuildXYZ(-dir.x, -dir.y, -dir.z);
1958 mcam = translation * rotation;
1959 }

◆ 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 1965 of file mxvk_math.h.

1965 {
1966 n = vec4D().Build(pos, target);
1967 n.Normalize();
1968 v.Set(0.0f, 1.0f, 0.0f);
1969 u = v.CrossProduct(n);
1970 u.Normalize();
1971 v = n.CrossProduct(u);
1972 v.Normalize();
1973 Mat4D translation(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, -pos.x, -pos.y, -pos.z, 1);
1974 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);
1975 mcam = translation * uvn;
1976 }
vec4D target
Camera look-at target.
Definition mxvk_math.h:1864
vec4D u
UVN camera U basis vector.
Definition mxvk_math.h:1855
vec4D v
UVN camera V basis vector.
Definition mxvk_math.h:1858
vec4D n
UVN camera N basis vector.
Definition mxvk_math.h:1861

◆ 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 1962 of file mxvk_math_eigen.hpp.

1962 {
1963 n = vec4D().Build(pos, target);
1964 n.Normalize();
1965 v.Set(0.0f, 1.0f, 0.0f);
1966 u = v.CrossProduct(n);
1967 u.Normalize();
1968 v = n.CrossProduct(u);
1969 v.Normalize();
1970 Mat4D translation(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, -pos.x, -pos.y, -pos.z, 1);
1971 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);
1972 mcam = translation * uvn;
1973 }

◆ 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 2014 of file mxvk_math.h.

2014 {
2015 for (auto &vertex : object.trans) {
2016 if (std::fabs(vertex.z) <= EPSILON) {
2017 continue;
2018 }
2019 vertex.x = view_dist * vertex.x / vertex.z;
2020 vertex.y = view_dist * vertex.y * aspect_ratio / vertex.z;
2021 }
2022 }
float aspect_ratio
Viewport aspect ratio.
Definition mxvk_math.h:1909
float view_dist
Distance from camera to view plane.
Definition mxvk_math.h:1867
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 2015 of file mxvk_math_eigen.hpp.

2015 {
2016 for (auto &vertex : object.trans) {
2017 if (std::fabs(vertex.z) <= EPSILON) {
2018 continue;
2019 }
2020 const Eigen::Vector2f projected =
2021 Eigen::Vector2f(vertex.x, vertex.y)
2022 .cwiseProduct(Eigen::Vector2f(view_dist, view_dist * aspect_ratio)) /
2023 vertex.z;
2024 vertex.x = projected.x();
2025 vertex.y = projected.y();
2026 }
2027 }

◆ CameraToPerspective() [3/4]

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

Project a render list from camera space to perspective space.

Definition at line 1998 of file mxvk_math.h.

1998 {
1999 for (auto &poly : list.polys) {
2000 if (poly.state == 0 || (poly.state & MX_BACKFACE) != 0) {
2001 continue;
2002 }
2003 for (auto &vertex : poly.tlist) {
2004 if (std::fabs(vertex.z) <= EPSILON) {
2005 continue;
2006 }
2007 vertex.x = view_dist * vertex.x / vertex.z;
2008 vertex.y = view_dist * vertex.y * aspect_ratio / vertex.z;
2009 }
2010 }
2011 }
@ MX_BACKFACE
Polygon is marked as a backface.
Definition mxvk_math.h:1364

◆ CameraToPerspective() [4/4]

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

Project a render list from camera space to perspective space.

Definition at line 1995 of file mxvk_math_eigen.hpp.

1995 {
1996 for (auto &poly : list.polys) {
1997 if (poly.state == 0 || (poly.state & MX_BACKFACE) != 0) {
1998 continue;
1999 }
2000 for (auto &vertex : poly.tlist) {
2001 if (std::fabs(vertex.z) <= EPSILON) {
2002 continue;
2003 }
2004 const Eigen::Vector2f projected =
2005 Eigen::Vector2f(vertex.x, vertex.y)
2006 .cwiseProduct(Eigen::Vector2f(view_dist, view_dist * aspect_ratio)) /
2007 vertex.z;
2008 vertex.x = projected.x();
2009 vertex.y = projected.y();
2010 }
2011 }
2012 }

◆ 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 1935 of file mxvk_math.h.

1935 {
1936 attr = camera_attr;
1937 pos = camera_pos;
1938 dir = camera_dir;
1939 target = camera_target != nullptr ? *camera_target : vec4D();
1940 near_clip_z = near_z;
1941 far_clip_z = far_z;
1942 fov = fov_degrees;
1943 viewport_width = std::max(1.0f, width);
1944 viewport_height = std::max(1.0f, height);
1945 viewport_center_x = (viewport_width - 1.0f) * 0.5f;
1946 viewport_center_y = (viewport_height - 1.0f) * 0.5f;
1948 viewplane_width = 2.0f;
1950 view_dist = (viewplane_width * 0.5f) / std::tan(deg2rad(fov * 0.5f));
1951 mcam.LoadIdentity();
1952 mper.LoadIdentity();
1953 mscr.LoadIdentity();
1954 }
float viewport_width
Viewport width in pixels.
Definition mxvk_math.h:1897
float far_clip_z
Far clipping plane Z.
Definition mxvk_math.h:1876
int attr
Camera attributes.
Definition mxvk_math.h:1846
float near_clip_z
Near clipping plane Z.
Definition mxvk_math.h:1873
float viewport_height
Viewport height in pixels.
Definition mxvk_math.h:1900
float viewplane_width
View-plane width.
Definition mxvk_math.h:1894
float viewport_center_y
Viewport center Y coordinate.
Definition mxvk_math.h:1906
float viewplane_height
View-plane height.
Definition mxvk_math.h:1891
float fov
Vertical field of view in degrees.
Definition mxvk_math.h:1870
float viewport_center_x
Viewport center X coordinate.
Definition mxvk_math.h:1903
float deg2rad(float ang)
Convert degrees to radians.
Definition mxvk_math.h:124

◆ 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 1932 of file mxvk_math_eigen.hpp.

1932 {
1933 attr = camera_attr;
1934 pos = camera_pos;
1935 dir = camera_dir;
1936 target = camera_target != nullptr ? *camera_target : vec4D();
1937 near_clip_z = near_z;
1938 far_clip_z = far_z;
1939 fov = fov_degrees;
1940 viewport_width = std::max(1.0f, width);
1941 viewport_height = std::max(1.0f, height);
1942 viewport_center_x = (viewport_width - 1.0f) * 0.5f;
1943 viewport_center_y = (viewport_height - 1.0f) * 0.5f;
1945 viewplane_width = 2.0f;
1947 view_dist = (viewplane_width * 0.5f) / std::tan(deg2rad(fov * 0.5f));
1948 mcam.LoadIdentity();
1949 mper.LoadIdentity();
1950 mscr.LoadIdentity();
1951 }

◆ InitalizeForEuler() [1/2]

void mxvk::Camera::InitalizeForEuler ( )
inline

Initialize camera fields for the Euler example path.

Definition at line 1928 of file mxvk_math.h.

1928 {
1929 pos.Set(100.0f, 200.0f, 300.0f);
1930 dir.Set(-48.0f, 0.0f, 0.0f);
1931 BuildEuler(5);
1932 }
void BuildEuler(int)
Build the world-to-camera matrix from Euler direction angles.
Definition mxvk_math.h:1957

◆ InitalizeForEuler() [2/2]

void mxvk::Camera::InitalizeForEuler ( )
inline

Initialize camera fields for the Euler example path.

Definition at line 1925 of file mxvk_math_eigen.hpp.

1925 {
1926 pos.Set(100.0f, 200.0f, 300.0f);
1927 dir.Set(-48.0f, 0.0f, 0.0f);
1928 BuildEuler(5);
1929 }

◆ 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 2040 of file mxvk_math.h.

2040 {
2041 const float alpha = viewport_center_x;
2042 const float beta = viewport_center_y;
2043 for (auto &vertex : object.trans) {
2044 vertex.x = alpha + alpha * vertex.x;
2045 vertex.y = beta - beta * vertex.y;
2046 }
2047 }

◆ 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 2049 of file mxvk_math_eigen.hpp.

2049 {
2050 const float alpha = viewport_center_x;
2051 const float beta = viewport_center_y;
2052 for (auto &vertex : object.trans) {
2053 const Eigen::Vector2f screen =
2054 Eigen::Vector2f(alpha, beta) +
2055 Eigen::Vector2f(alpha, -beta)
2056 .cwiseProduct(Eigen::Vector2f(vertex.x, vertex.y));
2057 vertex.x = screen.x();
2058 vertex.y = screen.y();
2059 }
2060 }

◆ PerspectiveToScreen() [3/4]

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

Convert a render list from perspective coordinates to screen coordinates.

Definition at line 2025 of file mxvk_math.h.

2025 {
2026 const float alpha = viewport_center_x;
2027 const float beta = viewport_center_y;
2028 for (auto &poly : list.polys) {
2029 if (poly.state == 0 || (poly.state & MX_BACKFACE) != 0) {
2030 continue;
2031 }
2032 for (auto &vertex : poly.tlist) {
2033 vertex.x = alpha + alpha * vertex.x;
2034 vertex.y = beta - beta * vertex.y;
2035 }
2036 }
2037 }

◆ PerspectiveToScreen() [4/4]

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

Convert a render list from perspective coordinates to screen coordinates.

Definition at line 2030 of file mxvk_math_eigen.hpp.

2030 {
2031 const float alpha = viewport_center_x;
2032 const float beta = viewport_center_y;
2033 for (auto &poly : list.polys) {
2034 if (poly.state == 0 || (poly.state & MX_BACKFACE) != 0) {
2035 continue;
2036 }
2037 for (auto &vertex : poly.tlist) {
2038 const Eigen::Vector2f screen =
2039 Eigen::Vector2f(alpha, beta) +
2040 Eigen::Vector2f(alpha, -beta)
2041 .cwiseProduct(Eigen::Vector2f(vertex.x, vertex.y));
2042 vertex.x = screen.x();
2043 vertex.y = screen.y();
2044 }
2045 }
2046 }

◆ 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 1991 of file mxvk_math.h.

1991 {
1992 for (auto &vertex : object.trans) {
1993 vertex = mcam.MulVec(vertex);
1994 }
1995 }

◆ 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 1988 of file mxvk_math_eigen.hpp.

1988 {
1989 for (auto &vertex : object.trans) {
1990 vertex = mcam.MulVec(vertex);
1991 }
1992 }

◆ WorldToCamera() [3/4]

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

Transform a render list from world space to camera space.

Definition at line 1979 of file mxvk_math.h.

1979 {
1980 for (auto &poly : list.polys) {
1981 if (poly.state == 0 || (poly.state & MX_BACKFACE) != 0) {
1982 continue;
1983 }
1984 for (auto &vertex : poly.tlist) {
1985 vertex = mcam.MulVec(vertex);
1986 }
1987 }
1988 }

◆ WorldToCamera() [4/4]

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

Transform a render list from world space to camera space.

Definition at line 1976 of file mxvk_math_eigen.hpp.

1976 {
1977 for (auto &poly : list.polys) {
1978 if (poly.state == 0 || (poly.state & MX_BACKFACE) != 0) {
1979 continue;
1980 }
1981 for (auto &vertex : poly.tlist) {
1982 vertex = mcam.MulVec(vertex);
1983 }
1984 }
1985 }

Member Data Documentation

◆ aspect_ratio

float mxvk::Camera::aspect_ratio = 1.0f

Viewport aspect ratio.

Definition at line 1909 of file mxvk_math.h.

◆ attr

int mxvk::Camera::attr = 0

Camera attributes.

Definition at line 1846 of file mxvk_math.h.

◆ bt_clip_plane

Plane3D mxvk::Camera::bt_clip_plane

Bottom clipping plane.

Definition at line 1888 of file mxvk_math.h.

◆ dir

vec4D mxvk::Camera::dir

Euler camera direction in degrees.

Definition at line 1852 of file mxvk_math.h.

◆ far_clip_z

float mxvk::Camera::far_clip_z = 1000.0f

Far clipping plane Z.

Definition at line 1876 of file mxvk_math.h.

◆ fov

float mxvk::Camera::fov = 90.0f

Vertical field of view in degrees.

Definition at line 1870 of file mxvk_math.h.

◆ lt_clip_plane

Plane3D mxvk::Camera::lt_clip_plane

Left clipping plane.

Definition at line 1882 of file mxvk_math.h.

◆ mcam

Mat4D mxvk::Camera::mcam

World-to-camera transform matrix.

Definition at line 1912 of file mxvk_math.h.

◆ mper

Mat4D mxvk::Camera::mper

Perspective transform matrix placeholder.

Definition at line 1915 of file mxvk_math.h.

◆ mscr

Mat4D mxvk::Camera::mscr

Screen transform matrix placeholder.

Definition at line 1918 of file mxvk_math.h.

◆ n

vec4D mxvk::Camera::n

UVN camera N basis vector.

Definition at line 1861 of file mxvk_math.h.

◆ near_clip_z

float mxvk::Camera::near_clip_z = 1.0f

Near clipping plane Z.

Definition at line 1873 of file mxvk_math.h.

◆ pos

vec4D mxvk::Camera::pos

Camera position.

Definition at line 1849 of file mxvk_math.h.

◆ rt_clip_plane

Plane3D mxvk::Camera::rt_clip_plane

Right clipping plane.

Definition at line 1879 of file mxvk_math.h.

◆ state

int mxvk::Camera::state = 0

Camera state flags.

Definition at line 1843 of file mxvk_math.h.

◆ target

vec4D mxvk::Camera::target

Camera look-at target.

Definition at line 1864 of file mxvk_math.h.

◆ tp_clip_plane

Plane3D mxvk::Camera::tp_clip_plane

Top clipping plane.

Definition at line 1885 of file mxvk_math.h.

◆ u

vec4D mxvk::Camera::u

UVN camera U basis vector.

Definition at line 1855 of file mxvk_math.h.

◆ v

vec4D mxvk::Camera::v

UVN camera V basis vector.

Definition at line 1858 of file mxvk_math.h.

◆ view_dist

float mxvk::Camera::view_dist = 1.0f

Distance from camera to view plane.

Definition at line 1867 of file mxvk_math.h.

◆ viewplane_height

float mxvk::Camera::viewplane_height = 2.0f

View-plane height.

Definition at line 1891 of file mxvk_math.h.

◆ viewplane_width

float mxvk::Camera::viewplane_width = 2.0f

View-plane width.

Definition at line 1894 of file mxvk_math.h.

◆ viewport_center_x

float mxvk::Camera::viewport_center_x = 0.0f

Viewport center X coordinate.

Definition at line 1903 of file mxvk_math.h.

◆ viewport_center_y

float mxvk::Camera::viewport_center_y = 0.0f

Viewport center Y coordinate.

Definition at line 1906 of file mxvk_math.h.

◆ viewport_height

float mxvk::Camera::viewport_height = 1.0f

Viewport height in pixels.

Definition at line 1900 of file mxvk_math.h.

◆ viewport_width

float mxvk::Camera::viewport_width = 1.0f

Viewport width in pixels.

Definition at line 1897 of file mxvk_math.h.


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