#include <fbxsdk.h>
#ifdef IOS_REF
#undef IOS_REF
#define IOS_REF (*(pSdkManager->GetIOSettings()))
#endif
#define SAMPLE_FILENAME "ExportDocument.fbx"
bool CreateDocument(FbxManager* pSdkManager, FbxDocument* pDocument);
void CreateMatDocument(FbxManager* pSdkManager, FbxDocument* pMatDocument);
void CreateLightDocument(FbxManager* pSdkManager, FbxDocument* pLightDocument);
FbxNode* CreatePlane(FbxManager* pSdkManager, char* pName);
FbxSurfacePhong* CreateMaterial(FbxManager* pSdkManager);
FbxTexture* CreateTexture(FbxManager* pSdkManager);
FbxNode* CreateLight(FbxManager* pSdkManager, FbxLight::EType pType);
void InitializeSdkObjects(FbxManager*& pSdkManager)
{
pSdkManager = FbxManager::Create();
if (!pSdkManager)
{
FBXSDK_printf("Unable to create the FBX SDK manager\n");
exit(0);
}
FbxIOSettings * ios = FbxIOSettings::Create(pSdkManager, IOSROOT );
pSdkManager->SetIOSettings(ios);
}
void DestroySdkObjects(FbxManager* pSdkManager)
{
if (pSdkManager) pSdkManager->Destroy();
pSdkManager = NULL;
}
bool SaveDocument(FbxManager* pSdkManager, FbxDocument* pDocument, const char* pFilename, int pFileFormat=-1, bool pEmbedMedia=false)
{
int lMajor, lMinor, lRevision;
bool lStatus = true;
FbxExporter* lExporter = FbxExporter::Create(pSdkManager, "");
if( pFileFormat < 0 || pFileFormat >= pSdkManager->GetIOPluginRegistry()->GetWriterFormatCount() )
{
pFileFormat = pSdkManager->GetIOPluginRegistry()->GetNativeWriterFormat();
if (!pEmbedMedia)
{
int lFormatIndex, lFormatCount = pSdkManager->GetIOPluginRegistry()->GetWriterFormatCount();
for (lFormatIndex=0; lFormatIndex<lFormatCount; lFormatIndex++)
{
if (pSdkManager->GetIOPluginRegistry()->WriterIsFBX(lFormatIndex))
{
FbxString lDesc =pSdkManager->GetIOPluginRegistry()->GetWriterFormatDescription(lFormatIndex);
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, pSdkManager->GetIOSettings()) == false)
{
FBXSDK_printf("Call to FbxExporter::Initialize() failed.\n");
FBXSDK_printf("Error returned: %s\n\n", lExporter->GetLastErrorString());
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;
InitializeSdkObjects(lSdkManager);
lDocument = FbxDocument::Create(lSdkManager,"RootDoc");
lResult = CreateDocument(lSdkManager, lDocument);
if(lResult == false)
{
FBXSDK_printf("\n\nAn error occurred while creating the document...\n");
DestroySdkObjects(lSdkManager);
return 0;
}
if(argc > 1)
{
lResult = SaveDocument(lSdkManager, lDocument, argv[1]);
}
else
{
lResult = SaveDocument(lSdkManager, lDocument, SAMPLE_FILENAME);
}
if(lResult == false)
{
FBXSDK_printf("\n\nAn error occurred while saving the document...\n");
DestroySdkObjects(lSdkManager);
return 0;
}
DestroySdkObjects(lSdkManager);
return 0;
}
bool CreateDocument(FbxManager* pSdkManager, FbxDocument* pDocument)
{
int lCount;
FbxDocumentInfo* lDocInfo = FbxDocumentInfo::Create(pSdkManager,"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(pSdkManager, "Plane");
pDocument->AddRootMember(lPlane);
lCount = pDocument->GetRootMemberCount();
lCount = pDocument->GetMemberCount();
FbxDocument* lMatDocument = FbxDocument::Create(pSdkManager,"Material");
CreateMatDocument(pSdkManager, lMatDocument);
pDocument->AddMember(lMatDocument);
FbxDocument* lLightDocument = FbxDocument::Create(pSdkManager,"Light");
CreateLightDocument(pSdkManager, lLightDocument);
pDocument->AddMember(lLightDocument);
lCount = pDocument->GetMemberCount();
pDocument->CreateAnimStack("PlanAnim");
lCount = pDocument->GetRootMemberCount();
lCount = pDocument->GetMemberCount();
lCount = pDocument->GetMemberCount(FBX_TYPE(FbxDocument));
return true;
}
void CreateMatDocument(FbxManager* pSdkManager, FbxDocument* pMatDocument)
{
FbxDocumentInfo* lDocInfo = FbxDocumentInfo::Create(pSdkManager,"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(pSdkManager));
}
void CreateLightDocument(FbxManager* pSdkManager, FbxDocument* pLightDocument)
{
FbxDocumentInfo* lDocInfo = FbxDocumentInfo::Create(pSdkManager,"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(pSdkManager, FbxLight::eSpot));
pLightDocument->AddMember(CreateLight(pSdkManager, FbxLight::ePoint));
}
FbxNode* CreatePlane(FbxManager* pSdkManager, char* pName)
{
int i;
FbxMesh* lMesh = FbxMesh::Create(pSdkManager,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(pSdkManager,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(pSdkManager, 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* pSdkManager)
{
FbxFileTexture* lTexture = FbxFileTexture::Create(pSdkManager,"");
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* pSdkManager)
{
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(pSdkManager, lMaterialName.Buffer());
lMaterial->Emissive .Set(lBlack);
lMaterial->Ambient .Set(lRed);
lMaterial->AmbientFactor .Set(1.);
lMaterial->Diffuse .ConnectSrcObject(CreateTexture(pSdkManager));
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* pSdkManager, 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(pSdkManager, 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(pSdkManager,lLightName+"Node");
lNode->SetNodeAttribute(lFbxLight);
lNode->LclTranslation.Set(FbxDouble3(20, 30, 100));
val3 = lNode->LclTranslation.Get();
return lNode;
}