【问题标题】:What is causing invalid operation on my draw arrays call?是什么导致我的绘制数组调用无效操作?
【发布时间】:2016-02-10 14:52:58
【问题描述】:

我正在尝试使用 WebGL 从头开始​​绘制一个简单的三角形。

我有使用 C++ 编写 openGL 应用程序的经验,并查看了 webGL 参考卡来翻译我的代码。

但是,我在调试应用程序时遇到了困难。 我收到的特定错误消息是:

GL ERROR :GL_INVALID_OPERATION : glDrawArrays: 尝试访问属性 0 中超出范围的顶点

完整代码在这里:https://github.com/gheshu/webGL_experiments

顶点数据布局为 3 个浮点数的 3 个向量。 存在三个属性:位置、法线和颜色,应绑定在索引 0、1、2 上。

一些重要的sn-ps:

网格类:

class Mesh{
    constructor(){
        this.num_vertices = 0;  
        this.vbo = gl.createBuffer();
        gl.bindBuffer(gl.ARRAY_BUFFER, this.vbo);
        gl.enableVertexAttribArray(0);
        gl.vertexAttribPointer(0, 3, gl.FLOAT, false, 4*3*3, 0);
        gl.enableVertexAttribArray(1);
        gl.vertexAttribPointer(1, 3, gl.FLOAT, false, 4*3*3, 4*3);
        gl.enableVertexAttribArray(2);
        gl.vertexAttribPointer(2, 3, gl.FLOAT, false, 4*3*3, 4*3*2);
    }
    upload(buffer){
        console.log(buffer);
        gl.bindBuffer(gl.ARRAY_BUFFER, this.vbo);
        gl.bufferData(gl.ARRAY_BUFFER, buffer, gl.STATIC_DRAW);
        this.num_vertices = buffer.length / 9;
    }
    draw(){
        gl.bindBuffer(gl.ARRAY_BUFFER, this.vbo);
        gl.drawArrays(gl.TRIANGLES, 0, this.num_vertices);
    }
    destroy(){
        gl.deleteBuffer(this.vbo);
    }
}

程序类:

class GLProgram{
    constructor(vertShader, fragShader){
        this.prog = gl.createProgram();
        gl.attachShader(this.prog, vertShader.id);
        gl.attachShader(this.prog, fragShader.id);
        gl.bindAttribLocation(this.prog, 0, "position");
        gl.bindAttribLocation(this.prog, 1, "normal");
        gl.bindAttribLocation(this.prog, 2, "color");
        gl.linkProgram(this.prog);
        var log = gl.getProgramInfoLog(this.prog);
        if(log.length){
            console.log();
        }
        gl.detachShader(this.prog, vertShader.id);
        vertShader.destroy();
        gl.detachShader(this.prog, fragShader.id);
        fragShader.destroy();
    }
    bind(){
        gl.useProgram(this.prog);
    }
    destroy(){
        gl.deleteProgram(this.prog);
    }
}

顶点着色器:

attribute vec3 position; 
attribute vec3 normal; 
attribute vec3 color; 

varying vec3 vColor;

void main(){    
    gl_Position = vec4(position, 1.0);  
    vColor = color; 
}   

片段着色器:

precision mediump float;    

varying vec3 vColor;    

void main(){    
    gl_FragColor = vec4(vColor, 1.0);   
}

非常感谢您在解决此问题时提供的任何帮助或提示。

【问题讨论】:

    标签: javascript webgl


    【解决方案1】:

    在你的draw.js文件的底部,你销毁mesh和prog:

    mesh.destroy();
    prog.destroy();
    

    在 JavaScript 中,window.requestAnimationFrame(onFrame); 实际上会在调用 destroy 方法之后调用 onFrame。因此,当 onFrame 被执行时,mesh 和 prog 都不存在。我建议您阅读更多关于 requestAnimationFrame 的内容,以便您可以稍后销毁它们(即在您的动画循环停止运行之后)。

    您可以通过删除那些 destroy 行来验证行为,它会呈现良好。

    【讨论】:

    • 这就是解决方案。我可以发誓我曾尝试删除这两条线。我肯定会研究该函数背后的更多细节以及 js 的一般异步特性。非常感谢。这是三角形:i.imgur.com/GVOHe2x.png
    猜你喜欢
    • 2014-08-15
    • 1970-01-01
    • 1970-01-01
    • 2014-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-08
    相关资源
    最近更新 更多