Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

3d - Webgl/Opengl drawElements and uniqueness of vertices

Consider the typical draw call

webgl.bindBuffer(webgl.ELEMENT_ARRAY_BUFFER, faces); 
webgl.drawElements(webgl.TRIANGLES, nfaces * 3, webgl.UNSIGNED_SHORT, 0);

Now, let's say for simplicity that I am rendering a cube. If the component of each Vertex are its position px, py, pz and its normal nx, ny, nz, then I have 8 unique vertices.

I can then keep a buffer with these 8 vertices, keep another “faces” buffer with indices to which vertex each face uses, and there you go, shared vertices through an indexed drawElements call. Fine.

When we introduce UV coordinates, though, I don't have 8 unique vertices anymore. I end up with each face requiring exactly three unique vertices.

The faces buffer, then, might as well be [(0,1,2), (3,4,5), (6,7,8) … and so on].

So, as vertices carry more information, they become impossible to share. Yet, I need an index buffer in order to be able to call drawElements. An index buffer whose content ends up being nothing but a sequence of 0 … nverts-1 integers.

Am I missing something?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

A cube with positions and normals needs 24 vertices minimum. The shared position on each corner of the cube has a different normal on each face of the cube

As for indices or not, you don't need to have an index buffer. You can call gl.drawArrays instead of gl.drawElements

As for sharing vertices, cubes have the issue that each face of the cube faces in a different direction and so needs different normals and the texture coordinates are often not shared either for a vertex with the same position. But, other shapes (a sphere, a torus, a human, an animal, a cup, a plate, a bottle, a mountain, a plant) have lots of opportunities to use shared vertices.

Also note that there are other ways of organizing vertices


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...