Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Sampling depth texture


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
8 replies to this topic

#1 d0nuts   Members   -  Reputation: 116

Like
0Likes
Like

Posted 21 January 2013 - 11:27 AM

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

Attached Thumbnails

  • debugger.png
  • output.png

Edited by d0nuts, 21 January 2013 - 04:36 PM.


Sponsor:

#2 L. Spiro   Crossbones+   -  Reputation: 14197

Like
0Likes
Like

Posted 21 January 2013 - 07:59 PM

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.
output 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


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

#3 d0nuts   Members   -  Reputation: 116

Like
0Likes
Like

Posted 21 January 2013 - 09:36 PM

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);

 

Edited by d0nuts, 21 January 2013 - 09:37 PM.


#4 L. Spiro   Crossbones+   -  Reputation: 14197

Like
0Likes
Like

Posted 22 January 2013 - 12:39 AM

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


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

#5 d0nuts   Members   -  Reputation: 116

Like
0Likes
Like

Posted 22 January 2013 - 09:46 AM

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;
}

Edited by d0nuts, 22 January 2013 - 09:47 AM.


#6 d0nuts   Members   -  Reputation: 116

Like
0Likes
Like

Posted 22 January 2013 - 10:05 AM

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!

Edited by d0nuts, 22 January 2013 - 10:15 AM.


#7 L. Spiro   Crossbones+   -  Reputation: 14197

Like
0Likes
Like

Posted 22 January 2013 - 05:50 PM

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


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 Hodgman   Moderators   -  Reputation: 31781

Like
0Likes
Like

Posted 22 January 2013 - 06:00 PM

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.


Edited by Hodgman, 22 January 2013 - 06:01 PM.


#9 d0nuts   Members   -  Reputation: 116

Like
0Likes
Like

Posted 22 January 2013 - 09:27 PM

Awesome! Thank you!






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