0

When drawArrays is called with an offset, (the "first" argument being non zero), does the first gl_VertexID still start at 0, or does it start at the offset value?

gman
  • 100,619
  • 31
  • 269
  • 393
Markus Fjellheim
  • 335
  • 2
  • 11

1 Answers1

1

update

This appears to be a bug in ANGLE on Windows. Filed a bug

https://github.com/KhronosGroup/WebGL/issues/2770


Let's try it

[...document.querySelectorAll('canvas')].forEach((canvas, ndx) => {
  const vs = `#version 300 es
  void main() {
    gl_Position = vec4(float(gl_VertexID) / 10., 0, 0, 1);
    gl_PointSize = 10.0;
  }`;
  const fs = `#version 300 es
  precision mediump float;
  out vec4 outColor;
  void main() {
    outColor = vec4(1, 0, 0, 1);
  }`;
  const gl = canvas.getContext('webgl2');
  if (!gl) {
    return alert('need webgl2');
  }
  const prg = twgl.createProgram(gl, [vs, fs]);
  gl.useProgram(prg);
  gl.drawArrays(gl.POINTS, ndx * 5, 5);
});
canvas {border: 1px solid black;}
<script src="https://twgljs.org/dist/4.x/twgl.min.js"></script>
<canvas></canvas>
<canvas></canvas>

Looks like the answer is it starts at the offset value.

Community
  • 1
  • 1
gman
  • 100,619
  • 31
  • 269
  • 393
  • To me it looks like they start at the center? X-coordinate is 0, so they do not start at the offset value? – Markus Fjellheim Jan 19 '19 at 12:25
  • There's 2 drawings. One starting at offset = 0, one at offset = 5. The one at offset 5 does not start at the center for me. Does it for you? – gman Jan 19 '19 at 16:29
  • This appears to be a bug: https://github.com/KhronosGroup/WebGL/issues/2770 – gman Jan 19 '19 at 20:11
  • Yes, both start in the middle. If it is a bug, do you interpret the documentation as the index starting from 0 or the offset value?https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/gl_VertexID.xhtml The link is for opengl, could not find any description for webgl2 – Markus Fjellheim Jan 19 '19 at 21:23
  • The WebGL2 spec is [here](https://www.khronos.org/registry/webgl/specs/latest/2.0/). It says refer to the OpenGL ES 3.0 spec which is [here](https://www.khronos.org/registry/OpenGL/specs/es/3.0/es_spec_3.0.pdf). FYI the page you linked to is not the opengl spec it's the opengl man pages. I know this will sound still but the man pages are not official, the spec is. For OpenGL that's [here](https://www.khronos.org/registry/OpenGL/specs/gl/glspec46.core.pdf). The man pages can be out of date. – gman Jan 20 '19 at 02:45
  • It looks to me the correct way is index = offset. The ES 3.0 spec it's ambiguous but the ES 3.2 spec clears it up: "The index of any element transferred to the GL ,,, is referred to as its vertex ID, and may be read by a vertex shader as gl_VertexID. The vertex ID of the ith element transferred is first + i" – gman Jan 20 '19 at 02:54