简体   繁体   English

最佳实践 - 声明opengles 2.0立方体顶点

[英]Best practices - Declare opengles 2.0 cube vertices

I've seen a lot of ways to declare the vertices of a cube in space. 我已经看到很多方法来声明空间中立方体的顶点。 Some codes declare 36 vertices, some other just eight, like that: 有些代码声明了36个顶点,其他一些只有8个,就像这样:

private float vertices[] = {
                                -1.0f, -1.0f, -1.0f, //p1 lower left far plane (0)
                                1.0f, -1.0f, -1.0f,  //p2 lower right far plane (1)
                                1.0f,  1.0f, -1.0f,  //p3 top right far plane (2)
                                -1.0f, 1.0f, -1.0f,  //p4 top left far plane (3)
                                -1.0f, -1.0f,  1.0f, //p5 lower left near plane (4)
                                1.0f, -1.0f,  1.0f,  //p6 lower right near plane (5)
                                1.0f,  1.0f,  1.0f,  //p7 top right near plane (6)
                                -1.0f,  1.0f,  1.0f  //p8 top left near plane (7)
                                };

It looks like you draw just two faces and then fill the holes. 看起来你只绘制两个面,然后填充孔。 That seems preferable so I'm saving precious bytes. 这似乎更好,所以我节省了宝贵的字节。 Is that correct? 那是对的吗? What order shall I specify in the indices array? 我应该在indices数组中指定什么顺序?

---EDIT Ok, I've got it. ---编辑好的,我知道了。 This should be the order to declare the vertices, right? 这应该是声明顶点的顺序,对吧? But my cube has wrong triangles on some faces. 但我的立方体在某些面上有错误的三角形。

private short order[] = {
                              0, 4, 5, 0, 5, 1, //bottom face
                              1, 5, 6, 1, 6, 2, //right face
                              2, 6, 7, 2, 7, 3, // top face
                              3, 7, 4, 3, 4, 0, // left face
                              4, 7, 6, 4, 6, 5, // front face
                              3, 0, 1, 3, 1, 2 // back face
                              };

Yes that's right. 是的,这是正确的。 once you define the vertices for the front and back (near and far) faces you have given all the information needed to construct the cube. 一旦定义了前后面(近处和远处)的顶点,就可以获得构建立方体所需的所有信息。 Anything else is redundant and waste memory. 其他任何事情都是多余的,浪费内存。

The only thing you need to be careful about is this : When you actually use the vertices to construct the cube order matters . 您唯一需要注意的是:当您实际使用顶点来构造立方体顺序时

By default opengl expects for the vertices to be provided counter clockwise. 默认情况下,opengl期望逆时针提供顶点。 I believe you can override that behavior, but I have spent many hours trying to figure out what I thought were coding mistakes that turned out to be vertex ordering mistakes. 我相信你可以覆盖这种行为,但我花了很多时间试图弄清楚我认为编码错误的结果是顶点排序错误。 As long as you remember to always construct the vertices in the counter clockwise manner (as viewed from the outside of the cube) the rendering should work fine. 只要你记得总是以逆时针方式构造顶点(从立方体的外部看),渲染应该可以正常工作。

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

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