WebGL:enablevertexattribarray索引超出范围 [英] WebGL: enablevertexattribarray index out of range

查看:215
本文介绍了WebGL:enablevertexattribarray索引超出范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是我的顶点和片段着色器:

Here my vertex and fragment shaders:

    <script id="shader-fs" type="x-shader/x-fragment">
        precision mediump float;

        uniform sampler2D uSampler;

        varying vec4 vColor;
        varying vec2 vTextureCoord;

        void main(void) {
            gl_FragColor = vColor;
            // gl_FragColor = texture2D(uSampler, vec2(vTextureCoord.s, vTextureCoord.t));
        }
    </script>
    <script id="shader-vs" type="x-shader/x-vertex">
        attribute vec3 aVertexPosition;
        attribute vec4 aVertexColor;
        attribute vec2 aTextureCoord;

        uniform mat4 uMVMatrix;
        uniform mat4 uPMatrix;

        varying vec4 vColor;
        varying vec2 vTextureCoord;

        void main(void) {
          gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);
          vColor = aVertexColor;
          // vTextureCoord = aTextureCoord;
        }
    </script>

这是我的着色器初始化程序:

And here's my shader initializer:

function initShaders() {
  var fragmentShader = getShader(gl, "shader-fs");
  var vertexShader = getShader(gl, "shader-vs");

  shaderProgram = gl.createProgram();

  gl.attachShader(shaderProgram, vertexShader);
  gl.attachShader(shaderProgram, fragmentShader);
  gl.linkProgram(shaderProgram);

  if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) {
      alert("Could not initialise shaders");
  }
  gl.useProgram(shaderProgram);

  shaderProgram.vertexPositionAttribute = gl.getAttribLocation(shaderProgram, "aVertexPosition");
  gl.enableVertexAttribArray(shaderProgram.vertexPositionAttribute);

  shaderProgram.vertexColorAttribute = gl.getAttribLocation(shaderProgram, "aVertexColor");
  gl.enableVertexAttribArray(shaderProgram.vertexColorAttribute);

  shaderProgram.textureCoordAttribute = gl.getAttribLocation(shaderProgram, "aTextureCoord");
  gl.enableVertexAttribArray(shaderProgram.textureCoordAttribute);

  shaderProgram.pMatrixUniform = gl.getUniformLocation(shaderProgram, "uPMatrix");
  shaderProgram.mvMatrixUniform = gl.getUniformLocation(shaderProgram, "uMVMatrix");
  shaderProgram.samplerUniform = gl.getUniformLocation(shaderProgram, "uSampler");
}

错误来自这一行:

  gl.enableVertexAttribArray(shaderProgram.textureCoordAttribute);
  >> enablevertexattribarray index out of range

如何处理?

推荐答案

这只是因为你没有在你的顶点程序中使用 aTextureCoord ,所以GLSL-Compiler通过以下方式优化它删除它。你真的应该检查gl.GetAttribLocation()的结果是否有错误,并且只启用程序中存在的属性。在缺少属性的情况下发出警告就足够了,我知道无法通过编译器区分着色器创作错误和优化。

Thats simply because you do not use aTextureCoord in your vertex program, so the GLSL-Compiler optimizes it by removing it. You really should check the result of gl.GetAttribLocation() for errors, and enable only the attributes that are present in your program. Issuing a warning in case an attribute is missing would be sufficient, I know no way to distinguish shader-authoring-errors from optimizations by the compiler.

这篇关于WebGL:enablevertexattribarray索引超出范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆