使用 WebGL 索引缓冲区绘制网格

发布于 2024-11-08 14:10:30 字数 235 浏览 4 评论 0原文

3 个索引缓冲区提出了一个更困难的问题,但我觉得他们的主要问题归结为我的:是有没有办法在 WebGL 中使用索引缓冲区多次访问同一顶点而不是复制顶点? 我所能找到的就是使用索引缓冲区将纹理、法线等与模型中的顶点相关联。我无法找到一种方法来使用索引缓冲区来告诉 drawArrays 访问位置数组中顶点的顺序。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只有一腔孤勇 2024-11-15 14:10:30

是的,使用 gl.drawElements 并将带有顶点索引的缓冲区上传到 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);

请参阅 https://developer.mozilla.org/en/WebGL/Creating_3D_objects_using_WebGL 了解完整信息例子。

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.

相对绾红妆 2024-11-15 14:10:30

当您使用索引时,只需将索引加倍即可。然而,这对我来说没有意义,因为使用相同的全局状态多次处理相同的顶点数据,每次重复的结果都是相同的

我怀疑您真正想做的是对具有不同制服的相同顶点数据进行多次绘制调用

  1. 在启动时创建顶点数据/索引(如果需要)
  2. 每个帧,设置全局状态 A,调用 DrawElements/Array,设置状态 B,调用 DrawElements/Array
  3. 时不时地更改顶点数据(例如,如果对象正在移动)

..只要您不更改顶点数据,就只有少量上传到 GPU(需要低带宽)。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文