4
votes

I want to render a scene with an outline post processing effect in OpenGL ES 2.0.

First I render all the opaque objects. Then I use a post processing shader for silhouette detection that uses the depth buffer as a texture. And now I want to render all the objects using alpha blending, without writing to the depth buffer, but using depth testing with the values from the depth buffer texture used for silhouette detection.

If I would have rendered the translucent objects before the post processing, the post processing would have rendered the outlines of opaque objects over them since they don't write to the depth buffer.

How do you tell OpenGL ES 2.0 to use a texture as the depth buffer?

Thank you

2

2 Answers

3
votes

Check out the OES_depth_texture extension. With this you can

// generate and bind a new Framebuffer object
glGenFramebuffers( ... );
glBindFramebuffer( ... );

// create a depth texture
glTexImage2D(...,  GL_DEPTH_COMPONENT, ...);

// and attach it to the Framebuffer with     
glFramebufferTexture2D(..., GL_DEPTH_ATTACHMENT, ...);

Without OpenGL extensions there is no perfect solution. You can write depth values into the color buffer, but you will have limited precision there.

This SO thread covers the same question for WebGL which suffers from the same problem.
The GLES2 book covers Framebuffer Objects and Renderbuffer Objects, including extensions, in chapter 12.

0
votes

onDrawFrame(){ ...

            ////attach texture for SSAO
            GLES20.glActiveTexture(GLES30.GL_TEXTURE2);
            GLES20.glBindTexture(GLES30.GL_TEXTURE_2D, TextureXYZ);
            GLES20.glUniform1i(rs.u_textureXYZ, 2);
            GLES20.glActiveTexture(GLES30.GL_TEXTURE3);
            GLES20.glBindTexture(GLES30.GL_TEXTURE_2D, TextureAO_Norm);//нормали
            GLES20.glUniform1i(rs.u_textureAO_norm, 3);
            GLES20.glActiveTexture(GLES30.GL_TEXTURE4);
            GLES20.glBindTexture(GLES30.GL_TEXTURE_2D, Texture_depth);//Depth
            GLES20.glUniform1i(rs.u_textureDepth, 4);

fragment shader ...

uniform sampler2D u_textureXYZ;
uniform sampler2D u_textureAO_norm;
uniform sampler2D u_textureDepth;