Samstag, 24. September 2011

Tessellation Shader Tutorial with Source Code C++

To make it easier to start with Tessellation Shaders, here a GL4.0 example. (VS9,C++). 

The file can be downloaded from here:

Update: a first performance benchmark gives around 266 Million Polygons/second using tesselation, which is slower than using a common VBO or OpenGL DisplayList (400 MPoly/s). However, its more flexible, so geometry can be rendered much more efficient.

Source for the tessellation control shader:
#version 410 core

layout(vertices = 4) out;

void main(void)
{
 gl_TessLevelOuter[0] = 2.0;
 gl_TessLevelOuter[1] = 4.0;
 gl_TessLevelOuter[2] = 6.0;
 gl_TessLevelOuter[3] = 8.0;

 gl_TessLevelInner[0] = 8.0;
 gl_TessLevelInner[1] = 8.0;

 gl_out[gl_InvocationID].gl_Position = gl_in[gl_InvocationID].gl_Position;
}

Source for the quad tessellation evaluation shader:

#version 410 core

layout(quads, equal_spacing, ccw) in;

//quad interpol
vec4 interpolate(in vec4 v0, in vec4 v1, in vec4 v2, in vec4 v3)
{
 vec4 a = mix(v0, v1, gl_TessCoord.x);
 vec4 b = mix(v3, v2, gl_TessCoord.x);
 return mix(a, b, gl_TessCoord.y);
}

void main()
{ 
 gl_Position = interpolate(
  gl_in[0].gl_Position, 
  gl_in[1].gl_Position, 
  gl_in[2].gl_Position, 
  gl_in[3].gl_Position);
}