How to do it...

To create a shader program that varies the tessellation level based on the depth, use the following steps:

  1. Use the vertex shader and tessellation evaluation shader from the Tessellating a 3D surface recipe.
  2. Use the following code for the tessellation control shader:
layout( vertices=16 ) out; uniform int MinTessLevel; uniform int MaxTessLevel; uniform float MaxDepth; uniform float MinDepth; uniform mat4 ModelViewMatrix; void main() { // Position in camera coordinates vec4 p = ModelViewMatrix * gl_in[gl_InvocationID].gl_Position; // "Distance" from camera scaled between 0 and 1 float depth = clamp( (abs(p.z) - MinDepth) / (MaxDepth - MinDepth), 0.0, 1.0 ); // Interpolate between min/max tess levels float tessLevel = mix(MaxTessLevel, ...

Get OpenGL 4 Shading Language Cookbook - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.