glDrawElements显示问题

时间:2016-08-08 05:55:20

标签: ios objective-c iphone opengl-es fbx

我想在openGL ES 2中使用纹理绘制对象(立方体),但是结果却很奇怪(对我而言)。

我想绘制的Obj - 从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模式(仅供参考):

enter image description here

如果我使用GL_TRIANGLES

enter image description here

纹理(供参考)

enter image description here

所以看起来gl machine不会绘制一些额外的三角形,但我会堆叠它为什么会发生以及如何修复它... 有什么建议吗?

----更新-----

问题在于错误的指数索引和缺失部分,经过我得到的修正后(感谢@ codetiger

enter image description here

1 个答案:

答案 0 :(得分:1)

您有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