Quick background of where I\'m at (to make sure we\'re on the same page, and sanity check if I\'m missing/assuming something stupid):
Beginning with GLSL 1.30, there is no special texture lookup function (name anyway) for use with sampler2DShadow
. GLSL 1.30+ uses a bunch of overloads of texture (...)
that are selected based on the type of sampler
passed and the dimensions of the coordinates.
sampler2DShadow
you need to do two things differently:Texture comparison must be enabled or you will get undefined results
GL_TEXTURE_COMPARE_MODE
= GL_COMPARE_REF_TO_TEXTURE
The coordinates you pass to texture (...)
are 3D instead of 2D. The new 3rd coordinate is the depth value that you are going to compare.
texture (...)
returns when using sampler2DShadow
:If this comparison passes, texture (...)
will return 1.0, if it fails it will return 0.0. If you use a GL_LINEAR
texture filter on your depth texture, then texture (...)
will perform 4 depth comparisons using the 4 closest depth values in your depth texture and return a value somewhere in-between 1.0 and 0.0 to give an idea of the number of samples that passed/failed.
That is the proper way to do hardware anti-aliasing of shadow maps. If you tried to use a regular sampler2D
with GL_LINEAR
and implement the depth test yourself you would get a single averaged depth back and a boolean pass/fail result instead of the behavior described above for sampler2DShadow
.
As for getting a depth value to test from a world-space position, you were on the right track (though you forgot perspective division).
W
componentThe final step assumes you are using the default depth range... if you have not called glDepthRange (...)
then this will work.
The end result of step 3 serves as both a depth value (R
) and texture coordinates (ST
) for lookup into your depth map. This makes it possible to pass this value directly to texture (...)
. Recall that the first 2 components of the texture coordinates are the same as always, and that the 3rd is a depth value to test.