在OpenGL ES中绘制立方体需要多少个顶点? [英] How many vertices needed in to draw a cube in OpenGL ES?

查看:206
本文介绍了在OpenGL ES中绘制立方体需要多少个顶点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在不同的在线站点中看到了不同数量的顶点,以表示OpenGL ES中的相同立方体. 例如,这是一个:

I see different number of vertices in different online sites to represent the same cube in OpenGL ES. For Example this is one:

float vertices[] = { -width, -height, -depth, // 0
                              width, -height, -depth, // 1
                              width,  height, -depth, // 2
                             -width,  height, -depth, // 3
                             -width, -height,  depth, // 4
                              width, -height,  depth, // 5
                              width,  height,  depth, // 6
                             -width,  height,  depth // 7
        };  
  short indices[] = { 0, 2, 1,
                0, 3, 2,

                1,2,6,
                6,5,1,

                4,5,6,
                6,7,4,

                2,3,6,
                6,3,7,

                0,7,3,
                0,4,7,

                0,1,5,
                0,5,4
                         };

这是另一个:

float vertices[] = {
            //Vertices according to faces
                -1.0f, -1.0f, 1.0f, //Vertex 0
                1.0f, -1.0f, 1.0f,  //v1
                -1.0f, 1.0f, 1.0f,  //v2
                1.0f, 1.0f, 1.0f,   //v3

                1.0f, -1.0f, 1.0f,  //...
                1.0f, -1.0f, -1.0f,         
                1.0f, 1.0f, 1.0f,
                1.0f, 1.0f, -1.0f,

                1.0f, -1.0f, -1.0f,
                -1.0f, -1.0f, -1.0f,            
                1.0f, 1.0f, -1.0f,
                -1.0f, 1.0f, -1.0f,

                -1.0f, -1.0f, -1.0f,
                -1.0f, -1.0f, 1.0f,         
                -1.0f, 1.0f, -1.0f,
                -1.0f, 1.0f, 1.0f,

                -1.0f, -1.0f, -1.0f,
                1.0f, -1.0f, -1.0f,         
                -1.0f, -1.0f, 1.0f,
                1.0f, -1.0f, 1.0f,

                -1.0f, 1.0f, 1.0f,
                1.0f, 1.0f, 1.0f,           
                -1.0f, 1.0f, -1.0f,
                1.0f, 1.0f, -1.0f,
                                    };

哪种表示形式在OpenGL ES中性能最高(请注意,它是OpenGL ES而不是OpenGL)?当我要指定照明法线时,这是最好的表示.在关于SO的其他问题中,我得到的印象是,我需要为多维数据集指定24个顶点,另一个说我需要36个顶点,而另一个说我需要一个不同的数字来得到相同问题的矛盾答案.无论如何,我想知道技术上正确的正确答案.

Which representation is highest performance in OpenGL ES ( Note that it is OpenGL ES and not OpenGL)? Which is the best representation when I want to specify normals for lighting. In one my other quations on SO, I get the impression that I need to specify 24 vertices for the cube, another one says I need 36 vertices, yet another one says I need a different number getting contradictory answers to the same question. Anyway I want to know correct single answer that is technically correct.

我知道有用于绘制图形的三角形条或三角形风扇等概念,但是在我的情况下,我使用的是

I know there is a concept of Triangle Strip or Triangle fans etc. for drawing but in my case I am using:

gl.glDrawElements(GL10.GL_TRIANGLES, mNumOfIndices,
                GL10.GL_UNSIGNED_SHORT, mIndicesBuffer);

因此,这应该清楚地阐明我的要求.

So this should clearly clarify what I am asking.

推荐答案

如果使用索引,则只需提供8个不同的顶点即可在OpenGL ES中定义立方体.例如:

If using indices, you only need to provide 8 distinct vertices to define a cube in OpenGL ES. For example:

static const GLfloat cubeVertices[] = {
    -1.0, -1.0,  1.0,
    1.0, -1.0,  1.0,
    -1.0,  1.0,  1.0,
    1.0,  1.0,  1.0,
    -1.0, -1.0, -1.0,
    1.0, -1.0, -1.0,
    -1.0,  1.0, -1.0,
    1.0,  1.0, -1.0,
};

static const GLushort cubeIndices[] = {
    0, 1, 2, 3, 7, 1, 5, 4, 7, 6, 2, 4, 0, 1
};

在这里使用三角带作为几何图形,我只需要提供14个索引来指定立方体.您可以根据需要删除上述索引并按此顺序提供顶点.

Using a triangle strip for the geometry here, I only needed to provide 14 indices to specify the cube. You could remove the above indices and provide vertices in that order, if you'd like.

genpfault描述了为OpenGL ES提供纯三角形的情况,在这种情况下,您需要36个顶点.如您所见,这些顶点中有许多是多余的,因此,条带或索引可以减少您需要发送的几何图形.

genpfault describes the case where you provide pure triangles to OpenGL ES, in which case you'd need 36 vertices. As you can see, many of these vertices are redundant, so strips or indices can reduce the geometry you need to send.

Eric在他的评论中提出了一个很好的观点,即如果您需要为每张脸提供纹理或颜色(

Eric brings up a good point in his comment, that if you need to provide a texture or color to each face (which appears to be your goal) you'll want to use 24 vertices or indices. You need this so that you can address each face separately.

您的特定应用程序的最高性能将取决于您在其上运行的硬件.例如,在iOS设备内部的PowerVR芯片上,Apple

What's highest performance for your particular application will depend on the hardware you're running this on. For example, on the PowerVR chips inside iOS devices, Apple has this to say:

为了获得最佳性能,您的模型 应该作为一个提交 未索引的三角带使用 glDrawArrays具有很少的重复项 顶点.如果您的模特 需要复制许多顶点 (因为许多顶点由 没有出现的三角形 依次在三角带中或 因为您的应用程序合并了许多 较小的三角形带),您可以 使用 单独的索引缓冲区和调用 glDrawElements代替.有一个 权衡:未索引的三角带 必须定期复制整个 顶点,而索引三角形 列表需要额外的内存 索引并增加开销 顶点.为了获得最佳结果,请进行测试 您的模型同时使用索引和 未索引的三角带,并使用 表现最快的那一台.

For best performance, your models should be submitted as a single unindexed triangle strip using glDrawArrays with as few duplicated vertices as possible. If your models require many vertices to be duplicated (because many vertices are shared by triangles that do not appear sequentially in the triangle strip or because your application merged many smaller triangle strips), you may obtain better performance using a separate index buffer and calling glDrawElements instead. There is a trade off: an unindexed triangle strip must periodically duplicate entire vertices, while an indexed triangle list requires additional memory for the indices and adds overhead to look up vertices. For best results, test your models using both indexed and unindexed triangle strips, and use the one that performs the fastest.

这篇关于在OpenGL ES中绘制立方体需要多少个顶点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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