We’ve gotten a lot of attention from our earlier post on using logarithmic depth buffers with Triton and SilverLining. Although it’s not a panacea, logarithmic depth buffers can help a lot with z-fighting in scenes that have very high depth value ranges. SilverLining and Triton both have extensible shader frameworks that allow you to intercept their final clip-space coordinates, and adjust them in a way that’s consistent with the rest of your scene – and that’s all that logarithmic depth buffers require, really. It’s just an adjustment to the final Z clip space coordinates.
However, the mechanics on how to do this isn’t always clear – especially if you’re using an engine like OpenSceneGraph, where the OpenGL calls required to set shader uniforms in SilverLining and Triton aren’t readily available.
So, Triton version 3.38 now includes an example to go by for OpenSceneGraph users – and it’s probably useful to everyone else, as well. You’ll find that the osgDynamicHeightMapSample has a new option at the top of TritonDrawable.h:
Uncomment that and rebuild, if you want to see a log depth buffer in action. It’s not quite that simple, however – you also need to add the required logic into Triton’s user-vert-functions.glsl file inside its Resources folder. Just find the overridePosition function in there, and replace it with this:
vec4 overridePosition(in vec4 position)
if ( FCoef > 0.0 )
vec4 adjustedClipPosition = position;
adjustedClipPosition.z = (log2(max(1e-6, position.w+1.0))*FCoef - 1.0) * position.w;
At the top of user-vert-functions.glsl, you’ll also need to declare the FCoef uniform:
uniform float FCoef = 0;
And, that’s it. If you search the sample’s code for USE_LOG_DEPTH_BUFFER, you’ll see all of the relevant code changes to get an OSG application using a log depth buffer with Triton. Its own shaders for the terrain are modified in the same way, and a pre-draw callback on the camera keeps the FCoef value updated. Within TritonDrawable.cxx, you’ll see how Triton’s shader programs are obtained, and the same uniform value set on them.
SilverLining integration would work the same way, but you’ll find its overridePosition function in the Shaders/Resources/UserFunctions.glsl file instead.