Login  Register

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

Posted by jmaasing on Oct 17, 2016; 5:38pm
URL: https://forum.jogamp.org/FBO-s-I-cant-make-render-to-depth-buffer-work-tp4037322p4037323.html

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