Foundation
Loading...
Searching...
No Matches
ModelViewProjection.hpp
Go to the documentation of this file.
1#pragma once
2#include "Math.hpp"
3namespace Foundation::Math
4{
5 // zNear -> zNDC = 1, zFar (inf) -> zNDC = 0
6 // View perspective: Right = +X, Up = +Y, Forward = -Z
7 inline mat4 infinitePerspectiveRHReverseZ(float fovY /* radians */, float a /* aspect W/H */, float zNear)
8 {
9 float f = 1 / tan(fovY / 2);
10 return mat4{f / a, 0, 0, 0, 0, f, 0, 0,
11 // zNDC = zNear/z
12 0, 0, 0, -1, 0, 0, zNear, 0};
13 }
14
15 // zNear -> zNDC = 1, zFar -> zNDC = 0
16 // View perspective: Right = +X, Up = +Y, Forward = -Z
17 inline mat4 perspectiveRHReverseZ(float fovY, float a, float zNear, float zFar)
18 {
19 float f = 1 / tan(fovY / 2);
20 return mat4{f / a, 0, 0, 0, 0, f, 0, 0,
21 // zNDC = zNear/z
22 0, 0, zNear / (zFar - zNear), -1, 0, 0, (zFar * zNear) / (zFar - zNear), 0};
23 }
24
25 // Forward = -Z
26 inline mat4 viewMatrixRHReverseZ(vec3 pos, quat rot)
27 {
28 mat4 view = mat4_cast(rot);
29 view[3] = vec4(pos.x, pos.y, pos.z, 1.0f);
30 return inverse(view);
31 }
32
33 // (i,j,k,l), where left/right planes are ix +- jz = 0, top/bottom planes are ky +- lz = 0
34 // See Fast Extraction of Viewing Frustum Planes from the WorldView-Projection Matrix
35 // https://www.gamedevs.org/uploads/fast-extraction-viewing-frustum-planes-from-world-view-projection-matrix.pdf
36 inline float4 planeSymmetric(mat4 proj)
37 {
38 mat4 projT = transpose(proj);
39 float4 left = projT[3] + projT[0]; // (m41 + m11, m42 + m12, m43 + m13, m44 + m14)
40 float4 bottom = projT[3] + projT[1]; // (m41 + m21, m42 + m22, m43 + m23, m44 + m24)
41 // Normalize
42 left /= length(left.xyz());
43 bottom /= length(bottom.xyz());
44 return {left.x, left.z, bottom.y, bottom.z};
45 }
46} // namespace Foundation::Math
Definition Decompose.hpp:4
mat4 infinitePerspectiveRHReverseZ(float fovY, float a, float zNear)
Definition ModelViewProjection.hpp:7
vec4 float4
Definition Math.hpp:25
mat4 perspectiveRHReverseZ(float fovY, float a, float zNear, float zFar)
Definition ModelViewProjection.hpp:17
mat4 viewMatrixRHReverseZ(vec3 pos, quat rot)
Definition ModelViewProjection.hpp:26
float4 planeSymmetric(mat4 proj)
Definition ModelViewProjection.hpp:36