#include <fbxsdk.h>
#ifdef IOS_REF
#undef IOS_REF
#define IOS_REF (*(pManager->GetIOSettings()))
#endif
#define SAMPLE_FILENAME "ExportDocument.fbx"
bool CreateDocument(FbxManager* pManager, FbxDocument* pDocument);
void CreateMatDocument(FbxManager* pManager, FbxDocument* pMatDocument);
void CreateLightDocument(FbxManager* pManager, FbxDocument* pLightDocument);
FbxNode* CreatePlane(FbxManager* pManager, const char* pName);
FbxSurfacePhong* CreateMaterial(FbxManager* pManager);
FbxTexture* CreateTexture(FbxManager* pManager);
FbxNode* CreateLight(FbxManager* pManager, FbxLight::EType pType);
void InitializeSdkObjects(FbxManager*& pManager)
{
pManager = FbxManager::Create();
if( !pManager )
{
FBXSDK_printf("Error: Unable to create FBX Manager!\n");
exit(1);
}
else FBXSDK_printf("Autodesk FBX SDK version %s\n", pManager->GetVersion());
FbxIOSettings* ios = FbxIOSettings::Create(pManager, IOSROOT);
pManager->SetIOSettings(ios);
}
void DestroySdkObjects(FbxManager* pManager, bool pExitStatus)
{
if( pManager ) pManager->Destroy();
if( pExitStatus ) FBXSDK_printf("Program Success!\n");
}
bool SaveDocument(FbxManager* pManager, FbxDocument* pDocument, const char* pFilename, int pFileFormat=-1, bool pEmbedMedia=false)
{
int lMajor, lMinor, lRevision;
bool lStatus = true;
FbxExporter* lExporter = FbxExporter::Create(pManager, "");
if( pFileFormat < 0 || pFileFormat >= pManager->GetIOPluginRegistry()->GetWriterFormatCount() )
{
pFileFormat = pManager->GetIOPluginRegistry()->GetNativeWriterFormat();
if (!pEmbedMedia)
{
int lFormatIndex, lFormatCount = pManager->GetIOPluginRegistry()->GetWriterFormatCount();
for (lFormatIndex=0; lFormatIndex<lFormatCount; lFormatIndex++)
{
if (pManager->GetIOPluginRegistry()->WriterIsFBX(lFormatIndex))
{
FbxString lDesc =pManager->GetIOPluginRegistry()->GetWriterFormatDescription(lFormatIndex);
const char *lASCII = "ascii";
if (lDesc.Find(lASCII)>=0)
{
pFileFormat = lFormatIndex;
break;
}
}
}
}
}
IOS_REF.SetBoolProp(EXP_FBX_MATERIAL, true);
IOS_REF.SetBoolProp(EXP_FBX_TEXTURE, true);
IOS_REF.SetBoolProp(EXP_FBX_EMBEDDED, pEmbedMedia);
IOS_REF.SetBoolProp(EXP_FBX_ANIMATION, true);
IOS_REF.SetBoolProp(EXP_FBX_GLOBAL_SETTINGS, true);
if(lExporter->Initialize(pFilename, pFileFormat, pManager->GetIOSettings()) == false)
{
FBXSDK_printf("Call to FbxExporter::Initialize() failed.\n");
FBXSDK_printf("Error returned: %s\n\n", lExporter->GetStatus().GetErrorString());
return false;
}
FbxManager::GetFileFormatVersion(lMajor, lMinor, lRevision);
FBXSDK_printf("FBX version number for this version of the FBX SDK is %d.%d.%d\n\n", lMajor, lMinor, lRevision);
lStatus = lExporter->Export(pDocument);
lExporter->Destroy();
return lStatus;
}
int main(int argc, char** argv)
{
FbxManager* lSdkManager = NULL;
FbxDocument* lDocument = NULL;
bool lResult = false;
InitializeSdkObjects(lSdkManager);
lDocument = FbxDocument::Create(lSdkManager, "RootDoc");
const char* lSampleFileName = NULL;
for( int i = 1; i < argc; ++i )
{
if( FBXSDK_stricmp(argv[i], "-test") == 0 ) continue;
else if( !lSampleFileName ) lSampleFileName = argv[i];
}
if( !lSampleFileName ) lSampleFileName = SAMPLE_FILENAME;
lResult = CreateDocument(lSdkManager, lDocument);
if( lResult )
{
lResult = SaveDocument(lSdkManager, lDocument, lSampleFileName);
if( !lResult ) FBXSDK_printf("\n\nAn error occurred while saving the document...\n");
}
else FBXSDK_printf("\n\nAn error occurred while creating the document...\n");
DestroySdkObjects(lSdkManager, lResult);
return 0;
}
bool CreateDocument(FbxManager* pManager, FbxDocument* pDocument)
{
int lCount;
FbxDocumentInfo* lDocInfo = FbxDocumentInfo::Create(pManager,"DocInfo");
lDocInfo->mTitle = "Example document";
lDocInfo->mSubject = "Illustrates the creation of FbxDocument with geometries, materials and lights.";
lDocInfo->mAuthor = "ExportDocument.exe sample program.";
lDocInfo->mRevision = "rev. 1.0";
lDocInfo->mKeywords = "Fbx document";
lDocInfo->mComment = "no particular comments required.";
pDocument->SetDocumentInfo(lDocInfo);
FbxNode* lPlane = CreatePlane(pManager, "Plane");
pDocument->AddRootMember(lPlane);
lCount = pDocument->GetRootMemberCount();
lCount = pDocument->GetMemberCount();
FbxDocument* lMatDocument = FbxDocument::Create(pManager,"Material");
CreateMatDocument(pManager, lMatDocument);
pDocument->AddMember(lMatDocument);
FbxDocument* lLightDocument = FbxDocument::Create(pManager,"Light");
CreateLightDocument(pManager, lLightDocument);
pDocument->AddMember(lLightDocument);
lCount = pDocument->GetMemberCount();
pDocument->CreateAnimStack("PlanAnim");
lCount = pDocument->GetRootMemberCount();
lCount = pDocument->GetMemberCount();
lCount = pDocument->GetMemberCount<FbxDocument>();
return true;
}
void CreateMatDocument(FbxManager* pManager, FbxDocument* pMatDocument)
{
FbxDocumentInfo* lDocInfo = FbxDocumentInfo::Create(pManager,"DocInfo");
lDocInfo->mTitle = "Sub document for materials";
lDocInfo->mSubject = "Illustrates the creation of sub-FbxDocument with materials.";
lDocInfo->mAuthor = "ExportDocument.exe sample program.";
lDocInfo->mRevision = "rev. 1.0";
lDocInfo->mKeywords = "Fbx material document";
lDocInfo->mComment = "no particular comments required.";
pMatDocument->SetDocumentInfo(lDocInfo);
pMatDocument->AddMember(CreateMaterial(pManager));
}
void CreateLightDocument(FbxManager* pManager, FbxDocument* pLightDocument)
{
FbxDocumentInfo* lDocInfo = FbxDocumentInfo::Create(pManager,"DocInfo");
lDocInfo->mTitle = "Sub document for lights";
lDocInfo->mSubject = "Illustrates the creation of sub-FbxDocument with lights.";
lDocInfo->mAuthor = "ExportDocument.exe sample program.";
lDocInfo->mRevision = "rev. 1.0";
lDocInfo->mKeywords = "Fbx light document";
lDocInfo->mComment = "no particular comments required.";
pLightDocument->SetDocumentInfo(lDocInfo);
pLightDocument->AddMember(CreateLight(pManager, FbxLight::eSpot));
pLightDocument->AddMember(CreateLight(pManager, FbxLight::ePoint));
}
FbxNode* CreatePlane(FbxManager* pManager, const char* pName)
{
int i;
FbxMesh* lMesh = FbxMesh::Create(pManager,pName);
FbxVector4 lControlPoint0(-50, 0, 50);
FbxVector4 lControlPoint1(50, 0, 50);
FbxVector4 lControlPoint2(50, 100, 50);
FbxVector4 lControlPoint3(-50, 100, 50);
FbxVector4 lNormalZPos(0, 0, 1);
lMesh->InitControlPoints(4);
FbxVector4* lControlPoints = lMesh->GetControlPoints();
lControlPoints[0] = lControlPoint0;
lControlPoints[1] = lControlPoint1;
lControlPoints[2] = lControlPoint2;
lControlPoints[3] = lControlPoint3;
FbxGeometryElementNormal* lElementNormal= lMesh->CreateElementNormal();
lElementNormal->SetMappingMode(FbxGeometryElement::eByControlPoint);
lElementNormal->SetReferenceMode(FbxGeometryElement::eDirect);
lElementNormal->GetDirectArray().Add(lNormalZPos);
lElementNormal->GetDirectArray().Add(lNormalZPos);
lElementNormal->GetDirectArray().Add(lNormalZPos);
lElementNormal->GetDirectArray().Add(lNormalZPos);
int lPolygonVertices[] = { 0, 1, 2, 3 };
FbxGeometryElementUV* lUVDiffuseElement = lMesh->CreateElementUV( "DiffuseUV");
FBX_ASSERT( lUVDiffuseElement != NULL);
lUVDiffuseElement->SetMappingMode(FbxGeometryElement::eByPolygonVertex);
lUVDiffuseElement->SetReferenceMode(FbxGeometryElement::eIndexToDirect);
FbxVector2 lVectors0(0, 0);
FbxVector2 lVectors1(1, 0);
FbxVector2 lVectors2(1, 1);
FbxVector2 lVectors3(0, 1);
lUVDiffuseElement->GetDirectArray().Add(lVectors0);
lUVDiffuseElement->GetDirectArray().Add(lVectors1);
lUVDiffuseElement->GetDirectArray().Add(lVectors2);
lUVDiffuseElement->GetDirectArray().Add(lVectors3);
lUVDiffuseElement->GetIndexArray().SetCount(4);
lMesh->BeginPolygon(-1, -1, -1, false);
for(i = 0; i < 4; i++)
{
lMesh->AddPolygon(lPolygonVertices[i]);
lUVDiffuseElement->GetIndexArray().SetAt(i, i);
}
lMesh->EndPolygon ();
FbxNode* lNode = FbxNode::Create(pManager,pName);
lNode->SetNodeAttribute(lMesh);
lNode->SetShadingMode(FbxNode::eTextureShading);
lNode->LclRotation.Set(FbxVector4(90, 0, 0));
FbxGeometryElementMaterial* lMaterialElement = lMesh->CreateElementMaterial();
lMaterialElement->SetMappingMode(FbxGeometryElement::eByPolygon);
lMaterialElement->SetReferenceMode(FbxGeometryElement::eIndexToDirect);
if( !lMesh->GetElementMaterial( 0))
return NULL;
FbxString lMaterialName = "material_for_plane";
FbxString lShadingName = "Phong";
FbxSurfacePhong* lMaterial = FbxSurfacePhong::Create(pManager, lMaterialName.Buffer());
lMaterial->Diffuse.Set(FbxDouble3(1.0, 1.0, 0));
lMaterial->DiffuseFactor.Set(1.);
lNode->AddMaterial(lMaterial);
lMaterialElement->GetIndexArray().SetCount(lMesh->GetPolygonCount());
for(int i=0; i<lMesh->GetPolygonCount(); ++i)
lMaterialElement->GetIndexArray().SetAt(i,0);
return lNode;
}
FbxTexture* CreateTexture(FbxManager* pManager)
{
FbxFileTexture* lTexture = FbxFileTexture::Create(pManager,"");
FbxString lPath = FbxGetApplicationDirectory();
FbxString lTexPath = lPath + "\\Crate.jpg";
lTexture->SetFileName(lTexPath.Buffer());
lTexture->SetName("Diffuse Texture");
lTexture->SetTextureUse(FbxTexture::eStandard);
lTexture->SetMappingType(FbxTexture::eUV);
lTexture->SetMaterialUse(FbxFileTexture::eModelMaterial);
lTexture->SetSwapUV(false);
lTexture->SetAlphaSource (FbxTexture::eNone);
lTexture->SetTranslation(0.0, 0.0);
lTexture->SetScale(1.0, 1.0);
lTexture->SetRotation(0.0, 0.0);
return lTexture;
}
FbxSurfacePhong* CreateMaterial(FbxManager* pManager)
{
FbxString lMaterialName = "material";
FbxString lShadingName = "Phong";
FbxDouble3 lBlack(0.0, 0.0, 0.0);
FbxDouble3 lRed(1.0, 0.0, 0.0);
FbxDouble3 lDiffuseColor(0.75, 0.75, 0.0);
FbxSurfacePhong* lMaterial = FbxSurfacePhong::Create(pManager, lMaterialName.Buffer());
lMaterial->Emissive .Set(lBlack);
lMaterial->Ambient .Set(lRed);
lMaterial->AmbientFactor .Set(1.);
lMaterial->Diffuse .ConnectSrcObject(CreateTexture(pManager));
lMaterial->DiffuseFactor .Set(1.);
lMaterial->TransparencyFactor .Set(0.4);
lMaterial->ShadingModel .Set(lShadingName);
lMaterial->Shininess .Set(0.5);
lMaterial->Specular .Set(lBlack);
lMaterial->SpecularFactor .Set(0.3);
return lMaterial;
}
FbxNode* CreateLight(FbxManager* pManager, FbxLight::EType pType)
{
FbxString lLightName;
FbxDouble val;
switch (pType)
{
case FbxLight::eSpot:
lLightName = "SpotLight";
break;
case FbxLight::ePoint:
lLightName = "PointLight";
break;
case FbxLight::eDirectional:
lLightName = "DirectionalLight";
break;
default:
break;
}
FbxLight* lFbxLight = FbxLight::Create(pManager, lLightName.Buffer());
lFbxLight->LightType.Set(pType);
if (pType == FbxLight::eSpot)
{
lFbxLight->InnerAngle.Set(40.0);
val = lFbxLight->InnerAngle.Get();
lFbxLight->OuterAngle.Set(40);
val = lFbxLight->OuterAngle.Get();
}
FbxDouble3 lColor;
lColor[0] = 0.0;
lColor[1] = 1.0;
lColor[2] = 0.5;
lFbxLight->Color.Set(lColor);
FbxDouble3 val3 = lFbxLight->Color.Get();
lFbxLight->Intensity.Set(100.0);
val = lFbxLight->Intensity.Get();
FbxNode* lNode = FbxNode::Create(pManager,lLightName+"Node");
lNode->SetNodeAttribute(lFbxLight);
lNode->LclTranslation.Set(FbxDouble3(20, 30, 100));
val3 = lNode->LclTranslation.Get();
return lNode;
}