Rendering depth buffer to a texture

Hello,

I want to render the depth buffer to a texture but i’ve been unsuccesfull so far.
What I’m doing is rendering the scene to a framebuffer with both a color and a depth component attached to it and then rendering the depth component texture to a full screen quad.

Here is how i create the framebuffer and attach a color texture to it

	void Framebuffer::initFramebuffer2D(GLsizei width, GLsizei height, int nComponents)
	{
		GLuint fboHandle;
		glGenFramebuffers(1, &fboHandle);
		glBindFramebuffer(GL_FRAMEBUFFER, fboHandle);

		GLuint textureHandle;
		glGenTextures(1, &textureHandle);
		glBindTexture(GL_TEXTURE_2D, textureHandle);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, width, height, 0, GL_RGBA, GL_FLOAT, 0);

		GLuint colorbuffer;
		glGenRenderbuffers(1, &colorbuffer);
		glBindRenderbuffer(GL_RENDERBUFFER, colorbuffer);
		glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, textureHandle, 0);

		if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
			FS_CORE_WARN("FRAMEBUFFER 2D TEXTURE ERROR");

		this->FBOHandle = fboHandle;
		this->colorTextureHandle = textureHandle;
		this->framebufferDim = GL_TEXTURE_2D;

		glClearColor(0, 0, 0, 0);
		glClear(GL_COLOR_BUFFER_BIT);

		glBindFramebuffer(GL_FRAMEBUFFER, 0);
		glBindTexture(GL_TEXTURE_2D, 0);

		this->width = width;
		this->height = height;
		this->depth = 1;
		this->nComponents = nComponents;
	}

And here is where i attach the depth component

	void Framebuffer::AttachDepthBufferTexture()
	{

		glBindFramebuffer(GL_FRAMEBUFFER, FBOHandle);

		glGenTextures(1, &depthTextureHandle);
		glBindTexture(GL_TEXTURE_2D, depthTextureHandle);

		glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, width, height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, 0);

		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);


		glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, depthTextureHandle, 0);



		if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
			FS_CORE_WARN("FRAMEBUFFER DEPTH ERROR");

		glBindFramebuffer(GL_FRAMEBUFFER, 0);

	}

I then render the scene using the framebuffer and then render the framebuffer textures to a quad.

        glBindFramebuffer(GL_FRAMEBUFFER, depthBuffer->GetFramebufferHandle());

        glViewport(0, 0, screenSize.x, screenSize.y);

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        
        scene->Draw();
        
        glBindFramebuffer(GL_FRAMEBUFFER, 0);
        glBindVertexArray(quadVAO);

        glClear(GL_COLOR_BUFFER_BIT);

        drawTexture->Use();
        glActiveTexture(GL_TEXTURE0);
        glBindTexture(GL_TEXTURE_2D, depthBuffer->GetDepthTextureHandle());
        drawTexture->SetInt("depthTexture", 0);
		glActiveTexture(GL_TEXTURE1);
		glBindTexture(GL_TEXTURE_2D, depthBuffer->GetColorTextureHandle());
        drawTexture->SetInt("colorTexture", 1);


        glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);

Here is the fragment shader code

#version 410

in vec2 uv;
out vec4 Fragcolor;

uniform sampler2D depthTexture;
uniform sampler2D colorTexture;

 
void main() {
 
  if(uv.x < 0.5) {
    Fragcolor = texture(colorTexture, uv);
  }
  else {
 
    Fragcolor = texture(depthTexture, uv);
  }
}

The scene contains a single cube; what I’m expecting to see is the normal scene on the first half of the screen and the depth buffer on the second half. The normal scene render just fine, while the depth buffer texture is completely white.

Does anyone know what I’m doing wrong?

I’m not seeing a call to glUseProgram() in here before you render to the window.

My bad for not making the variable names more clear, but drawTexture->Use() is basically a wrapper function for glUseProgram()