How to draw cube without indices?OpenGL ES 2.0
-
08-02-2021 - |
سؤال
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
المحلول
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;
}
لا تنتمي إلى StackOverflow