Question

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..

Here you can find a working version (the GL2) and the one in progress (the GL3)

https://github.com/elect86/modern-jogl-examples/tree/master/modern-jogl-examples/src/depthPeeling

I can't see any layer behind...

I guess there are some problems with the alpha value..

I tried to debug it and in the core part

    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 < 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);
    }

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

I modified the for cicle in order to get only a cicle

for (int layer = 1; layer < 2; layer++) {

and

//            gl2.glBindFramebuffer(GL2.GL_FRAMEBUFFER, fboId[currentId]);
            gl2.glBindFramebuffer(GL2.GL_FRAMEBUFFER, 0);

In order to see visually the intermediate result

Well, in the GL2 I get

enter image description here

while in the GL3

enter image description here

My dpPeel program is based on dpPeel_VS

#version 330

layout (location = 0) in vec4 position;

layout(std140) uniform mvpMatrixes  {

    mat4 projectionMatrix;
    mat4 cameraMatrix;
};

void main(void)
{
        gl_Position = projectionMatrix * cameraMatrix * position;
}

And dpPeel_FS plus shade_FS

#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 <= 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;
}

Do you see the error(s)?

Was it helpful?

Solution

Ok solved, it was a problem at the blending passage and the full screen quad

If I would like to do lighting now, where should I apply it?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top