坏顶点数据常见的陷阱/原因?

时间:2013-06-27 03:14:06

标签: c++ linux opengl 3d mesh

所以,过去一周半,我一直在研究这个.OBJ / .MTL网格解析器。在这段时间里,我一直在追踪/修复很多错误,清理代码,记录代码等等。

问题在于,我修复的每一个错误都会出现这个问题,因为一张图片胜过千言万语......

使用GL_LINE_LOOP

(注意:右边的金字塔从球体向外倾斜是这里的问题)

sphere line loop

使用GL_TRIANGLES

sphere triangles

更有趣的是,这个“坏”的顶点数据在浮动场景时似乎与相机一起移动......除了它在网格外部缩放和粘住。

这里奇怪的是,虽然我确定这个问题与内存有关,但我一直在检查与解析算法是否正常工作相矛盾的问题。经过一些单元测试后,它似乎工作正常。

所以,我认为它可能是Linux nVidia驱动程序问题。我将驱动程序更新到下一个版本,重新启动,但仍然没有骰子。

经过深思熟虑后,我一直试图在以下代码中找到错误。

            //! every 3 vertices should represent a triangle, therefore we'll want to
            //! use the indices to grab their corresponding vertices. Since the cross product
            //! of two sides of every triangle (where one side = Vn - Vm, 'n' and 'm' being on the range of 1..3),
            //! we first grab the three vertices, and then compute the normal using the their differences.

            const uInt32 length = mesh->vertices.size();

            //! declare a pointer to the vector so we can perform simple
            //! memory copies to get the indices for each triangle within the
            //! iteration.

            GLuint* const pIndexBuf = &mesh->indices[ 0 ];

            for ( uInt32 i = 0; i < length; i += 3 )
            {
                GLuint thisTriIndices[ 3 ];

                memcpy( thisTriIndices, pIndexBuf + i, sizeof( GLuint ) * 3 );

                vec3 vertexOne   = vec3( mesh->vertices[ thisTriIndices[ 0 ] ] );
                vec3 vertexTwo   = vec3( mesh->vertices[ thisTriIndices[ 1 ] ] );
                vec3 vertexThree = vec3( mesh->vertices[ thisTriIndices[ 2 ] ] );

                vec3 sideOne        = vertexTwo - vertexOne;
                vec3 sideTwo        = vertexThree - vertexOne;

                vec3 surfaceNormal  = glm::cross( sideOne, sideTwo );

                mesh->normals.push_back( surfaceNormal );
            }

图片中显示的当前图像甚至没有正常数据,因此想法是为它计算表面法线,因此上面的代码。虽然我已经做了一些检查以查看索引数据是否在循环中正确加载,但我还没有找到任何东西。

我认为我记忆的方式也可能存在问题,但我无法完全理解问题所在。如果我错过了什么,我会抛出我的glVertexAttribPointer调用:

//! Gen some buf handles

    glGenBuffers( NUM_BUFFERS_PER_MESH, mesh->buffers );

    //! Load the respective buffer data for the mesh

    __LoadVec4Buffer( mesh->buffers[ BUFFER_VERTEX ], mesh->vertices );      //! positons
    __LoadVec4Buffer( mesh->buffers[ BUFFER_COLOR ], mesh->colors );         //! material colors
    __LoadVec3Buffer( mesh->buffers[ BUFFER_NORMAL ], mesh->normals );       //! normals
    __LoadIndexBuffer( mesh->buffers[ BUFFER_INDEX ], mesh->indices );       //! indices

    //! assign the vertex array a value

    glGenVertexArrays( 1, &mesh->vertexArray );

    //! Specify the memory layout for each attribute

    glBindVertexArray( mesh->vertexArray );

    //! Position and color are both stored in BUFFER_VERTEX.

    glBindBuffer( GL_ARRAY_BUFFER, mesh->buffers[ BUFFER_VERTEX ] );

    glEnableVertexAttribArray( meshProgram->attributes[ "position" ] );
    glVertexAttribPointer( meshProgram->attributes[ "position" ],               //! index
                           4,                                                   //! num vals
                           GL_FLOAT, GL_FALSE,                                  //! value type, normalized?
                           sizeof( vec4 ),                                      //! number of bytes until next value in the buffer
                           ( void* ) 0 );                                       //! offset of the memory in the buffer

    glBindBuffer( GL_ARRAY_BUFFER, mesh->buffers[ BUFFER_COLOR ] );

    glEnableVertexAttribArray( meshProgram->attributes[ "color" ] );
    glVertexAttribPointer( meshProgram->attributes[ "color" ],
                           4,
                           GL_FLOAT, GL_FALSE,
                           sizeof( vec4 ),
                           ( void* ) 0 );

    //! Now we specify the layout for the normals

    glBindBuffer( GL_ARRAY_BUFFER, mesh->buffers[ BUFFER_NORMAL ] );

    glEnableVertexAttribArray( meshProgram->attributes[ "normal" ] );
    glVertexAttribPointer( meshProgram->attributes[ "normal" ],
                           3,
                           GL_FLOAT, GL_FALSE,
                           sizeof( vec3 ),
                           ( void* )0 );

    //! Include the index buffer within the vertex array

    glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, mesh->buffers[ BUFFER_INDEX ] );

    glBindVertexArray( 0 );

至少在正确方向上的任何一点都会受到赞赏:我不知道这些问题的常见原因是什么。

修改:根据要求发布绘制代码

glBindVertexArray( mMeshes[ i ]->vertexArray );

UBO::LoadMatrix4( UBO::MATRIX_MODELVIEW, modelView.top() );
UBO::LoadMatrix4( UBO::MATRIX_PROJECTION, camera.projection() );

glDrawElements( GL_TRIANGLES, mMeshes[ i ]->indices.size(), GL_UNSIGNED_INT, ( void* )0 );

glBindVertexArray( 0 );

1 个答案:

答案 0 :(得分:0)

我找到了最后的罪魁祸首,结合@ radical7的建议,这些解决了大部分问题。

            // round mesh->indices.size() down if it's not already divisible by 3.
            // the rounded value is stored in numTris

        std::vector< vec4 > newVertices;

        uInt32 indicesLen = Math_FloorForMultiple( mesh->indices.size(), 3 );

            // declare a pointer to the vector so we can perform simple
            // memory copies to get the indices for each triangle within the
            // iteration.

        newVertices.reserve( indicesLen );

        const GLuint* const pIndexBuf = &mesh->indices[ 0 ];

        for ( uInt32 i = 0; i < indicesLen; i += 3 )
        {
            const GLuint* const thisTriIndices = pIndexBuf + i;

            vec4 vertexOne   = mesh->vertices[ thisTriIndices[ 0 ] - 1 ];
            vec4 vertexTwo   = mesh->vertices[ thisTriIndices[ 1 ] - 1 ];
            vec4 vertexThree = mesh->vertices[ thisTriIndices[ 2 ] - 1 ];

            vec4 sideOne     = vertexTwo - vertexOne;
            vec4 sideTwo     = vertexThree - vertexOne;

            vec3 surfaceNormal = glm::cross( vec3( sideOne ), vec3( sideTwo ) );

            mesh->normals.push_back( surfaceNormal );
            mesh->normals.push_back( surfaceNormal + vec3( sideOne ) );
            mesh->normals.push_back( surfaceNormal + vec3( sideTwo ) );

            newVertices.push_back( vertexOne );
            newVertices.push_back( vertexTwo );
            newVertices.push_back( vertexThree );
        }


        mesh->vertices.clear();
        mesh->vertices = newVertices;

请注意,当在循环中抓取顶点时,通过调用mesh->vertices[ thisTriIndices[ x ] - 1 ]- 1非常重要:OBJ网格文件存储从1 ... N个索引开始的面部索引,而不是0 .... N-1指数。

索引本身也不应该用于绘制网格,而是用作从已经临时的顶点缓冲区获取顶点的新缓冲区的方法:使用索引访问临时顶点内的元素,然后对于从临时缓冲区获得的每个顶点,您该顶点添加到新缓冲区。这样,您将获得以正确的绘制顺序指定的顶点数。因此,您只想使用顶点数组绘制它们。