TachyonUI 1.1.0
High-performance UI for building tools.
Loading...
Searching...
No Matches
gui_math3d.h
Go to the documentation of this file.
1#pragma once
2
3// 3D math for a viewport: vectors, rotations, the camera's matrices and rays.
4//
5// Matrices act on column vectors: p' = M * p, with r0..r3 the matrix's rows; the translation
6// is the last column (r0.w, r1.w, r2.w). A GLSL shader takes them as they are with
7// `layout(row_major)`, or transposed. Right-handed, y up; clip space is Vulkan's: x and y
8// from -1 to 1 with y down, depth from 0 (near) to 1 (far).
9
12
13#include <cmath>
14
15namespace tachyon
16{
19
20 inline float Dot(float3 a, float3 b) { return a.x * b.x + a.y * b.y + a.z * b.z; }
21 inline float3 Cross(float3 a, float3 b) { return { a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x }; }
22 inline float Length(float3 v) { return std::sqrt(Dot(v, v)); }
25 {
26 const float length = Length(v);
27 return length > 0.0f ? v / length : float3{};
28 }
29 inline float3 Scale(float3 a, float3 b) { return { a.x * b.x, a.y * b.y, a.z * b.z }; }
30
32 struct quat
33 {
34 float x = 0.0f, y = 0.0f, z = 0.0f, w = 1.0f;
35
36 bool operator==(const quat&) const = default;
37
39 static quat AxisAngle(float3 axis, float radians)
40 {
41 const float3 a = Normalize(axis);
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) };
44 }
45
47 quat operator*(const quat& b) const
48 {
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 };
53 }
54
57 {
58 const float3 u{ x, y, z };
59 const float3 t = Cross(u, v) * 2.0f;
60 return v + t * w + Cross(u, t);
61 }
62
65 {
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 };
69 }
70 };
71
72 inline float4 Mul(const float4x4& m, float4 v)
73 {
74 return { m.r0.x * v.x + m.r0.y * v.y + m.r0.z * v.z + m.r0.w * v.w,
75 m.r1.x * v.x + m.r1.y * v.y + m.r1.z * v.z + m.r1.w * v.w,
76 m.r2.x * v.x + m.r2.y * v.y + m.r2.z * v.z + m.r2.w * v.w,
77 m.r3.x * v.x + m.r3.y * v.y + m.r3.z * v.z + m.r3.w * v.w };
78 }
79
81 {
82 const float4 r = Mul(m, { p.x, p.y, p.z, 1.0f });
83 const float w = r.w != 0.0f ? r.w : 1.0f;
84 return { r.x / w, r.y / w, r.z / w };
85 }
86
88 {
89 return { { 1, 0, 0, t.x }, { 0, 1, 0, t.y }, { 0, 0, 1, t.z }, { 0, 0, 0, 1 } };
90 }
91
93 inline float4x4 TRS(float3 position, quat rotation, float3 scale)
94 {
95 const float3 ax = rotation.Rotate({ scale.x, 0, 0 });
96 const float3 ay = rotation.Rotate({ 0, scale.y, 0 });
97 const float3 az = rotation.Rotate({ 0, 0, scale.z });
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 } };
100 }
101
103 inline float4x4 LookAt(float3 eye, float3 target, float3 up)
104 {
105 const float3 f = Normalize(target - eye);
106 float3 s = Normalize(Cross(f, up));
107 if (Length(s) == 0.0f) // looking straight up or down: any side will do
108 s = Normalize(Cross(f, std::fabs(f.z) < 0.9f ? float3{ 0, 0, 1 } : float3{ 1, 0, 0 }));
109 const float3 u = Cross(s, f);
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 } };
112 }
113
115 inline float4x4 Perspective(float fovY, float aspect, float nearZ, float farZ)
116 {
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 } };
120 }
121
125 inline float4x4 ToClipSpace(float4x4 projection, GraphicsAPI api)
126 {
127 if (api == GraphicsAPI::D3D12 || api == GraphicsAPI::SDLGPU || api == GraphicsAPI::Metal)
128 projection.r1 = float4{ -projection.r1.x, -projection.r1.y, -projection.r1.z, -projection.r1.w };
129 return projection;
130 }
131
133 inline float4x4 Orthographic(float height, float aspect, float nearZ, float farZ)
134 {
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 } };
138 }
139
142
144 struct Ray
145 {
148
149 float3 At(float t) const { return origin + direction * t; }
150 };
151
154 inline float IntersectPlane(const Ray& ray, float3 point, float3 normal)
155 {
156 const float denom = Dot(ray.direction, normal);
157 if (std::fabs(denom) < 1e-6f)
158 return std::nanf("");
159 return Dot(point - ray.origin, normal) / denom;
160 }
161
164 inline float ClosestOnLine(const Ray& ray, float3 point, float3 direction)
165 {
166 const float3 w = point - ray.origin;
167 const float b = Dot(direction, ray.direction);
168 const float denom = 1.0f - b * b;
169 if (std::fabs(denom) < 1e-6f)
170 return std::nanf("");
171 return (b * Dot(ray.direction, w) - Dot(direction, w)) / denom;
172 }
173
175}
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