Function frustum_LH_M11

Build a perspective projection matrix with left-hand NDC and [-1 .. 1] depth clipping

Mat4!T frustum_LH_M11(T) (
  in T l,
  in T r,
  in T b,
  in T t,
  in T n,
  in T f
);

Parameters

NameDescription
l X position of the left edge at the near plane
r X position of the right edge at the near plane
b Y position of the bottom edge at the near plane
t Y position of the top edge at the near plane
n distance from origin to near plane (in Z-)
f distance from origin to far plane (in Z-)

Returns

a matrix that maps from eye space to clip space. To obtain NDC, the vector must be divided by w.

Example

const m = frustum_LH_01!float(-2, 2, -4, 4, 2, 4);
const vl = fvec(-2, -4, -2);
const vh = fvec(4, 8, -4);
const vc = fvec(0, 0, -3);

auto toNdc(in FVec3 v) {
    const clip = m * fvec(v, 1);
    return (clip / clip.w).xyz;
}

import gfx.math.approx : approxUlp;
assert(approxUlp( toNdc(vl), fvec(-1, -1, 0) ));
assert(approxUlp( toNdc(vh), fvec(1, 1, 1) ));
assert(approxUlp( toNdc(vc), fvec(0, 0, 2f/3f) ));