Files
physics/geometry/frustum.sakura
T
2026-10-30 21:59:14 -05:00

138 lines
5.3 KiB
Plaintext

#! Contains `frustum`, the part of space a camera sees: the six planes bounding
#! what a projection draws, each facing in, and whether a place, a box, or a
#! sphere is within them, so a renderer draws only what might be seen. The
#! planes' numbers are lanes, so an executable holding vectors in registers
#! tests four planes at once, then the other two: as two vectors of four, which
#! it holds in registers as it works them, where one of eight went through
#! memory a half at a time, twice as slow.
import malunal::linear;
module malunal::geometry;
#! The part of space a camera sees, within six planes facing in: left, right,
#! bottom, top, near, and far. It holds their normals' `x`s, `y`s, and `z`s, and
#! how far the origin is in front of each, eight of each, read as two `f32x4`s;
#! the last two are of planes of zeros, which nothing is behind.
pub struct frustum {
var _x: f32[8];
var _y: f32[8];
var _z: f32[8];
var _d: f32[8];
}
#! The frustum of what a matrix projects into its clip space, its planes in the
#! space it projects from: of `projection * view`, the world's, and of a
#! projection alone, the camera's. Clip space bounds x and y between -w and w,
#! and depth between 0 and w, so each plane is the matrix's row of w with
#! another row added or taken away, and the near plane its row of depth alone.
pub def frustum::of(clip: mat4): frustum {
val m := &clip.m;
val x := vec4{ m[0], m[4], m[8], m[12] };
val y := vec4{ m[1], m[5], m[9], m[13] };
val z := vec4{ m[2], m[6], m[10], m[14] };
val w := vec4{ m[3], m[7], m[11], m[15] };
return frustum::of_planes([plane_of(w + x), plane_of(w - x), plane_of(w + y), plane_of(w - y), plane_of(z), plane_of(w - z)]);
}
# The plane of a matrix's row: the places that, with a 1 after them, it makes
# 0 of, in front where it makes more, made of length 1.
def plane_of(row: vec4): plane {
val l := row.xyz.length;
return plane{ row.xyz / l, -row.w / l };
}
#! The frustum of six planes facing in: left, right, bottom, top, near, and far.
pub def frustum::of_planes(planes: plane[6]): frustum {
var made := frustum{};
for i in 0..6u {
made._x[i] = planes[i].normal.x;
made._y[i] = planes[i].normal.y;
made._z[i] = planes[i].normal.z;
made._d[i] = -planes[i].distance;
}
return made;
}
#! Its planes: left, right, bottom, top, near, and far.
pub val frustum::planes: plane[6] {
get {
var made: plane[6];
for i in 0..6u {
made[i] = plane{ vec3{ this._x[i], this._y[i], this._z[i] }, -this._d[i] };
}
return made;
}
}
#! Whether a place is within it.
pub def frustum::contains(&this, p: vec3): bool {
if lanes_held() {
val x := f32x4::splat(p.x);
val y := f32x4::splat(p.y);
val z := f32x4::splat(p.z);
return in_front(this.ahead(0, x, y, z), this.ahead(4, x, y, z));
}
for i in 0..6u {
return false if this._x[i] * p.x + this._y[i] * p.y + this._z[i] * p.z + this._d[i] < 0;
}
return true;
}
#! Whether a box might be seen: whether, of each plane, the box's corner
#! furthest in front of it is in front. A box beside a corner of the frustum,
#! outside it but in front of every plane, counts as seen, which a renderer
#! draws for nothing; one it reaches always counts.
pub def frustum::overlaps(&this, b: bounds): bool {
if lanes_held() {
return in_front(this.ahead_of(0, &b), this.ahead_of(4, &b));
}
for i in 0..6u {
val x := this._x[i] < 0 ? b.min.x : b.max.x;
val y := this._y[i] < 0 ? b.min.y : b.max.y;
val z := this._z[i] < 0 ? b.min.z : b.max.z;
return false if this._x[i] * x + this._y[i] * y + this._z[i] * z + this._d[i] < 0;
}
return true;
}
#! Whether a sphere might be seen: whether its center is no further behind any
#! plane than its radius. As of a box, one beside a corner counts too.
pub def frustum::overlaps(&this, s: sphere): bool {
val c := s.center;
if lanes_held() {
val x := f32x4::splat(c.x);
val y := f32x4::splat(c.y);
val z := f32x4::splat(c.z);
val r := f32x4::splat(s.radius);
return in_front(this.ahead(0, x, y, z) + r, this.ahead(4, x, y, z) + r);
}
for i in 0..6u {
return false if this._x[i] * c.x + this._y[i] * c.y + this._z[i] * c.z + this._d[i] + s.radius < 0;
}
return true;
}
# How far places, a lane each, are in front of four planes from one, as lanes.
def frustum::ahead(&this, first: u64, x: f32x4, y: f32x4, z: f32x4): f32x4 {
return lanes_at(&this._x[first]) * x + lanes_at(&this._y[first]) * y + lanes_at(&this._z[first]) * z + lanes_at(&this._d[first]);
}
# How far a box's corner furthest in front of each of four planes from one is
# in front of it, as lanes.
def frustum::ahead_of(&this, first: u64, b: &bounds): f32x4 {
val zero := f32x4::splat(0);
val x := lanes_at(&this._x[first]);
val y := lanes_at(&this._y[first]);
val z := lanes_at(&this._z[first]);
return x * select(x.less(&zero), f32x4::splat(b.min.x), f32x4::splat(b.max.x))
+ y * select(y.less(&zero), f32x4::splat(b.min.y), f32x4::splat(b.max.y))
+ z * select(z.less(&zero), f32x4::splat(b.min.z), f32x4::splat(b.max.z)) + lanes_at(&this._d[first]);
}
# Whether something is in front of each of eight planes, by how far it's in
# front of each, as lanes.
def in_front(first: f32x4, last: f32x4): bool {
val zero := f32x4::splat(0);
return (first.less(&zero) | last.less(&zero)).none();
}