I read data from .obj file to draw a model.I draw the model correctly by using textures and vertexs. However when I want to draw using lighting, I have an error. This error is WebGL:
INVALID_VALUE: vertexAttribPointer: index out of range, INVALID_VALUE: enableVertexAttribArray: index out of range.
This code is my vertex shader
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";
this is my fragment shader code
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);"+
" }"+
"}"
and I try to draw like this.
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);
}
Any ideas?What should I do?
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";
things will probably work fine. If this is the case, it's likely because WebGL optimized away the attribute since it wasn't used.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With