glsl sampler2DShadow and shadow2D clarification

别等时光非礼了梦想. 提交于 2019-11-28 06:04:40

First of all, what version of GLSL are you using?

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.

Second, if you do use sampler2DShadow you need to do two things differently:

  1. Texture comparison must be enabled or you will get undefined results

    • GL_TEXTURE_COMPARE_MODE = GL_COMPARE_REF_TO_TEXTURE​

  2. 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.

Last, you should understand what 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).

There are three things you must do to generate a depth from a world-space position:

  1. Multiply the world-space position by your (light's) projection and view matrices
  2. Divide the resulting coordinate by its W component
  3. Scale and bias the result (which will be in the range [-1,1]) into the range [0,1]

The 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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!