如何绘制没有索引的立方体?OpenGL ES 2.0

发布于 2024-12-09 02:29:02 字数 915 浏览 0 评论 0原文

我想绘制没有索引并使用 VBO 的立方体。我在互联网上找不到任何内容(教程,示例)。 我尝试过:

const GLfloat Vertices[] = {
    -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,
};


glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);
 glDrawArrays(GL_TRIANGLE_STRIP, 0, 24);

但事实证明有些奇怪的东西,但不是立方体

I want to draw cube without indices and using VBO. I can't find anything in internet(tutorials,examples).
What I tried:

const GLfloat Vertices[] = {
    -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,
};


glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);
 glDrawArrays(GL_TRIANGLE_STRIP, 0, 24);

But it turns out that something strange, but not the cube

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

世界如花海般美丽 2024-12-16 02:29:02

首先检查您的 glDrawPrimatives 调用,确保您使用 GL_QUADS 作为绘制模式。其他任何事情都会产生奇怪的结果。

不过你的顶点很好。它们的顺序正确且位置正确。

编辑:由于您不能使用四边形,因此您必须单独定义每个三角形,您的第一个块将如下所示:

-1.0f, -1.0f, 1.0f, //v0
1.0f, -1.0f, 1.0f,  //v1
-1.0f, 1.0f, 1.0f,  //v2

-1.0f, 1.0f, 1.0f,  //v2
1.0f, -1.0f, 1.0f,  //v1
1.0f, 1.0f, 1.0f,   //v3

不过,我强烈建议使用索引,如果您保留与定义相同的顶点缓冲区,则可以设置你的索引是这样的:

byte indices[6 * 6];
int n = 0;
for(int i = 0; i < 4 * 6; i += 4)
{
   indices[n++] = i;
   indices[n++] = i + 1;
   indices[n++] = i + 2;

   indices[n++] = i + 2;
   indices[n++] = i + 1;
   indices[n++] = i + 3;
}

First check in your glDrawPrimatives call,that you're using GL_QUADS as your draw mode. Anything else will give weird results.

You vertices are fine though. They're in the correct order and in the correct positions.

EDIT: Since you can't use quads, you will have to define each triangle individually, your first block will look like this:

-1.0f, -1.0f, 1.0f, //v0
1.0f, -1.0f, 1.0f,  //v1
-1.0f, 1.0f, 1.0f,  //v2

-1.0f, 1.0f, 1.0f,  //v2
1.0f, -1.0f, 1.0f,  //v1
1.0f, 1.0f, 1.0f,   //v3

I would highly suggest using indices though, if you keep the same vertex buffer as you defined, you can set up your indices like this:

byte indices[6 * 6];
int n = 0;
for(int i = 0; i < 4 * 6; i += 4)
{
   indices[n++] = i;
   indices[n++] = i + 1;
   indices[n++] = i + 2;

   indices[n++] = i + 2;
   indices[n++] = i + 1;
   indices[n++] = i + 3;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文