在构造着色器时,我注意到有时构造一个名为main的空函数来定义着色器。当属性被迭代时,main似乎被添加到着色器中。
我想弄清楚什么时候在main中定义着色器,而不是仅仅在着色器的顶层定义空间内。
来自regl教程的示例代码。
var drawTriangle = regl({
//
// First we define a vertex shader. This is a program which tells the GPU
// where to draw the vertices.
//
vert: `
// This is a simple vertex shader that just passes the position through
attribute vec2 position;
void main () {
gl_Position = vec4(position, 0, 1);
}
`,
//
// Next, we define a fragment shader to tell the GPU what color to draw.
//
frag: `
// This is program just colors the triangle white
void main () {
gl_FragColor = vec4(1, 1, 1, 1);
}
`,
// Finally we need to give the vertices to the GPU
attributes: {
position: [
[1, 0],
[0, 1],
[-1, -1]
]
},
// And also tell it how many vertices to draw
count: 3
})发布于 2018-06-12 19:37:02
我不熟悉regl,但是GLSL着色器总是需要一个main()函数。这是着色器的入口点。例如,在顶点着色器的情况下,将对每个顶点执行着色器的main()函数。在您的教程代码中,gl_Position是顶点着色器的内置输出(与用户定义的输出相反)。attribute vec2 position;为顶点着色器定义了一个输入变量,它允许访问position属性。
https://stackoverflow.com/questions/50821974
复制相似问题