2016-08-08 139 views
0

我想在openGL ES 2中使用紋理繪製對象(立方體),但得到了奇怪的結果(如我)。glDrawElements顯示問題

的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不會畫一些額外的三角形,但我爲什麼會發生這種情況,以及如何解決它... 有什麼建議嗎?

---- ----- UPDATE

的問題是不正確的指標指數以及它們的缺失部分,修正後我得到了(感謝@codetiger

enter image description here

回答

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 
+0

我也嘗試用指數爲u建議(從1到23)進行測試 - 導致它的一點點好,但不如預期......,我猜測我應該有8個索引索引(因爲cube有8個verises),所以這應該是0到8範圍內的三角形的組合,對嗎? – gbk

+0

@gbk我已經糾正了頂點的順序,這應該適合你。 – codetiger

+0

謝謝,你是對的,我認爲indicess是爲三角形,而不是爲vertises - 我的秋天,非常感謝你的建議,pleasee看到更新 – gbk