I’ve got two seemingly stupid problems regarding depth writes in shader (the first one could very well go to the beginner’s section, but anyway)
1st (sillier):
From steve baker’s many-times referenced article about z-buffer, we have the following :
a = zFar / ( zFar - zNear )
b = zFar * zNear / ( zNear - zFar )
gl_FragDepth = a + (b / eyeZ)
But, since ‘a’ is positive & usually around 1, and ‘b’ negative, shouldn’t eyeZ be positive, in order to get a value less than 1.0? But eyeZ is always negative. So, in the above formula eyeZ should be positive, or my logic is dead wrong??
2nd
I have some quads for which I want to specify depth. I have some linear depth values in a texture, from 0.0 to 1.0, and I want to map them in order to recreate a virtual volume (cube), say with an extent cubeExt.
Now, when I render the quads, in the fragment shader I add cubeExt*sampledDepth to the eyeZ, and using the above formula, I compute & write gl_FragDepth. But the results are … wrong. The code is this :
oc2k1 : shouldn’t the formula, if we replace the pm[3].z & pm[2].z with their variable equivalents (-2/(far - near), -(far + near)/(far - near)), result in the same formula I wrote initially? (Either it doesn’t, or I’ve forgotten maths)
Xmas : Actually I found and solved the problem, but another one (which I expected) appeared. I’m trying to pack the depth values along with transparency (its either opaque or not), but the mipmap generation unleashes hell to the values as I lose my 0-or-1 transparency. Any chance to pack 1-bit transparency with 7-bit depth values, in a mipmap friendly way?? (probably asking too much)