首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >WebGL drawElements超出范围了吗?

WebGL drawElements超出范围了吗?
EN

Stack Overflow用户
提问于 2013-04-02 04:59:17
回答 2查看 17K关注 0票数 17

我正在尝试使用WebGL来学习它,所以我从webgl教程中提取了一些代码,并尝试添加我自己的代码行,但每当我运行它时,它都会给我这个错误:

.WebGLRenderingContext: GL ERROR :GL_INVALID_OPERATION : glDrawElements: attempt to access out of range vertices in attribute 0 

注意:属性0是我的顶点缓冲区

我的缓冲区初始化代码是(如果没有定义,显然假设是全局变量)

    cubeVertexPositionBuffer = gl.createBuffer(); // create a buffer
gl.bindBuffer(gl.ARRAY_BUFFER, cubeVertexPositionBuffer);

//for laziness
var _f=1.0/3.0;

vertices = [ // this is code from the tutorial
// Front face
-1.0, -1.0, 1.0, 1.0, -1.0, 1.0, 1.0, 1.0, 1.0, -1.0, 1.0, 1.0,
// Back face
-1.0, -1.0, -1.0, -1.0, 1.0, -1.0, 1.0, 1.0, -1.0, 1.0, -1.0, -1.0,
// Top face
-1.0, 1.0, -1.0, -1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, -1.0,
// Bottom face
-1.0, -1.0, -1.0, 1.0, -1.0, -1.0, 1.0, -1.0, 1.0, -1.0, -1.0, 1.0,
// Right face
1.0, -1.0, -1.0, 1.0, 1.0, -1.0, 1.0, 1.0, 1.0, 1.0, -1.0, 1.0,
// Left face
-1.0, -1.0, -1.0, -1.0, -1.0, 1.0, -1.0, 1.0, 1.0, -1.0, 1.0, -1.0, 

// this is my own code
-1.0+  _f, -1.0, 1.0,   -1.0+  _f, -1.0, -1.0,
-1.0+2*_f, -1.0, 1.0,   -1.0+2*_f, -1.0, -1.0,
-1.0+3*_f, -1.0, 1.0,   -1.0+3*_f, -1.0, -1.0,
-1.0+4*_f, -1.0, 1.0,   -1.0+4*_f, -1.0, -1.0,
-1.0+5*_f, -1.0, 1.0,   -1.0+5*_f, -1.0, -1.0


];
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices),
        gl.STATIC_DRAW);
cubeVertexPositionBuffer.itemSize = 3;
cubeVertexPositionBuffer.numItems = 34;

    // color buffer code is irrelevant because my color buffer is attribute 1 not 0
    // plus it's all copied from the tutorial

    // index buffer 
    // note I changed some code so the cube drawn is wireframe instead of solid
    // I tested that without extra vertex or index buffer data and it worked
    cubeVertexIndexBuffer = gl.createBuffer(); // this modified a bit from the tutorial
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, cubeVertexIndexBuffer);
cubeVertexIndices = 
    [ 0, 1, 2, 3, 0, // Front face 
      4, 5, 6, 7, 4, // Back face 
      8, 9, 10, 11, 8, // Top face 
      12, 13, 14, 15, 12, // Bottom face 
      16, 17, 18, 19, 16, // Right face 
      20, 21, 22, 23, 20, // Left face

      // this is my code
      24, 25, 26, 27, 28, 29, 30, 31, 32, 33
    ];
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(
        cubeVertexIndices), gl.STATIC_DRAW);
cubeVertexIndexBuffer.itemSize = 1;
cubeVertexIndexBuffer.numItems = 40;

这是我的绘图代码

// set up perspective and stuff
    gl.bindBuffer(gl.ARRAY_BUFFER, cubeVertexPositionBuffer);
gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute,
        cubeVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0);

gl.bindBuffer(gl.ARRAY_BUFFER, cubeVertexColorBuffer);
gl.vertexAttribPointer(shaderProgram.vertexColorAttribute,
        cubeVertexColorBuffer.itemSize, gl.FLOAT, false, 0, 0);

gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, cubeVertexIndexBuffer);

gl.uniformMatrix4fv(shaderProgram.pMatrixUniform, false, pMatrix); // perspective matrix
gl.uniformMatrix4fv(shaderProgram.mvMatrixUniform, false, mvMatrix); // model view matrix

gl.lineWidth(1.0);
gl.drawElements(gl.LINES, cubeVertexIndexBuffer.numItems,
        gl.UNSIGNED_SHORT, 0);
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-04-02 07:51:49

实际上我想通了。作为一个WebGL新手,我忘记在我的颜色缓冲区中添加每个顶点的索引。我从来没有想到我的着色器需要每个顶点都有一种颜色。(不过,如果WebGL说这是属性1(我的颜色属性)而不是属性0(我的顶点位置属性)中的错误,那就太好了。

票数 14
EN

Stack Overflow用户

发布于 2015-07-11 16:20:03

如果您在以下位置分配的bufferData也会出现此问题:

gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(
    cubeVertexIndices), gl.STATIC_DRAW);

太短(请参见第二个参数)。

如果顶点的创建是以编程方式进行的,则会发生这种情况。至少,这件事发生在我身上。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15751791

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档