Graphics Shaders
图形着色器
Graphics is rendered using a rendering pipeline split into stages. There are multiple APIs to control graphics rendering. Qt supports OpenGL, Metal, Vulcan, and Direct3D. Looking at a simplified OpenGL pipeline, we can spot a vertex and fragment shader. These concepts exists for all other rendering pipelines too.
图形是使用分为多个阶段的渲染管道进行渲染的。有多个API来控制图形渲染。Qt支持OpenGL、Metal、Vulcan和Direct3D。通过一个简化的OpenGL管道,我们可以发现一个顶点和片段着色器。这些概念也适用于所有其他渲染管道。
In the pipeline, the vertex shader receives vertex data, i.e. the location of the corners of each element that makes up the scene, and calculates a gl_Position
. This means that the vertex shader can move graphical elements. In the next stage, the vertexes are clipped, transformed and rasterized for pixel output. Then the pixels, also known as fragments,are passed through the fragment shader, which calculates the color of each pixel. The resulting color returned through the gl_FragColor
variable.
在管道中,顶点着色器接收顶点数据,即构成场景的每个元素的角点位置,并计算gl_Position
。这意味着顶点着色器可以移动图形元素。在下一阶段中,顶点被剪裁、变换和光栅化,以获得像素输出。然后,像素(也称为片段)通过片段着色器,该着色器计算每个像素的颜色。通过gl_FragColor变量返回的结果颜色。
To summarize: the vertex shader is called for each corner point of your polygon (vertex = point in 3D) and is responsible for any 3D manipulation of these points. The fragment (fragment = pixel) shader is called for each pixel and determines the color of that pixel.
总结:顶点着色器用于多边形的每个角点(顶点=3D中的点),并负责这些点的任何3D操作。为每个像素调用片段(片段=像素)着色器,并确定该像素的颜色。
As Qt is independent of the underlying rendering API, Qt relies on a standard language for writing shaders. The Qt Shader Tools rely on a Vulcan-compatible GLSL. We will look more at this in the examples in this chapter.
由于Qt独立于底层的渲染API,因此Qt依赖于编写着色器的标准语言。Qt着色器工具依赖于Vulcan兼容的GLSL。我们将在本章的示例中详细介绍这一点。