FBO's - I cant make render to depth buffer work

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

FBO's - I cant make render to depth buffer work

phil1234
Hi,

I am getting insane trying to make this work

the point is to get the depth buffer values (with FBOs) for later post processing in a shader

once I add a depth texture, the framebuffer gets corrupted, and I really dont understand what's going on

some say to use render buffer, some say I should use textures instead

plz healp

    public void createFrameBuffer(GL2ES2 gl,int width,int height,SceneGraph frameBufferSceneGraph,boolean depthBuffer)
    {
        this.frameBufferSceneGraph=frameBufferSceneGraph;

        frameBufferObject = genFBO(gl);
        gl.glBindFramebuffer(GL.GL_FRAMEBUFFER, frameBufferObject);

        Texture frameBufferTexture=material.getDiffuseTexture();
        frameBufferTexture.bind(gl);
        gl.glFramebufferTexture2D(GL.GL_FRAMEBUFFER, GL.GL_COLOR_ATTACHMENT0, GL.GL_TEXTURE_2D, frameBufferTexture.getTextureObject(), 0);
       
        if(depthBuffer)
        {
            depthBufferTexture=Tools.createEmptyTexture(gl,frameBufferTexture.getWidth(),frameBufferTexture.getHeight(),Color.yellow);
            depthBufferTexture.bind(gl);
            gl.glFramebufferTexture2D(GL.GL_FRAMEBUFFER, GL.GL_DEPTH_ATTACHMENT, GL.GL_TEXTURE_2D, depthBufferTexture.getTextureObject(), 0);
        }

        gl.glDrawBuffers(1,new int[]{GL.GL_COLOR_ATTACHMENT0},0);
       
        if (gl.glCheckFramebufferStatus(GL.GL_FRAMEBUFFER) != GL.GL_FRAMEBUFFER_COMPLETE)
            System.err.println("Error! FrameBuffer is not complete");
       
        gl.glBindFramebuffer(GL.GL_FRAMEBUFFER, 0);        
        //System.out.println("frameBufferObject: "+frameBufferObject+" renderBuffer:"+renderBuffer);
    }


thanks
Reply | Threaded
Open this post in threaded view
|

Re: FBO's - I cant make render to depth buffer work

jmaasing
I don't have a self contain running sample but I can give you some snippets of the code I use (Open GL 4.1):

The depth buffer is not a texture but frame buffer.

this.textureNames = new int[1] ;
this.frameBufferNames = new int[1];

// Generate texture
gl.glGenTextures(this.textureNames.length, this.textureNames, 0);
gl.glBindTexture(GL4.GL_TEXTURE_2D, this.textureNames[0]);
// MUST turn of mip-maps or the texture isn't "complete" and things will not work (usually just a black texture)
gl.glTexParameteri(GL4.GL_TEXTURE_2D, GL4.GL_TEXTURE_BASE_LEVEL, 0);
gl.glTexParameteri(GL4.GL_TEXTURE_2D, GL4.GL_TEXTURE_MAX_LEVEL, 0);
gl.glTexImage2D(
		GL4.GL_TEXTURE_2D,
		0,
		GL4.GL_RGBA,
		SCREEN__WIDTH,
		SCREEN_HEIGHT,
		0,
		GL4.GL_RGBA,
		GL4.GL_UNSIGNED_BYTE,
		ByteBuffer.allocate(SCREEN__WIDTH * SCREEN_HEIGHT * 4)
);

gl.glGenFramebuffers(this.frameBufferNames.length, this.frameBufferNames, 0);
gl.glBindFramebuffer(GL4.GL_DRAW_FRAMEBUFFER, this.frameBufferNames[0]);
// Setup color attachment (texture)
gl.glFramebufferTexture2D(GL4.GL_DRAW_FRAMEBUFFER, GL4.GL_COLOR_ATTACHMENT0, GL4.GL_TEXTURE_2D, this.textureNames[0], 0);
gl.glDrawBuffer(GL4.GL_COLOR_ATTACHMENT0);
// Setup depth buffer
gl.glGenRenderbuffers(this.renderBufferNames.length, this.renderBufferNames, 0);
gl.glBindRenderbuffer(GL4.GL_RENDERBUFFER, this.renderBufferNames[0]);
gl.glRenderbufferStorage(GL4.GL_RENDERBUFFER, GL4.GL_DEPTH_COMPONENT, SCREEN__WIDTH, SCREEN_HEIGHT);
gl.glFramebufferRenderbuffer(GL4.GL_FRAMEBUFFER, GL4.GL_DEPTH_ATTACHMENT, GL4.GL_RENDERBUFFER, this.renderBufferNames[0]);

if (gl.glCheckFramebufferStatus(GL4.GL_FRAMEBUFFER) != GL4.GL_FRAMEBUFFER_COMPLETE) {
	throw new IllegalStateException("Framebuffer : " + this.frameBufferNames[0] + " is not complete");
}

When rendering you bind the framebuffer and then let the shaders draw as usual, someting like this:

gl.glBindFramebuffer(GL4.GL_DRAW_FRAMEBUFFER, this.frameBufferNames[0]);
gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f - ambientLight);
gl.glClearDepthf(1.0f);
gl.glClear(GL4.GL_COLOR_BUFFER_BIT | GL4.GL_DEPTH_BUFFER_BIT);
gl.glEnable(GL4.GL_DEPTH_TEST);
gl.glDepthFunc(GL4.GL_LEQUAL);
gl.glDepthMask(true);
gl.glDepthRange(0.0f, 1.0f);

// Just some draw commands here
gl.glUseProgram(this.flatColorShaderProgram);
gl.glBindVertexArray(this.triangleVertexArrayInfo.getVAOId());
gl.glEnableVertexAttribArray(this.shaderPosition);
gl.glDrawArrays(GL4.GL_TRIANGLES, 0, 3);

// Reset some values back to the default
gl.glDisableVertexAttribArray(this.shaderPosition);
gl.glBindVertexArray(0);
gl.glUseProgram(0);
gl.glBindFramebuffer(GL4.GL_DRAW_FRAMEBUFFER, 0);

The you can draw the texture to screen (or whatever you want).
gl.glActiveTexture(GL4.GL_TEXTURE0);
gl.glBindTexture(GL4.GL_TEXTURE_2D, this.textureNames[0]);
// Draw the texture on something, I use two triangles to blit the texture to screen
gl.glUseProgram(this.textureShaderProgram);
gl.glUniform1i(this.textureUniform, textureUnit);
Reply | Threaded
Open this post in threaded view
|

Re: FBO's - I cant make render to depth buffer work

phil1234
This post was updated on .
- what I want to achieve is to get the depth values from the z buffer for a second pass (post processing)

I you check my code I try to enable 2 textures for the FBO

- you also use a texture for the color
and for the depth you use a render buffer (you cannot retreive values from a render buffer, it's there to make things work)

I already did that and it worked fine

Reply | Threaded
Open this post in threaded view
|

Re: FBO's - I cant make render to depth buffer work

jmaasing
Oh, my bad, I missed that part. Sorry, I have not done that so I have no clue why it isn't working.
Reply | Threaded
Open this post in threaded view
|

Re: FBO's - I cant make render to depth buffer work

phil1234
In reply to this post by phil1234