glDrawElements 显示问题

glDrawElements display issue

我想在 openGL ES 2 中绘制带有纹理的对象(立方体),但结果很奇怪(对我来说)。

我想绘制的对象 - 从 FBX 文件中提取并具有类似数据(我也可以提取法线,但我不使用它们,所以跳过):

----Indices----24
0
1
2
3
4
7
6
5
0
4
5
1
1
5
6
2
2
6
7
3
4
0
3
7

----Coordinates(Vertises)----72
1 1 -1 
1 -1 -1 
-1 -1 -1 
-1 1 -1 
1 0.999999 1 
-1 1 1 
-1 -1 1 
0.999999 -1 1 
1 1 -1 
1 0.999999 1 
0.999999 -1 1 
1 -1 -1 
1 -1 -1 
0.999999 -1 1 
-1 -1 1 
-1 -1 -1 
-1 -1 -1 
-1 -1 1 
-1 1 1 
-1 1 -1 
1 0.999999 1 
1 1 -1 
-1 1 -1 
-1 1 1 

----Texture UV----48
0.499999 9.7692e-05
0.499999 0.333364
0.250049 0.333364
0.250049 9.78112e-05
0.250049 0.666633
0.499999 0.666633
0.499999 0.9999
0.250049 0.9999
0.74995 0.333366
0.74995 0.666633
0.5 0.666633
0.5 0.333367
0.499998 0.333366
0.499998 0.666632
0.250048 0.666633
0.250048 0.333366
0.25005 0.333367
0.25005 0.666633
0.000100091 0.666633
0.000100024 0.333367
0.749953 0.666639
0.749953 0.333373
0.999903 0.333373
0.999903 0.666639

所以我设置缓冲区 :

- (void)bindBuffer
{
    glGenVertexArraysOES(1, &_vertextArray);
    glBindVertexArrayOES(_vertextArray);
    //vertises
    glGenBuffers(1, &_vertexBuffer);
    glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
    glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * _drawModel.numberOfVertices, _drawModel.vertices, GL_STATIC_DRAW);

    glEnableVertexAttribArray(GLKVertexAttribPosition);
    glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 3, NULL);
    //textures
    glGenBuffers(1, &_indexBuffer);
    glBindBuffer(GL_ARRAY_BUFFER, _indexBuffer);
    glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * _drawModel.numberOfTextCoords, _drawModel.texCoords, GL_STATIC_DRAW);

    glEnableVertexAttribArray(attributes[ATTRIBUTES_TEXTURE_COORDINATE]);
    glVertexAttribPointer(attributes[ATTRIBUTES_TEXTURE_COORDINATE], 2, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 2, NULL);
    //indises
    glGenBuffers(1, &_indisesBuffer);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indisesBuffer);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLuint) * _drawModel.numberOfIndises, _drawModel.indises, GL_STATIC_DRAW);

    glBindVertexArrayOES(0);
}

然后画出来

- (void)draw
{
    glDisable(GL_DEPTH_TEST);
    glDepthMask(GL_FALSE);
    glDisable(GL_CULL_FACE);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glClearColor(0.65f, 0.65f, 0.65f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glUseProgram(_programm);
    glBindVertexArrayOES(_vertextArray);

    glUniformMatrix4fv(uniforms[UNIFORM_MODELVIEWPROJECTION_MATRIX], 1, 0, _mvpMatrix.m);
    glUniform1i(uniforms[UNIFORM_TEXTURE], 0);
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, _texture.name);
    glDrawElements(GL_TRIANGLES, _drawModel.numberOfIndises, GL_UNSIGNED_INT, 0);
}

结果我得到了下一个:

如果我使用GL_LINES模式(仅供参考):

如果我使用 GL_TRIANGLES:

纹理(供参考)

所以看起来 gl machine 没有画一些额外的三角形,但我总结了它发生的原因以及如何解决它...... 有什么建议吗?

----更新-----

问题是 Indices 索引不正确并且缺少一部分,在更正后我得到了(感谢@codetiger

您有 24 个顶点,但索引仅引用前 7 个顶点。这意味着,索引是错误的。

尝试按顺序输入索引

 0,  1,  2,  0,  2,  3,   //front
 4,  5,  6,  4,  6,  7,   //right
 8,  9,  10, 8,  10, 11,  //back
 12, 13, 14, 12, 14, 15,  //left
 16, 17, 18, 16, 18, 19,  //upper
 20, 21, 22, 20, 22, 23