emulate NV_depth_clamp on ATI

I’m looking for a way to clamp depth values to near plane without clipping. I tried small fragment shader:


void main()
{
	gl_FragDepth = max( gl_FragCoord.z, 0. );
}

but polygons are still clipped.

the clipping runs before the fragment shader. So the fragments are already clipped away.

I’ve finally found solution. This is for ortho projection, for perspective it will need additional check on Zeye - to clip everything behind the eye.


// Vertex shader:
varying float Zw;
void main()
{
	vec4 clip = ftransform();
	float Zd = clip.z/clip.w;
	clip.z = max( Zd, -1. )*clip.w;
	Zw = Zd*0.5 + 0.5;
	gl_Position = clip;
}

// Fragment shader:
varying float Zw;
void main()
{
	gl_FragDepth = max( Zw, 0. );
}