glDrawElements显示问题 [英] glDrawElements display issue

查看:158
本文介绍了glDrawElements显示问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to draw object (cube) with texture in openGL ES 2, but got strange (as for me) result.

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

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

所以我将缓冲区绑定:

- (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模式(仅供参考):

if i use GL_LINES mode (just for reference):

,如果我使用GL_TRIANGLES:

纹理(仅供参考)

所以看起来gl machine不会画一些其他的三角形,但是我堆叠了它为什么会发生以及如何解决的问题... 有什么建议吗?

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

问题出在校正后,我得到了错误的索引索引,并且缺少索引的一部分(感谢@ codetiger )

The problem was in incorrect Indices index and missing part of them, after correction i got (thanks to @codetiger)

推荐答案

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

You have 24 vertices but the indices are referring only the first 7 vertices. Which means, the indices are wrong.

尝试按顺序输入索引

 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

这篇关于glDrawElements显示问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆