To create a shader program that will generate a set of triangles using quad tessellation from a patch of four vertices, use the following steps:
- Use the following code for the vertex shader:
layout (location = 0 ) in vec2 VertexPosition; void main() { gl_Position = vec4(VertexPosition, 0.0, 1.0); }
- Use the following code as the tessellation control shader:
layout( vertices=4 ) out; uniform int Outer; uniform int Inner; void main() { // Pass along the vertex position unmodified gl_out[gl_InvocationID].gl_Position = gl_in[gl_InvocationID].gl_Position; gl_TessLevelOuter[0] = float(Outer); gl_TessLevelOuter[1] = float(Outer); gl_TessLevelOuter[2] = float(Outer); gl_TessLevelOuter[3] = float(Outer); gl_TessLevelInner[0] = float(Inner); gl_TessLevelInner[1] = float(Inner); }
- Use the following code as the tessellation evaluation shader:
layout( quads, equal_spacing, ccw ) in; uniform mat4 MVP; void main() { float u = gl_TessCoord.x; float v = gl_TessCoord.y; vec4 p0 = gl_in[0].gl_Position; vec4 p1 = gl_in[1].gl_Position; vec4 p2 = gl_in[2].gl_Position; vec4 p3 = gl_in[3].gl_Position; // Linear interpolation gl_Position = p0 * (1-u) * (1-v) + p1 * u * (1-v) + p3 * v * (1-u) + p2 * u * v; // Transform to clip coordinates gl_Position = MVP * gl_Position; }
- Use the geometry shader from the Drawing a wireframe on top of a shaded mesh recipe
- Use the following code as the fragment shader:
uniform float LineWidth; uniform vec4 LineColor; uniform vec4 QuadColor; noperspective in vec3 GEdgeDistance; // From geom. shader layout ( location = 0 ) out vec4 FragColor; float edgeMix() { // ** insert code here to determine how much of the edge // color to include (see recipe "Drawing a wireframe on // top of a shaded mesh"). ** } void main() { float mixVal = edgeMix(); FragColor = mix( QuadColor, LineColor, mixVal ); }
- Within the render function of your main OpenGL program, define the number of vertices within a patch:
glPatchParameteri(GL_PATCH_VERTICES, 4);
- Render the patch as four 2D vertices in counterclockwise order