#include <fbxsdk.h>
#ifdef IOS_REF
#undef IOS_REF
#define IOS_REF (*(pSdkManager->GetIOSettings()))
#endif
#define SAMPLE_FILENAME "ExportDocument.fbx"
bool CreateDocument(KFbxSdkManager* pSdkManager, KFbxDocument* pDocument);
void CreateMatDocument(KFbxSdkManager* pSdkManager, KFbxDocument* pMatDocument);
void CreateLightDocument(KFbxSdkManager* pSdkManager, KFbxDocument* pLightDocument);
KFbxNode* CreatePlane(KFbxSdkManager* pSdkManager, char* pName);
KFbxSurfacePhong* CreateMaterial(KFbxSdkManager* pSdkManager);
KFbxTexture* CreateTexture(KFbxSdkManager* pSdkManager);
KFbxNode* CreateLight(KFbxSdkManager* pSdkManager, KFbxLight::ELightType pType);
void InitializeSdkObjects(KFbxSdkManager*& pSdkManager)
{
pSdkManager = KFbxSdkManager::Create();
if (!pSdkManager)
{
printf("Unable to create the FBX SDK manager\n");
exit(0);
}
KFbxIOSettings * ios = KFbxIOSettings::Create(pSdkManager, IOSROOT );
pSdkManager->SetIOSettings(ios);
}
void DestroySdkObjects(KFbxSdkManager* pSdkManager)
{
if (pSdkManager) pSdkManager->Destroy();
pSdkManager = NULL;
}
bool SaveDocument(KFbxSdkManager* pSdkManager, KFbxDocument* pDocument, const char* pFilename, int pFileFormat=-1, bool pEmbedMedia=false)
{
int lMajor, lMinor, lRevision;
bool lStatus = true;
KFbxExporter* lExporter = KFbxExporter::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))
{
KString 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)
{
printf("Call to KFbxExporter::Initialize() failed.\n");
printf("Error returned: %s\n\n", lExporter->GetLastErrorString());
return false;
}
KFbxSdkManager::GetFileFormatVersion(lMajor, lMinor, lRevision);
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)
{
KFbxSdkManager* lSdkManager = NULL;
KFbxDocument* lDocument = NULL;
bool lResult;
InitializeSdkObjects(lSdkManager);
lDocument = KFbxDocument::Create(lSdkManager,"RootDoc");
lResult = CreateDocument(lSdkManager, lDocument);
if(lResult == false)
{
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)
{
printf("\n\nAn error occurred while saving the document...\n");
DestroySdkObjects(lSdkManager);
return 0;
}
DestroySdkObjects(lSdkManager);
return 0;
}
bool CreateDocument(KFbxSdkManager* pSdkManager, KFbxDocument* pDocument)
{
int lCount;
KFbxDocumentInfo* lDocInfo = KFbxDocumentInfo::Create(pSdkManager,"DocInfo");
lDocInfo->mTitle = "Example document";
lDocInfo->mSubject = "Illustrates the creation of KFbxDocument 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);
KFbxNode* lPlane = CreatePlane(pSdkManager, "Plane");
pDocument->AddRootMember(lPlane);
lCount = pDocument->GetRootMemberCount();
lCount = pDocument->GetMemberCount();
KFbxDocument* lMatDocument = KFbxDocument::Create(pSdkManager,"Material");
CreateMatDocument(pSdkManager, lMatDocument);
pDocument->AddMember(lMatDocument);
KFbxDocument* lLightDocument = KFbxDocument::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(KFbxDocument));
KFbxObject* lObj = pDocument->GetMember(FBX_TYPE(KFbxDocument), 1);
return true;
}
void CreateMatDocument(KFbxSdkManager* pSdkManager, KFbxDocument* pMatDocument)
{
KFbxDocumentInfo* lDocInfo = KFbxDocumentInfo::Create(pSdkManager,"DocInfo");
lDocInfo->mTitle = "Sub document for materials";
lDocInfo->mSubject = "Illustrates the creation of sub-KFbxDocument 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(KFbxSdkManager* pSdkManager, KFbxDocument* pLightDocument)
{
KFbxDocumentInfo* lDocInfo = KFbxDocumentInfo::Create(pSdkManager,"DocInfo");
lDocInfo->mTitle = "Sub document for lights";
lDocInfo->mSubject = "Illustrates the creation of sub-KFbxDocument 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, KFbxLight::eSPOT));
pLightDocument->AddMember(CreateLight(pSdkManager, KFbxLight::ePOINT));
}
KFbxNode* CreatePlane(KFbxSdkManager* pSdkManager, char* pName)
{
int i;
KFbxMesh* lMesh = KFbxMesh::Create(pSdkManager,pName);
KFbxVector4 lControlPoint0(-50, 0, 50);
KFbxVector4 lControlPoint1(50, 0, 50);
KFbxVector4 lControlPoint2(50, 100, 50);
KFbxVector4 lControlPoint3(-50, 100, 50);
KFbxVector4 lNormalZPos(0, 0, 1);
lMesh->InitControlPoints(4);
KFbxVector4* lControlPoints = lMesh->GetControlPoints();
lControlPoints[0] = lControlPoint0;
lControlPoints[1] = lControlPoint1;
lControlPoints[2] = lControlPoint2;
lControlPoints[3] = lControlPoint3;
KFbxGeometryElementNormal* lElementNormal= lMesh->CreateElementNormal();
lElementNormal->SetMappingMode(KFbxGeometryElement::eBY_CONTROL_POINT);
lElementNormal->SetReferenceMode(KFbxGeometryElement::eDIRECT);
lElementNormal->GetDirectArray().Add(lNormalZPos);
lElementNormal->GetDirectArray().Add(lNormalZPos);
lElementNormal->GetDirectArray().Add(lNormalZPos);
lElementNormal->GetDirectArray().Add(lNormalZPos);
int lPolygonVertices[] = { 0, 1, 2, 3 };
KFbxGeometryElementUV* lUVDiffuseElement = lMesh->CreateElementUV( "DiffuseUV");
K_ASSERT( lUVDiffuseElement != NULL);
lUVDiffuseElement->SetMappingMode(KFbxGeometryElement::eBY_POLYGON_VERTEX);
lUVDiffuseElement->SetReferenceMode(KFbxGeometryElement::eINDEX_TO_DIRECT);
KFbxVector2 lVectors0(0, 0);
KFbxVector2 lVectors1(1, 0);
KFbxVector2 lVectors2(1, 1);
KFbxVector2 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 ();
KFbxNode* lNode = KFbxNode::Create(pSdkManager,pName);
lNode->SetNodeAttribute(lMesh);
lNode->SetShadingMode(KFbxNode::eTEXTURE_SHADING);
lNode->LclRotation.Set(KFbxVector4(90, 0, 0));
KFbxGeometryElementMaterial* lMaterialElement = lMesh->CreateElementMaterial();
lMaterialElement->SetMappingMode(KFbxGeometryElement::eBY_POLYGON);
lMaterialElement->SetReferenceMode(KFbxGeometryElement::eINDEX_TO_DIRECT);
if( !lMesh->GetElementMaterial( 0))
return NULL;
KString lMaterialName = "material_for_plane";
KString lShadingName = "Phong";
KFbxSurfacePhong* lMaterial = KFbxSurfacePhong::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;
}
KFbxTexture* CreateTexture(KFbxSdkManager* pSdkManager)
{
KFbxFileTexture* lTexture = KFbxFileTexture::Create(pSdkManager,"");
KString lPath = KFbxGetApplicationDirectory();
KString lTexPath = lPath + "\\Crate.jpg";
lTexture->SetFileName(lTexPath.Buffer());
lTexture->SetName("Diffuse Texture");
lTexture->SetTextureUse(KFbxTexture::eSTANDARD);
lTexture->SetMappingType(KFbxTexture::eUV);
lTexture->SetMaterialUse(KFbxFileTexture::eMODEL_MATERIAL);
lTexture->SetSwapUV(false);
lTexture->SetAlphaSource (KFbxTexture::eNONE);
lTexture->SetTranslation(0.0, 0.0);
lTexture->SetScale(1.0, 1.0);
lTexture->SetRotation(0.0, 0.0);
return lTexture;
}
KFbxSurfacePhong* CreateMaterial(KFbxSdkManager* pSdkManager)
{
KString lMaterialName = "material";
KString 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);
KFbxSurfacePhong* lMaterial = KFbxSurfacePhong::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;
}
KFbxNode* CreateLight(KFbxSdkManager* pSdkManager, KFbxLight::ELightType pType)
{
KString lLightName;
fbxDouble1 val;
switch (pType)
{
case KFbxLight::eSPOT:
lLightName = "SpotLight";
break;
case KFbxLight::ePOINT:
lLightName = "PointLight";
break;
case KFbxLight::eDIRECTIONAL:
lLightName = "DirectionalLight";
break;
default:
break;
}
KFbxLight* lFbxLight = KFbxLight::Create(pSdkManager, lLightName.Buffer());
lFbxLight->LightType.Set(pType);
KFbxLight::ELightType lType = lFbxLight->LightType.Get();
if (pType == KFbxLight::eSPOT)
{
lFbxLight->HotSpot.Set(40.0);
val = lFbxLight->HotSpot.Get();
lFbxLight->ConeAngle.Set(40);
val = lFbxLight->ConeAngle.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();
KFbxNode* lNode = KFbxNode::Create(pSdkManager,lLightName+"Node");
lNode->SetNodeAttribute(lFbxLight);
lNode->LclTranslation.Set(fbxDouble3(20, 30, 100));
val3 = lNode->LclTranslation.Get();
return lNode;
}