首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >什么时候与regl一起使用主着色器函数是合适的?

什么时候与regl一起使用主着色器函数是合适的?
EN

Stack Overflow用户
提问于 2018-06-12 16:34:22
回答 1查看 182关注 0票数 0

在构造着色器时,我注意到有时构造一个名为main的空函数来定义着色器。当属性被迭代时,main似乎被添加到着色器中。

我想弄清楚什么时候在main中定义着色器,而不是仅仅在着色器的顶层定义空间内。

来自regl教程的示例代码。

代码语言:javascript
复制
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
})
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-12 19:37:02

我不熟悉regl,但是GLSL着色器总是需要一个main()函数。这是着色器的入口点。例如,在顶点着色器的情况下,将对每个顶点执行着色器的main()函数。在您的教程代码中,gl_Position是顶点着色器的内置输出(与用户定义的输出相反)。attribute vec2 position;为顶点着色器定义了一个输入变量,它允许访问position属性。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50821974

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档