OpenGL Programming Guide (8)

1, subdivision shader (Tessellation shader) there are two stages to generate a model of the geometry grid.

 1) the vertex shading stage, to set all grid segments, triangles process, using an ordered list of vertices to generate a new target primitives.

 2) the newly generated primitive vertex placed on the specified location to the next stage.

 

2, tessellation shader processing elements called new patch (Patch), which is a list of vertices to ensure the desired sequence. The number of vertices of the patch needs to set its own, with a drawing command processed surface sheet size is the same.

  void glPathcParameteri (GLenum pname, GLint value) // pname must GL_PATCH_VERTICES, value their own needs to be between 0 and GL_MAX_PATCH_VERTICES

  The default number of vertices for the patch 3, less than this value will be ignored process does not produce any geometry. To set a patch, a drawing command input type to GL_PATCHES, the following is an example:

  float vertices[][2] = {{-0.75, 0.25}, {0.5, 0.5}, {0.55, 0.53}, {-0.5, 0.55}};

  glBindVertexArray (VAO);

  glBindBuffer(GL_ARRAY_BUFFER, VBO);

  glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

  glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0 nullptr);

  glEnableVertexAttribArray(0);

  glPatchParameteri(GL_PATCH_VERTICES, 4);

  glDrawArrays(GL_PATCHES, 0, 4);

  gl_in obtained using tessellation shader vertex data, the length gl_PatchVerticesIn available gl_in, gl_PatchVerticesOut is the number of vertices of the output dough sheet.

 

3, subdivision control shader (optional step)

 1) generating a subdivision of the output patch vertices stored in segments and transferred to the computing gl_out shader while updating the attribute values ​​for all the vertices of the face sheet

 2) Set the detailed level factor to generate a control operation of primitives, gl_TessLevelInner with implicit declaration gl_TessLevelOuter

 

4, the output provided by the layout quantifier patch vertices, gl_InvocationID indicates output current vertex is the first of several, i.e. gl_out index.

  layout (vertices = 4) out; // Set the number of the top surface of sheet output point 4 simultaneously represent the number of executions of the shader (vertex executed once for each output)

  #version 420 core

  layout (vertices = 4) out;

  void main()

  {

    gl_TessLevelOuter[0] = 2.0;

    gl_TessLevelOuter[1] = 3.0;

    gl_TessLevelOuter[2] = 2.0;

    gl_TessLevelOuter[3] = 5.0;

    gl_TessLevelInner[0] = 3.0;

    gl_TessLevelInner[1] = 4.0;

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

    //......

  }

 

5, the total amount of inner segments (gl_TessLevelInner, the length of the element array 2, an internal subdivision is responsible for the control mode) and the outer (gl_TessLevelOuter, the array length of the four elements, responsible for controlling the perimeter area subdivision) jointly decided. Their values ​​may be a shader written in the control segment may be written as in the above example in the application, when the set value is less than 1 or a NaN, the patch will be discarded without processing .

  void glPatchParameterfv (GLenum pname, const GLfloat * values) // pname corresponds to GL_PATCH_DEFAULT_OUTER_LEVEL, GL_PATCH_DEFAULT_INNER_LEVEL, values ​​corresponding set values ​​array

 

6, three kinds of line segments manner

 1)equal_spacing

 

 

 2)fractional_even_spacing

 

 3)fractional_odd_spacing

 

 

 

7, OpenGL supports three segments subdivided domain: set of quadrilateral, triangular, contour.

 1) a quadrilateral subdivided field: use all the values gl_TessLevelOuter gl_TessLevelInner, the coordinates in the patch subdivision using the (u, v) 2-dimensional coordinates. 4 an illustration of the parameters set forth in the breakdown is as follows:

 

 But I painted looks like this (labeled above gl_TessLevelInner [0] and gl_TessLevelInner [1] should swap position)

 

 

 

 2) contour segment domain: using the two values ​​before gl_TessLevelOuter. Considering the case where two faces share one edge of the sheet, the first line is not displayed.

 3) Triangle subdivision domain: using the first three values ​​in the first value gl_TessLevelOuter gl_TessLevelInner. Segment coordinate system using the center of gravity coordinates (x, y, z) represents, for the individual components range [0.0, 1.0] while z + y + x = 1.0.

 

 

8、细分计算着色器:细分生成的顶点都要执行一次细分计算着色器将细分坐标转换为下文能够识别的屏幕坐标。细分计算着色器首先设置图元生成器,通过layout设置细分域、图元类型(quads、triangles、isolines)、图元面朝向(cw、ccw)、生成方式,然后根据前面的参数计算出需要的坐标等信息。

  layout (quads, equal_spacing, ccw, points) out;//这些参数的顺序不是严格的

  #version 420 core

  layout (quads, equal_spacing, ccw) in;

  void main()

  {

    float u = gl_TessCoord.x;

    float v = gl_TessCoord.y;

    float mu = 1.0 - u;

    float mv = 1.0 - v;

    gl_Position = mu*mv*gl_in[0].gl_Position + u*mv*gl_in[1].gl_Position + u*v*gl_in[2].gl_Position + mu*v*gl_in[3].gl_Position;

  }

 

10、https://www.khronos.org/opengl/wiki/Tessellation

 

Guess you like

Origin www.cnblogs.com/haihuahuang/p/12423868.html