For the project GlowScript (glowscript.org) I’ve written the code to handle transparency using depth peeling but alas it doesn’t work. I think I’ve narrowed the problem down to the following test cases, and I could sure use some advice/insight.
FIRST TEST CASE
I render the opaque objects to a texture I call D0, with false colors representing depths.
Next I render the opaque objects to a texture I call C0, with real colors determined by object color, lighting, and textures and bumpmaps if any.
If I render to a quad applying the texture D0, I see (as I should) bands of color representing constant depths on the opaque objects, which shows that I’ve created a texture that can serve as a depth buffer.
If I render to a quad applying the texture C0, I see correctly lit and textured opaque objects.
SECOND TEST CASE
After creating D0, I render to C0 but this time I color the fragment like this, where the canvas is 512x512:
gl_FragColor = vec4( texture2D(D0, gl_FragCoord.xy/512.0).xyz, 1.0 );
Alas, when I render to a quad applying the texture C0, it is evident that I’ve set gl_FragColor always to (0.0, 0.0, 0.0, 1.0). All of my tests are consistent with the summary statement that I can apply D0 to a quad but I can’t read information from D0 in the fragment shader, which I need to be able to do for the depth peeling algorithm.
I’ve verified by other tests that gl_FragCoord.x and y range over the interval 0 to 511, so I assume I need to divide by 512 to index into the texture, which I understand to be accessed by variables ranging from 0 to 1; since that’s the case in applying a texture to a triangle.
Any ideas? Of course I may have simply made some simple programming error, but do you see something fundamentally wrong with my test cases?
If this worked, presumably the rest of the already completed algorithm would work. The next step is to render to a texture C1 using the depth information in D0 to discard any points farther away than the opaque objects. The step after that is to render transparent objects to a texture D1 with false colors representing depths, discarding fragments that don’t lie between the opaque objects and C1, and use D0 and D1 to generate C2. Etc. I don’t see any efficient stop criterion, so my plan was simply to go as far as C4 (4 transparent layers) and piously hope that this would be good enough.