javascript - WebGL triangles in a cube - Stack Overflow

In this tutorial author displays a cube by defining its 6 faces (6*4 vertices) and then telling webgl a

In this tutorial author displays a cube by defining its 6 faces (6*4 vertices) and then telling webgl about triangles in each face.

Isn't this wasteful? Wouldn't it be better to define just 8 vertices and tell webgl how to connect them to get triangles? 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? He could specify all triangles with just 8 vertices in the vertex array.

In this tutorial author displays a cube by defining its 6 faces (6*4 vertices) and then telling webgl about triangles in each face.

Isn't this wasteful? Wouldn't it be better to define just 8 vertices and tell webgl how to connect them to get triangles? 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? He could specify all triangles with just 8 vertices in the vertex array.

Share Improve this question edited Sep 19, 2011 at 6:22 oOo asked Sep 18, 2011 at 18:45 oOooOo 454 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

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. A particular vertex might be be the bundle <(1, -1, 1), red>. 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.

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.

It's not hugely efficient in terms of memory, but memory's cheap pared to the CPU power that a more normalised representation would require for efficient processing.

Hope that clarifies things.

You can use Vertex Buffer Objects (VBO). See this example. They create a list of Vertices and and a list of Indexes "pointing" to the vertices (no duplication of vertices).

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745180875a4615412.html

相关推荐

  • javascript - WebGL triangles in a cube - Stack Overflow

    In this tutorial author displays a cube by defining its 6 faces (6*4 vertices) and then telling webgl a

    3小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信