Jump to content

  • Log In with Google      Sign In   
  • Create Account


Depth-testing in the fragment shader


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
10 replies to this topic

#1 Fabonymous   Members   -  Reputation: 109

Like
0Likes
Like

Posted 30 April 2012 - 01:50 PM

Hi,

I'm currently trying to implement a fragment shader that discards all fragments that have a great distance to the current depth value. The wished for effect: clipping decals. All fragments near to a wall get rendered. All fragments that are overlaping should get discard.

My setup is as follows:

1. Render the scene into a framebuffer object with a depth buffer attached.
2. Load the depth buffer as texture into the fragment shader.
3. Render the decals

In the fragment shader I have now two depth values, one from gl_FragCoord and one from the texture. Let calls them: fragDepth and textDepth.

float fragDepth = gl_FragCoord.z;
float textDepth = texture2D( depth_texture, gl_FragCoord.xy ).r;

So far, so good. But the next step fails (flickering/does not what it should):

float depthDiff = textDepth - fragDepth;
float threshold = 0.005;

if( depthDiff > threshold )
{
   discard;
}

I'm assuming this fails due to precision issues (?)
What is the best way to proceed? Linearize the depth values? Or am I here completly on the wrong path?

Sponsor:

#2 mhagain   Crossbones+   -  Reputation: 7610

Like
0Likes
Like

Posted 30 April 2012 - 05:48 PM

Any reason not to just use standard depth testing? It will run better and give you more predictable results.

It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.


#3 Fabonymous   Members   -  Reputation: 109

Like
0Likes
Like

Posted 02 May 2012 - 12:04 AM

Any reason not to just use standard depth testing? It will run better and give you more predictable results.


With standard depth testing, glDepthFunc(GLenum func), you can only specify simple test like "GL_LESS", "GL_GREATER", ... as far as I understand, it is not possible to do more complicated processing in the Depth Testing pipeline stage. Maybe it is possible with running multiple passes or an extension, I don't know. That's why I'm asking :-)

#4 mikiex   Members   -  Reputation: 237

Like
0Likes
Like

Posted 02 May 2012 - 02:55 AM

I would look at example implmentations of soft particles as this is a similar test to what you want to do

#5 L. Spiro   Crossbones+   -  Reputation: 12965

Like
0Likes
Like

Posted 02 May 2012 - 03:19 AM

Is there a reason why ::glPolygonOffset() will not work? It works for standard decals. Are yours not standard?


L. Spiro
It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

#6 Fabonymous   Members   -  Reputation: 109

Like
0Likes
Like

Posted 02 May 2012 - 06:12 AM

Is there a reason why ::glPolygonOffset() will not work? It works for standard decals. Are yours not standard?


L. Spiro


As far as I know is glPolygonOffset() used to avoid z-fighting. Can it also be used for clipping?

#7 L. Spiro   Crossbones+   -  Reputation: 12965

Like
0Likes
Like

Posted 02 May 2012 - 10:57 PM

Clipping in what way? What do you want to clip? Perhaps you should explain in what way your decal system differs from the standard.
Your original post says, “clipping decals”. I don’t see what the purpose of doing that is. Z-fighting is the only issue there is to overcome with standard decals.


L. Spiro
It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

#8 Digitalfragment   Members   -  Reputation: 794

Like
1Likes
Like

Posted 02 May 2012 - 11:19 PM

@L.Spiro:
I believe Fabonymous is referring to clipping decals so they dont overhang geometry - for example a splash-damage decal on the edge of a wall.

@Fabonymous:
If this is the case, I don't think depth clipping is viable either as a moving object can end up having polygons close enough to co-planar with the decal sprite, and as a result get drawn to as well. Depending on your platform, there are a few easier ways of solving it, for example performing clipping and rejection in a geometry shader and writing out to a decal vertex buffer.

#9 Fabonymous   Members   -  Reputation: 109

Like
1Likes
Like

Posted 03 May 2012 - 06:00 AM

@Digitalfragment, I come to agree that depth clipping might be the wrong approach. Do you have references/sample code/tutorial on how to implement the clipping stage in a geometry shader?

#10 MJP   Moderators   -  Reputation: 10632

Like
0Likes
Like

Posted 03 May 2012 - 12:33 PM

@Digitalfragment, I come to agree that depth clipping might be the wrong approach. Do you have references/sample code/tutorial on how to implement the clipping stage in a geometry shader?


DICE has a presentation. It's complicated. You may not want to give up on projected decals so easily...games have shipped with it. Posted Image

#11 BigStink   Members   -  Reputation: 102

Like
0Likes
Like

Posted 12 July 2012 - 01:03 PM

@Fabonymous

Late to the party but in case you or someone else happens to this:

float textDepth = texture2D( depth_texture, gl_FragCoord.xy ).r;


This is not going to give you the depth value you expect. gl_FragCoord gives you screen coordinates (screenWidth, screenHeight), so depending on your screen resolution the values change dramatically. For instance at 800x600 gl_FragCoord will give you something in the range of (0-800, 0-600).

You want your texture coordinates to be between 0-1 when sampling the depth texture. So what you need to do is convert gl_FragCoord (screen coords) to something usable. To do that, you simply divide by the screenWidth and screenHeight.

// Samples wrong location in the texture
//float textDepth = texture2D( depth_texture, gl_FragCoord.xy ).r;
// First translate the screen coordinates to texture coordinates
vec2 texCoords = vec2(gl_FragCoord.x / screenWidth, gl_FragCoord.y / screenHeight);
// Now sample the depth
float textDepth = texture2D( depth_texture, texCoords);

If you know the screenWidth and screenHeight are always the same, you can just hard code in the values. Otherwise, you can pass them to your shader using Uniforms




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS