26 const float length =
Length(v);
27 return length > 0.0f ? v / length :
float3{};
34 float x = 0.0f,
y = 0.0f,
z = 0.0f,
w = 1.0f;
42 const float s = std::sin(radians * 0.5f);
43 return { a.
x * s, a.
y * s, a.
z * s, std::cos(radians * 0.5f) };
49 return {
w * b.
x +
x * b.
w +
y * b.
z -
z * b.
y,
50 w * b.
y -
x * b.
z +
y * b.
w +
z * b.
x,
51 w * b.
z +
x * b.
y -
y * b.
x +
z * b.
w,
52 w * b.
w -
x * b.
x -
y * b.
y -
z * b.
z };
60 return v + t *
w +
Cross(u, t);
66 const float length = std::sqrt(
x *
x +
y *
y +
z *
z +
w *
w);
67 if (!(length > 0.0f))
return {};
68 return {
x / length,
y / length,
z / length,
w / length };
83 const float w = r.
w != 0.0f ? r.
w : 1.0f;
84 return { r.
x / w, r.
y / w, r.
z / w };
89 return { { 1, 0, 0, t.
x }, { 0, 1, 0, t.
y }, { 0, 0, 1, t.
z }, { 0, 0, 0, 1 } };
98 return { { ax.
x, ay.
x, az.
x, position.
x }, { ax.
y, ay.
y, az.
y, position.
y },
99 { ax.
z, ay.
z, az.
z, position.
z }, { 0, 0, 0, 1 } };
110 return { { s.
x, s.
y, s.
z, -
Dot(s, eye) }, { u.
x, u.
y, u.
z, -
Dot(u, eye) },
111 { -f.
x, -f.
y, -f.
z,
Dot(f, eye) }, { 0, 0, 0, 1 } };
117 const float f = 1.0f / std::tan(fovY * 0.5f);
118 const float range = farZ / (nearZ - farZ);
119 return { { f / aspect, 0, 0, 0 }, { 0, -f, 0, 0 }, { 0, 0, range, nearZ * range }, { 0, 0, -1, 0 } };
128 projection.
r1 =
float4{ -projection.
r1.
x, -projection.
r1.
y, -projection.
r1.
z, -projection.
r1.
w };
135 const float h = 2.0f / height, w = h / aspect;
136 const float range = 1.0f / (nearZ - farZ);
137 return { { w, 0, 0, 0 }, { 0, -h, 0, 0 }, { 0, 0, range, nearZ * range }, { 0, 0, 0, 1 } };
157 if (std::fabs(denom) < 1e-6f)
158 return std::nanf(
"");
159 return Dot(point - ray.
origin, normal) / denom;
168 const float denom = 1.0f - b * b;
169 if (std::fabs(denom) < 1e-6f)
170 return std::nanf(
"");
float4x4 Translation(float3 t)
Definition gui_math3d.h:87
float3 Normalize(float3 v)
v at length 1; zero stays zero.
Definition gui_math3d.h:24
float4x4 TRS(float3 position, quat rotation, float3 scale)
Scale, then rotate, then move: an object's transform in the world.
Definition gui_math3d.h:93
float4x4 Perspective(float fovY, float aspect, float nearZ, float farZ)
A perspective projection to Vulkan's clip space: fovY in radians, aspect width over height.
Definition gui_math3d.h:115
float IntersectPlane(const Ray &ray, float3 point, float3 normal)
How far along ray it meets the plane through point with normal; negative or NaN when it doesn't (para...
Definition gui_math3d.h:154
float3 TransformPoint(const float4x4 &m, float3 p)
A point through m, divided by w.
Definition gui_math3d.h:80
float4x4 LookAt(float3 eye, float3 target, float3 up)
The view matrix of a camera at eye looking at target, up roughly up.
Definition gui_math3d.h:103
float ClosestOnLine(const Ray &ray, float3 point, float3 direction)
Where along the line through point with direction (length 1) it comes closest to ray; NaN when they'r...
Definition gui_math3d.h:164
float4 Mul(const float4x4 &m, float4 v)
Definition gui_math3d.h:72
float Dot(float3 a, float3 b)
Definition gui_math3d.h:20
float4x4 Orthographic(float height, float aspect, float nearZ, float farZ)
An orthographic projection to Vulkan's clip space, height world units high.
Definition gui_math3d.h:133
float Length(float3 v)
Definition gui_math3d.h:22
float4x4 Inverse(const float4x4 &m)
m's inverse; the identity when it has none.
float3 Scale(float3 a, float3 b)
Definition gui_math3d.h:29
float3 Cross(float3 a, float3 b)
Definition gui_math3d.h:21
float4x4 ToClipSpace(float4x4 projection, GraphicsAPI api)
A projection to Vulkan's clip space (Perspective, Orthographic) made one to api's: Direct3D's,...
Definition gui_math3d.h:125
Definition gui_logging.h:24
GraphicsAPI
The graphics API a backend presents with; window backends create their windows to match (SDL: SDL_WIN...
Definition gui_graphics_api.h:14
@ SDLGPU
SDL3's GPU API, over Vulkan, Direct3D 12 or Metal as SDL picks.
Definition gui_graphics_api.h:18
@ Metal
Metal, on macOS.
Definition gui_graphics_api.h:19
@ D3D12
Direct3D 12, on Windows.
Definition gui_graphics_api.h:17
A ray: from origin, along direction (length 1).
Definition gui_math3d.h:145
float3 origin
Definition gui_math3d.h:146
float3 At(float t) const
Definition gui_math3d.h:149
float3 direction
Definition gui_math3d.h:147
Definition gui_base_types.h:149
float y
Definition gui_base_types.h:150
float x
Definition gui_base_types.h:150
float z
Definition gui_base_types.h:150
Definition gui_base_types.h:173
float y
Definition gui_base_types.h:174
float x
Definition gui_base_types.h:174
float z
Definition gui_base_types.h:174
float w
Definition gui_base_types.h:174
Definition gui_base_types.h:258
float4 r3
Definition gui_base_types.h:259
float4 r1
Definition gui_base_types.h:259
float4 r2
Definition gui_base_types.h:259
float4 r0
Definition gui_base_types.h:259
A rotation, as a unit quaternion.
Definition gui_math3d.h:33
quat operator*(const quat &b) const
This rotation after b: rotating by the product is rotating by b, then this.
Definition gui_math3d.h:47
static quat AxisAngle(float3 axis, float radians)
radians round axis (any length but zero), counterclockwise looking down it.
Definition gui_math3d.h:39
float y
Definition gui_math3d.h:34
quat Normalized() const
At length 1; a zero quaternion becomes no rotation.
Definition gui_math3d.h:64
float z
Definition gui_math3d.h:34
float3 Rotate(float3 v) const
v rotated.
Definition gui_math3d.h:56
float x
Definition gui_math3d.h:34
float w
Definition gui_math3d.h:34
bool operator==(const quat &) const =default