Reputation: 2112
Currently I am learning the tessellation shader of OpenGL. But when it comes to the built in variable "gl_TessCoord" in the tessellation evaluation shader, I can't understand how it is calculated.
I know that the "gl_TessCoord" is different when different tessellation mode is used (quads or triangles). And I checked both the blue book and red book, yet they only give a rough explanation telling that it represents the weight in terms of the input vertices. Any ideas?
Here is the sample code of tessellation of a rectangle:
#version 430 core
layout ( quads) in;
void main( void)
{
// Interpolate along bottom edge using x component of the
// tessellation coordinate
vec4 p1 = mix(gl_in[0].gl_Position, gl_in[1].gl_Position, gl_TessCoord.x);
// Interpolate along top edge using x component of the
// tessellation coordinate
vec4 p2 = mix(gl_in[2].gl_Position, gl_in[3].gl_Position, gl_TessCoord.x);
// Now interpolate those two results using the y component
// of tessellation coordinate
gl_Position = mix(p1, p2, gl_TessCoord.y);
}
Here is the sample code of tessellation of a triangle:
#version 430 core
layout ( triangles) in;
void main( void)
{
gl_Position = (gl_TessCoord.x * gl_in[0].gl_Position) +
(gl_TessCoord.y * gl_in[1].gl_Position) +
(gl_TessCoord.z * gl_in[2].gl_Position);
}
Upvotes: 3
Views: 2689
Reputation: 43359
gl_TessCoord
represents the same concept with all primitive topologies.
This vector is the position within the current primitive, and it is probably best not to think of it as x
, y
and z
. Since it is relative to a surface, u
, v
and w
(optional) are more intuitive. You can call the coordinates whatever you want (e.g. xyz
, rgb
, stp
or uvw
) , mind you, but uvw
is the canonical notation.
OpenGL 4.5 (Core Profile) - 11.2.2 Tessellation Primitive Generation - p. 392
Each vertex produced by the primitive generator has an associated (u, v, w) or (u, v) position in a normalized parameter space, with parameter values in the range [0, 1], as illustrated in figure 11.1.
For triangles, the vertex position is a barycentric coordinate (u, v, w), where u + v + w = 1, and indicates the relative influence of the three vertices of the triangle on the position of the vertex.
For quads and isolines, the position is a (u, v) coordinate indicating the relative horizontal and vertical position of the vertex relative to the subdivided rectangle
In the case of quads, each component measures the distance from two edges of a unit square. In the case of triangles, it is the distance from the three edges of a triangle (barycentric coordinates). This is why your example tessellation shader for quads uses only u
and v
, while the triangle example uses u
, v
and w
.
Upvotes: 5