开发者

How to emulate GL_DEPTH_CLAMP_NV?

I have a platform where this extension is not available ( non NVIDIA ). How could I emulate this function开发者_运维知识库ality ? I need it to solve far plane clipping problem when rendering stencil shadow volumes with z-fail algorithm.


Since you say you're using OpenGL ES, but also mentioned trying to clamp gl_FragDepth, I'm assuming you're using OpenGL ES 2.0, so here's a shader trick:

You can emulate ARB_depth_clamp by using a separate varying for the z-component.

Vertex Shader:

varying float z;
void main()
{
    gl_Position = ftransform();

    // transform z to window coordinates
    z = gl_Position.z / gl_Position.w;
    z = (gl_DepthRange.diff * z + gl_DepthRange.near + gl_DepthRange.far) * 0.5;

    // prevent z-clipping
    gl_Position.z = 0.0;
}

Fragment shader:

varying float z;
void main()
{
    gl_FragColor = vec4(vec3(z), 1.0);
    gl_FragDepth = clamp(z, 0.0, 1.0);
}


"Fall back" to ARB_depth_clamp?

Check if NV_depth_clamp exists anyway? For example my ATI card supports five "NVidia-only" GL extensions.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜