0

I've got a single triangle rendering using gl.TRIANGLE_STRIP, but when I try to change it to gl.TRIANGLE, the faces do not render. It appears like the vertices are rendering as really tiny dots, but the faces are empty.

My understanding is that the vertex format for a TRIANGLE vs TRIANGLE_STRIP should be identical for a single triangle.

// vertex setup
const buffer = gl.createBuffer();
const vertices = new Float32Array([
    1,  -1, -1,     1,   1.3, 1.5,  1,
    1,  -1,  1,     1.3, 1,   1.5,  1,
    0,   1,  0,     1,   1,   1.75, 1
]);

gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW);

const length = vertices.length / 7;
const mode = gl.TRIANGLE_STRIP;

return {buffer, length, mode};

That works as expected with the following render code:

// render frame
gl.bindBuffer(gl.ARRAY_BUFFER, shape.buffer);
gl.vertexAttribPointer(attribs.position, 3, gl.FLOAT, false, 28, 0);
gl.vertexAttribPointer(attribs.color, 4, gl.FLOAT, false, 28, 12);
gl.enableVertexAttribArray(attribs.position);
gl.enableVertexAttribArray(attribs.color);
gl.useProgram(programs.colored);
gl.uniformMatrix4fv(uniforms.projection, false, projection);
gl.uniformMatrix4fv(uniforms.modelView, false, modelView);
gl.drawArrays(shape.mode, 0, shape.length);

But if I change the mode to gl.TRIANGLE, no faces appear, with the vertices just barely visible as tiny dots.

What am I misunderstanding here?

rich remer
  • 3,407
  • 2
  • 34
  • 47
  • 3
    `gl.TRIANGLE` isn't a valid `mode` type for `gl.drawArrays`, but `gl.TRIANGLES` is (see [here](https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/drawArrays). Hopefully, it's that simple to fix your problem. You could also check the WebGL error state by calling `gl.getError()` before your draw call (to clear the error flag if it's set), and then again after and seeing if you get an error back (see [here](https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/getError) for details) – radical7 Jul 17 '19 at 00:49
  • I knew it was going to be a dumb mistake. Thanks! – rich remer Jul 17 '19 at 01:04
  • 2
    You can use the techniques listed on [this page](https://www.khronos.org/webgl/wiki/Debugging) to find those kinds of errors. – gman Jul 17 '19 at 01:41

0 Answers0