WEBGL:无效\u值:VertexAttributePointer:索引超出范围,无效\u值:EnableVertexAttributeArray:索引超出范围
我从.obj文件中读取数据来绘制模型。我使用纹理和顶点正确地绘制模型。但是,当我想使用照明绘制时,我有一个错误。 此错误是WebGL: 无效的\u值:VertexAttributePointer:索引超出范围,无效的\u值:EnableVertexAttributeArray:索引超出范围 这段代码是我的顶点着色器WEBGL:无效\u值:VertexAttributePointer:索引超出范围,无效\u值:EnableVertexAttributeArray:索引超出范围,webgl,fragment-shader,vertex-shader,lighting,normals,Webgl,Fragment Shader,Vertex Shader,Lighting,Normals,我从.obj文件中读取数据来绘制模型。我使用纹理和顶点正确地绘制模型。但是,当我想使用照明绘制时,我有一个错误。 此错误是WebGL: 无效的\u值:VertexAttributePointer:索引超出范围,无效的\u值:EnableVertexAttributeArray:索引超出范围 这段代码是我的顶点着色器 var VertexShaderCode = "precision mediump float;\n"+ "attribute v
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" +
" gl_Position = projectionMatrix * modelViewMatrix * vec4(vertexPos, 1.0); \n" +
" vTextureCoord = aTextureCoord;\n"+
"}\n";
这是我的片段着色器代码
var FragmentShaderCode = "precision lowp float;"+
"uniform vec4 color;"+
"varying vec2 vTextureCoord;"+
"uniform vec3 u_Ambient_color;\n" +
"uniform sampler2D uSampler;"+
"uniform int texturecontrol;"+
"void main() { "+
"vec3 uColor;"+
"uColor = u_Ambient_color * vec3(color);"+
" if(texturecontrol !=0 )"+
" {"+
" gl_FragColor =texture2D(uSampler, vTextureCoord)*vec4(uColor,color.a);"+
" }"+
" else{"+
" gl_FragColor = vec4(uColor,color.a);"+
" }"+
"}"
我试着这样画
if(this.Materyals[i].HasTexture){
this.gl.uniform1i(this.FGlobe.PModeltexturecontrol,1)
this.gl.uniform3fv(this.FGlobe.PModelAmbientColorLoc,this.Materyals[i].ambient)
this.gl.uniform4fv(this.FGlobe.PModelColorLoc,this.Materyals[i].color)
this.gl.bindTexture(this.gl.TEXTURE_2D,this.Materyals[i].Texture)
this.gl.bindBuffer(this.gl.ARRAY_BUFFER,this.Materyals[i].textureCoord)
this.gl.vertexAttribPointer(this.FGlobe.PModeltextCoordLoc,2,this.gl.FLOAT,false,0,0) //şu anda this.gl.ARRAY_BUFFER'a geçerli olan tamponun mevcut köşe tamponu nesnesinin genel bir vertex özniteliğine bağlanması ve düzenini belirtir.
this.gl.enableVertexAttribArray(this.FGlobe.PModeltextCoordLoc)
}else{
this.gl.uniform1i(this.FGlobe.PModeltexturecontrol,0)
this.gl.uniform3fv(this.FGlobe.PModelAmbientColorLoc,this.Materyals[i].ambient)
this.gl.uniform4fv(this.FGlobe.PModelColorLoc,this.Materyals[i].color)
this.gl.disableVertexAttribArray(this.FGlobe.PModeltextCoordLoc)
}
this.gl.bindBuffer(this.gl.ARRAY_BUFFER,this.Materyals[i].normalBuffer)
this.gl.vertexAttribPointer(this.FGlobe.PModelnormalPos,3, this.gl.FLOAT, false, 0, 0)
this.gl.enableVertexAttribArray(this.FGlobe.PModelnormalPos)
this.gl.bindBuffer(this.gl.ARRAY_BUFFER,this.Materyals[i].vertexBuffer)
this.gl.vertexAttribPointer(this.FGlobe.PModelvertexPos,3, this.gl.FLOAT, false, 0, 0)
this.gl.enableVertexAttribArray(this.FGlobe.PModelvertexPos)
this.gl.drawArrays(this.gl.TRIANGLES,0, this.Materyals[i].TriCnt*3);
}
有什么想法吗?我该怎么办?您为
VertexAttributePointer
和启用VertexAttributeArray
指定了不正确的属性索引。您尚未提供设置这些值的代码,但应仔细检查代码中是否存在获取属性和位置的键入错误。您为vertexAttribPointer
和enableVertexAttribArray
指定了不正确的属性索引。您尚未提供用于设置这些值的代码,但应仔细检查代码中是否存在获取属性和制服位置的拼写错误。我怀疑这是因为您有一个未使用的属性vertexNormal。如果在代码中添加一行:
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";
事情可能会很顺利。如果是这种情况,很可能是因为WebGL优化了该属性,因为它没有被使用。我怀疑这是因为您有一个未使用的属性vertexNormal。如果在代码中添加一行:
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";
事情可能会很顺利。如果是这种情况,很可能是因为WebGL优化了该属性,因为它没有被使用。这发生在我身上,我只是删除了javascript端未使用的属性,消息就消失了。这发生在我身上,我只是删除了javascript端未使用的属性,消息就消失了。