Stephen Standridge
Stephen Standridge

Reputation: 74

webgl drawElements:attribs not setup correctly (repeated n times)

So, I've begun a rather instantaneous trip into the world of visual, 3-d programming. I'm currently heavily invested in webgl with a rather strong background in JavaScript and most web-oriented languages but this is my first graphics language.

While trying to draw my first rather simple shape, I've run into an error I can't seem to locate a solution for. It reads in chrome as a:

WebGL: INVALID_OPERATION: drawElements:attribs not setup correctly (repeated n times)

where n is a number that varies seemingly randomly. The code in question is here:

var tessVertexPositionBuffer;
var tessVertexColorBuffer;
var tessVertexIndexBuffer;
function initBuffers () {
    tessVertexPositionBuffer = gl.createBuffer();
    gl.bindBuffer(gl.ARRAY_BUFFER, tessVertexPositionBuffer);
    var vertices = [
    //innerfront
        -1.0, 1.0, 1.0,
        1.0, 1.0, 1.0,
        1.0, -1.0, 1.0,
        -1.0, -1.0, 1.0,
    //innerleft
        -1.0, 1.0, 1.0,
        -1.0, -1.0, 1.0,
        -1.0, 1.0, -1.0,
        -1.0, -1.0, -1.0,
    //innerback
        -1.0, 1.0, -1.0,
        1.0, 1.0, -1.0,
        -1.0, -1.0, -1.0,
        1.0, -1.0, -1.0,
    //innerright
        1.0, 1.0, 1.0,
        1.0, -1.0, 1.0,
        1.0, 1.0, -1.0,
        1.0, -1.0, -1.0,
                                            
    //topfront
        -1.0, 1.0, 1.0,
        1.0, 1.0, 1.0,
        -2.0, 2.0, 2.0,
        2.0, 2.0, 2.0,
    //topleft
        -1.0, 1.0, 1.0,
        -2.0, 2.0, 2.0,
        -1.0, 1.0, -1.0,
        -2.0, 2.0, -2.0,
    //topback
        -1.0, 1.0, -1.0,
        1.0, 1.0, -1.0,
        -2.0, 2.0, -2.0,
        2.0, 2.0, -2.0,         
    //topright
        1.0, 1.0, 1.0,
        2.0, 2.0, 2.0,
        1.0, 1.0, -1.0,
        2.0, 2.0, -2.0,
        
                                
    //outerfront
        -2.0, 2.0, 2.0,
        2.0, 2.0, 2.0,
        -2.0, -2.0, 2.0,
        2.0, -2.0, 2.0,
    //outerleft
        -2.0, 2.0, 2.0,
        -2.0, -2.0, 2.0,
        -2.0, 2.0, -2.0,
        -2.0, -2.0, -2.0,           
    //outerback
        -2.0, 2.0, -2.0,
        2.0, 2.0, -2.0,
        -2.0, -2.0, -2.0,
        2.0, -2.0, -2.0,
    //outerright
        2.0, 2.0, 2.0,
        2.0, -2.0, 2.0,
        2.0, 2.0, -2.0,
        2.0, -2.0, -2.0,            
            
    //bottomfront       
        2.0, 2.0, 2.0,
        -2.0, 2.0, 2.0,
        -2.0, -2.0, 2.0,
        2.0, -2.0, 2.0,
    //bottomleft
        -1.0, -1.0, 1.0,
        -2.0, -2.0, 2.0,
        -1.0, -1.0, -1.0,
        -2.0, -2.0, -2.0,
    //bottomback
        -1.0, -1.0, -1.0,
        1.0, -1.0, -1.0,
        -2.0, -2.0, -2.0,
        2.0, -2.0, -2.0,
    //bottomright
        1.0, -1.0, 1.0,
        2.0, -2.0, 2.0,
        1.0, -1.0, -1.0,
        2.0, -2.0, -2.0
    ];
    gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
    tessVertexPositionBuffer.itemSize = 3;
    tessVertexPositionBuffer.numItems = 64;
    
    tessVertexColorBuffer = gl.createBuffer();
    gl.bindBuffer(gl.ARRAY_BUFFER, tessVertexColorBuffer);
    var colors = [
        [0.7, 0.7, 0.7, 1.0], //all inner sides
        [0.7, 0.7, 0.7, 1.0], 
        [0.7, 0.7, 0.7, 1.0], 
        [0.7, 0.7, 0.7, 1.0], 
        
        [0.7, 0.0, 0.7, 1.0], //all top sides
        [0.7, 0.0, 0.7, 1.0], 
        [0.7, 0.0, 0.7, 1.0], 
        [0.7, 0.0, 0.7, 1.0], 
        
        [0.7, 0.7, 0.0, 1.0], //all outer sides
        [0.7, 0.7, 0.0, 1.0], 
        [0.7, 0.7, 0.0, 1.0], 
        [0.7, 0.7, 0.0, 1.0], 
        
        [0.0, 0.7, 0.7, 1.0], //all bottom sides
        [0.0, 0.7, 0.7, 1.0],
        [0.0, 0.7, 0.7, 1.0],
        [0.0, 0.7, 0.7, 1.0],
    ];
    var unpackedColors = [];
    for (var i in colors) {
        var color = colors[i];
        for (var j=0; j< 4; j++) {
            unpackedColors = unpackedColors.concat(color);
        }   
    }
    
    gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(unpackedColors), gl.STATIC_DRAW);
    tessVertexColorBuffer.itemSize = 4;
    tessVertexColorBuffer.numItems = 64;


    tessVertexIndexBuffer = gl.createBuffer();
    gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, tessVertexIndexBuffer);
    var tessVertexIndices = [
        0, 1, 2,  0, 2, 3,
        4, 5, 6,  5, 6, 7,
        8, 9, 10,  9, 10, 11,
        12, 13, 14,  13, 14, 15,
        16, 17, 18,  17, 18, 19,
        20, 21, 22,  21, 22, 23,
        24, 25, 26,  25, 26, 27,
        28, 29, 30,  29, 30, 31,
        32, 33, 34,  33, 34, 35,
        36, 37, 38,  37, 38, 39,
        40, 41, 42,  41, 42, 43,
        44, 45, 46,  45, 46, 47,
        48, 49, 50,  48, 50, 51,
        52, 53, 54,  53, 54, 55,
        56, 57, 58,  57, 58, 59,
        60, 61, 62,  61, 62, 63
    ];  
    gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(tessVertexIndices), gl.STATIC_DRAW);
    tessVertexIndexBuffer.itemSize = 1;
    tessVertexIndexBuffer.numItems = 96;
}

and the actual drawing of the buffers is here:

    gl.bindBuffer(gl.ARRAY_BUFFER, tessVertexPositionBuffer);
    gl.vertexAttribPointer(shaderProgram.vetexPositionAttribute, tessVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0);
    
    gl.bindBuffer(gl.ARRAY_BUFFER, tessVertexColorBuffer);
    gl.vertexAttribPointer(shaderProgram.vertexColorAttribute, tessVertexColorBuffer.itemSize, gl.FLOAT, false, 0, 0);
    
    gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, tessVertexIndexBuffer);
    setMatrixUniforms();
    gl.drawElements(gl.TRIANGLES, tessVertexIndexBuffer.numItems, gl.UNSIGNED_SHORT, 0);

Now, this is relatively verbatim from learningwebgl, I'm really just trying to draw a simple shape. I'm relatively certain that my problem lies in my buffer types because I honestly don't uderstand much about them (and most literature on webGl I've found is either novice with a general understanding of the language or of the HYPERSUPERPRO variety).

I've checked over the actual vertex positions/colors/indices multiple times and unless I've just become familiar with the code enough that I'm blind to the simple errors, I can't find an error there.

Upvotes: 4

Views: 2256

Answers (1)

Anton
Anton

Reputation: 12435

Looks like your problem is just a small typo. The second line of your "actual drawing of buffers" code should be he following:

gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, tessVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0);

You were missing the first r in vertexPositionAttribute on that line.

I modified the lesson 4 LearningWebGL tutorial to use your code (with the fixed typo) and I have a link to it here.

If you notice there is a blue triangle and a yellow triangle being drawn at the exact same position in space which causes some flickering. I'm not sure if that was intentional.

Upvotes: 3

Related Questions