Render Depth Buffer to a texture

Hi,

I’m trying to render depth buffer to texture, and after much trying, it seems to be comming out plain at a fixed value.

I’m using a Multisampled FrameBuffer.

I have a render to linearize it and it seems to be coming out plain, I’ve made an if statement to check its value, it seems its coming bigger than 1.0.

		public void attach() {
			renderBufferObject = glGenRenderbuffers();
			texture  	       = glGenTextures();
			glBindRenderbuffer(GL_RENDERBUFFER, renderBufferObject);
			glRenderbufferStorageMultisample(GL_RENDERBUFFER, samples, GL_DEPTH24_STENCIL8, width, height);	
			glBindRenderbuffer(GL_RENDERBUFFER, renderBufferObject);
			glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, renderBufferObject);

			glBindTexture(GL_TEXTURE_2D, texture);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
			glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32F, width, height, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, (ByteBuffer) null);
			glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, texture, 0);
		}

Is there something wrong here?

OK, so how do you know that it comes out “plain at a fixed value”? You cannot read a multisample depth buffer with glReadPixels or even glGetTexImage.

So how do you know what values are in it?

In the fragment shader.

So, doing a G-Buffer Shader (MRT), and the DepthMap texture is comming with all values bigger than 1.0 for some reason, using this code below gives me a plain blue color:

float d = linear_depth(textureDepth, tex_coord, viewport);
if(d > 1.0) 
	color = vec3(0.0, 0.0, 1.0);
else color = vec3(1.0, 0.0, 0.0);

and then I am drawing all the attachments this way:

	public void renderTextures() {
		for(int i = 0; i < attachments.length; i++) {
			glBindFramebuffer(GL_READ_FRAMEBUFFER, multisampledFbo);
			glReadBuffer(GL_COLOR_ATTACHMENT0+i);
			glBindFramebuffer(GL_DRAW_FRAMEBUFFER, finalFbo);
			glDrawBuffer(GL_COLOR_ATTACHMENT0+i);
			glBlitFramebuffer(0, 0, width, height, 0, 0, width, height, GL_COLOR_BUFFER_BIT, GL_NEAREST);
		}

		glBindFramebuffer(GL_READ_FRAMEBUFFER, multisampledFbo);
		glReadBuffer(GL_DEPTH_ATTACHMENT);
		glBindFramebuffer(GL_DRAW_FRAMEBUFFER, finalFbo);
		glDrawBuffer(GL_DEPTH_ATTACHMENT);
		glBlitFramebuffer(0, 0, width, height, 0, 0, width, height, GL_DEPTH_BUFFER_BIT, GL_NEAREST);
	}

The color attachment part works fine, and I’m able to write to to it using gl_FragData[i] and acess it afterwards from the texture in the fragment shader.

The depth component, for some reason is not being set to the texture.

Is it something I am missing?

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.