using WebGL index buffers for drawing meshes

无人久伴 提交于 2019-12-04 09:52:44

Yes, use gl.drawElements and upload the buffer with your vertex indices to gl.ELEMENT_ARRAY_BUFFER.

... upload vertex data to buffers, vertexAttribPointer them.

gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indicesBuffer);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, indicesData, gl.STATIC_DRAW);
gl.drawElements(gl.TRIANGLES, indicesData.length/3, gl.UNSIGNED_SHORT, 0);

See https://developer.mozilla.org/en/WebGL/Creating_3D_objects_using_WebGL for a full example.

When you use indices, you can just double the indexes. However, this makes no sense to me as processing the same vertex data multiple times with the same global state, results would be the same for each duplicate.

What I suspect you really want to do is to make multiple Draw calls on the same vertex data with different uniforms.

  1. Create vertex data/indices (if wanted) at startup
  2. Each Frame, set global state A, call DrawElements/Array, set state B, call DrawElements/Array
  3. From time to time, change vertex data (e.g. if objects are moving)..

As long as you don't change vertex data, there is just minor uploads to the gpu (needing low bandwidth).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!