• Advertisement
Sign in to follow this  

Storing Depth

This topic is 2355 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi Everybody,

Working on DFS implementation and mostly reaching to the point when i need to perform lighting pass. Before I start with that just want to make sure that I collect data from previous passes properly. I'm just not very much convenient with depth part.

This is what I do:
1) Create an FBO and bind depth render buffer to it:

glGenRenderbuffersEXT(1, &_depthbuffer);
glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, _depthbuffer);
glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT, _size.X(), _size.Y());
glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, _depthbuffer);

2) Create a texture to store depth values:

2.1) glTexImage2D( GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, size.X(), size.Y(), 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, NULL);

2.2) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F_ARB, size.X(), size.Y(), 0, GL_RGB, GL_FLOAT, NULL);

Please advise which would be the best format for storing depth component

3) Bind depth texture to FBO by
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, texture->GetNativeID(), 0);

4) Then I render the depth using following shader:
uniform float depth;

// vertex shader

void main (void)
{
gl_Position = gl_ModelViewMatrix * gl_Vertex;
depth = gl_Position.z;
gl_Position = gl_ProjectionMatrix * gl_Position;
}

// pixel shader

void main (void)
{
gl_FragColor = vec4(depth, depth, depth, 1.0);
}


I'd appreciate if you could tell me what is wrong and what is done extra here. First, I have a feeling that I maintain two placeholders for the depth, one as a part of depth render buffer, and the second is my depth texture. Should I get rid of one of those? Second suspicion is the depth texture format. Which one should i use DEPTH_COMPONENT_24 or RGBA32F or something else? Third question would be how to actually write the depth value in the shader? I need to write a high precision float number into a pixel, not sure how it would work. Should i perform bitwize operations to split the float value into .r .g .b .a channels of gl_FragColor?

Thank you,
Ruben

P.S. I might also be missing depth /= farClipPlane, am I?

Share this post


Link to post
Share on other sites
Advertisement

Third question would be how to actually write the depth value in the shader?


gl_FragDepth = "your value as float";

by default gl_FragDepth = gl_FragCoord.z;




 


Second suspicion is the depth texture format. Which one should i use DEPTH_COMPONENT_24 or RGBA32F or something else? 




You can't use GL_RGB32F to depth buffer. So, the answer is DEPTH_COMPONENT_24...


 


// vertex shader

void main (void)
{
gl_Position = gl_ModelViewMatrix * gl_Vertex;
depth = gl_Position.z;
gl_Position = gl_ProjectionMatrix * gl_Position;
}






Don't forget that if you want to put depth into fragment shader's gl_FragDepth it clamps in range (... see glDepthClamp parameters). So you what to do...




Share this post


Link to post
Share on other sites
[font="Arial"]
In my current sample i keep gl_FragDepth calculation as it is by default and just write "my depth" texture into a separate FBO attached texture. This way I can keep depth testing more precise close to camera and at the same time even precision when reconstructing position from depth.


Some more details what I do currently:


Initialization:

// Create Diffuse Texture
glGenTextures(1, &_dfsDiffuseTexture);
glBindTexture(GL_TEXTURE_2D, _dfsDiffuseTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, size.X(), size.Y(), 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);


// Create Depth Texture
glGenTextures(1, &_dfsDepthTexture)
glBindTexture(GL_TEXTURE_2D, _dfsDepthTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, size.X(), size.Y(), 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);


// Create FBO
glGenFramebuffersEXT(1, &_fbo);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, _fbo);


// Create Depth Render Buffer
glGenRenderbuffersEXT(1, &_depthbuffer);
glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, _depthbuffer);
glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT, _size.X(), _size.Y());
glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, _depthbuffer);





During Render:

glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, _fbo);
glViewport(0, 0, _size.X(), _size.Y());


glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT + slot, GL_TEXTURE_2D, _dfsDepthTexture, 0);
RenderObjects();


glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT + slot, GL_TEXTURE_2D, _dfsDiffuseTexture, 0);
glClear(GL_COLOR_BUFFER_BIT);
RenderObjects();


// etc...









Shader used to construct depth:

varying float depth;


void main (void)
{
gl_Position = gl_ProjectionMatrix * gl_ModelViewMatrix * gl_Vertex;
vec4 pos = gl_ModelViewMatrix * gl_Vertex;
depth = pos.z;
}




uniform float FarClipPlane;
varying float depth;


void main (void)
{
float d = depth / FarClipPlane;
gl_FragColor = vec4(d, d, d, 1.0);
}





As you see a keep a separate texture to store depth. So my question is should I do that or just try to store depth value into depth render buffer?


If I decide to keep depth texture separate, what would be the way to increase depth precision on that _dfsDepthTexture? How to define the texture, how to write it in the shader, and how to read from it in combine step?[color=#010001][size=2]
[/font]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement