问题
I want to draw object (cube) with texture in openGL ES 2, but got strange (as for me) result.
Obj that i want to draw - extracted from FBX file and has data like (also i can extract Normals but i don't use them so just skip):
----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
So i bund buffers :
- (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);
}
and draw them
- (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);
}
As result i got next:
if i use GL_LINES
mode (just for reference):
and if i use GL_TRIANGLES
:
Texture (for reference)
So looks like gl machine
do not draw some additional triangles, but i stack why it's happens and how to fix it...
Any suggestions?
---- UPDATE-----
The problem was in incorrect Indices index and missing part of them, after correction i got (thanks to @codetiger)
回答1:
You have 24 vertices but the indices are referring only the first 7 vertices. Which means, the indices are wrong.
Try feeding the indices in order
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
来源:https://stackoverflow.com/questions/38822103/gldrawelements-display-issue