简体   繁体   English

多维数据集中的WebGL三角形

[英]WebGL triangles in a cube

In this tutorial author displays a cube by defining its 6 faces (6*4 vertices) and then telling webgl about triangles in each face. 本教程中,作者通过定义其6个面(6 * 4个顶点)然后告诉webgl关于每个面中的三角形来显示一个立方体。

Isn't this wasteful? 这不是浪费吗? Wouldn't it be better to define just 8 vertices and tell webgl how to connect them to get triangles? 仅仅定义8个顶点并告诉webgl如何将它们连接起来获取三角形不是更好吗? Are colors shared by multiple vertices a problem? 多个顶点共享的颜色是否有问题?

To make my concern evident: if the author defines triangles with indices array, why does he need so many vertices? 让我的关注明显:如果作者用indices数组定义三角形,为什么他需要这么多顶点? He could specify all triangles with just 8 vertices in the vertex array. 他可以指定顶点数组中只有8个顶点的所有三角形。

Author of the example here. 这里的例子的作者。 The issue is, as you suspected, to do with the colouring of the cube. 正如您所怀疑的那样,问题在于立方体的着色。

The way to understand this kind of code most easily is to think of WebGL's "vertices" as being not just simple points in space, but instead bundles of attributes. 最容易理解这种代码的方法是将WebGL的“顶点”视为空间中的简单点,而不是属性束。 A particular vertex might be be the bundle <(1, -1, 1), red>. 特定顶点可能是束<(1,-1,1),红色>。 A different vertex that was at the same point in space but had a different colour (eg. <(1, -1, 1), green>) would be a different vertex entirely as far as WebGL is concerned. 就WebGL而言,位于空间中相同点但具有不同颜色(例如<(1,-1,1),绿色>)的不同顶点将完全是不同的顶点。

So while a cube has only 8 vertices in the mathematical sense of points in space, if you want to have a different colour per face, each of those points must be occupied by three different vertices, one per colour -- which makes 8x3=24 vertices in the WebGL sense. 因此,当一个立方体在空间中的点数学意义上只有8个顶点时,如果你想在每个面上有不同的颜色,那么每个点必须由三个不同的顶点占据,每个颜色一个 - 这使得8x3 = 24 WebGL意义上的顶点。

It's not hugely efficient in terms of memory, but memory's cheap compared to the CPU power that a more normalised representation would require for efficient processing. 它在内存方面并不是非常有效,但与CPU功率相比,内存便宜,而更加规范化的表示需要高效处理。

Hope that clarifies things. 希望澄清事情。

You can use Vertex Buffer Objects (VBO). 您可以使用顶点缓冲区对象(VBO)。 See this example. 这个例子。 They create a list of Vertices and and a list of Indexes "pointing" to the vertices (no duplication of vertices). 它们创建了一个顶点列表和一个“指向”顶点的索引列表(没有重复的顶点)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM