将深度剥离示例移植到现代OpenGL中

时间:2013-10-11 16:01:30

标签: transparency alpha opengl-3 depth

我试图将深度剥离(Order Independent Transparency技术)的一个例子移植到所谓的现代OpenGL(3.3+),但由于我是初学者,所以并不容易......

在这里,您可以找到工作版本(GL2)和正在进行的版本(GL3)

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

我看不到背后的任何层......

我想alpha值存在一些问题..

我尝试调试它并在核心部分

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

面对GL2和GL3程序版本的第一个和最后一个段落(1和3)看起来是正确的,所以问题在于2

我修改了for cicle以便只得到一个cicle

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

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

为了直观地看到中间结果

好吧,在GL2中我得到了

enter image description here

在GL3中

enter image description here

我的dpPeel程序基于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;
}

和dpPeel_FS加上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;
}

你看到了错误吗?

1 个答案:

答案 0 :(得分:0)

好的解决了,这是混合通道和全屏四边形的问题

如果我现在想要照明,我应该在哪里应用它?