Sampling depth texture

Started by
7 comments, last by d0nuts 11 years, 3 months ago

Opengl novice here so sorry ahead of time! Just hoping to get some questions answered about sampling textures. I was messing around with rendering a scene to textures so that they could be used later. I then wanted to make a shader to view a depth texture by linearizing the depth values and then just bliting it on screen using a single quad. I can see that everything is being drawn into the texture correctly through gDEBugger (debugger.png) but am having some trouble sampling it .

First, to render the texture I output a single quad covering the screen. Then in vertex shader I multiply by modelviewproj matrix and save the xy value. My understanding is this should perfectly match the screen coordinates but when I check these values I am only getting the top right corner (output.png).

Second, I always seem to get values of 0 when sampling my texture. I can see in the debugger data view that (0,0) has a value of like ~.9997. However I sample it, I always seem to get 0s. It is a floating point texture with GL_DEPTH_ATTACHMENT.

Here's the fragment and vertex shader code (my graphics card doesn't have geometry shader):



ShadowVisualDataOutput ShadowVisualVertexMain(ShadowVisualDataInput vdi, uniform float4x4 modelViewProj) {
  ShadowVisualDataOutput ret;
  ret.projv = mul(modelViewProj, vdi.vertex); 
  ret.saved = mul(modelViewProj, vdi.vertex).xy;
  return ret;
}


ShadowVisualDataPixelOutput ShadowVisualFragmentMain(
	ShadowVisualDataOutput pdi, 
	uniform sampler2D depthTex,
	uniform float camNear, 
	uniform float camFar) {
		
	ShadowVisualDataPixelOutput ret;	
	if (pdi.saved.x < 0 || pdi.saved.x > 720) {
		ret.color = float4(0,0,1,1);
		return ret;
	}
	if (pdi.saved.y < 0 || pdi.saved.y > 480) {
		ret.color = float4(0,0,1,1);
		return ret;
	}	

	float2 lookup = float2(0, 0);
	float depth = tex2D(depthTex, lookup).x;
	if (depth == 0.0f) {
			ret.color = float4(0,0,0,0);
			return ret;
	}

	ret.color = float4(1,1,1,1);
	return ret;

}

Thanks for any help ahead of time smile.png

Advertisement
Opengl novice here so sorry ahead of time!

We all start somewhere. smile.png

I am only getting the top right corner (output.png).

I don’t know what you mean by this being the only thing you are “getting”. This is the only region over which you are not drawing blue. See image.
[attachment=13331:output copy.png=copy.png]

Second, I always seem to get values of 0 when sampling my texture. I can see in the debugger data view that (0,0) has a value of like ~.9997. However I sample it, I always seem to get 0s. It is a floating point texture with GL_DEPTH_ATTACHMENT.
Thanks for any help ahead of time smile.png

This is the only mystery part.
Try sampling the center of the texture first (0.5f, 0.5f).
Then sample multiple values and see if you can get anything from any part of the texture.





float2 lookup = float2( (pdi.saved.x - 720.0f) / 720.0f, (pdi.saved.y - 480.0f) / 480.0f );
float depth = tex2D(depthTex, lookup).x;
ret.color = float4(depth,depth,depth,1);
return ret;

L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

Thanks for the response! smile.png

I don’t know what you mean by this being the only thing you are “getting”. This is the only region over which you are not drawing blue. See image.

I am rendering a single quad that should cover up the whole screen exactly. If I use a fragment shader that just outputs a single color, it fills the screen completely. But when I multiply the vertex coordinates by model view projection matrix, store them, and then check in the fragment shader if they are exactly the screen (0 < x < 720 and 0 < y < 480) only the top right half projects into this. I am misunderstanding something about the projection?

(0.5f, 0.5f) and (1.0f, 1.0f) also return 0. Maybe I am setting something up wrong:


// Creation

glGenTextures(1, &depthTexture);
glBindTexture(GL_TEXTURE_2D, depthTexture);	
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);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, width, height, 0, GL_DEPTH_COMPONENT, GL_FLOAT , 0);	
glBindTexture(GL_TEXTURE_2D, 0);

...

// Attach to fbo
glGenFramebuffersEXT(1, &depthFramebuffer);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, depthFramebuffer);

glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depthTexture, 0); glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, colorTexture, 0);

checkStatus() <-- returns complete okay!

// Bind to fbo, render scene once, then switch back to default

glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, depthFramebuffer);

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

renderHWLoop();

glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);

...

//switch to visualizing programs

//eventually these get called

fragmentDepthText = cgGetNamedParameter(fragmentProgram, "depthTex");

cgGLSetTextureParameter(fragmentDepthText, depthTexture);

I misread your shader and I still don’t fully understand your explanation so I am being more harmful than helpful.

Let’s start over. Your test case should be the simplest possible test case you can make, so don’t do all that branching, etc. in your shader.

Just add texture coordinates to your full-screen quad from 0 to one both ways and sample the texture using just that and nothing else.

L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

Sorry for the confusion. Let me try rewording it. With the simplest shader just outputing a color, it fills the screen. But when I project the points by multiplying by the modelviewproj and check if they are within the bounds of the screen, only the top corner projects into this range. Am I misunderstanding something about projection?

And I tried this fragment shader with just pass-through texture coordinates and am still getting all black.


ShadowVisualDataPixelOutput ret;

float depth = tex2D(depthTex, pdi.texture).x;
if (depth == 0.0f) {
	ret.color = float4(0,0,0,0);
	return ret;
} else {
	ret.color = float4(depth, depth, depth, 1);
	return ret;
}
Found it! Apparently there is something called cgEnableTextureParameter(). I don't understand why you would set a texture parameter but not have it enabled :/

But my question about the projection still stands!

Why do you check if they are in the bounds of the screen? Just output a solid color if you want to see where the points really land when you apply the projection.

Adding checks just increases the potential for mistakes.

L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

After projection, the "screen coordinates" are known as normalized device coordinates (or NDC). NDC's x/y axis have a range from -1 to +1, not 0 to width/height.

For your texcoord, you probably want to use pdi.saved.xy*0.5+0.5.

Awesome! Thank you!

This topic is closed to new replies.

Advertisement