Пример #1
0
//Read the custom file and reconstruct node hierarchy.
bool FBXImporterReader::Read(KFbxDocument* pDocument, KFbxStreamOptions* pStreamOptions)
{
    if (!pDocument)
    {
        GetError().SetLastErrorID(eINVALID_DOCUMENT_HANDLE);
        return false;
    }
    KFbxScene*      lScene = KFbxCast<KFbxScene>(pDocument);
    bool            lIsAScene = (lScene != NULL);
    bool            lResult = false;

    if(lIsAScene)
    {
        KFbxNode* lRootNode = lScene->GetRootNode();
        KFbxNodeAttribute * lRootNodeAttribute = KFbxNull::Create(mManager,"");
        lRootNode->SetNodeAttribute(lRootNodeAttribute);

        int lSize;
        char* lBuffer;    
        if(mFilePointer != NULL)
        {
            //To obtain file size
            fseek (mFilePointer , 0 , SEEK_END);
            lSize = ftell (mFilePointer);
            rewind (mFilePointer);

            //Read file content to a string.
            lBuffer = (char*) malloc (sizeof(char)*lSize);
            size_t lRead = fread(lBuffer, 1, lSize, mFilePointer);
            lBuffer[lRead]='\0';
            KString lString(lBuffer);

            //Parse the string to get name and relation of Nodes. 
            KString lSubString, lChildName, lParentName;
            KFbxNode* lChildNode;
            KFbxNode* lParentNode;
            KFbxNodeAttribute* lChildAttribute;
            int lEndTokenCount = lString.GetTokenCount("\n");

            for (int i = 0; i < lEndTokenCount; i++)
            {
                lSubString = lString.GetToken(i, "\n");
                KString lNodeString;
                lChildName = lSubString.GetToken(0, "\"");
                lParentName = lSubString.GetToken(2, "\"");

                //Build node hierarchy.
                if(lParentName == "RootNode")
                {
                    lChildNode = KFbxNode::Create(mManager,lChildName.Buffer());
                    lChildAttribute = KFbxNull::Create(mManager,"");
                    lChildNode->SetNodeAttribute(lChildAttribute);

                    lRootNode->AddChild(lChildNode);
                }
                else
                {
                    lChildNode = KFbxNode::Create(mManager,lChildName.Buffer());
                    lChildAttribute = KFbxNull::Create(mManager,"");
                    lChildNode->SetNodeAttribute(lChildAttribute);

                    lParentNode = lRootNode->FindChild(lParentName.Buffer());
                    lParentNode->AddChild(lChildNode);
                }
            }
        }
        lResult = true;
    }    
    return lResult;
}