How to write texture declared with GL_DEPTH_COMPONENT from shader?

Hi! I created a 2d texture with internal format GL_DEPTH_COMPONENT and attached it to my frame buffer as GL_COLOR_ATTACHMENT1:

glGenTextures(1, &m_nId);
glBindTexture(GL_TEXTURE_2D, m_nId);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, m_nWidth, m_nHeight, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
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_COMPARE_MODE, GL_NONE);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, m_nId, 0);

I don’t use this texture for depth testing, so I don’t need to use GL_DEPTH_ATTACHMENT (I have another texture for that purpose).

Now, I am trying to write the texture from a simple fragment shader:

//Already bound my frame buffer
glDrawBuffer(GL_COLOR_ATTACHMENT1);

fragment code:

#version 330 core 

in vec4  color;

layout (location = 0) out float Depth;//I have tried also with vec3


void main()
{    
   
	Depth = gl_FragCoord.z;//I have tried also with vec3(gl_FragCoord.z)
	
	//scrivo sul depth buffer per effettuare il test
	gl_FragDepth = gl_FragCoord.z;

}

My GL_COLOR_ATTACHMENT1 is always dark, it seems I cannot write on it.
Plus I cannot clear the color attachment with:

//Framebuffer bind
	  glDrawBuffer(GL_COLOR_ATTACHMENT1);
     glClearColor(1.0f, 1.0f, 1.0f, 1.0f);

My GL_COLOR_ATTACHMENT1 is still black.
I think I am messing up with the internal format (GL_DEPTH_COMPONENT).

I appreciate any helps :slight_smile:

That’s wrong. A GL_DEPTH_COMPONENT (or sized variant) should only be attached to GL_DEPTH_ATTACHMENT.

AFAICT, colour textures and depth textures are fundamentally incompatible. You cannot use a non-depth texture as a depth buffer and can’t use a depth texture as a colour buffer. You cannot create texture views for depth textures. AFAICT, the only way to transfer information between them is via glGetTexImage and glTexSubImage*. Depth textures cannot be used as images (glBindImageTexture).

1 Like

You can create views of depth textures. It’s just that the view must also be a depth texture (if the texture had stencil information, then the view can be created as a stencil-presenting texture), so you can’t use the view as a color texture. :wink:

1 Like

Thanks for your answer. This is totally right.

I was messing up trying to read the GL_DEPTH_COMPONENT.
My mistake comes from a misunderstanding of glTexImage2D function for the format and the internalformat parameters.

I thougt that giving GL_FLOAT as input makes the GL_DEPTH_COMPONENT readable as float values from the shaders (like a “normal” texture).
I found a good explanation of the function here: https://stackoverflow.com/questions/34497195/difference-between-format-and-internalformat

Finally I was able to save my gl_FragCoord.z values using a high precision texture :smiley: (I found out that my original problem was a cast/precision problem).

Thanks again :slight_smile: