Definitions/Variables used

Name | Description |
---|---|

n | Distance from camera position to near plane |

Camera View direction | Positive z axis |

f | Distance from camera position to far plane |

s | Aspect ratio=width/height |

g | Distance from camera position to projection plane |

Field of View | Angle between left/right or top/bottom frustum planes. |

fovY = 2tan^{-1}(1/g)

g = tan^{-1}(fovY/2)

To calculate camera-space frustum planes:

Frustum Plane | Camera-space (x,y,z,w) |
---|---|

Near | (0,0,1,-n) |

Far | (0,0,-1,f) |

Left | (1/sqrt(g^{2}+s^{2}))(g,0,s,0) |

Right | (1/sqrt(g^{2}+s^{2}))(-g,0,s,0) |

Top | (1/sqrt(g^{2}+1))(0,g,1,0) |

Bottom | (1/sqrt(g^{2}+1))(0,-g,1,0) |

To calculate the corner points on the near/far frustum planes **q**0,**q**1,**q**2,**q**3:

q0 | c + ((us)/g)x + (u/g)y + uz |

q1 | c + ((us)/g)x - (u/g)y + uz |

q2 | c - ((us)/g)x - (u/g)y + uz |

q3 | c - ((us)/g)x + (u/g)y + uz |

**x** = **M**_{camera[0]} (column 0),
**y** = **M**_{camera[0]} (column 1),
**z** = **M**_{camera[0]} (column 2),
**c** = **M**_{camera[3]} (column 3),
*u* = *n* for near plane points, *f* for far plane points.

Reversing the depth buffer value/z allows for less floating point errors/more accuracy at farther z-positions away from the camera as opposed to very close to the near clip plane:

Normal Perspective Projection Matrix:

Infinite Distance Perspective Projection Matrix:

Reversed depth infinite distance perspective projection matrix:

This is a good choice for the default projection matrix to use, just need to make sure to flip the depth test pass condition from "greater than" to "less than"

Orthographic Projection Matrix:

Extracting camera-space planes from "canonical view volume" (normalized device coordinates cube):

Frustum Plane | Clip-Space | Camera-Space |
---|---|---|

Left | (1,0,0,1) | P3 + P0 |

Right | (-1,0,0,1) | P3 - P0 |

Top | (0,1,0,1) | P3 + P1 |

Bottom | (0,-1,0,1) | P3 - P1 |

Regular projection matrix | ||

Near | (0,0,1,0) | P2 |

Far | (0,0,-1,1) | P3-P2 |

Reversed Depth projection matrix | ||

Near | (0,0,-1,1) | R3-R2 |

Far | (0,0,1,0) | R2 |

Pi means projection matrix zero-based index row *i*, Ri for reversed-depth projection
matrix row *i*

To reflect scene about a world-space boundary plane **k**,
**M**_{reflection} = **H**_{reflect}(**k**)**M**_{camera}.

Then to modify a projection matrix to clip points by the boundary plane,
replace the third row of P with m**k**, or R3 - m**k** for reversed depth
proj mat.

m = 1/dot(k,**v**_{camera})

**v**_{camera} = (sign(k_{x})-P_{02})/P_{00},
(sign(k_{y})-P_{12})/P_{11}, 1,
(1-P_{22})/P_{23})