10 Tessellation Shaders
R. Mukundan Department of Computer Science and Software Engineering University of Canterbury, .
Applications of Tessellation
Copyright By PowCoder代写 加微信 powcoder
3D modelling using mesh subdivision and smoothing
Terrain modelling using subdivision and Level of Detail (LoD)
Applications of Tessellation
Real-time Rendering
Tessellation allows an application to render higher detailed
surfaces from low resolution models Saves lots of memory and bandwidth
Key Parts of a Tessellation Method
Parametric Domain Tessellation Levels Patch
Blending Functions Surface Rendering
Subdividing a line in 3D space
P1 and P2 are points in 3D space.
We require a subdivision of the line P1P2 as shown below.
The subdivision uses a parameter t (0 ≤ t ≤ 1) to find the positions of points on the line.
t=0.2 t=0.1
P2t=1 t=0.9
P = (1−t) P1 + t P2
Subdivision as a Mapping of Parametric Domain
P = (1−t) P1 + t P2 Blending functions
Parametric Domain
Vertices P1, P2
A normalized parametric domain
A subdivision pattern (tessellation) of the domain
A set of blending functions
Parametric Domains
A parametric domain (or parametric space) is a space defined by a regular shape, within which points are specified using parametric values in the range [0, 1].
We consider two types of 2D parametric domains: A quad domain: This is a unit square.
(0.8, 0.2)
(0, 0) (1, 0)
A triangle domain: This is an equilateral triangle
(1, 0, 0) COSC363
(0.33, 0.33, 0.33)
NOTE: The coordinates shown here are not 3D Cartesian coordinates. The triangle is a 2D domain. The coordinates are barycentric coordinates (explained later).
Quad Domain: A General Example
Suppose we want to map points from the parametric domain to a general quad in 3D space given by points P0..P3,
where Pi = (xi, yi, zi)
A 3D polygon of arbitrary
(0, 0) u 0u1
(1, 0) 0v1
We could use a bi-linear interpolation formula for the above mapping.
P1 size and shape
Q=(1−v){(1−u)P0 +uP1 } + v { (1−u) P3 + u P2 }
Quad Domain: Tessellation
Tessellating a quad of arbitrary shape in 3D space is difficult. If we can tessellate a quad domain (which has a regular shape in 2D), then we can map the points to any quad in 3D using the method on the previous slide.
Triangle Mesh
Tessellation Coordinates
Quad Domain
0u1 0v1
P0 COSC363
Q = (1−v) { (1−u) P0 + u P1 } + v{(1−u)P3+uP2}
Triangle Domain: Barycentric Coordinates
The barycentric coordinates (u, v, w) of a point inside a triangle ABC specify the position of the point relative to its
Barycentric coordinates of p: (u, v, w) u = Area (PBC) / Area (ABC)
v = Area (PCA) / Area (ABC)
w = Area (PAB) / Area (ABC)
Properties:
B = (0, 1, 0),
will have a value between 0 and 1.
u + v + w = 1. E.g. p = (0.2, 0.4, 0.4) COSC363
A = (1, 0, 0),
C = (0, 0, 1)
For any point inside the triangle, the barycentric coordinates
Triangle Domain: Barycentric Mapping
If we know the barycentric coordinates of a point inside a triangle, we can find its map on any other triangle!
Consider a triangle in 3D space, with vertices P0, P1, P2,
where Pi = (xi, yi, zi). (0, 0, 1)
0 u 1 0w1
(u, v, w) (0, 1, 0)
The barycentric mapping of the point p is given by:
Q=uP0 +vP1 +wP2
Triangle Domain: Tessellation
Triangles in 3D space are tessellated with the help of a triangle domain.
The triangle domain is tessellated and the points mapped to the 3D space using barycentric coordinates.
Triangle Mesh
Tessellation Coordinates
Triangle Domain
0u1 0v1
0 w 1,
u + v + w = 1.
(1, 0, 0) P1
Q=uP0 +vP1 +wP2
P0 COSC363
Outer Tessellation Levels
Two types of tessellation levels are defined for parametric domains: Outer and Inner
Outer tessellation levels specify the number of subdivisions along the edges of the domain
Outer level = 3
Outer level = 2 Quad Domain
Outer level = 2 Triangle Domain
Outer level = 5
Outer level = 4
Inner Tessellation Levels
Quad domain: The minimum number of subdivisions (or steps) along a path from one edge to the opposite edge.
Two inner levels corresponding to the horizontal direction and vertical direction of the paths.
Triangle domain: The minimum number of subdivisions (or steps) along a path from one vertex to the opposite edge.
Only one inner level corresponding to all three directions.
Inner level = 3
Inner level = 4
Inner level = 4
Tessellation
The methods given on slides 9, 12 provide only tessellation of planar surfaces (quads or triangles) without modifying the surface geometry. Such tessellations have limited use.
Linear Blending Functions
The quad domain mapping method combines a set of vertices P0, P1, P2, P3 using linear blending functions in u, v.
Q = (1−v) (1−u) P0 + (1−v) u P1 + v (1−u) P3 + v u P2
Blending functions: (1 − u), u, (1 − v), v. Patch vertices: P0, P1, P2, P3
The patch vertices define the shape of the tessellated surface
P0 COSC363
Tessellation
Tessellations (subdivisions) of surfaces are commonly used for smoothing or approximation.
Such surfaces can be generated using higher order blending functions in the tessellation coordinates u, v.
Higher order blending functions will require more (> 4) patch vertices
Higher Order Blending Functions
The following example uses quadratic functions in u, v: Blending functions: (1−u)2, 2u(1−u), u2, (1−v)2 , 2v(1−v), v2.
Patch vertices: P0,…,P8
The patch vertices are control points defining the shape of the tessellated surface.
The generated surface may not pass through all patch vertices.
Quad Domain
0u1 0v1
A 3×3 Patch
Example:
Higher Order Blending Functions
Q = (1−v)2 { (1−u)2 P0 + 2u(1−u) P1 + u2 P2} + 2v(1−v) { (1−u)2 P3 + 2u(1−u) P4 + u2 P5} + v2 { (1−u)2 P6 + 2u(1−u) P7 + u2 P8}
Quad Domain
u (1, 0) 0u1
0v1 COSC363
A bi-quadratic Bezier surface patch!
Tessellation Shader Stage
The tessellation stage of the OpenGL-4 pipeline can be used to generate a mesh of triangles based on vertices of a patch (a new geometric primitive).
There are two shading stages used in tessellation:
Tessellation controller (optional): Sets tessellation parameters
and any additional patch vertices.
Tessellation evaluator: Positions the vertices of the generated mesh on the patch using mapping equations defined by user.
GL_PATCHES
Tessellation stage
Tessellation Control
Primitive Generator
Tessellation Evaluation
Vertex Shader
Fragment Shader
A patch is simply an ordered list of vertices, the order determined by the user.
A patch is cannot be directly rendered (it is not a renderable primitive)
If the tessellation stage is active, the input must be a patch.
Examples of patches
For a patch, the rendering command is glDrawArrays(GL_PATCHES, 0, n);
or, glDrawElements(GL_PATCHES,..);
You should also specify in your application, the
number of vertices in each patch:
glPatchParameteri(GL_PATCH_VERTICES, 9);
From Patches to Triangles
Modify patch vertices, if needed. Specify tessellation levels
Tessellation Coordinates
Vertices of Triangles
A simple pass- through shader
Vertex Shader
Tessellation Control
Primitive Generator
Tessellation Evaluation
• The primitive generator can output only triangles.
• The triangles form a tessellation of either a square
or a triangle domain.
• The vertices of every triangle in the tessellation will
have normalized coordinates.
The evaluation shader converts the primitive vertices (u, v) to 3D points using use-defined functions, and outputs them in clip coordinate space.
Vertex Shader
This vertex shader does nothing! It simply passes the patch vertices to the tessellation control shader (TCS)
Since a patch is not a renderable primitive, its vertices are not converted to clip coordinate space.
#version 330
layout (location = 0) in vec4 position;
void main() {
gl_Position = position; }
patch vertices
Tessellation Levels
The amount of tessellation of a domain (quad or triangle) is determined by tessellation levels.
Outer tessellation level:
4 values (one for each side of the quad; for a triangle the last
value is 0) stored in arrays
gl_TesslevelOuter[0]… gl_TesslevelOuter[3]
Inner tessellation level:
2 values for a quad, 1 for a triangle stored in arrays
gl_TesslevelInner[0], gl_TesslevelInner[1]
Tessellation Levels: Quad
gl_TessLevelOuter[3] = 3
gl_TessLevelOuter[1] = 2
gl_TessLevelInner[0] = 3
gl_TessLevelOuter[2] = 4
gl_TessLevelInner[1] = 4
gl_TessLevelOuter[0] = 5
Tessellation Levels: Quad
3333; 44 2222; 33 2222; 22
Outer Inner
A quad domain has 4 outer tessellation levels and 2 inner levels
Tessellation Levels: Triangle
A gl_TessLevelOuter[2]=2 B
(1, 0, 0) (0, 1, 0)
gl_TessLevelInner[0] = 4 gl_TessLevelInner[0] = 3
A triangle domain has 3 outer tessellation levels and 1 inner level
Tessellation Levels: Triangle
3330; 30 3330; 20 2220; 30 Outer Inner
Tessellation Control Shader (TCS)
The tessellation control shader is commonly used to set the inner and outer tessellation levels.
Optionally, the shader can also create new or remove existing patch vertices. All patch vertices are available inside the shader in an array.
The tessellation control shader will execute once for each output patch vertex.
gl_Position
Patch Vertices
gl_in[]. gl_Position
Patch Vertices
gl_out[]. gl_Position
Patch Vertices
gl_in[]. gl_Position
Tessell Levels
Triangle Mesh
Patch Vertices
Vertex Shader
Tessellation Control
Primitive Generator
Tessellation Control Shader: Example
#version 400
layout(vertices = 9) out;
void main() {
output patch vertices
Index of the current out vertex
gl_out[gl_InvocationID].gl_Position
= gl_in[gl_InvocationID].gl_Position;
gl_TessLevelOuter[0] = 6; gl_TessLevelOuter[1] = 6; gl_TessLevelOuter[2] = 6; gl_TessLevelOuter[3] = 6; gl_TessLevelInner[0] = 5; gl_TessLevelInner[1] = 5;
Tessellation Control Shader
The tessellation control shader on the previous slide is a simple pass-through shader, and does not modify tessellation levels.
Such pass-through control shaders may be omitted (bypassed). If the tessellation levels remain constant for all patches, they can be specified in the OpenGL application as follows:
float outer[4] = {6, 6, 6, 6};
float inner[2] = {5, 5}; glPatchParameterfv(GL_PATCH_DEFAULT_OUTER_LEVEL, outer); glPatchParameterfv(GL_PATCH_DEFAULT_INNER_LEVEL, inner);
From Patches to Triangles
The primitive generator outputs a mesh of triangles having the following characteristics:
The pattern of tessellation is based on the “inner” and “outer” tessellation levels specified by the user.
The pattern of tessellation also depends on the type of the domain (quad or triangle).
The output primitive at this stage is always triangles.
The vertex coordinates of the generated triangles will have
values in the range [0, 1].
The Tessellation Evaluation Shader operates on one mesh vertex (u, v) for a quad domain or (u, v, w) for a triangle domain. The evaluation shader acts like a vertex shader for the vertices emitted by the primitive generator.
Tessellation Coords
Triangle Mesh
gl_in[]. gl_Position
Clip Coords
Tessellation Evaluation Shader
Patch Mesh Vertices Vertex
gl_Position
Primitive Generator
Tessellation Evaluation
The tessellation evaluator repositions each mesh vertex (u, v) using patch vertices, and outputs them in clip coordinates.
The evaluation shader executes once for each input mesh vertex.
Tessellation Evaluation Shader (Quad Domain)
#version 400
layout(quads, equal_spacing, ccw) in;
uniform mat4 mvpMatrix;
vec4 posn;
void main() {
float u = gl_TessCoord.x;
float v = gl_TessCoord.y;
posn = (1-u)* (1-v) * gl_in[0].gl_Position
+ u * (1-v) * gl_in[1].gl_Position
+ u * v * gl_in[2].gl_Position + (1-u) * v * gl_in[3].gl_Position;
Tessellation coordinates of the tessellated mesh vertices
gl_Position = mvpMatrix * posn;
Clip Coords
Patch vertices
See slide 16
Tessellation Evaluation Shader (Triangle Domain)
#version 400
layout(triangles, equal_spacing, ccw) in; uniform mat4 mvpMatrix;
vec4 posn;
void main() {
Tessellation coords (barycentric)
Patch vertices
posn = gl_TessCoord.x * gl_in[0].gl_Position + gl_TessCoord.y * gl_in[1].gl_Position + gl_TessCoord.z * gl_in[2].gl_Position;
See slide gl_Position = mvpMatrix * posn; 12
Fragment Shader
The tessellation evaluation shader receives only the tessellation coordinates for each vertex of the triangle mesh generated by the primitive generator. It does not receive any other vertex attributes.
If surface normal vectors are not available, the primitives cannot be rendered using lighting equations.
Therefore the fragment shader generally has a simple form: #version 330
void main() {
gl_FragColor = vec4(0, 0, 1, 1); }
Applications of Tessellation Shaders
Modelling using bi-cubic Bezier patches The teapot!
Terrain rendering
Bi-cubic Bezier Surface
P P9 P10 P11
Control polygonal patch
Bezier surface
Q = (1−v)3 {(1−u)3 P0 + 3u(1−u)2 P1 + 3u2(1−u) P2 + u3 P3}
+ 3v(1−v)2 {(1−u)3 P4 + 3u(1−u)2 P5 + 3u2(1−u) P6 + u3 P7}
+ 3v2(1−v) {(1−u)3 P8 + 3u(1−u)2 P9 + 3u2(1−u) P10 + u3 P11} + v3 {(1−u)3 P12 + 3u(1−u)2 P13 + 3u2(1−u) P14 + u3 P15}
Bi-Cubic Bezier Surface – Evaluation Shader
float Bez(int i, float u) {
if(i == 0) return (1-u)*(1-u)*(1-u); else if(i == 1) return 3*u*(1-u)*(1-u); else if(i == 2) return 3*u*u*(1-u); else if(i == 3) return u*u*u;
else return 0;
void main() {
vec4 posn = vec4(0);
float u = gl_TessCoord.x; float v = gl_TessCoord.y; for(int j = 0; j < 4; j++) {
for(int i = 0; i < 4; i++) {
posn += Bez(i, u) * Bez(j, v) * gl_in[4*j+i].gl_Position;
gl_Position = mvpMatrix * posn;
Modelling Complex Shapes
A coarse description of a shape could be specified using several 4x4 control patches. Each patch is then used to create a Bezier surface.
The combined model should not have any discontinuities where the Bezier surfaces meet. This requirement can be met by making sure that the corresponding polygonal edges are collinear (for Bezier curves) or the corresponding polygons of the patches are coplanar (for Bezier surfaces)
Control Patch-1
Control Patch-2
Bezier curves/surfaces
32 control polygonal patches, each patch containing 16 vertices.
Total number of vertices: 512
The 4x4 polygonal patches are defined such that polygons on either side of an edge where two patches meet are coplanar.
The Teapot Family
The Teapot
32 Control patches
The Teacup
26 Control patches
The Teaspoon 16 Control patches
Terrain Generation
The vertices of the tessellated mesh are assigned height values based on a height map.
A patch consists of vertices of a small rectangular segment of a terrain grid
Tessellation Coordinates
Height Map
Vertices of Triangles
Vertex Shader
Tessellation Control
Primitive Generator
Tessellation Evaluation
Terrain Construction
Construct the terrain base. (Application)
Tessellate the base to the required level. (Control Shader)
Reposition (u, v) using patch coordinates. ( )
Use a height map to modify vertices of the tessellated mesh. (Evaluation Shader)
Use Geometry Shader (next topic) to: Perform lighting computation.
Apply height based texture mapping.
Step 1: Terrain Base
The terrain base is represented by rectangular array of points on a large quad.
Each patch has 4 vertices.
Two VBO: Vertex coordinates, Element array.
Step 2: Tessellation
Tessellation levels: {1, 1, 1, 1; 1, 1} Tessellation levels: {6, 6, 6, 6; 6, 6} A simple pass-thru vertex shader !
The tessellation control shader receives 4 patch vertices and sets the outer and inner tessellation levels.
P0 Tessellation Evaluation Shader:
Quad Domain
Receives 4 tessellation coordinates and 4 patch vertices. The current mesh vertex (u, v) is repositioned using patch
u = gl_TessCoord.x; v = gl_TessCoord.y;
Patch Vertices
Step 4: Height Map
Terrain Base Texture Coords
Height Map Texture
The height map is mapped to the whole terrain base. The four corners of the base are therefore assumed to have texture coordinates (0,0), (1, 0), (0,1) and (1,1).
The xmin, xmax, zmin, zmax values of the base are used to compute the texture coordinates of any vertex at position “posn”:
s = (posn.x – xmin)/(xmax-xmin); t = (posn.z – zmin)/(zmax-zmin);
The height map is sampled using texture coordinates, and the y-
value of the vertex (posn.y) is updated. COSC363
Terrain Rendering
Height Map
Geometry Shader
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com