Geometry Shader
A Geometry Shader (GS) is a Shader program written in GLSL that governs the processing of Primitives. Geometry shaders reside between the Vertex Shaders (or the optional Tessellation stage) and the fixed-function Vertex Post-Processing stage.
顶点着色器以顶点数据作为输入数据,而几何着色器则以完整的图元(Primitive)作为输入数据。经过几何着色器处理后,得到的是一系列位于齐次裁剪空间的顶点所组成的图元,然后送到下一阶段进行顶点后处理(Vertex Post-Processing)
举一个简单的例子,用顶点着色器传入的顶点产生直线图元:
layout(points) in;
layout(line_strip, max_vertices = 2) out;
void main()
{
gl_position = gl_in[0].gl_position + vec4(1.0, 0.0, 0.0, 0.0);
EmitVertex();
gl_position = gl_in[0].gl_position + vec4(-1.0, 0.0, 0.0, 0.0;
EmitVertex();
EndPrimitive();
}
第一行表示了输入的数据类型,示例中为 points 也就是传入的数据为一个点,几何着色器的输入为图元,可用的图元类型为:
points -- GL_POINTS
lines -- GL_LINES GL_LINE_STRIP GL_LINE_LIST
lines_adjacency -- GL_LINES_ADJACENCY GL_LINE_STRIP_ADJACENCY
triangles -- GL_TRIANGLES GL_TRIANGLE_STRIP GL_TRIANGLE_FAN
triangles_adjacency -- GL_TRIANGLES_ADJACENCY GL_TRIANGLE_STRIP_ADJACENCY
传入 geometry shader 的数据类型取决于调用 glDrawArrays 或者 glDrawElements 时传入的图元类型参数。例如你使用了 GL_POINTS
那么就应当输入 layout(points) in;
第二行 layout(line_strip, max_vertices = 2) out;
指定了输出图元类型和图元的最大顶点数,输出图元的类型可以是以下几种:
points
line_strip
triangle_strip
max_vertices
规定了最大输出顶点数目,当EmitVertex()
的数量超出该值时,OpenGL将不会绘制更多的顶点。
也就是说,对于一个 geometry shader,你必须指定以下内容:
layout(input_primitive) in;
layout(output_primitive, max_vertices = vert_count) out;
Instancing
geometry shader(GS) 可以被用于实例化(和实例化渲染不同,该实例化仅局限于GS),对于同一个输入图元, GS 将执行多次,每次调用都将获得不同的 gl_InvocationID
,一般用于分层渲染和输出到多个流。注:GS Instancing 功能为 OpenGL 4.0 以上的功能。
如果想使用 instancing,需要制定一个 input layout:
layout(invocations = num_instances) in;
num_instances
的值不能大于MAX_GEOMETRY_SHADER_INVOCATIONS
(至少为32)。内置值gl_InvocationID
指定该着色器的特定实例,该 ID 位于 [0, num_instances) 内。
从 Vertex Shader 输出的变量以数组的形式送到 GS,数组的长度取决于图元的种类,例如 points 就是 1,lines 就是 2, triangles 就是 3,输入数组中顶点的顺序与之前阶段中指定的顶点顺序一致。
Geometry Shader 提供了如下的内置输入变量,该变量声明为 ”interface block“:
in gl_PerVertex
{
vec4 gl_Position;
float gl_PointSize;
float gl_ClipDistance[];
} gl_in[];
gl_Position
the clip-space output position of the current vertex. This value must be written if you are emitting a vertex to stream 0, unless rasterization is off.
gl_PointSize
the pixel width/height of the point being rasterized. It is only necessary to write to this when outputting point primitives.
gl_ClipDistance
allows the shader to set the distance from the vertex to each User-Defined Clip Plane. A positive distance means that the vertex is inside/behind the clip plane, and a negative distance means it is outside/in front of the clip plane. In order to use this variable, the user must manually redeclare it (and therefore the interface block) with an explicit size.
我们比较常用的是通过 gl_in
来获取输入顶点的 gl_position
,如果你想从上一阶段传送自定义的数据到 GS,比如说 color,那么你需要在上一阶段的着色器中声明 out vec4 color
;并在 GS 中声明 in vec4 color[]
;注意输入的数据为数组;然后你就可以在 GS 中通过数组下标取得某个顶点的 color 值了。
继续看上面的示例中的 main 函数中的部分,输出图元中的每个顶点的 gl_position
都由输入的 gl_in
中的 gl_position
通过变换得到,并且每个顶点设置完毕之后调用 EmitVertex()
表示当前顶点结束,在一个完整图元的所有的顶点结束时,要调用 EndPrimitive()
表示当前图元结束,将上面的两个点组装成一条线。这样可以得到下面的结果:
triangle_strip 形成三角形图元的过程
triangle_strip
可以通过复用顶点来获得三角形,第一个三角形绘制完成之后,随后的每个顶点在第一个三角形旁边生成另一个三角形:每三个邻接的顶点形成一个三角形。例如下面的六个顶点:
我们可以得到4个三角形:(1,2,3), (2,3,4), (3,4,5) 和 (4,5,6); triangle_strip 至少需要 3 个顶点,然后生成 N - 2 个三角形。
line_strip 也是同样的道理。
Geometry Shader 的应用
- 爆炸效果
实现原理是通过将绘制的三角形沿着法线方向向外偏移一定的距离,从而得到整个物体像是爆炸的效果:
首先需要获得每个传入的三角形图元的法向量,这个可以通过三角形的两条边叉乘得到:
vec3 GetNormal()
{
vec3 a = vec3(gl_in[0].gl_Position) - vec3(gl_in[1].gl_Position);
vec3 b = vec3(gl_in[2].gl_Position) - vec3(gl_in[1].gl_Position);
return normalize(cross(a, b));
}
这里要注意叉乘的顺序,否则会得到与期望向量相反方向的向量。
接着就可以将顶点沿着某个方向进行位移:
vec4 explode(vec4 position, vec3 normal)
{
float magnitude = 2.0;
vec3 direction = normal * ((sin(time) + 1.0) / 2.0) * magnitude;
return position + vec4(direction, 0.0);
}
- 法线可视化
有时候我们需要判断法线贴图传入的法线经过变换之后是否正确,一般情况下通过颜色输出很难直观的观察到法线的异常,但是 Geometry Shader 可以轻松的做到这一点,将法线直接绘制出来,这样一来就可以直观的观察到法线方向了。
在 Vertex Shader 将计算好的法线输出到 GS:
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aNormal;
out VS_OUT {
vec3 normal;
} vs_out;
uniform mat4 view;
uniform mat4 model;
void main()
{
gl_Position = view * model * vec4(aPos, 1.0);
mat3 normalMatrix = mat3(transpose(inverse(view * model)));
vs_out.normal = normalize(vec3(vec4(normalMatrix * aNormal, 0.0)));
}
在几何着色器中每个顶点按照输入的 normal 生成一条线即可:
layout (triangles) in;
layout (line_strip, max_vertices = 6) out;
in VS_OUT {
vec3 normal;
} gs_in[];
const float MAGNITUDE = 0.4;
uniform mat4 projection;
void GenerateLine(int index)
{
gl_Position = projection * gl_in[index].gl_Position;
EmitVertex();
gl_Position = projection * (gl_in[index].gl_Position +
vec4(gs_in[index].normal, 0.0) * MAGNITUDE);
EmitVertex();
EndPrimitive();
}
void main()
{
GenerateLine(0); // first vertex normal
GenerateLine(1); // second vertex normal
GenerateLine(2); // third vertex normal
}
当然这项技术也可以被用到毛发渲染当中。
- 公告牌 (BillBoards)
公告牌技术被用于利用 2D 图像来代替 3D 物体,从而极大的节省资源。它的原理是让 2D 图像永远面向观察者,看上去就像是立体的了,一般用于大批量树木植物的绘制。
如何通过 GS 来实现 BillBoards 呢?
通过将给定公告牌的中心位置扩展成为四边形来实现:
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aNormal;
out VS_OUT {
vec3 normal;
} vs_out;
uniform mat4 view;
uniform mat4 model;
void main()
{
gl_Position = view * model * vec4(aPos, 1.0);
mat3 normalMatrix = mat3(transpose(inverse(view * model)));
vs_out.normal = normalize(vec3(vec4(normalMatrix * aNormal, 0.0)));
}
ref:
几何着色器
LearnOpenGL