c++ - Assimp-如何使用任何文件格式导入带有纹理的网格?

标签 c++ import mesh assimp

导入网格物体时,我得到了 Material ,但是无法访问纹理的文件名。 .mtl文件显式显示纹理的文件名。在代码中,它的纹理计数为1,但文件名字段显示为空字符串,fullPath输出为“* 0”。在mTexture中,它确实显示纹理文件扩展名“.png”,但不显示纹理本身的文件名。谢谢你的帮助。

    if (scene->HasMaterials())
    {
        for (unsigned int i = 0; i < scene->mNumMaterials; ++i)
        {
            aiMaterial* material = scene->mMaterials[i];
            aiString name;
            material->Get(AI_MATKEY_NAME, name);
            aiReturn texFound = scene->mMaterials[i]->GetTexture(aiTextureType_DIFFUSE, i, &name);

            if (material->GetTextureCount(aiTextureType_DIFFUSE) > 0)
            {
                aiString path;
                if (material->GetTexture(aiTextureType_DIFFUSE, 0, &path, NULL, NULL, NULL, NULL, NULL) == AI_SUCCESS)
                {
                    std::string fullPath = path.data;


                }
            }
        }
    }

最佳答案

如果有人对我如何解决我的问题感到好奇,则如下所示。在.mtl文件中,我必须添加map_Kddiffusefile.png来添加漫反射文件,以便assimp可以拾取纹理文件。

        if (scene->HasMaterials())//True when number of materials is greater than 0
        {
            for (unsigned int m = 0; m < scene->mNumMaterials; ++m)
            {
                aiMaterial* material = scene->mMaterials[m];//Get the current material
                aiString materialName;//The name of the material found in mesh file
                aiReturn ret;//Code which says whether loading something has been successful of not

                ret = material->Get(AI_MATKEY_NAME, materialName);//Get the material name (pass by reference)
                if (ret != AI_SUCCESS) materialName = "";//Failed to find material name so makes var empty

                //Diffuse maps
                int numTextures = material->GetTextureCount(aiTextureType_DIFFUSE);//Amount of diffuse textures
                aiString textureName;//Filename of the texture using the aiString assimp structure

                if (numTextures > 0)
                {
                    //Get the file name of the texture by passing the variable by reference again
                    //Second param is 0, which is the first diffuse texture
                    //There can be more diffuse textures but for now we are only interested in the first one
                    ret = material->Get(AI_MATKEY_TEXTURE(aiTextureType_DIFFUSE, 0), textureName);

                    std::string textureType = "diff_";
                    std::string textureFileName = textureType + textureName.data;//The actual name of the texture file
                }
            }

关于c++ - Assimp-如何使用任何文件格式导入带有纹理的网格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59258084/

相关文章:

c++ - 为什么这个悬空的 std::weak_ptr 不会导致 SEGFAULT?

c# - 如何导入HTML格式的Excel

c++ - 无法在 VS 2010 中编译 Vcg 库

c# - Unity C# 生成的网格是偏移的,为什么它不在正确的位置?

c++ - 为什么调用析构函数?

c++ - 测量分支被错误预测的频率

c++ - malloc/free 用于调整数组大小

java - 无法在 Netbeans 的 Maven 项目中导入任何 javax-servlet

css - @import 在我的网站上不起作用

optimization - mesh.optimize 到底做了什么?