Hi all,
I'm trying to use a rendered depth texture in my shader, but I seem to get a uniform value when I sample the texture in the shader. I've tried LOTS of things to get this working but all have failed so far. The best I can get is some kind of contours that makes me think there may be *some* values in there, but I can't get anything usable. Is there a way to read the 'depth' val of the current fragment?
I've also read something about 'DEPTH_TEXTURE_MODE' and 'TEXTURE_COMPARE_MODE' in the GL spec, but it's rather unclear...
Can anyone spot any mistakes here..?
Relevant code:
Init depth texture:
glBindTexture(GL_TEXTURE_2D, ETxDepthRender); // Creates a texture object.
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, KRenderedTextureSize, KRenderedTextureSize, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
Get depth render to texture:
glBindTexture(GL_TEXTURE_2D, ETxDepthRender);
glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, 0, 0, KRenderedTextureSize, KRenderedTextureSize, 0);
Fragment shader excerpts:
uniform sampler2DShadow WaterDepth2d;
...
{
// Add sea colour for water depth:
const float depthVal = shadow2DProj(WaterDepth2d, ProjTexCoord).x;
float ecWaterPos = -ecPos.z*0.005; // Looks ok
float sceneDepth = ZPosFromDepthBufferValue(depthVal);
float distUnderwater = max(0.0f, sceneDepth - ecWaterPos);
...
vec3 color = vec3(depthVal, 0.0, 0.0);
}
float ZPosFromDepthBufferValue(float depthBufferValue)
{
const float f1 = gl_DepthRange.far / (gl_DepthRange.far - gl_DepthRange.near);
const float f2 = gl_DepthRange.far * gl_DepthRange.near / (gl_DepthRange.near - gl_DepthRange.far);
return f2 / (depthBufferValue - f1);
}
Ed: I know my texture projection stuff works ok.
[Edited by - Aph3x on March 7, 2007 4:30:10 PM]