using WebGL index buffers for drawing meshes

牧云@^-^@ 提交于 2020-01-01 12:16:24

问题


3 index buffers asks a more difficult question, but I feel like their main problem boils down to mine: is there a way to use index buffers to visit the same vertex multiple times in WebGL rather than duplicating the vertices? All I was able to find is using index buffers to associate textures, normals, etc. to vertices in a model. I wasn't able to find a way to use an index buffer to tell drawArrays the order in which to visit the vertices in a position array.


回答1:


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.




回答2:


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).



来源:https://stackoverflow.com/questions/6077002/using-webgl-index-buffers-for-drawing-meshes

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