javascript - 错误 : WebGL warning: vertexAttribPointer: -1 is not a valid `index`

标签 javascript glsl webgl

我最近遇到了一个非常奇怪的错误。在我的顶点着色器中,我有三个属性:

attribute vec3 vPosition;
attribute vec3 vNormal;
attribute vec3 vColor;

我以相同的方式初始化与所有缓冲区关联的缓冲区,一切看起来都很好。但是,在我的渲染周期中,当我将属性绑定(bind)到缓冲区时,第一个和第三个属性工作正常,而第二个属性给我一个奇怪的错误。这让我很烦恼,因为我对所有 3 个属性使用相同的代码。

这是我的绑定(bind)代码:

gl.bindBuffer(gl.ARRAY_BUFFER, this.vBuffer);
gl.vertexAttribPointer(gl.getAttribLocation(program, "vPosition"), 3, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(gl.getAttribLocation(program, "vPosition"));

gl.bindBuffer(gl.ARRAY_BUFFER, this.nBuffer);
gl.vertexAttribPointer(gl.getAttribLocation(program, "vNormal"), 3, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(gl.getAttribLocation(program, "vNormal"));

gl.bindBuffer(gl.ARRAY_BUFFER, this.cBuffer);
gl.vertexAttribPointer(gl.getAttribLocation(program, "vColor"), 3, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(gl.getAttribLocation(program, "vColor"));

这是我得到的错误代码(我得到了两次,每行一次调用 getAttribLocation(program, "vNormal"):

Error: WebGL warning: vertexAttribPointer: -1 is not a valid `index`. This value probably comes from a getAttribLocation() call, where this return value -1 means that the passed name didn't correspond to an active attribute in the specified program.

这似乎表明我对 getAttribLocation 做错了什么,但我将着色器中的变量的实际名称传递给它,就像我对其他两个变量所做的那样。有什么很明显我想念的吗?我该如何解决这个问题?

最佳答案

getAttribLocation 获取事件属性的属性索引。一个属性就是一个程序资源。

WebGL 1.0 Specification对于 5.14.10 Uniforms and attributes - getAttribLocation指向 OpenGL ES 2.0 Specification - 2.10.4 Shader Variables ,其中指定:

A generic attribute variable is considered active if it is determined by the compiler and linker that the attribute may be accessed when the shader is executed. Attribute variables that are declared in a vertex shader but never used are not considered active. [...]

这意味着,在顶点着色器程序中未“使用”的属性变量不会变为事件状态。因此,当查询变量的索引时,变量未与属性索引相关联,getAttribLocation 返回 -1。

关于javascript - 错误 : WebGL warning: vertexAttribPointer: -1 is not a valid `index` ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57735263/

相关文章:

c++ - OpenGL 着色器 GLSL 上的投影矩阵出现问题

opengl - 每片段光照坐标系

c - 如何使用 glShaderStorageBinding 绑定(bind)着色器缓冲区 block ?

webgl - 是否有可用的 THREE.js API 文档?

javascript - 可以在 nodeJS 中创建 3D 多人游戏服务器

javascript - 将基准线过渡到线性线会缩短/隐藏部分线

javascript - 如何在 Protractor 中使用 browser.actions().sendKeys 和 "?"符号?

glsl - gl_PointCoord 的精确坐标?

opengl - 使用着色器将纹素移动到其中心

javascript - 如何从 Javascript 触发 iPhone 应用程序中方法的执行?