Thursday, November 25, 2010

The ultimate projection calculation (almost)


The following builds perspective and parallel projection matrices with support for center offsets and stereoscopic 3D.


Variables:

    s      : stereo-scopic 3D eye separation
    p      : perspective (0 == parallel, 1 == perspective)
    n, f   : near and far z clip depths
    w, h   : width and height of viewport (at depth vz)
    vxyz   : center of viewport
    exyz   : center of projection (eye position)

W-Plane:

    Wx = 0
    Wy = 0
    Wz = p/(vz-ez)
    Ww = (vz(1-p)-ez)/(vz-ez)

Z-Plane:

    Zx = 0
    Zy = 0
    Zz = (2(vz(1-p)-ez)+p(f+n))/((f-n)(vz-ez))
    Zw = -((vz(1-p)-ez)(f+n)+2fnp)/((f-n)(vz-ez))

Y-Plane:

    Yx = 0
    Yy = 2/h
    Yz = 2(ey-vy)/(h(vz-ez))
    Yw = 2(vyez-eyvz)/(h(vz-ez))

X-Plane:

    Xx = 2/w
    Xy = 0
    Xz = (2(ex-vx)+[±s])/(w(vz-ez))
    Xw = (2(vxez-exvz)-[±svz])/(w(vz-ez))

Notes:

The projection produced by this formula has x, y and z extents of -1:+1.

The perspective control value p is not restricted to integer values.

The view plane is defined by z. Objects on the view plane will have a homogeneous w value of 1.0 after the transform.


Upcoming posts will explain what this formulation does, what it does not do and why you should use it.



1 comments:

  1. Thanks to Martin Ridgers for pointing out an error in the original posting.

    ReplyDelete