Assimp无法从层文件中读取纹理坐标信息

时间:2019-04-01 22:31:35

标签: textures game-engine mesh assimp

我正在尝试使用assimp加载一些层文件。我正在获取顶点位置,并且法线也正确加载(我看到它们显示在屏幕上,所以一切都很好),但是似乎从来没有加载纹理坐标。这导致纹理没有出现在模型和我的漫反射颜色上出现在它们上而不是纹理上。

我仔细检查了代码,发现问题出在我制作的assimp loader类中。 mTextureCoords [0] [index]完全为空。

我尝试更改围绕分配的纹理坐标的if,但是我认为这是正确的吗?

这是loader方法的一小段,其中包含获取纹理坐标的代码

    if (scene->HasMeshes()) {   //Check to see if the filename was valid

        aiMesh* aiMesh = *scene->mMeshes;

        theMesh.numberOfVertices = aiMesh->mNumVertices;

        //Create the number of vertices that this mesh has.
        theMesh.pVertices = new sVertex_xyz_rgba_n_uv2_bt[theMesh.numberOfVertices];

        theMesh.minXYZ.x = aiMesh->mVertices[0].x;
        theMesh.minXYZ.y = aiMesh->mVertices[0].y;
        theMesh.minXYZ.z = aiMesh->mVertices[0].z;

        theMesh.maxXYZ = theMesh.minXYZ;

        for (unsigned int index = 0; index != theMesh.numberOfVertices; index++) {
            theMesh.pVertices[index].x = aiMesh->mVertices[index].x;
            theMesh.pVertices[index].y = aiMesh->mVertices[index].y;
            theMesh.pVertices[index].z = aiMesh->mVertices[index].z;

            if (theMesh.pVertices[index].x < theMesh.minXYZ.x) { theMesh.minXYZ.x = theMesh.pVertices[index].x; }
            if (theMesh.pVertices[index].y < theMesh.minXYZ.y) { theMesh.minXYZ.y = theMesh.pVertices[index].y; }
            if (theMesh.pVertices[index].z < theMesh.minXYZ.z) { theMesh.minXYZ.z = theMesh.pVertices[index].z; }

            if (theMesh.pVertices[index].x > theMesh.maxXYZ.x) { theMesh.maxXYZ.x = theMesh.pVertices[index].x; }
            if (theMesh.pVertices[index].y > theMesh.maxXYZ.y) { theMesh.maxXYZ.y = theMesh.pVertices[index].y; }
            if (theMesh.pVertices[index].z > theMesh.maxXYZ.z) { theMesh.maxXYZ.z = theMesh.pVertices[index].z; }


            theMesh.pVertices[index].nx = aiMesh->mNormals[index].x;
            theMesh.pVertices[index].ny = aiMesh->mNormals[index].y;
            theMesh.pVertices[index].nz = aiMesh->mNormals[index].z;


            int numUVComponents = aiMesh->mNumUVComponents[0];

            if (numUVComponents == 2) {
                theMesh.pVertices[index].u1 = aiMesh->mTextureCoords[0][index].x;
                theMesh.pVertices[index].v1 = aiMesh->mTextureCoords[0][index].y;
            }
        }

        theMesh.numberOfTriangles = scene->mMeshes[0]->mNumFaces;
        theMesh.pTriangles = new cTriangle[theMesh.numberOfTriangles];

        //Copy the triangle vertex information (indices)
        for (int triIndex = 0; triIndex != theMesh.numberOfTriangles; triIndex++) {
            theMesh.pTriangles[triIndex].vertex_ID_0 = aiMesh->mFaces[triIndex].mIndices[0];
            theMesh.pTriangles[triIndex].vertex_ID_1 = aiMesh->mFaces[triIndex].mIndices[1];
            theMesh.pTriangles[triIndex].vertex_ID_2 = aiMesh->mFaces[triIndex].mIndices[2];
        }
    }

这是我正在尝试使用assimp加载的模型文件的一小段,该文件带有标头以及顶点,法线和uvs的几行。

ply
format ascii 1.0
comment VCGLIB generated
element vertex 289
property float x
property float y
property float z
property float nx
property float ny
property float nz
property float texture_u
property float texture_v
element face 512
property list uchar int vertex_indices
end_header
-400 1.748456e-05 400 0 0.25 -1.092785e-08 1.000000 0.000000
-350 1.748456e-05 400 0 0.25 -1.092785e-08 1.000000 0.062500
-300 1.748456e-05 400 0 0.25 -1.092785e-08 1.000000 0.125000
-250 1.748456e-05 400 0 0.25 -1.092785e-08 1.000000 0.187500
-200 1.748456e-05 400 0 0.25 -1.092785e-08 1.000000 0.250000
-150 1.748456e-05 400 0 0.25 -1.092785e-08 1.000000 0.312500
-100 1.748456e-05 400 0 0.25 -1.092785e-08 1.000000 0.375000
-50 1.748456e-05 400 0 0.25 -1.092785e-08 1.000000 0.437500

希望有人知道我在搞砸=) 预先感谢!

1 个答案:

答案 0 :(得分:0)

还是有点不确定为什么assimp无法加载层。但我确认我的代码适用于FBX文件。也许assimp与这种风格不兼容?