使用WebGL索引缓冲区绘制网格物体 [英] using WebGL index buffers for drawing meshes

查看:140
本文介绍了使用WebGL索引缓冲区绘制网格物体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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.

推荐答案

是的,使用gl.drawElements并将带顶点索引的缓冲区上传到gl.ELEMENT_ARRAY_BUFFER。

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

参见 https://developer.mozilla.org/en/WebGL/Creating_3D_objects_using_WebGL 获取完整示例。

这篇关于使用WebGL索引缓冲区绘制网格物体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆