简体   繁体   English

使用C ++和OpenGL加载obj文件的错误

[英]Bugs with loading obj file with c++ and opengl

I wrote obj loader and got following: 我写了obj loader并得到以下信息: 在此处输入图片说明

It is yellow eagle but as you see it has some additional triangles that go from its leg to wing. 它是黄色的鹰,但如您所见,它的腿和翼之间还有一些三角形。 The code that I used: 我使用的代码:

{....
    glBindBuffer(GL_ARRAY_BUFFER,vbo);
    glBufferData(GL_ARRAY_BUFFER,sizeof(data),data,GL_STATIC_DRAW);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER,numOfIndices*sizeof(GLuint),indices,GL_STATIC_DRAW);
}

void Mesh::draw( )
{

    glEnableVertexAttribArray(0);
    glBindBuffer(GL_ARRAY_BUFFER,vbo);
    glVertexAttribPointer(
            0,                  // attribute 0. No particular reason for 0, but must match the layout in the shader.
            3,                  // size
            GL_FLOAT,           // type
            GL_FALSE,           // normalized?
            0,                  // stride
            (void*)0            // array buffer offset
        );
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,ibo);
    glDrawElements(GL_TRIANGLES,numOfIndices,GL_UNSIGNED_INT,(void*)0  );
    glDisableVertexAttribArray(0);
}

Where data is array of vertices and indices is array of indices. 其中data是顶点数组,而index索引数组。

When I take and save data and indices in obj format and open resulting file in 3D editor eagle looks fine and doesn't have these additional triangles (that implies that both data and indices are fine). 当我以obj格式获取并保存数据和索引并在3D编辑器中打开生成的文件时,eagle看起来很好,并且没有这些额外的三角形(这意味着数据和索引都很好)。

I spent hours trying to to fix code and make eagle look normal but now I run out of ideas. 我花了几个小时试图修复代码,并使eagle看起来正常,但现在我的想法已经用完了。 So please if you have any ideas how to make eagle normal share them with me. 因此,如果您有任何使鹰正常的想法,请与我分享。

For those who think the problem is in loader here is screen of obj model that is made out of data from loader (from data[] and indices[]) 对于那些认为问题在于加载程序的人,这里是obj模型的屏幕,它是由来自加载程序的数据(来自data []和indexs [])组成的 在此处输入图片说明

Finally found solution. 终于找到了解决方案。

Indexing in obj. 索引obj。 format starts at 1 (not 0 ) and when you load vertices to GL_ARRAY_BUFFER vertex #1 becomes vertex#0 and whole indexing breaks. 格式从1(不是0)开始,并且当您将顶点加载到GL_ARRAY_BUFFER时,顶点1变为顶点0,整个索引中断。

Therefore it is necessary to decrease all values of indices by 1 and then index that pointed to vertex #1 will point to vertex #0 and indexing will become correct. 因此,必须将索引的所有值都减小1,然后指向顶点#1的索引将指向顶点#0,索引将变得正确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM