web::json::value gltfWriter::WriteTexture (FbxTexture *pTexture) { utility::string_t name =utility::conversions::to_string_t (pTexture->GetNameWithoutNameSpacePrefix ().Buffer ()) ; utility::string_t uri =utility::conversions::to_string_t (FbxCast<FbxFileTexture> (pTexture)->GetRelativeFileName ()) ; web::json::value image =web::json::value::object ({{ name, web::json::value::object ({ { U("name"), web::json::value::string (name) }, { U("uri"), web::json::value::string (uri) } }) } }) ; if ( GetIOSettings ()->GetBoolProp (IOSN_FBX_GLTF_EMBEDMEDIA, false) ) { // data:[<mime type>][;charset=<charset>][;base64],<encoded data> FbxString imageFile =FbxCast<FbxFileTexture> (pTexture)->GetFileName () ; image [name] [U("uri")] =web::json::value::string (IOglTF::dataURI (utility::conversions::to_string_t (imageFile.Buffer ()))) ; } /*else*/ if ( GetIOSettings ()->GetBoolProp (IOSN_FBX_GLTF_COPYMEDIA, false) ) { FbxString path =FbxPathUtils::GetFolderName (utility::conversions::to_utf8string (_fileName).c_str ()) ; #if defined(_WIN32) || defined(_WIN64) path +="\\" ; #else path +="/" ; #endif FbxString imageFile =FbxCast<FbxFileTexture> (pTexture)->GetFileName () ; std::ifstream src (imageFile.Buffer (), std::ios::binary) ; std::ofstream dst (path + FbxPathUtils::GetFileName (imageFile), std::ios::binary) ; dst << src.rdbuf () ; } utility::string_t texName =createTextureName (pTexture->GetNameWithoutNameSpacePrefix ()) ; utility::string_t samplerName =createSamplerName (pTexture->GetNameWithoutNameSpacePrefix ()) ; web::json::value textureDef =web::json::value::object ({ { U("name"), web::json::value::string (texName) }, { U("format"), web::json::value::number ((int)IOglTF::RGBA) }, // todo { U("internalFormat"), web::json::value::number ((int)IOglTF::RGBA) }, // todo { U("sampler"), web::json::value::string (samplerName) }, // todo do I need one everytime { U("source"), web::json::value::string (name) }, { U("target"), web::json::value::number ((int)IOglTF::TEXTURE_2D) }, { U("type"), web::json::value::number ((int)IOglTF::UNSIGNED_BYTE) } // todo }) ; web::json::value texture =web::json::value::object ({{ texName, textureDef }}) ; //TODO: Shall try to find a similar sampler defined already vs create one each time? web::json::value samplerDef =web::json::value::object ({ { U("name"), web::json::value::string (samplerName) }, { U("magFilter"), web::json::value::number ((int)IOglTF::LINEAR) }, { U("minFilter"), web::json::value::number ((int)IOglTF::LINEAR_MIPMAP_LINEAR) }, { U("wrapS"), web::json::value::number ((int)(pTexture->WrapModeU.Get () == FbxTexture::eRepeat ? IOglTF::REPEAT : IOglTF::CLAMP_TO_EDGE)) }, { U("wrapT"), web::json::value::number ((int)(pTexture->WrapModeV.Get () == FbxTexture::eRepeat ? IOglTF::REPEAT : IOglTF::CLAMP_TO_EDGE)) } }) ; return (web::json::value::object ({ { U("images"), image }, { U("textures"), web::json::value::object ({{ texName, textureDef }}) }, { U("samplers"), web::json::value::object ({{ samplerName, samplerDef }}) } })) ; }
bool gltfPackage::WriteScene (const std::string &outdir) { auto pMgr =fbxSdkMgr::Instance ()->fbxMgr () ; int iFormat =pMgr->GetIOPluginRegistry ()->FindWriterIDByExtension ("gltf") ; if ( iFormat == -1 ) return (false) ; FbxAutoDestroyPtr<FbxExporter> pExporter (FbxExporter::Create (pMgr, "")) ; std::string newFn =outdir + (_scene->GetName ()) + (".gltf") ; bool bRet =pExporter->Initialize ((newFn).c_str (), iFormat, pMgr->GetIOSettings ()) ; assert( bRet ) ; if ( !bRet ) return (false) ; // The next line will call the exporter bRet =pExporter->Export (_scene) ; return (bRet) ; }
bool gltfPackage::LoadScene (const std::string &fn) { auto pMgr =fbxSdkMgr::Instance ()->fbxMgr () ; FbxAutoDestroyPtr<FbxImporter> pImporter (FbxImporter::Create (pMgr, "")) ; if ( !pImporter->Initialize ((fn).c_str (), -1, pMgr->GetIOSettings ()) ) return (false) ; if ( pImporter->IsFBX () ) { // From this point, it is possible to access animation stack information without // the expense of loading the entire file. // Set the import states. By default, the import states are always set to true. } bool bStatus =pImporter->Import (_scene) ; if ( _ioSettings._name.length () ) _scene->SetName ((_ioSettings._name).c_str ()) ; else if ( _scene->GetName () == FbxString ("") ) //_scene->SetName ("untitled") ; _scene->SetName ((gltfPackage::filename (fn)).c_str ()) ; //if ( bStatus == false && pImporter->GetStatus ().GetCode () == FbxStatus::ePasswordError ) { //} // Get current UpAxis of the FBX file. // This have to be done before ConvertiAxisSystem(), cause the function will always change SceneAxisSystem to Y-up. // First, however, if we have the ForcedFileAxis activated, we need to overwrite the global settings. //switch ( IOS_REF.GetEnumProp (IMP_FILE_UP_AXIS, FbxMayaUtility::eUPAXIS_AUTO) ) { // default: // case FbxMayaUtility::eUPAXIS_AUTO: // break ; // case FbxMayaUtility::eUPAXIS_Y: // _scene->GetGlobalSettings ().SetAxisSystem (FbxAxisSystem::MayaYUp) ; // break ; // case FbxMayaUtility::eUPAXIS_Z: // _scene->GetGlobalSettings ().SetAxisSystem (FbxAxisSystem::MayaZUp) ; // break ; //} //FbxAxisSystem sceneAxisSystem =_scene->GetGlobalSettings ().GetAxisSystem () ; //int lSign =0 ; //FbxAxisSystem::EUpVector upVectorFromFile =sceneAxisSystem.GetUpVector (lSign) ; FbxAxisSystem::MayaYUp.ConvertScene (_scene) ; // We want the Y up axis for glTF FbxSystemUnit sceneSystemUnit =_scene->GetGlobalSettings ().GetSystemUnit () ; // We want meter as default unit for gltTF if ( sceneSystemUnit != FbxSystemUnit::m ) { //const FbxSystemUnit::ConversionOptions conversionOptions ={ // false, // mConvertRrsNodes // true, // mConvertAllLimits // true, // mConvertClusters // false, // mConvertLightIntensity // true, // mConvertPhotometricLProperties // false // mConvertCameraClipPlanes //} ; //FbxSystemUnit::m.ConvertScene (_scene, conversionOptions) ; FbxSystemUnit::m.ConvertScene (_scene) ; } FbxGeometryConverter converter (fbxSdkMgr::Instance ()->fbxMgr ()) ; converter.Triangulate (_scene, true) ; // glTF supports triangles only converter.SplitMeshesPerMaterial (_scene, true) ; // Split meshes per material, so we only have one material per mesh (VBO support) // Set the current peripheral to be the NULL so FBX geometries that have been imported can be flushed _scene->SetPeripheral (NULL_PERIPHERAL) ; //int nb =_scene->GetSrcObjectCount<FbxNodeAttribute> () ; //int nbTot =5 * nb ; //int nbRest =4 * nb ; //int nbSteps =nbRest / 8 ; //FbxArray<FbxNode *> pBadMeshes =RemoveBadPolygonsFromMeshes (_scene) ; return (true) ; }
bool gltfWriter::WriteShaders () { //web::json::value buffer =web::json::value::object () ; //FbxString filename =FbxPathUtils::GetFileName (utility::conversions::to_utf8string (_fileName).c_str (), false) ; //buffer [U("name")] =web::json::value::string (utility::conversions::to_string_t (filename.Buffer ())) ; //buffer [U("uri")] =web::json::value::string (utility::conversions::to_string_t ((filename + ".bin").Buffer ())) ; //// The Buffer file should be fully completed by now. //if ( GetIOSettings ()->GetBoolProp (IOSN_FBX_GLTF_EMBEDMEDIA, false) ) { // // data:[<mime type>][;charset=<charset>][;base64],<encoded data> // buffer [U("uri")] =web::json::value::string (IOglTF::dataURI (_bin)) ; //} //if ( _writeDefaults ) // buffer [U("type")] =web::json::value::string (U("arraybuffer")) ; ; // default is arraybuffer //buffer [U("byteLength")] =web::json::value::number ((int)_bin.tellg ()) ; //_json [U("buffers")] [utility::conversions::to_string_t (filename.Buffer ())] =buffer ; //web::json::value techs =_json [U("techniques")] ; //for ( auto iter =techs.as_object ().begin () ; iter != techs.as_object ().end () ; iter++ ) { // glslTech test (iter->second) ; // ucout << test.vertexShader ().source () // << std::endl ; //} web::json::value materials =_json [U("materials")] ; for ( auto iter =materials.as_object ().begin () ; iter != materials.as_object ().end () ; iter++ ) { utility::string_t name =iter->first ; utility::string_t techniqueName =iter->second [U("technique")].as_string () ; glslTech tech ( _json [U("techniques")] [techniqueName], iter->second [U("values")], _json ) ; utility::string_t programName =_json [U("techniques")] [techniqueName] [U("program")].as_string () ; utility::string_t vsName =_json [U("programs")] [programName] [U("vertexShader")].as_string () ; utility::string_t fsName =_json [U("programs")] [programName] [U("fragmentShader")].as_string () ; if ( GetIOSettings ()->GetBoolProp (IOSN_FBX_GLTF_EMBEDMEDIA, false) ) { // data:[<mime type>][;charset=<charset>][;base64],<encoded data> _json [U("shaders")] [vsName] [U("uri")] =web::json::value::string (IOglTF::dataURI (tech.vertexShader ().source ())) ; _json [U("shaders")] [fsName] [U("uri")] =web::json::value::string (IOglTF::dataURI (tech.fragmentShader ().source ())) ; } else { FbxString gltfFilename (utility::conversions::to_utf8string (_fileName).c_str ()) ; utility::string_t vsFilename =_json [U("shaders")] [vsName] [U("uri")].as_string () ; { FbxString shaderFilename (utility::conversions::to_utf8string (vsFilename).c_str ()) ; #if defined(_WIN32) || defined(_WIN64) shaderFilename =FbxPathUtils::GetFolderName (gltfFilename) + "\\" + shaderFilename ; #else shaderFilename =FbxPathUtils::GetFolderName (fileName) + "/" + shaderFilename ; #endif std::wfstream shaderFile (shaderFilename, std::ios::out | std::ofstream::binary) ; //_bin.seekg (0, std::ios_base::beg) ; shaderFile.write (tech.vertexShader ().source ().c_str (), tech.vertexShader ().source ().length ()) ; shaderFile.close () ; } utility::string_t fsFileame =_json [U("shaders")] [fsName] [U("uri")].as_string () ; { FbxString shaderFilename (utility::conversions::to_utf8string (fsFileame).c_str ()) ; #if defined(_WIN32) || defined(_WIN64) shaderFilename =FbxPathUtils::GetFolderName (gltfFilename) + "\\" + shaderFilename ; #else shaderFilename =FbxPathUtils::GetFolderName (fileName) + "/" + shaderFilename ; #endif std::wfstream shaderFile (shaderFilename, std::ios::out | std::ofstream::binary) ; //_bin.seekg (0, std::ios_base::beg) ; shaderFile.write (tech.fragmentShader ().source ().c_str (), tech.fragmentShader ().source ().length ()) ; shaderFile.close () ; } } } return (true) ; }