c++ - 使用 fbx sdk C++ 从 fbx 读取纹理文件名

标签 c++ textures filenames fbx

我在尝试使用 fbx sdk 从 fbx 文件中获取信息时遇到了困难。我在 Maya 中创建了一个带有纹理的 fbx 模型。我想要做的就是在代码中找到所用纹理的完整文件名...

这里是一些代码:

void Model::GetFbxInfo(FbxNode* Node)
{
int numKids = Node->GetChildCount();
FbxNode* childNode = 0;

for (int i = 0; i < numKids; i++)
{
    childNode = Node->GetChild(i);
    FbxMesh* mesh = childNode->GetMesh();

    if (mesh != NULL)
    {
        // get vertices, indices, uv, etc
        // some more code...
        // get vertices, indices, uv, etc

        //================= Texture ========================================

        int materialCount = childNode->GetSrcObjectCount<FbxSurfaceMaterial>();

        for (int index = 0; index < materialCount; index++)
        {
            FbxSurfaceMaterial* material = (FbxSurfaceMaterial*)childNode->GetSrcObject<FbxSurfaceMaterial>(index);

            if (material != NULL)
            {
                LOG(INFO) << "\nmaterial: " << material->GetName() << std::endl;
                // This only gets the material of type sDiffuse, you probably need to traverse all Standard Material Property by its name to get all possible textures.
                FbxProperty prop = material->FindProperty(FbxSurfaceMaterial::sDiffuse);

                // Check if it's layeredtextures
                int layeredTextureCount = prop.GetSrcObjectCount<FbxLayeredTexture>();

                if (layeredTextureCount > 0)
                {
                    for (int j = 0; j < layeredTextureCount; j++)
                    {
                        FbxLayeredTexture* layered_texture = FbxCast<FbxLayeredTexture>(prop.GetSrcObject<FbxLayeredTexture>(j));
                        int lcount = layered_texture->GetSrcObjectCount<FbxTexture>();

                        for (int k = 0; k < lcount; k++)
                        {
                            FbxTexture* texture = FbxCast<FbxTexture>(layered_texture->GetSrcObject<FbxTexture>(k));
                            // Then, you can get all the properties of the texture, include its name
                            const char* textureName = texture->GetName();
                            LOG(INFO) << textureName;
                        }
                    }
                }
                else 
                {
                    // Directly get textures
                    int textureCount = prop.GetSrcObjectCount<FbxTexture>();
                    for (int j = 0; j < textureCount; j++)
                    {
                        FbxTexture* texture = FbxCast<FbxTexture>(prop.GetSrcObject<FbxTexture>(j));
                        // Then, you can get all the properties of the texture, include its name
                        const char* textureName = texture->GetName();
                        LOG(INFO) << textureName;

                        FbxProperty p = texture->RootProperty.Find("Filename");
                        LOG(INFO) << p.Get<FbxString>() << std::endl;


                        HRESULT hr;
                        D3DX11CreateTextureFromFile(Game::GetInstance()->GetRenderer()->GetDevice(), textureName, 0, 0, &m_texture, &hr);
                        if(FAILED(hr))
                        {
                            std::string message;
                            message.append("Load Texture: ");
                            message.append(texture->GetName());
                            message.append(" failed");
                            SHOWMESSAGEBOX(hr, message.c_str());
                        }
                    }
                }
            }
        }

        this->GetFbxInfo(childNode);
    }
}

它是一个漫射非分层纹理......所以 else 分支很重要...... textureCount 是 1 - 所以纹理被找到但是文件名和纹理 getname 只返回“file1”

这是我正在加载的 ascii-fbx 文件中的一段数据

Video: 900530112, "Video::file1", "Clip" {
    Type: "Clip"
    Properties70:  {
        P: "Path", "KString", "XRefUrl", "", "D:/Dropbox/Projects/MyModel/MyTexture.jpg"
    }
    UseMipMap: 0
    Filename: "D:/Dropbox/Projects/MyModel/MyTexture.jpg"
    RelativeFilename: "MyTexture.jpg"
}
Texture: 785759328, "Texture::file1", "" {
    Type: "TextureVideoClip"
    Version: 202
    TextureName: "Texture::file1"
    Properties70:  {
        P: "CurrentTextureBlendMode", "enum", "", "",0
        P: "UVSet", "KString", "", "", "map1"
        P: "UseMaterial", "bool", "", "",1
    }
    Media: "Video::file1"
    FileName: "D:/Dropbox/Projects/MyModel/MyTexture.jpg"
    RelativeFilename: "MyTexture.jpg"
    ModelUVTranslation: 0,0
    ModelUVScaling: 1,1
    Texture_Alpha_Source: "None"
    Cropping: 0,0,0,0
}

谁能帮助我理解我做错了什么?

谢谢!

最佳答案

必须将您的 fbx 纹理转换为 fbx 文件纹理...然后获取文件名

关于c++ - 使用 fbx sdk C++ 从 fbx 读取纹理文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19634369/

相关文章:

c++ - 在运行时 C++ 中使用派生类

c++ - 没有目标函数的进化算法

ios - iOS 7+ 上从 BGRA 字节获取 CVPixelBufferRef 的最快方法

java - OpenGL ES 中的纹理质量差

batch-file - 批量SUBSTRING一个文件名

c++ - GLFW - 在不使用循环的情况下保持窗口

c++ - sem_post(sem_t * sem) 和 sem_wait(sem_t * sem) 周围是否存在完整的内存屏障?

macos - 如何在 Metal 的四边形中正确渲染纹理?

java - JFileChooser getCurrentDirectory() 到 String

c# - 用C#替换字符串中的数字