Opengl es 尝试从vec4获取颜色数据时出现WebGL错误

Opengl es 尝试从vec4获取颜色数据时出现WebGL错误,opengl-es,glsl,webgl,Opengl Es,Glsl,Webgl,在WebGL中渲染正方形时出现问题。当我在Chrome中运行程序时,我得到了一个错误: GL ERROR :GL_INVALID_OPERATION : glDrawArrays: attempt to access out of range vertices in attribute 0 我假设这是因为,在某些情况下,缓冲区在试图获取数据时正在查看错误的数组。我已经把这个问题精确地指出了 gl.vertexAttribPointer(pColorIndex, 4, gl.FLOAT, fal

在WebGL中渲染正方形时出现问题。当我在Chrome中运行程序时,我得到了一个错误:

GL ERROR :GL_INVALID_OPERATION : glDrawArrays: attempt to access out of range vertices in attribute 0
我假设这是因为,在某些情况下,缓冲区在试图获取数据时正在查看错误的数组。我已经把这个问题精确地指出了

gl.vertexAttribPointer(pColorIndex, 4, gl.FLOAT, false, 0, 0);
下面代码中的行。i、 e.如果我将4改为2,代码将运行,但不正常(因为我在这里查看的是颜色数据的vec4)。数组的绑定方式是否存在问题

    bindColorDisplayShaders();
// clear the framebuffer
gl.clear(gl.COLOR_BUFFER_BIT);

// bind the shader
gl.useProgram(shader);

// set the value of the uniform variable in the shader
var shift_loc = gl.getUniformLocation(shader, "shift");
gl.uniform1f(shift_loc, .5);

// bind the buffer
gl.bindBuffer(gl.ARRAY_BUFFER, vertexbuffer);

// get the index for the a_Position attribute defined in the vertex shader
var positionIndex = gl.getAttribLocation(shader, 'a_Position');
if (positionIndex < 0) {
    console.log('Failed to get the storage location of a_Position');
    return;
}

// "enable" the a_position attribute
gl.enableVertexAttribArray(positionIndex);

// associate the data in the currently bound buffer with the a_position attribute
// (The '2' specifies there are 2 floats per vertex in the buffer.  Don't worry about
// the last three args just yet.)
gl.vertexAttribPointer(positionIndex, 2, gl.FLOAT, false, 0, 0);

gl.bindBuffer(gl.ARRAY_BUFFER, null);

// bind the buffer with the color data
gl.bindBuffer(gl.ARRAY_BUFFER, chosencolorbuffer);

var pColorIndex = gl.getUniformLocation(shader, 'a_ChosenColor');
if(pColorIndex < 0){
    console.log('Failed to get the storage location of a_ChosenColor');
}

gl.enableVertexAttribArray(pColorIndex);

gl.vertexAttribPointer(pColorIndex, 4, gl.FLOAT, false, 0, 0);

gl.bindBuffer(gl.ARRAY_BUFFER, null);

// draw, specifying the type of primitive to assemble from the vertices
gl.drawArrays(gl.TRIANGLES, 0, numPoints);
bindColorDisplayShaders();
//清除帧缓冲区
总账清除(总账颜色缓冲位);
//绑定着色器
gl.useProgram(着色器);
//在着色器中设置均匀变量的值
var shift_loc=gl.getUniformLocation(着色器,“shift”);
gl.uniform1f(班次位置,.5);
//绑定缓冲区
gl.bindBuffer(gl.ARRAY\u BUFFER,vertexbuffer);
//获取顶点着色器中定义的a_位置属性的索引
var positionIndex=gl.getAttributeLocation(着色器“a_位置”);
如果(位置索引<0){
console.log('未能获取_位置的存储位置');
返回;
}
//“启用”a_位置属性
gl.EnableVertexAttributeArray(位置索引);
//将当前绑定缓冲区中的数据与a_position属性关联
//(2指定缓冲区中每个顶点有2个浮动。不要担心
//最后三个参数刚刚开始。)
gl.VertexAttributePointer(位置索引,2,gl.FLOAT,false,0,0);
gl.bindBuffer(gl.ARRAY\u BUFFER,null);
//将缓冲区与颜色数据绑定
gl.bindBuffer(gl.ARRAY\u BUFFER,chosencolorbuffer);
var pColorIndex=gl.getUniformLocation(着色器“a_ChosenColor”);
if(pColorIndex<0){
console.log('未能获取a_ChosenColor'的存储位置');
}
gl.EnableVertexAttributeArray(pColorIndex);
gl.VertexAttributePointer(pColorIndex,4,gl.FLOAT,false,0,0);
gl.bindBuffer(gl.ARRAY\u BUFFER,null);
//绘制,指定要从顶点组装的基本体的类型
gl.DrawArray(gl.TRIANGLES,0,numPoints);

您只能使用
统一属性或
顶点属性,
这是两件不同的事情

使用顶点属性时必须匹配位置缓冲区中的顶点数量,并使用
gl.getAttriblLocation
获取位置

使用uniform时,您不是通过数组缓冲区提供其数据,而是使用
gl.uniform*
方法设置其值


在您的示例中,
gl.uniform4fv(pColorIndex,yourColorVector)

您只能使用
uniform
a
顶点属性,
这是两件不同的事情

使用顶点属性时必须匹配位置缓冲区中的顶点数量,并使用
gl.getAttriblLocation
获取位置

使用uniform时,您不是通过数组缓冲区提供其数据,而是使用
gl.uniform*
方法设置其值


在您的示例中,
gl.uniform4fv(pColorIndex,yourColorVector)

您只能使用
uniform
a
顶点属性,
这是两件不同的事情

使用顶点属性时必须匹配位置缓冲区中的顶点数量,并使用
gl.getAttriblLocation
获取位置

使用uniform时,您不是通过数组缓冲区提供其数据,而是使用
gl.uniform*
方法设置其值


在您的示例中,
gl.uniform4fv(pColorIndex,yourColorVector)

您只能使用
uniform
a
顶点属性,
这是两件不同的事情

使用顶点属性时必须匹配位置缓冲区中的顶点数量,并使用
gl.getAttriblLocation
获取位置

使用uniform时,您不是通过数组缓冲区提供其数据,而是使用
gl.uniform*
方法设置其值


在您的示例中,
gl.uniform4fv(pColorIndex,yourColorVector)

看起来不错,颜色缓冲区的长度是否与位置缓冲区中的顶点数量匹配?@LJ颜色缓冲区中只有一种颜色(即4个浮点数),而位置缓冲区中有几个顶点。这不会有问题吧?看起来不错,颜色缓冲区的长度与位置缓冲区中的顶点数量匹配吗?@LJ颜色缓冲区中只有一种颜色(4个浮动),而位置缓冲区中有几个顶点。这不会有问题吧?看起来不错,颜色缓冲区的长度与位置缓冲区中的顶点数量匹配吗?@LJ颜色缓冲区中只有一种颜色(4个浮动),而位置缓冲区中有几个顶点。这不会有问题吧?看起来不错,颜色缓冲区的长度与位置缓冲区中的顶点数量匹配吗?@LJ颜色缓冲区中只有一种颜色(4个浮动),而位置缓冲区中有几个顶点。这不会是个问题吧?哦,我知道我做错了什么。我以前没注意到,谢谢!哦,我知道我做错了什么。我以前没注意到,谢谢!哦,我知道我做错了什么。我以前没注意到,谢谢!哦,我知道我做错了什么。我以前没注意到,谢谢!