generalizedProjection

fun generalizedProjection(eye: Vector3, viewportCenter: Vector3, viewportWidth: Double, viewportHeight: Double, near: Double, far: Double, perspective: Double = 1.0, stereoOffset: Double = 0.0): Matrix44(source)

Builds the generalized projection matrix described in https://and-what-happened.blogspot.com/2010/11/ultimate-projection-calculation-almost.html

The projection is defined by an off-axis rectangular viewport -- of size viewportWidth by viewportHeight, centered at viewportCenter -- and a center of projection eye, both given in the same (e.g. world or view) space. eye and viewportCenter only need to differ in their z coordinate for an on-axis projection; any difference in x/y produces an off-axis (sheared) frustum, which is what lets this same formula describe an asymmetric viewport -- as used for multi-monitor/CAVE-style setups, where the "eye" can be off-center relative to a fixed screen.

perspective continuously blends between a parallel/orthographic projection (0.0) and a full perspective projection (1.0); values outside that range extrapolate the blend. stereoOffset shifts the viewport horizontally by half an eye separation for stereoscopic rendering -- pass +separation / 2.0 and -separation / 2.0 for the two eyes of a stereo pair, or 0.0 for an ordinary, single-eye projection.

In every case the result maps near..far and the viewportWidth/viewportHeight extent of the viewport rectangle (at its own depth) onto the standard -1..1 normalized device coordinate cube on all three axes; a point exactly on the viewport plane has a homogeneous w of 1.0 after the transform.

near and far are literal z coordinates in the same space as eye and viewportCenter, not positive distances the way e.g. org.openrndr.math.transforms.perspective takes them -- since eye and viewportCenter can differ in x/y too, there is no single "forward" axis this function could use to turn a distance into a coordinate. For a camera at the origin looking down -Z with its near plane one unit in front of it, pass near = -1.0, not near = 1.0.

Parameters

eye

the center of projection (the "camera" position)

viewportCenter

the center of the viewport rectangle, at the depth the rectangle's viewportWidth/viewportHeight are measured at

viewportWidth

width of the viewport rectangle

viewportHeight

height of the viewport rectangle

near

the near clipping plane's z coordinate (see above -- not a distance)

far

the far clipping plane's z coordinate (see above -- not a distance)

perspective

0.0 for parallel (orthographic), 1.0 for perspective, or a blend in between

stereoOffset

a signed horizontal eye-separation offset; 0.0 for a non-stereo projection