# Funny Depth Value Behaviour

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

## Recommended Posts

Hi all, first off, im using Cg on an ATI Radeon 9500 Cat 4.7 WinXP SP2. the problem, it seems that the depth value for a fragment is not being interpolated properly or something (this is not an ATI driver bug, checked this with a 5900XT) basically here is my CgVP
struct appdata {
float4 position : POSITION;
};

struct V2FI {
float4  pos:  POSITION;
float2 tex0:  TEXCOORD0;
float  npos:  TEXCOORD1;
};

V2FI main( appdata IN, uniform float4x4 matMVP ) {
V2FI OUT;

float4 p = mul( matMVP, IN.position );
OUT.pos = p;
OUT.npos = p.z;
OUT.tex0 = float2(0,0);

return OUT;
}


and all i do is get the interpolated Z value in my FP by a simple
 float depth = IN.pos.z
I've tried: 1. dividing the depth by IN.pos.w, nothing 2. tried hard coded values to see if the rest of the FP is working (it is.) 3. used some color debugging, eg. if depth value < x then fragment = blue else fragment = red, behaves incorrectly when rendering my scene, but is correct for hardcoded values. Odd things: 1. if i use "depth = IN.npos" i get totaly different results (still incorrect though) 2. this should be a very simple task, i have no lighting or textures, just a sphere (which is called from a DL btw) any pointers? i'd be glad to provide more code if need be, but prefer not to. Cheers, Danu PS. are there any Pixel Shader debuggers around (appart from NV-FXComposer)?

##### Share on other sites
You don't mention what you want to do with the depth value. Anyway, you want to get the depth value, but in what space? Object space,eye,clip coordinates, window coordinates(the value that is written in the z-buffer) ?

##### Share on other sites
im doing a DOF blur (Depth of Field) so i want window coords sorry (ie. Z buffer value of that fragment)

basically i need to test if a fragment's z is infront or behind the focal "plane" (its just a single float value)

is this safe to do?

Cheers,
Danu

##### Share on other sites
I don't know much about CG, but in GLSL and fragment shaders you can access the window coordinates with gl_FragCoord, and you can obtain the depth value with gl_FragCoord.z . I think there is a WPOS input variable in CG, or am I wrong?

If that doesn't exist, you can compute the depth value yourself in the vertex shader and interpolate it. So far you've computed the vertex position (Xc,Yc,Zc,Wc) in clip coordinates.
You have to:

1)Perspective division:
Zd=Zc/Wc;

2)Window coordinates:
Zw=[(f − n)/2]Zd + (n + f)/2
n,f are the values you supply to glDepthRange(), so they will probably be n=0,f=1.

Zw is the depth value in window coordinates, so you can interpolate it and retrieve it in the fragment program.

[Edited by - mikeman on October 5, 2004 6:33:16 AM]

##### Share on other sites
thanks, ill try that

##### Share on other sites
i was looking at an ATI presentation on DOF (DX HLSL) they
sample the POS.z value and send it to the fragment sh just like
how im doing it...

odd, do you think D3D HLSL and Cg would behave diff in this respect?

Cheers
Danu

##### Share on other sites
Quote:
 Original post by silvermacei was looking at an ATI presentation on DOF (DX HLSL) theysample the POS.z value and send it to the fragment sh just likehow im doing it...odd, do you think D3D HLSL and Cg would behave diff in this respect?CheersDanu

It all depends on the coordinate system you decide to use. You say you need to compare the fragment's z value to a focal plane,which I assume you supply. If you express the focal plane in eye coordinates, you'll need the fragments pos in eye coordinates. If you express it in window coords, you'll need the fragment's window zvalue, and so on.

##### Share on other sites
hi mike,

i tried the window coord calculation, still dosnt work
the fragments color just comes out plane white, when it should
be an "alpha" value ranging from 0-1

heres all the Cg Code:
// Vertex Programstruct appdata {  float4 position : POSITION;};struct V2FI {  float4 pos: POSITION;  float fDepth: TEXCOORD0;};V2FI main( appdata IN, uniform float4x4 matMVP ){	V2FI OUT;	float4 p = mul( matMVP, IN.position );	OUT.pos = p;	float Zd = p.z / p.w;	float f = 1;	float n = 0;	float z = ((f - n)/2) * Zd + ((n + f)/2);	OUT.fDepth = z;	return OUT;}// Fragment Programstruct V2FI {	float4 pos: POSITION;	float  fDepth: TEXCOORD0;};float4 main( V2FI vin, 		uniform float near, 		uniform float far, 		uniform float focalPlane,		uniform float clampBlurFactor ) : COLOR{	float depth = vin.fDepth;	float f;	if( depth < focalPlane ) {		f = (depth - focalPlane) / (focalPlane - near);	}	else {		f = (depth - focalPlane) / (far - focalPlane);		f = clamp(f, 0, clampBlurFactor);	}	// range compress to 'alpha'	f = f * 0.5f + 0.5f;	return f;}

funny thing is the fragment program is straight out of an ATI DOF presentation, they say that "depth" should be in "view" coordinates (i beleive this is World * View * Proj == Modelview * Projection)

Thanks for your help on this btw, Rate++.

-Danu

##### Share on other sites
You get eye coordinates if you multiply with the ModelView matrix. The way you're doing it(MVP*vector), you get clip coordinates.

Also remember, the fragment z-value will be negative,due to the coordinate system GL uses. I'm not sure, but the calculations seem to require positive values, and you probably supply the focal plane as positive too, right? In that case, just do "float depth = -vin.fDepth;" in the fragment program to adjust things right.

I suppose the "far" and "near" values are the far and near planes as you define them in gluPerspective.

##### Share on other sites
i changed the compare signs and now i interpolate
fDepth = mul( matMV, IN.pos ).z

still dosn't work, there is absolutly no change in depth
value when im using glTranslate or glRotate on the scene

i also check that i am binding the Cg shader and matricies
AFTER transformations.

Any more ideas?

Cheers,
Danu

PS. you've been really patient, i really appreciate it.

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 18
• 13
• 14
• 43
• 63