order independent transparency

hello all,

i need to implement a GLSL version for the ARB_fragment_program of the nvidia oit_3x example:

   "!!ARBfp1.0
"
    "OPTION ARB_fragment_program_shadow;
"
    "PARAM offset = program.env[0];
"
    "TEMP R0;
"
    "MOV R0, fragment.position;
"
    "ADD R0.x, R0.x, - offset;
"
    "TEX R0.x, R0, texture[0], SHADOWRECT;
"
    "ADD R0.x, R0.x, -0.5;
"
    "KIL R0.x;
"
    "MOV result.color, fragment.color;
"
    "END
";

I try a GLSL code, but it doesn’t work, it does only show black blots.
see my GLSL code:

uniform sampler2DRectShadow shadow;
uniform int offset;
void main(){
   vec3 r0 = gl_FragCoord.xyz;
   r0.x = r0.x - float(offset);
   float tmp = shadow2DRect(shadow, r0).x;
   if (tmp < 0.5)
      discard;
   gl_FragColor = gl_Color;
}

In the C++ code I changed only the part:

glEnable(GL_FRAGMENT_PROGRAM_ARB);
    
if(peel)
   peel_shader.bind();
   //fp_peel.bind();
else
   no_peel_shader.bind();
   //fp_nopeel.bind();
  

where no_peel_shader is

 void main(){
  gl_FragColor = gl_Color;
}  

I assumed that the other parts of the c++ code are correcly initialized, texture_rectangles for example. I believe don’t need modify anything, isn’t it?

thanks for your attention,

Do you set the value of the offset uniform correctly?

Yes, i do it in the same way that ARB_fragment_program parameters.

The offset parameter isn’t necessary as OpenGL samples from the center of texels (unlike Direct3D).

I downloaded the depth peeling 2 demo from nvida (it was the only one I could find) and their ARB fragment program is:

char _fp_peel[] =
        "!!ARBfp1.0
"
        "OPTION ARB_fragment_program_shadow;
"
        "TEMP R0;
"
        "TEX R0.x, fragment.position, texture[0], SHADOWRECT;
"
        "ADD R0.x, R0.x, -0.5;
"
        "KIL R0.x;
"
        "MOV result.color, fragment.color;
"
        "END
";

This would translate to GLSL as:

void main()
{
    vec4 tmp = textureRect(rt, gl_FragPos.xy);
    // or use shadowRect or whatever texture function will do the depth compare for you
    if(tmp.x - 0.5 < 0.0)
    {
        discard;
    }
    gl_FragColor = gl_Color;
}

your code was very close, but offsetting the texture lookup is a bug.

But, you might want to make sure that your shader is actually compiling and linking (by checking the infolog) because it also might sound like your hardware isn’t supporting something in the application (like the shader or rendertargets, etc.).

Hope this helps some.

Originally posted by yalmar:
Yes, i do it in the same way that ARB_fragment_program parameters.
You’re using glUniform*() calls, right? Not glProgramEnvParameter*ARB()?

Of course, for using offset in GLSL i used glUniform.
I implemented the glsl version for the layerz_3x demo, and it works fine, but in the oit_3x demo I no yet tested. I believe that works too.

Take a look at the implementation in VTK:

http://www.vtk.org/Wiki/VTK/Depth_Peeling

in VTK/Rendering/vtkOpenGLRenderer.cxx, there is the following code:

uniform sampler2DRectShadow shadowTex;
uniform sampler2DRectShadow opaqueShadowTex;
uniform float offsetX;
uniform float offsetY;
uniform int useTexture;
uniform sampler2D texture;
void main()
{
 vec4 r0=gl_FragCoord;
 r0.x=r0.x-offsetX;
 r0.y=r0.y-offsetY;
 float r1=shadow2DRect(opaqueShadowTex,r0.xyz).x;
 r1=r1-0.5;
 if(r1<0.0)
 {
  discard;
 }
 r0.x=shadow2DRect(shadowTex,r0.xyz).x;
 r0.x=r0.x-0.5;
 if(r0.x<0.0)
 {
  discard;
 }
 if(useTexture==1)
 {   gl_FragColor=gl_Color*texture2D(texture,gl_TexCoord[0].xy);
 }
 else
 {
  gl_FragColor=gl_Color;
 }
}

VTK Rendering? I tested it, but … I think isn’t a good implementation of OIT, and in 8XXX series’ NVIDIA GPUs doesn’t work fine.

Someone has an implementation like as “Multi-layer depth peeling via fragment sort”?