Function perspective_RH_01

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

Mat4!T perspective_RH_01(T) (
  in T fovx,
  in T aspect,
  in T near,
  in T far
)
if (isFloatingPoint!T);

Parameters

NameDescription
fovx horizontal field of view in degrees
aspect aspect ratio (width / height)
near position of the near plane
far position of the far plane

Returns

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

Example

const m = perspective_RH_01!float(90, 2, 2, 4);
const vl = fvec(-2, -1, -2);
const vh = fvec(4, 2, -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) ));