title: Porting a depth peeling example to modern OpenGL
body: <p>I am trying to port an example of the depth peeling, an Order Independent Transparency technique, to the so-called modern OpenGL (3.3+) but since I am a beginner, it is not that easy..</p>
  
  <p>Here you can find a working version (the GL2) and the one in progress (the GL3)</p>
  
  <p><a href="https://github.com/elect86/modern-jogl-examples/tree/master/modern-jogl-examples/src/depthPeeling" rel="nofollow noreferrer">https://github.com/elect86/modern-jogl-examples/tree/master/modern-jogl-examples/src/depthPeeling</a></p>
  
  <p>I can't see any layer behind...</p>
  
  <p>I guess there are some problems with the alpha value..</p>
  
  <p>I tried to debug it and in the core part</p>
  
  <pre><code>    private void renderDepthPeeling(GL3 gl3) {
          /**
           * (1) Initialize min depth buffer.
           */
          gl3.glBindFramebuffer(GL3.GL_FRAMEBUFFER, colorBlenderFboId[0]);
          gl3.glDrawBuffer(GL3.GL_COLOR_ATTACHMENT0);
  
          gl3.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
          gl3.glClear(GL3.GL_COLOR_BUFFER_BIT | GL3.GL_DEPTH_BUFFER_BIT);
  
          gl3.glEnable(GL3.GL_DEPTH_TEST);
  
          dpInit.bind(gl3);
          {
              gl3.glUniform1f(dpInit.getAlphaUnLoc(), opacity);
  
              drawModel(gl3);
          }
          dpInit.unbind(gl3);
  
          /**
           * (2) Depth peeling + blending.
           */
          int layersNumber = (passesNumber - 1) * 2;
  //        System.out.println("layersNumber: " + layersNumber);
          for (int layer = 1; layer &lt; 2; layer++) {
  
              int currentId = layer % 2;
              int previousId = 1 - currentId;
  
  //            gl3.glBindFramebuffer(GL3.GL_FRAMEBUFFER, fboId[currentId]);
              gl3.glBindFramebuffer(GL3.GL_FRAMEBUFFER, 0);
              gl3.glDrawBuffer(GL3.GL_COLOR_ATTACHMENT0);
  
              gl3.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
              gl3.glClear(GL3.GL_COLOR_BUFFER_BIT | GL3.GL_DEPTH_BUFFER_BIT);
  
              gl3.glDisable(GL3.GL_BLEND);
  
              gl3.glEnable(GL3.GL_DEPTH_TEST);
              {
                  dpPeel.bind(gl3);
                  {
                      gl3.glActiveTexture(GL3.GL_TEXTURE0);
                      gl3.glBindTexture(GL3.GL_TEXTURE_RECTANGLE, depthTextureId[previousId]);
                      gl3.glUniform1i(dpPeel.getDepthTexUnLoc(), 0);
                      {
                          gl3.glUniform1f(dpPeel.getAlphaUnLoc(), opacity);
                          drawModel(gl3);
                      }
                      gl3.glBindTexture(GL3.GL_TEXTURE_RECTANGLE, 0);
                  }
                  dpPeel.unbind(gl3);
  
                  gl3.glBindFramebuffer(GL3.GL_FRAMEBUFFER, colorBlenderFboId[0]);
                  gl3.glDrawBuffer(GL3.GL_COLOR_ATTACHMENT0);
              }
              gl3.glDisable(GL3.GL_DEPTH_TEST);
  
              gl3.glEnable(GL3.GL_BLEND);
              {
                  gl3.glBlendEquation(GL3.GL_FUNC_ADD);
                  gl3.glBlendFuncSeparate(GL3.GL_DST_ALPHA, GL3.GL_ONE, GL3.GL_ZERO, GL3.GL_ONE_MINUS_SRC_ALPHA);
  
                  dpBlend.bind(gl3);
                  dpBlend.bindTextureRECT(gl3, "TempTex", colorTextureId[currentId], 0);
                  {
  //                    gl3.glCallList(quadDisplayList);
                      drawFullScreenQuad(gl3);
                  }
                  dpBlend.unbind(gl3);
              }
              gl3.glDisable(GL3.GL_BLEND);
          }
  
          /**
           * (3) Final pass.
           */
  //        gl3.glBindFramebuffer(GL3.GL_FRAMEBUFFER, 0);
  //        gl3.glDrawBuffer(GL3.GL_BACK);
  //        gl3.glDisable(GL3.GL_DEPTH_TEST);
  //
  //        dpFinal.bind(gl3);
  //        {
  //            gl3.glUniform3f(dpFinal.getBackgroundColorUnLoc(), 1.0f, 1.0f, 1.0f);
  //
  ////            dpFinal.bindTextureRECT(gl3, "ColorTex", colorBlenderTextureId[0], 0);
  //            gl3.glActiveTexture(GL3.GL_TEXTURE0);
  //            gl3.glBindTexture(GL3.GL_TEXTURE_RECTANGLE, colorBlenderTextureId[0]);
  //            gl3.glUniform1i(dpFinal.getColorTexUnLoc(), 0);
  //            {
  ////                gl3.glCallList(quadDisplayList);
  //                drawFullScreenQuad(gl3);
  //            }
  //            gl3.glBindTexture(GL3.GL_TEXTURE_RECTANGLE, 0);
  //        }
  //        dpFinal.unbind(gl3);
      }
  </code></pre>
  
  <p>confronting between the GL2 and GL3 program version the the first and the last passage (1 and 3) looks correct, so the problem lies in the 2</p>
  
  <p>I modified the for cicle in order to get only a cicle</p>
  
  <pre><code>for (int layer = 1; layer &lt; 2; layer++) {
  </code></pre>
  
  <p>and</p>
  
  <pre><code>//            gl2.glBindFramebuffer(GL2.GL_FRAMEBUFFER, fboId[currentId]);
              gl2.glBindFramebuffer(GL2.GL_FRAMEBUFFER, 0);
  </code></pre>
  
  <p>In order to see visually the intermediate result</p>
  
  <p>Well, in the GL2 I get </p>
  
  <p><img src="https://i.stack.imgur.com/SAozB.png" alt="enter image description here"></p>
  
  <p>while in the GL3 </p>
  
  <p><img src="https://i.stack.imgur.com/bpbm2.png" alt="enter image description here"></p>
  
  <p>My dpPeel program is based on dpPeel_VS</p>
  
  <pre><code>#version 330
  
  layout (location = 0) in vec4 position;
  
  layout(std140) uniform mvpMatrixes  {
  
      mat4 projectionMatrix;
      mat4 cameraMatrix;
  };
  
  void main(void)
  {
          gl_Position = projectionMatrix * cameraMatrix * position;
  }
  </code></pre>
  
  <p>And dpPeel_FS plus shade_FS</p>
  
  <pre><code>#version 330
  
  uniform samplerRect DepthTex;
  
  vec4 ShadeFragment();
  
  out vec4 outputColor;
  
  void main(void)
  {
          // Bit-exact comparison between FP32 z-buffer and fragment depth
          float frontDepth = texture(DepthTex, gl_FragCoord.xy).r;
          if (gl_FragCoord.z &lt;= frontDepth) {
                  discard;
          }
  
          // Shade all the fragments behind the z-buffer
          vec4 color = ShadeFragment();
          outputColor = vec4(color.rgb * color.a, color.a);
  }
  
  
  #version 330
  
  uniform float Alpha;
  
  vec4 ShadeFragment()
  {
          vec4 color;
          color.rgb = vec3(.4,.85,.0);
          color.a = Alpha;
          return color;
  }
  </code></pre>
  
  <p>Do you see the error(s)?</p>