I have a texture with GL_DEPTH_COMPONENT and retrieved the values via glCopyTexture function, in debugger everything is fine and depth can be seen properly. I can’t figure out how to read the depth in a fragment shader since on the web some suggest texture2D function while others say it will return undefined results and shadow2D is the only way. Also the compare texture mode is confusing. Help and source examples are welcome.
DmitryM’s got you covered. A bit more detail from what I’ve read.
You have a choice: do hardware depth compare (with or without PCF) or not.
For pre-GLSL 1.3: For depth compare, use a Shadow sampler, enable GL_TEXTURE_COMPARE_MODE and use shadow* texture access method (e.g. shadow2DProj). For no depth compare, use a non-Shadow sampler, don’t enable GL_TEXTURE_COMPARE_MODE and use a texture* texture access method (e.g. texture2DProj).
For GLSL 1.3+: Same, except GLSL 1.3 removes the typing redundancy between sampler names and texture access method names. Just use the appropriate texture* texture access method in all cases (e.g. textureProj).
Thanks so far. I tried both ways and seen that the returned value is actually strange for sampler2D. Someone on the web powered the returned R to 70 to get proper depth. <pow(depthv,70.0)>. Apparently they are distributed in 0.9 - 1.0 range or something like that. Why the specs say using GL_DEPTH_COMPONENT an sampler2D will be undefined yet many people use it? I’m missing something?
Could easily be. It all depends on the projection transform in-play when you’re rendering that depth map. With a standard perspective (point lights), most of your precision is clustered near the near plane (0.0).
For better precision in your scene (e.g. widen that 0.9…1.0 range), push the near clip out before computing your light-space projection transform.