首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Assimp -如何使用任何文件格式导入带有纹理的网格?

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

Stack Overflow用户
提问于 2019-12-09 23:26:44
回答 2查看 2.1K关注 0票数 1

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

代码语言:javascript
复制
    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;


                }
            }
        }
    }
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-12-30 14:31:43

如果有人对我是如何解决我的问题感到好奇的话,请看下面。在.mtl文件中,我必须添加map_Kd diffusefile.png来添加一个漫射文件,这样assimp就可以获取纹理文件。

代码语言:javascript
复制
        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
                }
            }
票数 1
EN

Stack Overflow用户

发布于 2019-12-23 12:16:21

这将正常工作。请您提供obj-模型及其相应的材料文件,并在这里生成一个新的问题报告:https://github.com/assimp/assimp/issues

然后我们可以试着研究一下你的例子中的问题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59258084

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档