Class View2Camera
- java.lang.Object
-
- renderer.pipeline.View2Camera
-
public final class View2Camera extends Object
Transform eachVertex
of aModel
from theCamera
's (shared) view coordinates to normalized camera coordinates.This stage transforms the
Camera
's view volume from a user defined shape (in the view coordinate system) into the standard normalized view volume (in the camera coordinate system) used by theClip
pipeline stage.There are two standard normalized view volumes, one for perspective projection and one for orthographic projection.
The standard normalized perspective view volume is the infinitely long pyramid with its apex at the origin and intersecting the image plane
z = -1
at the corners(-1, -1, -1)
and(+1, +1, -1)
.The standard normalized orthographic view volume is the infinitely long parallelepiped centered on the z-axis and intersecting the image plane
z = -1
at the corners(-1, -1, -1)
and(+1, +1, -1)
.The user defined view volume determined by the
Scene
'sCamera
object is either the infinitely long pyramid with its apex at the origin and intersecting the image planez = -1
at the corners(left, bottom, -1)
and(right, top, -1)
, or it is the infinitely long parallelepiped parallel to the z-axis and intersecting the image planez = -1
at the corners(left, bottom, -1)
and(right, top, -1)
.The view coordinate system is relative to the user defined view volume.
The normalized camera coordinate system is relative to the normalized view volume.
The transformation formulas that transform the user defined view volume into the normalized view volume also transform the view coordinate system into the normalized camera coordinate system.
We use two steps to transform the camera's perspective view volume into the standard perspective view volume. The first step skews the camera's view volume so that its center line is the negative z-axis (this takes an asymmetric view volume and makes it symmetric). The second step scales the skewed view volume so that it intersects the image plane,
z = -1
, with corners(-1, -1, -1)
and(+1, +1, -1)
(this gives the symmetric view volume a 90 degree field-of-view).We also use two steps to transform the camera's orthographic view volume into the standard orthographic view volume. The first step translates the camera's view volume so that its center line is the negative z-axis (this takes an asymmetric view volume and makes it symmetric). The second step scales the translated view volume so that it intersects the image plane
z = -1
with corners(-1, -1, -1)
and(+1, +1, -1)
.Let us derive the formulas for transforming the camera's perspective view volume into the standard perspective view volume. Suppose the camera's perspective view volume has an asymmetrical cross section in the yz-plane that is determined by the top and bottom points
(t, -1)
and(b, -1)
. The center line of this cross section is determined by the point((t+b)/2, -1)
. We want to skew the yz-plane in the y-direction along the z-axis so that the field-of-view's center line becomes the z-axis. So we need to solve this matrix equation for the value of the skew factors
.
This simplifies to[ 1 s ] * [ (t+b)/2 ] = [ 0 ] [ 0 1 ] [ -1 ] [ -1 ]
(t + b)/2 - s = 0, s = (t + b)/2.
A similar calculation can be made for skewing the field-of-view in the xz-plane.
The following matrix equation skews the camera's view volume along the z-axis so that the transformed view volume will be centered on the negative z-axis.
[ 1 0 (r+l)/2 ] [ x ] [ x + z * (r + l)/2 ] [ 0 1 (t+b)/2 ] * [ y ] = [ y + z * (t + b)/2 ] [ 0 0 1 ] [ z ] [ z ]
Once the field-of-view in the yz-plane has been made symmetric with respect to the z-axis, we want to scale it in the y-direction so that the scaled field-of-view has an angle at the origin of 90 degrees. We need to scale the point
((t-b)/2, -1)
to the point(1, -1)
(and the point((b-t)/2, -1)
to the point(-1, -1)
). So we need to solve this matrix equation for the value of the scale factors
.
This simplifies to[ s 0 ] * [ (t-b)/2 ] = [ 1 ] [ 0 1 ] [ -1 ] [ -1 ]
s * (t - b)/2 = 1, s = 2/(t - b).
A similar calculation can be made for scaling the skewed field-of-view in the xz-plane.
The following matrix equation scales the skewed view volume to be 2 units wide and 2 units tall at the image plane
z = -1
.[ 2/(r-l) 0 0 ] [ x ] [ (2 * x)/(r - l) ] [ 0 2/(t-b) 0 ] * [ y ] = [ (2 * y)/(t - b) ] [ 0 0 1 ] [ z ] [ z ]
The formulas for transforming the camera's orthographic view volume into the standard orthographic view volume (a translation followed by a scale) are similar but a bit easier to derive. The derivation is left as an exercise for the reader.