【问题标题】:WebGL: Switch between ShadersWebGL:在着色器之间切换
【发布时间】:2013-05-02 10:08:39
【问题描述】:

我写了一个小函数来在着色器之间切换,但不幸的是它不起作用。 我创建着色器并将它们作为着色器对象返回。

  1. program = gl.createProgram(vs, fs, "VertexPosition", "TextureCoord", "VertexNormal");

  2. depthprogram = gl.createProgram(vs, fs, "VertexPosition", false, false);

我在更新功能中使用了开关功能,其中包括:

    function Update(){ 
            gl.switchProgram(program);
            gl.Draw(Teapot, cam);
    };

模型的所有buffer都绑定在draw函数中,(参数:模型,相机位置)

    this.switchProgram = function(program){

    if (this.program !== program){
        this.lastprogram = this.program;
        this.program = program;
        gl.useProgram(this.program);

    };

};

这是产生的错误:WebGL:INVALID_OPERATION:drawElements:attribs 未正确设置 如果我评论一切正常,但我无法切换:(

depthprogram = gl.createProgram(vs, fs, "VertexPosition", false, false);
gl.switchProgram(program);

【问题讨论】:

    标签: webgl shader


    【解决方案1】:

    两个着色器是否使用相同的属性? 据我从您的代码中得出的结论,第一个着色器使用:

    attribute vertexPos;
    attribute vertexNormal;
    attribute textureCoord;
    

    只有第二次使用

    attribute vertexPos;
    

    如果您尝试发送未在程序的顶点着色器中使用的属性,或者您不发送顶点着色器请求的属性,那么您将收到警告。 我会说你不会为第一个着色器发送 vertexNormal 和 textureCoord,尽管着色器需要它们。

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-03
      • 1970-01-01
      相关资源
      最近更新 更多