WebGL:绘制点失败:glDrawArrays:"attempt to access out of range vertices in attribute 1"

WebGL: Failing at drawing points: glDrawArrays: "attempt to access out of range vertices in attribute 1"

我正在尝试使用 WebGL 显示点(实际上是点云)。在过去的 2 天里,我学习了 this 教程,并且仍然有很多基于它的代码。

我正在尝试加载一个 .ply 文件,然后将每个坐标显示为彩色点,但在尝试绘制时遇到此错误:

[.WebGLRenderingContext-0x111ff17bc300]GL ERROR :GL_INVALID_OPERATION : glDrawArrays: attempt to access out of range vertices in attribute 1

我当然做了一些研究,但一般的答案是缓冲数组的长度会不同,我已经确定不是这种情况。不过,这很可能是一个类似的菜鸟错误:

function getShader(gl, id) {
        var shaderScript = document.getElementById(id);
        if (!shaderScript) {
            return null;
        }
        var str = "";
        var k = shaderScript.firstChild;
        while (k) {
            if (k.nodeType == 3) {
                str += k.textContent;
            }
            k = k.nextSibling;
        }
        var shader;
        if (shaderScript.type == "x-shader/x-fragment") {
            shader = gl.createShader(gl.FRAGMENT_SHADER);
        } else if (shaderScript.type == "x-shader/x-vertex") {
            shader = gl.createShader(gl.VERTEX_SHADER);
        } else {
            return null;
        }
        gl.shaderSource(shader, str);
        gl.compileShader(shader);
        if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
            alert(gl.getShaderInfoLog(shader));
            return null;
        }
        return shader;
    }
    var shaderProgram;
    function initShaders() {
        var fragmentShader = getShader(gl, "shader-fs");
        var vertexShader = getShader(gl, "shader-vs");

        shaderProgram = gl.createProgram();
        gl.attachShader(shaderProgram, vertexShader);
        gl.attachShader(shaderProgram, fragmentShader);
        gl.linkProgram(shaderProgram);

        if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) {
            alert("Could not initialise shaders");
        }

        gl.useProgram(shaderProgram);

        shaderProgram.vertexPositionAttribute = gl.getAttribLocation(shaderProgram, "aVertexPosition");
        gl.enableVertexAttribArray(shaderProgram.vertexPositionAttribute);

        shaderProgram.vertexColorAttribute = gl.getAttribLocation(shaderProgram, "aVertexColor");
        gl.enableVertexAttribArray(shaderProgram.vertexColorAttribute);

        shaderProgram.pMatrixUniform = gl.getUniformLocation(shaderProgram, "uPMatrix");
        shaderProgram.mvMatrixUniform = gl.getUniformLocation(shaderProgram, "uMVMatrix");
    }

    var mvMatrix = mat4.create();
    var mvMatrixStack = [];
    var pMatrix = mat4.create();

    function mvPushMatrix() {
        var copy = mat4.create();
        mat4.set(mvMatrix, copy);
        mvMatrixStack.push(copy);
    }

    function mvPopMatrix() {
        if (mvMatrixStack.length == 0) {
            throw "Invalid popMatrix!";
        }
        mvMatrix = mvMatrixStack.pop();
    }

    function setMatrixUniforms() {
        gl.uniformMatrix4fv(shaderProgram.pMatrixUniform, false, pMatrix);
        gl.uniformMatrix4fv(shaderProgram.mvMatrixUniform, false, mvMatrix);
    }

    function degToRad(degrees) {
        return degrees * Math.PI / 180;
    }


    var pointcloudVertexPositionBuffer;
    var pointcloudColourBuffer;
    var pointcloudIndexBuffer;
    function handlePoints(pointCoords, pointColours, indices) {
        pointcloudVertexPositionBuffer = gl.createBuffer();
        gl.bindBuffer(gl.ARRAY_BUFFER, pointcloudVertexPositionBuffer);
        gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(pointCoords), gl.STATIC_DRAW);
        pointcloudVertexPositionBuffer.itemSize = 3;
        pointcloudVertexPositionBuffer.numItems = pointCoords.length / 3;

        pointcloudColourBuffer = gl.createBuffer();
        gl.bindBuffer(gl.ARRAY_BUFFER, pointcloudColourBuffer);
        gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(pointColours), gl.STATIC_DRAW);
        pointcloudColourBuffer.itemSize = 3;
        pointcloudColourBuffer.numItems = pointColours.length / 3;

        pointcloudIndexBuffer = gl.createBuffer();
        gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, pointcloudIndexBuffer);
        gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(indices), gl.STATIC_DRAW);
        pointcloudIndexBuffer.itemSize = 1;
        pointcloudIndexBuffer.numItems = indices.length;
    }

    var coordList = [];
    var colList = [];
    var indexList = [];
    function loadPLY(){
        jQuery.get('some_ascii.ply', function(data) {
            var lines = data.split('\n');
            var coords = false;

            for(var line = 0; line < lines.length; line++){
                if (coords) {
                    var splitString = lines[line].split(" ");
                    coordList.push(parseFloat(splitString[0]), parseFloat(splitString[1]), parseFloat(splitString[2]));
                    colList.push(parseFloat(splitString[3]), parseFloat(splitString[4]), parseFloat(splitString[5]));
                    indexList.push(parseFloat(line));
                }
                else{
                    if (lines[line] == "end_header"){
                        console.log("end header");
                        coords = true;
                    }
                }
            }
            handlePoints(coordList, colList, indexList);
        });
    }

    function drawScene(){
        gl.viewport(0, 0, gl.viewportWidth, gl.viewportHeight);
        gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);

        mat4.perspective(45, gl.viewportWidth / gl.viewportHeight, 0.1, 100.0, pMatrix);

        mat4.identity(mvMatrix);

        //mat4.translate(mvMatrix, [0, 0, -6]);

        //gl.uniform1i(shaderProgram.samplerUniform, 0);

        gl.bindBuffer(gl.ARRAY_BUFFER, pointcloudColourBuffer);
        gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, pointcloudColourBuffer.itemSize, gl.FLOAT, false, 0, 0);

        gl.bindBuffer(gl.ARRAY_BUFFER, pointcloudVertexPositionBuffer);
        gl.vertexAttribPointer(shaderProgram.textureCoordAttribute, pointcloudVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0);

        setMatrixUniforms();
        //gl.drawElements(gl.TRIANGLES, pointcloudIndexBuffer.numItems, gl.UNSIGNED_SHORT, 0);
        //console.log("1", pointcloudIndexBuffer.numItems);
        //console.log("2", pointcloudColourBuffer.numItems);
        //console.log("3", pointcloudVertexPositionBuffer.numItems);
        gl.drawArrays(gl.POINTS, 0, pointcloudIndexBuffer.numItems, gl.UNSIGNED_SHORT, 0);
    }


    function tick() {
        requestAnimFrame(tick);
        drawScene();
    }

    function webGLStart(){
        var canvas = document.getElementById("canvas");
        initGL(canvas);
        initShaders();
        loadPLY();

        gl.clearColor(0.0, 0.0, 0.0, 1.0);
        gl.enable(gl.DEPTH_TEST);

        tick();
    }

我再次查看了您的代码,发现了几件事:

如果要绘制 gl.POINT,则需要在顶点着色器中包含 gl_PointSize = 8.0;,否则不会绘制点。

您的颜色数据不正确。负色没有意义。

你的 drawArrays 调用不正确,应该是 gl.drawArrays(gl.POINTS, 0, pointcloudIndexBuffer.numItems);

我禁用了透视图,因为如果没有相机很难判断事物是否正确绘制,但在视图之外。

你提到的 Cannot read property 'itemSize' of undefined 是因为它试图在你的 .ply 数据被解析之前绘制场景。要修复,仅在解析完成后才开始绘图。

所以这是我编辑过的版本,它至少绘制了(你必须更改 src):

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
</head>
<input type="file" name="file" id="file">
<script type="text/javascript" src="aaa/glMatrix-0.9.5.min.js"></script>
<script type="text/javascript" src="aaa/webgl-utils.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>

<script id="shader-fs" type="x-shader/x-fragment">
    precision mediump float;

    varying vec4 vColor;

    void main(void) {
        //gl_FragColor = vColor;
        gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
    }
</script>

<script id="shader-vs" type="x-shader/x-vertex">
    attribute vec3 aVertexPosition;
    attribute vec4 aVertexColor;

    uniform mat4 uMVMatrix;
    uniform mat4 uPMatrix;

    varying vec4 vColor;

    void main(void) {
        gl_PointSize = 8.0;
        //gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);
        gl_Position = vec4(aVertexPosition, 1.0);
        vColor = aVertexColor;
    }

</script>


<script type="text/javascript">
    var gl;
    function initGL(canvas) {
        try {
            gl = canvas.getContext("experimental-webgl");
            gl.viewportWidth = canvas.width;
            gl.viewportHeight = canvas.height;
        } catch (e) {
        }
        if (!gl) {
            alert("Could not initialise WebGL, sorry :-(");
        }
    }
    function getShader(gl, id) {
        var shaderScript = document.getElementById(id);
        if (!shaderScript) {
            return null;
        }
        var str = "";
        var k = shaderScript.firstChild;
        while (k) {
            if (k.nodeType == 3) {
                str += k.textContent;
            }
            k = k.nextSibling;
        }
        var shader;
        if (shaderScript.type == "x-shader/x-fragment") {
            shader = gl.createShader(gl.FRAGMENT_SHADER);
        } else if (shaderScript.type == "x-shader/x-vertex") {
            shader = gl.createShader(gl.VERTEX_SHADER);
        } else {
            return null;
        }
        gl.shaderSource(shader, str);
        gl.compileShader(shader);
        if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
            alert(gl.getShaderInfoLog(shader));
            return null;
        }
        return shader;
    }
    var shaderProgram;
    function initShaders() {
        var fragmentShader = getShader(gl, "shader-fs");
        var vertexShader = getShader(gl, "shader-vs");

        shaderProgram = gl.createProgram();
        gl.attachShader(shaderProgram, vertexShader);
        gl.attachShader(shaderProgram, fragmentShader);
        gl.linkProgram(shaderProgram);

        if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) {
            alert("Could not initialise shaders");
        }

        gl.useProgram(shaderProgram);

        shaderProgram.vertexPositionAttribute = gl.getAttribLocation(shaderProgram, "aVertexPosition");
        gl.enableVertexAttribArray(shaderProgram.vertexPositionAttribute);

        shaderProgram.vertexColorAttribute = gl.getAttribLocation(shaderProgram, "aVertexColor");
        gl.enableVertexAttribArray(shaderProgram.vertexColorAttribute);

        shaderProgram.pMatrixUniform = gl.getUniformLocation(shaderProgram, "uPMatrix");
        shaderProgram.mvMatrixUniform = gl.getUniformLocation(shaderProgram, "uMVMatrix");

        console.log(shaderProgram);
    }

    var mvMatrix = mat4.create();
    var mvMatrixStack = [];
    var pMatrix = mat4.create();

    function mvPushMatrix() {
        var copy = mat4.create();
        mat4.set(mvMatrix, copy);
        mvMatrixStack.push(copy);
    }

    function mvPopMatrix() {
        if (mvMatrixStack.length == 0) {
            throw "Invalid popMatrix!";
        }
        mvMatrix = mvMatrixStack.pop();
    }

    function setMatrixUniforms() {
        gl.uniformMatrix4fv(shaderProgram.pMatrixUniform, false, pMatrix);
        gl.uniformMatrix4fv(shaderProgram.mvMatrixUniform, false, mvMatrix);
    }

    function degToRad(degrees) {
        return degrees * Math.PI / 180;
    }


    var pointcloudVertexPositionBuffer;
    var pointcloudColourBuffer;
    var pointcloudIndexBuffer;
    function handlePoints(pointCoords, pointColours, indices) {
        pointcloudVertexPositionBuffer = gl.createBuffer();
        gl.bindBuffer(gl.ARRAY_BUFFER, pointcloudVertexPositionBuffer);
        gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(pointCoords), gl.STATIC_DRAW);
        pointcloudVertexPositionBuffer.itemSize = 3;
        pointcloudVertexPositionBuffer.numItems = pointCoords.length / 3;

        pointcloudColourBuffer = gl.createBuffer();
        gl.bindBuffer(gl.ARRAY_BUFFER, pointcloudColourBuffer);
        gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(pointColours), gl.STATIC_DRAW);
        pointcloudColourBuffer.itemSize = 3;
        pointcloudColourBuffer.numItems = pointColours.length / 3;

        pointcloudIndexBuffer = gl.createBuffer();
        gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, pointcloudIndexBuffer);
        gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(indices), gl.STATIC_DRAW);
        pointcloudIndexBuffer.itemSize = 1;
        pointcloudIndexBuffer.numItems = indices.length;
    }

    var coordList = [];
    var colList = [];
    var indexList = [];
    function loadPLY(){
        jQuery.get('aaa/untitled.ply', function(data) {
            var lines = data.split('\n');
            var coords = false;

            for(var line = 0; line < lines.length; line++){
                if (coords) {
                    var splitString = lines[line].split(" ");
                    if (splitString.length === 6) {
                        coordList.push(parseFloat(splitString[0]), parseFloat(splitString[1]), parseFloat(splitString[2]));
                        colList.push(parseFloat(splitString[3]), parseFloat(splitString[4]), parseFloat(splitString[5]));
                        indexList.push(parseFloat(line));
                    }
                    else{
                        coords = false;
                    }
                }
                else{
                    if (lines[line].indexOf("end_header")===0){
                        console.log("end header");
                        coords = true;
                    }
                }
            }
            handlePoints(coordList, colList, indexList);
            tick();
        });

    }

    function drawScene(){
        gl.viewport(0, 0, gl.viewportWidth, gl.viewportHeight);
        gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);

        mat4.perspective(45, gl.viewportWidth / gl.viewportHeight, 0.1, 100.0, pMatrix);

        mat4.identity(mvMatrix);

        //mat4.translate(mvMatrix, [0, 0, -6]);

        //gl.uniform1i(shaderProgram.samplerUniform, 0);

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

        gl.bindBuffer(gl.ARRAY_BUFFER, pointcloudVertexPositionBuffer);
        gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, pointcloudVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0);

        setMatrixUniforms();
        //gl.drawElements(gl.TRIANGLES, pointcloudIndexBuffer.numItems, gl.UNSIGNED_SHORT, 0);
        //console.log("1", pointcloudIndexBuffer.numItems);
        //console.log("2", pointcloudColourBuffer.numItems);
        //console.log("3", pointcloudVertexPositionBuffer.numItems);
        gl.drawArrays(gl.POINTS, 0, pointcloudIndexBuffer.numItems);
    }


    function tick() {
        requestAnimFrame(tick);
        drawScene();
    }

    function webGLStart(){
        var canvas = document.getElementById("canvas");
        initGL(canvas);
        initShaders();
        loadPLY();

        gl.clearColor(0.0, 0.0, 0.0, 1.0);
        gl.enable(gl.DEPTH_TEST);

    }


</script>
</head>

<style type="text/css">
    #loadingtext {
        position:absolute;
        top:250px;
        left:150px;
        font-size:2em;
        color: white;
    }
</style>

<body onload="webGLStart();">
<canvas id="canvas" style="border: none;" width="500" height="500"></canvas>
</body>

</html>