Search code examples
openglshadowinfinity

Extrude shadow volumes to infinity


I want to use the depth-fail algorithm to make shadow volumes and all works great, but i can't figure out how to extrude the shadow volume quads to infinity.

The aim is to create the shadow volume for a triangle that is lit up from a point light. I have red that i first have to change the perspective matrix that it has no far clip pane and set the w coordinate to 0. But what are the x,y,z coordinates then?

An example would be very helpful, but i also want to understand how its done.


Solution

  • This link shows an example of a projection matrix. It has the form:

        a   0   b              0
    A = 0   d   e              0
        0   0   -(f+n)/(f-n)   -2fn/(f-n)
        0   0   -1             0
    

    f is the far plane and you want f -> infinity.

    limit f -> infinity of (f+n)/(f-n) = limit f -> infinity of (1+n/f)/(1-n/f)
    

    and

    limit f -> infinity of 2fn/(f-n) = limit f -> infinity of 2n/(1-n/f)
    

    since

    f -> infinity => n/f -> 0
    

    your matrix with f -> infinity becomes

        a   0   b    0
    B = 0   d   e    0
        0   0   -1   -2n
        0   0   -1   0
    

    if you transform your (x,y,z,w=0) with B you'll get

    x' = ax + bz
    y' = dy + ez
    z' = -z
    w' = -z
    

    and the perspective division gives

    x' = -ax/z - b
    y' = -dy/z - e
    z' = 1
    

    While x' and y' are the same as transforming (x,y,z,w=0) with A, z' is now a constant that is always equal to the far plane in normalized device coordinates.

    This article shows an efficient implementation of shadow volumes with capping at infinity.