您的位置:首页 > 其它

Geometry Shaders(几何造型Shader)

2019-07-19 06:05 1616 查看

周一到周五,每天一篇,北京时间早上7点准时更新~,中英文对照,一边学编程一边弹吉他,做一个奇葩码农!

请不要怀疑翻译是否有问题,我们的翻译工程师是蓝翔毕业的呢!

The geometry shader is logically the last shader stage in the front end(逻辑上来说,geometry shader是整个渲染管线的最后一个shader阶段), sitting after the vertex and tessellation stages and before the rasterizer(在vertex和tessellation之后,光栅化之前). The geometry shader runs once per primitive and has access to all of the input vertex data for all of the vertices that make up the primitive being processed(每个geometry shader一次处理一个图元,每次处理的时候,可以访问该图元的所有点的信息). The geometry shader is also unique among the shader stages in that it is able to increase or reduce the amount of data flowing through the pipeline in a programmatic way(geometry shader也是非常独特的,它可以通过编程的方式增加或者减少输出的图元). Tessellation shaders can also increase or decrease the amount of work in the pipeline, but only implicitly by setting the tessellation levelfor the patch(虽然tessellation也可以增加或者减少图元,但是只能隐式的通过一些设置去做). Geometry shaders, in contrast, include two functions—EmitVertex() and EndPrimitive()(相反,geometry shader有专门的函数去显示的增加或者减少发送给光栅化的图元的个数)— that explicitly produce vertices that are sent to primitive assembly and rasterization

Another unique feature of geometry shaders is that they can change the primitive mode mid-pipeline(另一个独特点就是geometry shader可以在中途改变图元的模式). For example, they can take triangles as input and produce a bunch of points or lines as output(比如他们可以以三角形作为输入图元,最后却输出点或者线), or even create triangles from independent points(甚至是通过输入的点创建更多三角形). An example geometry shader is shown in Listing 3.9.(Listing3.9显示了一个geometry shader的样本)

#version 450 core
layout (triangles) in;
layout (points, max_vertices = 3) out;
void main(void)
{
int i;
for (i = 0; i < gl_in.length(); i++)
{
gl_Position = gl_in[i].gl_Position;
EmitVertex();
}
}
Listing 3.9: Our first geometry shader

The shader shown in Listing 3.9 acts as another simple pass-through shader that converts triangles into points so that we can see their vertices(Listing 3.9显示了一个简单的geometry shader,它简单的将三角形转换成点). The first layout qualifier indicates that the geometry shader is expecting to see triangles as its input(第一个修饰符表示geometry shader希望得到三角形作为输入). The second layout qualifier tells OpenGL that the geometry shader will produce points and that the maximum number of points that each shader will produce will be three(第二个修饰符说明的是geometry shader输出的是点,并且每次最多输出3个点). In the main function, a loop runs through all of the members of the gl_in array, which is determined by calling its .length() function(在主函数里,有一个循环,会遍历gl_in数组,它的大小可以通过length接口获取)

We actually know that the length of the array will be three because we are processing triangles and every triangle has three vertices(其实我们已经知道数组大小是3了,因为我们输入的数据是三角形,并且三角形有三个点). The outputs of the geometry shader are again similar to those of a vertex shader(这些输出数据再一次变得更vertex shader的输出数据一样了). In particular, we write to gl_Position to set the position of the resulting vertex(特别是,我们使用gl_Position去设置顶点的位置). Next, we call EmitVertex(), which produces a vertex at the output of the geometry shader(然后我们调用EmitVertex来输出一个点). Geometry shaders automatically call EndPrimitive() at the end of your shader, so calling this function explicitly is not necessary in this example(geometry shader会自动调用EndPrimitive,所以你不必在shader中显示的去调用它). As a result of running this shader, three vertices will be produced and rendered as points(运行之后,将会有三个点被渲染出来)

By inserting this geometry shader into our simple one tessellated triangle example, we obtain the output shown in Figure 3.2(在把这个geometry shader塞到我们的渲染管线了之后,我们将会得到图3.2的结果). To create this image, we set the point size to 5.0 by calling glPointSize()(为了得到这个图片,我们把点的大小设置成了5.0,从而使得点能够大一点,且能够容易被看到). This makes the points large and highly visible

本日的翻译就到这里,明天见,拜拜~~

第一时间获取最新桥段,请关注东汉书院以及图形之心公众号

东汉书院,等你来玩哦

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: