WEBGL:INVALID_VALUE:vertexAttribPointer:索引超出范围,INVALID_VALUE:enableVertexAttribArray:索引超出范围

Has*_*baş 2 lighting normals webgl vertex-shader fragment-shader

我从 .obj 文件读取数据来绘制模型。我使用纹理和顶点正确绘制模型。但是,当我想使用光照进行绘制时,出现错误。\n此错误是 WebGL:

\n\n
\n

INVALID_VALUE:vertexAttribPointer:索引超出范围,INVALID_VALUE:enableVertexAttribArray:索引超出范围。

\n
\n\n

这段代码是我的顶点着色器

\n\n
var VertexShaderCode =  "precision mediump float;\\n"+\n                         "attribute vec3 vertexPos;\\n" +\n                         "attribute vec3 vertexNormal;\\n"+\n                         "uniform mat4 modelViewMatrix;\\n" +\n                         "uniform mat4 projectionMatrix;\\n" +\n                         "attribute vec2 aTextureCoord;\\n" +\n                         "varying vec2 vTextureCoord;\\n" +\n                         "    void main(void) {\\n" +\n                         "    gl_Position =  projectionMatrix * modelViewMatrix * vec4(vertexPos, 1.0); \\n" +\n                         "    vTextureCoord = aTextureCoord;\\n"+\n                         "}\\n";\n
Run Code Online (Sandbox Code Playgroud)\n\n

这是我的片段着色器代码

\n\n
var FragmentShaderCode =  "precision lowp float;"+\n                           "uniform vec4 color;"+\n                           "varying vec2 vTextureCoord;"+\n                            "uniform vec3 u_Ambient_color;\\n" +\n                           "uniform sampler2D uSampler;"+\n                           "uniform int texturecontrol;"+\n                           "void main() { "+\n                           "vec3 uColor;"+\n                           "uColor = u_Ambient_color * vec3(color);"+\n                           " if(texturecontrol !=0 )"+\n                           " {"+\n                           "  gl_FragColor =texture2D(uSampler, vTextureCoord)*vec4(uColor,color.a);"+\n                           " }"+\n                           " else{"+\n                           "  gl_FragColor = vec4(uColor,color.a);"+\n                           " }"+\n                           "}"\n
Run Code Online (Sandbox Code Playgroud)\n\n

我尝试这样画。

\n\n
if(this.Materyals[i].HasTexture){\n    this.gl.uniform1i(this.FGlobe.PModeltexturecontrol,1)\n    this.gl.uniform3fv(this.FGlobe.PModelAmbientColorLoc,this.Materyals[i].ambient)\n    this.gl.uniform4fv(this.FGlobe.PModelColorLoc,this.Materyals[i].color)\n    this.gl.bindTexture(this.gl.TEXTURE_2D,this.Materyals[i].Texture)\n    this.gl.bindBuffer(this.gl.ARRAY_BUFFER,this.Materyals[i].textureCoord)\n    this.gl.vertexAttribPointer(this.FGlobe.PModeltextCoordLoc,2,this.gl.FLOAT,false,0,0)  //\xc5\x9fu anda this.gl.ARRAY_BUFFER\'a ge\xc3\xa7erli olan tamponun mevcut k\xc3\xb6\xc5\x9fe tamponu nesnesinin genel bir vertex \xc3\xb6zniteli\xc4\x9fine ba\xc4\x9flanmas\xc4\xb1 ve d\xc3\xbczenini belirtir.\n    this.gl.enableVertexAttribArray(this.FGlobe.PModeltextCoordLoc)\n}else{            \n    this.gl.uniform1i(this.FGlobe.PModeltexturecontrol,0)\n    this.gl.uniform3fv(this.FGlobe.PModelAmbientColorLoc,this.Materyals[i].ambient)\n    this.gl.uniform4fv(this.FGlobe.PModelColorLoc,this.Materyals[i].color)\n    this.gl.disableVertexAttribArray(this.FGlobe.PModeltextCoordLoc)\n}\nthis.gl.bindBuffer(this.gl.ARRAY_BUFFER,this.Materyals[i].normalBuffer)\nthis.gl.vertexAttribPointer(this.FGlobe.PModelnormalPos,3, this.gl.FLOAT, false, 0, 0)\nthis.gl.enableVertexAttribArray(this.FGlobe.PModelnormalPos)\n\n\nthis.gl.bindBuffer(this.gl.ARRAY_BUFFER,this.Materyals[i].vertexBuffer)\nthis.gl.vertexAttribPointer(this.FGlobe.PModelvertexPos,3, this.gl.FLOAT, false, 0, 0)\nthis.gl.enableVertexAttribArray(this.FGlobe.PModelvertexPos)\nthis.gl.drawArrays(this.gl.TRIANGLES,0, this.Materyals[i].TriCnt*3);\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

有什么想法吗?我该怎么办?

\n

小智 6

I suspect it's because you have an unused attribute vertexNormal. If you add a line into the code:

var VertexShaderCode =  "precision mediump float;\n"+
                     "attribute vec3 vertexPos;\n" +
                     "attribute vec3 vertexNormal;\n"+
                     "uniform mat4 modelViewMatrix;\n" +
                     "uniform mat4 projectionMatrix;\n" +
                     "attribute vec2 aTextureCoord;\n" +
                     "varying vec2 vTextureCoord;\n" +
                     "    void main(void) {\n" +
                     "    vertexNormal; \n" +               /* This line here */
                     "    gl_Position =  projectionMatrix * modelViewMatrix * vec4(vertexPos, 1.0); \n" +
                     "    vTextureCoord = aTextureCoord;\n"+
                     "}\n";
Run Code Online (Sandbox Code Playgroud)

things will probably work fine. If this is the case, it's likely because WebGL optimized away the attribute since it wasn't used.