#include <fbxsdk.h>
#include "../Common/Common.h"
#include "MyKFbxMesh.h"
#define SAMPLE_FILENAME_MC "ExportScene03_MC.fbx"
#define SAMPLE_FILENAME_PC2 "ExportScene03_PC2.fbx"
#define PID_MY_GEOMETRY_LEMENT 0
bool CreateScene(KFbxScene* pScene, char* pSampleFileName);
KFbxNode* CreateCubeWithTexture(KFbxScene* pScene, char* pName);
KFbxNode* CreatePyramidWithMaterials(KFbxScene* pScene, char* pName);
KFbxNode* CreateTriangle(KFbxScene* pScene, char* pName);
KFbxNode* CreateCubeWithMaterialAndMyKFbxMesh(KFbxScene* pScene, char* pName);
void CreateTexture(KFbxScene* pScene, KFbxMesh* pMesh);
void CreateMaterials(KFbxScene* pScene, KFbxMesh* pMesh);
void CreateMaterialsWithMyKFbxMesh(KFbxScene* pScene, MyKFbxMesh* pMyKFbxMesh);
void MapShapeOnPyramid(KFbxScene* pScene, KFbxNode* pPyramid);
void MapVertexCacheOnTriangle(KFbxScene* pScene, KFbxNode* pTriangle, char* pSampleFileName);
void SetCubeDefaultPosition(KFbxNode* pCube);
void SetPyramidDefaultPosition(KFbxNode* pPyramid);
void SetTriangleDefaultPosition(KFbxNode* pTriangle);
void SetMyKFbxMeshCubeDefaultPosition(KFbxNode* pMyKFbxCube);
void Animate(KFbxNode* pNode, KFbxAnimLayer* pAnimLayer);
void AnimateVertexCacheOnTriangleDoubleVertex(KFbxNode* pNode, double pFrameRate);
void AnimateVertexCacheOnTriangleInt32(KFbxNode* pNode, double pFrameRate);
void AnimateVertexCacheOnTriangleFloat(KFbxNode* pNode, double pFrameRate);
bool gExportVertexCacheMCFormat = true;
int gCacheType = 2;
int main(int argc, char** argv)
{
KFbxSdkManager* lSdkManager = NULL;
KFbxScene* lScene = NULL;
char* lSampleFileName = NULL;
bool lResult;
InitializeSdkObjects(lSdkManager, lScene);
lSdkManager->RegisterFbxClass("MyKFbxMesh", FBX_TYPE(MyKFbxMesh), FBX_TYPE(KFbxMesh));
lSdkManager->RegisterFbxClass("MyFbxObject", FBX_TYPE(MyFbxObject), FBX_TYPE(KFbxObject), "MyFbxObjectType", "MyFbxObjectSubType");
if(argc > 1)
{
lSampleFileName = argv[1];
}
else
{
lSampleFileName = gExportVertexCacheMCFormat ? (char *)SAMPLE_FILENAME_MC : (char *)SAMPLE_FILENAME_PC2;
}
if(argc > 2)
{
gCacheType = atoi(argv[2]);
}
lResult = CreateScene(lScene, lSampleFileName);
if(lResult == false)
{
printf("\n\nAn error occurred while creating the scene...\n");
DestroySdkObjects(lSdkManager);
return 0;
}
lResult = SaveScene(lSdkManager, lScene, lSampleFileName);
if(lResult == false)
{
printf("\n\nAn error occurred while saving the scene...\n");
DestroySdkObjects(lSdkManager);
return 0;
}
DestroySdkObjects(lSdkManager);
return 0;
}
bool CreateScene(KFbxScene* pScene, char* pSampleFileName)
{
KFbxNode* lCube = CreateCubeWithTexture(pScene, "Cube");
KFbxNode* lPyramid = CreatePyramidWithMaterials(pScene, "Pyramid");
KFbxNode* lTriangle = CreateTriangle(pScene, "Triangle");
KFbxNode* lMyKFbxMeshCube = CreateCubeWithMaterialAndMyKFbxMesh(pScene, "CubeMyKFbxMesh");
MyFbxObject* lMyFbxObject = MyFbxObject::Create(pScene, "MyFbxObject 1");
MapShapeOnPyramid(pScene, lPyramid);
MapVertexCacheOnTriangle(pScene, lTriangle, pSampleFileName);
SetCubeDefaultPosition(lCube);
SetPyramidDefaultPosition(lPyramid);
SetTriangleDefaultPosition(lTriangle);
SetMyKFbxMeshCubeDefaultPosition(lMyKFbxMeshCube);
KFbxNode* lRootNode = pScene->GetRootNode();
lRootNode->AddChild(lCube);
lRootNode->AddChild(lPyramid);
lRootNode->AddChild(lMyKFbxMeshCube);
lRootNode->AddChild(lTriangle);
lRootNode->ConnectSrcObject(lMyFbxObject);
KFbxAnimStack* lAnimStack = KFbxAnimStack::Create(pScene, "Show all faces");
KFbxAnimLayer* lAnimLayer = KFbxAnimLayer::Create(pScene, "Base Layer");
lAnimStack->AddMember(lAnimLayer);
KFbxProperty lMyProperty = lMyFbxObject->FindProperty("MyAnimatedPropertyName");
if( lMyProperty.IsValid() )
{
lMyProperty.Set(0.0);
lMyProperty.ModifyFlag(KFbxProperty::eANIMATABLE, true);
lMyProperty.CreateCurveNode(lAnimLayer);
KFbxAnimCurve* lMyFCurve = lMyProperty.GetCurve<KFbxAnimCurve>(lAnimLayer, true);
if( lMyFCurve )
{
KFbxAnimCurveKey key;
key.Set(KTIME_ZERO, -100); lMyFCurve->KeyAdd(key.GetTime(), key);
key.Set(KTime(100), 0) ; lMyFCurve->KeyAdd(key.GetTime(), key);
key.Set(KTime(200), 100) ; lMyFCurve->KeyAdd(key.GetTime(), key);
}
}
Animate(lCube, lAnimLayer);
Animate(lPyramid, lAnimLayer);
Animate(lMyKFbxMeshCube, lAnimLayer);
KFbxGlobalSettings& lGlobalSettings = pScene->GetGlobalSettings();
switch(gCacheType)
{
case 0:
default:
AnimateVertexCacheOnTriangleDoubleVertex(lTriangle, KTime::GetFrameRate(lGlobalSettings.GetTimeMode()));
break;
case 1:
AnimateVertexCacheOnTriangleInt32(lTriangle, KTime::GetFrameRate(lGlobalSettings.GetTimeMode()));
break;
case 2:
AnimateVertexCacheOnTriangleFloat(lTriangle, KTime::GetFrameRate(lGlobalSettings.GetTimeMode()));
break;
}
return true;
}
KFbxNode* CreateCubeWithTexture(KFbxScene* pScene, char* pName)
{
int i, j;
KFbxMesh* lMesh = KFbxMesh::Create(pScene,pName);
KFbxVector4 lControlPoint0(-50, 0, 50);
KFbxVector4 lControlPoint1(50, 0, 50);
KFbxVector4 lControlPoint2(50, 100, 50);
KFbxVector4 lControlPoint3(-50, 100, 50);
KFbxVector4 lControlPoint4(-50, 0, -50);
KFbxVector4 lControlPoint5(50, 0, -50);
KFbxVector4 lControlPoint6(50, 100, -50);
KFbxVector4 lControlPoint7(-50, 100, -50);
KFbxVector4 lNormalXPos(1, 0, 0);
KFbxVector4 lNormalXNeg(-1, 0, 0);
KFbxVector4 lNormalYPos(0, 1, 0);
KFbxVector4 lNormalYNeg(0, -1, 0);
KFbxVector4 lNormalZPos(0, 0, 1);
KFbxVector4 lNormalZNeg(0, 0, -1);
lMesh->InitControlPoints(24);
KFbxVector4* lControlPoints = lMesh->GetControlPoints();
lControlPoints[0] = lControlPoint0;
lControlPoints[1] = lControlPoint1;
lControlPoints[2] = lControlPoint2;
lControlPoints[3] = lControlPoint3;
lControlPoints[4] = lControlPoint1;
lControlPoints[5] = lControlPoint5;
lControlPoints[6] = lControlPoint6;
lControlPoints[7] = lControlPoint2;
lControlPoints[8] = lControlPoint5;
lControlPoints[9] = lControlPoint4;
lControlPoints[10] = lControlPoint7;
lControlPoints[11] = lControlPoint6;
lControlPoints[12] = lControlPoint4;
lControlPoints[13] = lControlPoint0;
lControlPoints[14] = lControlPoint3;
lControlPoints[15] = lControlPoint7;
lControlPoints[16] = lControlPoint3;
lControlPoints[17] = lControlPoint2;
lControlPoints[18] = lControlPoint6;
lControlPoints[19] = lControlPoint7;
lControlPoints[20] = lControlPoint1;
lControlPoints[21] = lControlPoint0;
lControlPoints[22] = lControlPoint4;
lControlPoints[23] = lControlPoint5;
KFbxGeometryElementNormal* lGeometryElementNormal= lMesh->CreateElementNormal();
lGeometryElementNormal->SetMappingMode(KFbxGeometryElement::eBY_CONTROL_POINT);
bool firstWayNormalCalculations=true;
if (firstWayNormalCalculations)
{
lGeometryElementNormal->SetReferenceMode(KFbxGeometryElement::eDIRECT);
lGeometryElementNormal->GetDirectArray().Add(lNormalZPos);
lGeometryElementNormal->GetDirectArray().Add(lNormalZPos);
lGeometryElementNormal->GetDirectArray().Add(lNormalZPos);
lGeometryElementNormal->GetDirectArray().Add(lNormalZPos);
lGeometryElementNormal->GetDirectArray().Add(lNormalXPos);
lGeometryElementNormal->GetDirectArray().Add(lNormalXPos);
lGeometryElementNormal->GetDirectArray().Add(lNormalXPos);
lGeometryElementNormal->GetDirectArray().Add(lNormalXPos);
lGeometryElementNormal->GetDirectArray().Add(lNormalZNeg);
lGeometryElementNormal->GetDirectArray().Add(lNormalZNeg);
lGeometryElementNormal->GetDirectArray().Add(lNormalZNeg);
lGeometryElementNormal->GetDirectArray().Add(lNormalZNeg);
lGeometryElementNormal->GetDirectArray().Add(lNormalXNeg);
lGeometryElementNormal->GetDirectArray().Add(lNormalXNeg);
lGeometryElementNormal->GetDirectArray().Add(lNormalXNeg);
lGeometryElementNormal->GetDirectArray().Add(lNormalXNeg);
lGeometryElementNormal->GetDirectArray().Add(lNormalYPos);
lGeometryElementNormal->GetDirectArray().Add(lNormalYPos);
lGeometryElementNormal->GetDirectArray().Add(lNormalYPos);
lGeometryElementNormal->GetDirectArray().Add(lNormalYPos);
lGeometryElementNormal->GetDirectArray().Add(lNormalYNeg);
lGeometryElementNormal->GetDirectArray().Add(lNormalYNeg);
lGeometryElementNormal->GetDirectArray().Add(lNormalYNeg);
lGeometryElementNormal->GetDirectArray().Add(lNormalYNeg);
}
else
{
lGeometryElementNormal->SetReferenceMode(KFbxGeometryElement::eINDEX_TO_DIRECT);
lGeometryElementNormal->GetDirectArray().Add(lNormalZPos);
lGeometryElementNormal->GetDirectArray().Add(lNormalXPos);
lGeometryElementNormal->GetDirectArray().Add(lNormalZNeg);
lGeometryElementNormal->GetDirectArray().Add(lNormalXNeg);
lGeometryElementNormal->GetDirectArray().Add(lNormalYPos);
lGeometryElementNormal->GetDirectArray().Add(lNormalYNeg);
lGeometryElementNormal->GetIndexArray().Add(0);
lGeometryElementNormal->GetIndexArray().Add(0);
lGeometryElementNormal->GetIndexArray().Add(0);
lGeometryElementNormal->GetIndexArray().Add(0);
lGeometryElementNormal->GetIndexArray().Add(1);
lGeometryElementNormal->GetIndexArray().Add(1);
lGeometryElementNormal->GetIndexArray().Add(1);
lGeometryElementNormal->GetIndexArray().Add(1);
lGeometryElementNormal->GetIndexArray().Add(2);
lGeometryElementNormal->GetIndexArray().Add(2);
lGeometryElementNormal->GetIndexArray().Add(2);
lGeometryElementNormal->GetIndexArray().Add(2);
lGeometryElementNormal->GetIndexArray().Add(3);
lGeometryElementNormal->GetIndexArray().Add(3);
lGeometryElementNormal->GetIndexArray().Add(3);
lGeometryElementNormal->GetIndexArray().Add(3);
lGeometryElementNormal->GetIndexArray().Add(4);
lGeometryElementNormal->GetIndexArray().Add(4);
lGeometryElementNormal->GetIndexArray().Add(4);
lGeometryElementNormal->GetIndexArray().Add(4);
lGeometryElementNormal->GetIndexArray().Add(5);
lGeometryElementNormal->GetIndexArray().Add(5);
lGeometryElementNormal->GetIndexArray().Add(5);
lGeometryElementNormal->GetIndexArray().Add(5);
}
int lPolygonVertices[] = { 0, 1, 2, 3,
4, 5, 6, 7,
8, 9, 10, 11,
12, 13, 14, 15,
16, 17, 18, 19,
20, 21, 22, 23 };
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);
KFbxGeometryElementUV* lUVAmbientElement = lMesh->CreateElementUV("AmbientUV");
lUVAmbientElement->SetMappingMode(KFbxGeometryElement::eBY_POLYGON_VERTEX);
lUVAmbientElement->SetReferenceMode(KFbxGeometryElement::eINDEX_TO_DIRECT);
lVectors0.Set(0, 0);
lVectors1.Set(1, 0);
lVectors2.Set(0, 0.418586879968643);
lVectors3.Set(1, 0.418586879968643);
lUVAmbientElement->GetDirectArray().Add(lVectors0);
lUVAmbientElement->GetDirectArray().Add(lVectors1);
lUVAmbientElement->GetDirectArray().Add(lVectors2);
lUVAmbientElement->GetDirectArray().Add(lVectors3);
KFbxGeometryElementUV* lUVEmissiveElement = lMesh->CreateElementUV("EmissiveUV");
lUVEmissiveElement->SetMappingMode(KFbxGeometryElement::eBY_POLYGON_VERTEX);
lUVEmissiveElement->SetReferenceMode(KFbxGeometryElement::eINDEX_TO_DIRECT);
lVectors0.Set(0.2343, 0);
lVectors1.Set(1, 0.555);
lVectors2.Set(0.333, 0.999);
lVectors3.Set(0.555, 0.666);
lUVEmissiveElement->GetDirectArray().Add(lVectors0);
lUVEmissiveElement->GetDirectArray().Add(lVectors1);
lUVEmissiveElement->GetDirectArray().Add(lVectors2);
lUVEmissiveElement->GetDirectArray().Add(lVectors3);
lUVDiffuseElement->GetIndexArray().SetCount(24);
lUVAmbientElement->GetIndexArray().SetCount(24);
lUVEmissiveElement->GetIndexArray().SetCount(24);
for(i = 0; i < 6; i++)
{
lMesh->BeginPolygon(-1, -1, false);
for(j = 0; j < 4; j++)
{
lMesh->AddPolygon(lPolygonVertices[i*4 + j]
);
lUVDiffuseElement->GetIndexArray().SetAt(i*4+j, j);
lUVAmbientElement->GetIndexArray().SetAt(i*4+j, j);
lUVEmissiveElement->GetIndexArray().SetAt(i*4+j, j);
}
lMesh->EndPolygon ();
}
KFbxNode* lNode = KFbxNode::Create(pScene,pName);
lNode->SetNodeAttribute(lMesh);
lNode->SetShadingMode(KFbxNode::eTEXTURE_SHADING);
CreateTexture(pScene, lMesh);
return lNode;
}
KFbxNode* CreatePyramidWithMaterials(KFbxScene* pScene, char* pName)
{
int i, j;
KFbxMesh* lMesh = KFbxMesh::Create(pScene, pName);
KFbxVector4 lControlPoint0(-50, 0, 50);
KFbxVector4 lControlPoint1(50, 0, 50);
KFbxVector4 lControlPoint2(50, 0, -50);
KFbxVector4 lControlPoint3(-50, 0, -50);
KFbxVector4 lControlPoint4(0, 100, 0);
KFbxVector4 lNormalP0(0, 1, 0);
KFbxVector4 lNormalP1(0, 0.447, 0.894);
KFbxVector4 lNormalP2(0.894, 0.447, 0);
KFbxVector4 lNormalP3(0, 0.447, -0.894);
KFbxVector4 lNormalP4(-0.894, 0.447, 0);
lMesh->InitControlPoints(16);
KFbxVector4* lControlPoints = lMesh->GetControlPoints();
lControlPoints[0] = lControlPoint0;
lControlPoints[1] = lControlPoint1;
lControlPoints[2] = lControlPoint2;
lControlPoints[3] = lControlPoint3;
lControlPoints[4] = lControlPoint0;
lControlPoints[5] = lControlPoint1;
lControlPoints[6] = lControlPoint4;
lControlPoints[7] = lControlPoint1;
lControlPoints[8] = lControlPoint2;
lControlPoints[9] = lControlPoint4;
lControlPoints[10] = lControlPoint2;
lControlPoints[11] = lControlPoint3;
lControlPoints[12] = lControlPoint4;
lControlPoints[13] = lControlPoint3;
lControlPoints[14] = lControlPoint0;
lControlPoints[15] = lControlPoint4;
KFbxGeometryElementNormal* lNormalElement= lMesh->CreateElementNormal();
lNormalElement->SetMappingMode(KFbxGeometryElement::eBY_CONTROL_POINT);
lNormalElement->SetReferenceMode(KFbxGeometryElement::eDIRECT);
lNormalElement->GetDirectArray().Add(lNormalP0);
lNormalElement->GetDirectArray().Add(lNormalP0);
lNormalElement->GetDirectArray().Add(lNormalP0);
lNormalElement->GetDirectArray().Add(lNormalP0);
lNormalElement->GetDirectArray().Add(lNormalP1);
lNormalElement->GetDirectArray().Add(lNormalP1);
lNormalElement->GetDirectArray().Add(lNormalP1);
lNormalElement->GetDirectArray().Add(lNormalP2);
lNormalElement->GetDirectArray().Add(lNormalP2);
lNormalElement->GetDirectArray().Add(lNormalP2);
lNormalElement->GetDirectArray().Add(lNormalP3);
lNormalElement->GetDirectArray().Add(lNormalP3);
lNormalElement->GetDirectArray().Add(lNormalP3);
lNormalElement->GetDirectArray().Add(lNormalP4);
lNormalElement->GetDirectArray().Add(lNormalP4);
lNormalElement->GetDirectArray().Add(lNormalP4);
int lPolygonVertices[] = { 0, 3, 2, 1,
4, 5, 6,
7, 8, 9,
10, 11, 12,
13, 14, 15 };
KFbxGeometryElementMaterial* lMaterialElement = lMesh->CreateElementMaterial();
lMaterialElement->SetMappingMode(KFbxGeometryElement::eBY_POLYGON);
lMaterialElement->SetReferenceMode(KFbxGeometryElement::eINDEX_TO_DIRECT);
lMesh->BeginPolygon(0);
for(j = 0; j < 4; j++)
{
lMesh->AddPolygon(lPolygonVertices[j]);
}
lMesh->EndPolygon ();
for(i = 1; i < 5; i++)
{
lMesh->BeginPolygon(i);
for(j = 0; j < 3; j++)
{
lMesh->AddPolygon(lPolygonVertices[4 + 3*(i - 1) + j]);
}
lMesh->EndPolygon ();
}
KFbxNode* lNode = KFbxNode::Create(pScene,pName);
lNode->SetNodeAttribute(lMesh);
CreateMaterials(pScene, lMesh);
return lNode;
}
KFbxNode* CreateTriangle(KFbxScene* pScene, char* pName)
{
KFbxMesh* lMesh = KFbxMesh::Create(pScene, pName);
KFbxVector4 lControlPoint0(-50, 0, 50);
KFbxVector4 lControlPoint1(50, 0, 50);
KFbxVector4 lControlPoint2(0, 50, -50);
lMesh->InitControlPoints(3);
KFbxVector4* lControlPoints = lMesh->GetControlPoints();
lControlPoints[0] = lControlPoint0;
lControlPoints[1] = lControlPoint1;
lControlPoints[2] = lControlPoint2;
lMesh->BeginPolygon();
lMesh->AddPolygon(0);
lMesh->AddPolygon(1);
lMesh->AddPolygon(2);
lMesh->EndPolygon();
KFbxNode* lNode = KFbxNode::Create(pScene,pName);
lNode->SetNodeAttribute(lMesh);
return lNode;
}
KFbxNode* CreateCubeWithMaterialAndMyKFbxMesh(KFbxScene* pScene, char* pName)
{
int i, j;
MyKFbxMesh* lMyKFbxMesh = MyKFbxMesh::Create(pScene,pName);
fbxDouble3 lVector3(0.1, 0.2, 0.3);
fbxDouble4 lVector4(0.1, 0.2, 0.3, 0.4);
fbxDouble4 lVector41(1.1, 1.2, 1.3, 1.4);
fbxDouble4 lVector42(2.1, 2.2, 2.3, 2.4);
fbxDouble4 lVector43(3.1, 3.2, 3.3, 3.4);
fbxDouble44 lMatrix(lVector4,lVector41,lVector42,lVector43);
KFbxColor lGreen(0.0, 0.0, 1.0);
KTime lTime(333);
KString lString = "My Property 5 Value";
lMyKFbxMesh->GetProperty((int) MyKFbxMesh::eMY_PROPERTY1).Set(true);
lMyKFbxMesh->GetProperty((int) MyKFbxMesh::eMY_PROPERTY2).Set((int) 1);
lMyKFbxMesh->GetProperty((int) MyKFbxMesh::eMY_PROPERTY3).Set((float)2.2);
lMyKFbxMesh->GetProperty((int) MyKFbxMesh::eMY_PROPERTY4).Set((double)3.3);
lMyKFbxMesh->GetProperty((int) MyKFbxMesh::eMY_PROPERTY5).Set(lString);
lMyKFbxMesh->GetProperty((int) MyKFbxMesh::eMY_PROPERTY6).Set(lVector3);
lMyKFbxMesh->GetProperty((int) MyKFbxMesh::eMY_PROPERTY6).Set(lGreen);
lMyKFbxMesh->GetProperty((int) MyKFbxMesh::eMY_PROPERTY8).Set(lVector4);
lMyKFbxMesh->GetProperty((int) MyKFbxMesh::eMY_PROPERTY9).Set(lMatrix);
lMyKFbxMesh->GetProperty((int) MyKFbxMesh::eMY_PROPERTY10).Set(3);
lMyKFbxMesh->GetProperty((int) MyKFbxMesh::eMY_PROPERTY10).AddEnumValue("AAA");
lMyKFbxMesh->GetProperty((int) MyKFbxMesh::eMY_PROPERTY10).AddEnumValue("BBB");
lMyKFbxMesh->GetProperty((int) MyKFbxMesh::eMY_PROPERTY10).AddEnumValue("CCC");
lMyKFbxMesh->GetProperty((int) MyKFbxMesh::eMY_PROPERTY10).AddEnumValue("DDD");
lMyKFbxMesh->GetProperty((int) MyKFbxMesh::eMY_PROPERTY11).Set(lTime);
KFbxVector4 lControlPoint0(-25, 0, 25);
KFbxVector4 lControlPoint1(25, 0, 25);
KFbxVector4 lControlPoint2(25, 50, 25);
KFbxVector4 lControlPoint3(-25, 50, 25);
KFbxVector4 lControlPoint4(-25, 0, -25);
KFbxVector4 lControlPoint5(25, 0, -25);
KFbxVector4 lControlPoint6(25, 50, -25);
KFbxVector4 lControlPoint7(-25, 50, -25);
KFbxVector4 lNormalXPos(1, 0, 0);
KFbxVector4 lNormalXNeg(-1, 0, 0);
KFbxVector4 lNormalYPos(0, 1, 0);
KFbxVector4 lNormalYNeg(0, -1, 0);
KFbxVector4 lNormalZPos(0, 0, 1);
KFbxVector4 lNormalZNeg(0, 0, -1);
lMyKFbxMesh->InitControlPoints(24);
KFbxVector4* lControlPoints = lMyKFbxMesh->GetControlPoints();
lControlPoints[0] = lControlPoint0;
lControlPoints[1] = lControlPoint1;
lControlPoints[2] = lControlPoint2;
lControlPoints[3] = lControlPoint3;
lControlPoints[4] = lControlPoint1;
lControlPoints[5] = lControlPoint5;
lControlPoints[6] = lControlPoint6;
lControlPoints[7] = lControlPoint2;
lControlPoints[8] = lControlPoint5;
lControlPoints[9] = lControlPoint4;
lControlPoints[10] = lControlPoint7;
lControlPoints[11] = lControlPoint6;
lControlPoints[12] = lControlPoint4;
lControlPoints[13] = lControlPoint0;
lControlPoints[14] = lControlPoint3;
lControlPoints[15] = lControlPoint7;
lControlPoints[16] = lControlPoint3;
lControlPoints[17] = lControlPoint2;
lControlPoints[18] = lControlPoint6;
lControlPoints[19] = lControlPoint7;
lControlPoints[20] = lControlPoint1;
lControlPoints[21] = lControlPoint0;
lControlPoints[22] = lControlPoint4;
lControlPoints[23] = lControlPoint5;
KFbxGeometryElementNormal* lGeometryElementNormal = lMyKFbxMesh->CreateElementNormal();
lGeometryElementNormal->SetMappingMode(KFbxGeometryElement::eBY_CONTROL_POINT);
lGeometryElementNormal->SetReferenceMode(KFbxGeometryElement::eINDEX_TO_DIRECT);
lGeometryElementNormal->GetDirectArray().Add(lNormalZPos);
lGeometryElementNormal->GetDirectArray().Add(lNormalXPos);
lGeometryElementNormal->GetDirectArray().Add(lNormalZNeg);
lGeometryElementNormal->GetDirectArray().Add(lNormalXNeg);
lGeometryElementNormal->GetDirectArray().Add(lNormalYPos);
lGeometryElementNormal->GetDirectArray().Add(lNormalYNeg);
lGeometryElementNormal->GetIndexArray().Add(0);
lGeometryElementNormal->GetIndexArray().Add(0);
lGeometryElementNormal->GetIndexArray().Add(0);
lGeometryElementNormal->GetIndexArray().Add(0);
lGeometryElementNormal->GetIndexArray().Add(1);
lGeometryElementNormal->GetIndexArray().Add(1);
lGeometryElementNormal->GetIndexArray().Add(1);
lGeometryElementNormal->GetIndexArray().Add(1);
lGeometryElementNormal->GetIndexArray().Add(2);
lGeometryElementNormal->GetIndexArray().Add(2);
lGeometryElementNormal->GetIndexArray().Add(2);
lGeometryElementNormal->GetIndexArray().Add(2);
lGeometryElementNormal->GetIndexArray().Add(3);
lGeometryElementNormal->GetIndexArray().Add(3);
lGeometryElementNormal->GetIndexArray().Add(3);
lGeometryElementNormal->GetIndexArray().Add(3);
lGeometryElementNormal->GetIndexArray().Add(4);
lGeometryElementNormal->GetIndexArray().Add(4);
lGeometryElementNormal->GetIndexArray().Add(4);
lGeometryElementNormal->GetIndexArray().Add(4);
lGeometryElementNormal->GetIndexArray().Add(5);
lGeometryElementNormal->GetIndexArray().Add(5);
lGeometryElementNormal->GetIndexArray().Add(5);
lGeometryElementNormal->GetIndexArray().Add(5);
int lPolygonVertices[] = { 0, 1, 2, 3,
4, 5, 6, 7,
8, 9, 10, 11,
12, 13, 14, 15,
16, 17, 18, 19,
20, 21, 22, 23 };
KFbxGeometryElementMaterial* lMaterialElement = lMyKFbxMesh->CreateElementMaterial();
lMaterialElement->SetMappingMode(KFbxGeometryElement::eBY_POLYGON);
lMaterialElement->SetReferenceMode(KFbxGeometryElement::eINDEX_TO_DIRECT);
KFbxGeometryElementUV* lUVElement = lMyKFbxMesh->CreateElementUV( "");
K_ASSERT( lUVElement != NULL);
lUVElement->SetMappingMode(KFbxGeometryElement::eBY_POLYGON_VERTEX);
lUVElement->SetReferenceMode(KFbxGeometryElement::eINDEX_TO_DIRECT);
KFbxVector2 lVectors0(0, 0);
KFbxVector2 lVectors1(1, 0);
KFbxVector2 lVectors2(1, 1);
KFbxVector2 lVectors3(0, 1);
lUVElement->GetDirectArray().Add(lVectors0);
lUVElement->GetDirectArray().Add(lVectors1);
lUVElement->GetDirectArray().Add(lVectors2);
lUVElement->GetDirectArray().Add(lVectors3);
for(i = 0; i < 6; i++)
{
lMyKFbxMesh->BeginPolygon(i);
for(j = 0; j < 4; j++)
{
lMyKFbxMesh->AddPolygon(lPolygonVertices[i*4 + j],
j);
}
lMyKFbxMesh->EndPolygon ();
}
KArrayTemplate<KFbxDataType> lArrayType;
KArrayTemplate<const char*> lArrayNames;
lArrayType.Add(DTFloat);
lArrayNames.Add("My Float");
lArrayType.Add(DTBool);
lArrayNames.Add("My Bool");
KFbxGeometryElementUserData* lKFbxGeometryElementUserData = KFbxGeometryElementUserData::Create(lMyKFbxMesh, "My Geometry Element",PID_MY_GEOMETRY_LEMENT,lArrayType, lArrayNames);
lKFbxGeometryElementUserData->SetMappingMode(KFbxGeometryElement::eBY_POLYGON_VERTEX);
lKFbxGeometryElementUserData->ResizeAllDirectArrays(lMyKFbxMesh->GetPolygonVertexCount());
KFbxLayerElementArrayTemplate<void*>* directArrayF = lKFbxGeometryElementUserData->GetDirectArrayVoid("My Float");
float *lDirectArrayFloat = NULL;
lDirectArrayFloat = directArrayF->GetLocked(lDirectArrayFloat);
KFbxLayerElementArrayTemplate<void*>* directArrayB = lKFbxGeometryElementUserData->GetDirectArrayVoid("My Bool");
bool *lDirectArrayBool = NULL;
directArrayB->GetLocked(lDirectArrayBool);
for(i=0; i<lMyKFbxMesh->GetPolygonVertexCount(); ++i)
{
if(lDirectArrayFloat)
lDirectArrayFloat[i]=(float)(i+0.5);
if(lDirectArrayBool)
lDirectArrayBool[i]= (i%2==0);
}
directArrayF->Release((void**)&lDirectArrayFloat);
directArrayB->Release((void**)&lDirectArrayBool);
KFbxNode* lNode = KFbxNode::Create(pScene,pName);
lNode->SetNodeAttribute(lMyKFbxMesh);
lNode->SetShadingMode(KFbxNode::eTEXTURE_SHADING);
CreateMaterialsWithMyKFbxMesh(pScene, lMyKFbxMesh);
return lNode;
}
void CreateTexture(KFbxScene* pScene, KFbxMesh* pMesh)
{
KFbxSurfacePhong* lMaterial = NULL;
KFbxNode* lNode = pMesh->GetNode();
if(lNode)
{
lMaterial = lNode->GetSrcObject<KFbxSurfacePhong>(0);
if (lMaterial == NULL)
{
KString lMaterialName = "toto";
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);
lMaterial = KFbxSurfacePhong::Create(pScene, lMaterialName.Buffer());
lMaterial->Emissive .Set(lBlack);
lMaterial->Ambient .Set(lRed);
lMaterial->AmbientFactor .Set(1.);
lMaterial->Diffuse .Set(lDiffuseColor);
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);
lNode->AddMaterial(lMaterial);
}
}
KFbxFileTexture* lTexture = KFbxFileTexture::Create(pScene,"Diffuse Texture");
lTexture->SetFileName("scene03.jpg");
lTexture->SetTextureUse(KFbxTexture::eSTANDARD);
lTexture->SetMappingType(KFbxTexture::eUV);
lTexture->SetMaterialUse(KFbxFileTexture::eMODEL_MATERIAL);
lTexture->SetSwapUV(false);
lTexture->SetTranslation(0.0, 0.0);
lTexture->SetScale(1.0, 1.0);
lTexture->SetRotation(0.0, 0.0);
if (lMaterial)
lMaterial->Diffuse.ConnectSrcObject(lTexture);
lTexture = KFbxFileTexture::Create(pScene,"Ambient Texture");
lTexture->SetFileName("gradient.jpg");
lTexture->SetTextureUse(KFbxTexture::eSTANDARD);
lTexture->SetMappingType(KFbxTexture::eUV);
lTexture->SetMaterialUse(KFbxFileTexture::eMODEL_MATERIAL);
lTexture->SetSwapUV(false);
lTexture->SetTranslation(0.0, 0.0);
lTexture->SetScale(1.0, 1.0);
lTexture->SetRotation(0.0, 0.0);
if (lMaterial)
lMaterial->Ambient.ConnectSrcObject(lTexture);
lTexture = KFbxFileTexture::Create(pScene,"Emissive Texture");
lTexture->SetFileName("spotty.jpg");
lTexture->SetTextureUse(KFbxTexture::eSTANDARD);
lTexture->SetMappingType(KFbxTexture::eUV);
lTexture->SetMaterialUse(KFbxFileTexture::eMODEL_MATERIAL);
lTexture->SetSwapUV(false);
lTexture->SetTranslation(0.0, 0.0);
lTexture->SetScale(1.0, 1.0);
lTexture->SetRotation(0.0, 0.0);
if (lMaterial)
lMaterial->Emissive.ConnectSrcObject(lTexture);
}
void CreateMaterials(KFbxScene* pScene, KFbxMesh* pMesh)
{
int i;
for (i = 0; i < 5; i++ )
{
KString lMaterialName = "material";
KString lShadingName = "Phong";
lMaterialName += i;
fbxDouble3 lBlack(0.0, 0.0, 0.0);
fbxDouble3 lRed(1.0, 0.0, 0.0);
fbxDouble3 lColor;
KFbxSurfacePhong *lMaterial = KFbxSurfacePhong::Create(pScene, lMaterialName.Buffer());
lMaterial->Emissive.Set(lBlack);
lMaterial->Ambient.Set(lRed);
lColor = fbxDouble3(i > 2 ? 1.0 : 0.0,
i > 0 && i < 4 ? 1.0 : 0.0,
i % 2 ? 0.0 : 1.0);
lMaterial->Diffuse.Set(lColor);
lMaterial->TransparencyFactor.Set(0.0);
lMaterial->ShadingModel.Set(lShadingName);
lMaterial->Shininess.Set(0.5);
KFbxNode* lNode = pMesh->GetNode();
if(lNode)
lNode->AddMaterial(lMaterial);
}
}
void CreateMaterialsWithMyKFbxMesh(KFbxScene* pScene, MyKFbxMesh* pMyKFbxMesh)
{
int i;
for (i = 0; i != 6; ++i )
{
KString lMaterialName = "material";
KString lShadingModelName = i%2==0 ? "Lambert" : "Phong";
lMaterialName += i;
fbxDouble3 lBlack(0.0, 0.0, 0.0);
fbxDouble3 lRed(1.0, 0.0, 0.0);
fbxDouble3 lColor;
KFbxSurfaceLambert *lMaterial = KFbxSurfaceLambert::Create(pScene, lMaterialName.Buffer());
lMaterial->Emissive.Set(lBlack);
lMaterial->Ambient.Set(lRed);
lColor = fbxDouble3(i > 2 ? 1.0 : 0.0,
i > 0 && i < 4 ? 1.0 : 0.0,
i % 2 ? 0.0 : 1.0);
lMaterial->Diffuse.Set(lColor);
lMaterial->TransparencyFactor.Set(0.0);
lMaterial->ShadingModel.Set(lShadingModelName);
KFbxNode* lNode = pMyKFbxMesh->GetNode();
if(lNode)
lNode->AddMaterial(lMaterial);
}
}
void MapShapeOnPyramid(KFbxScene* pScene, KFbxNode* pPyramid)
{
KFbxShape* lShape = KFbxShape::Create(pScene,"Upside Down");
KFbxVector4 lControlPoint0(-50, 100, 50);
KFbxVector4 lControlPoint1(50, 100, 50);
KFbxVector4 lControlPoint2(50, 100, -50);
KFbxVector4 lControlPoint3(-50, 100, -50);
KFbxVector4 lControlPoint4(0, 0, 0);
KFbxVector4 lNormalP0(0, 1, 0);
KFbxVector4 lNormalP1(0, -0.447, 0.894);
KFbxVector4 lNormalP2(0.894, -0.447, 0);
KFbxVector4 lNormalP3(0, -0.447, -0.894);
KFbxVector4 lNormalP4(-0.894, -0.447, 0);
lShape->InitControlPoints(16);
KFbxVector4* lControlPoints = lShape->GetControlPoints();
lControlPoints[0] = lControlPoint0;
lControlPoints[1] = lControlPoint1;
lControlPoints[2] = lControlPoint2;
lControlPoints[3] = lControlPoint3;
lControlPoints[4] = lControlPoint0;
lControlPoints[5] = lControlPoint1;
lControlPoints[6] = lControlPoint4;
lControlPoints[7] = lControlPoint1;
lControlPoints[8] = lControlPoint2;
lControlPoints[9] = lControlPoint4;
lControlPoints[10] = lControlPoint2;
lControlPoints[11] = lControlPoint3;
lControlPoints[12] = lControlPoint4;
lControlPoints[13] = lControlPoint3;
lControlPoints[14] = lControlPoint0;
lControlPoints[15] = lControlPoint4;
KFbxGeometryElementNormal* lNormalElement = lShape->CreateElementNormal();
lNormalElement->SetMappingMode(KFbxGeometryElement::eBY_CONTROL_POINT);
lNormalElement->SetReferenceMode(KFbxGeometryElement::eDIRECT);
lNormalElement->GetDirectArray().Add(lNormalP0);
lNormalElement->GetDirectArray().Add(lNormalP0);
lNormalElement->GetDirectArray().Add(lNormalP0);
lNormalElement->GetDirectArray().Add(lNormalP0);
lNormalElement->GetDirectArray().Add(lNormalP1);
lNormalElement->GetDirectArray().Add(lNormalP1);
lNormalElement->GetDirectArray().Add(lNormalP1);
lNormalElement->GetDirectArray().Add(lNormalP2);
lNormalElement->GetDirectArray().Add(lNormalP2);
lNormalElement->GetDirectArray().Add(lNormalP2);
lNormalElement->GetDirectArray().Add(lNormalP3);
lNormalElement->GetDirectArray().Add(lNormalP3);
lNormalElement->GetDirectArray().Add(lNormalP3);
lNormalElement->GetDirectArray().Add(lNormalP4);
lNormalElement->GetDirectArray().Add(lNormalP4);
lNormalElement->GetDirectArray().Add(lNormalP4);
KFbxBlendShape* lBlendShape = KFbxBlendShape::Create(pScene,"");
KFbxBlendShapeChannel* lBlendShapeChannel = KFbxBlendShapeChannel::Create(pScene, "");
pPyramid->GetMesh()->AddDeformer(lBlendShape);
lBlendShape->AddBlendShapeChannel(lBlendShapeChannel);
lBlendShapeChannel->AddTargetShape(lShape);
}
void MapVertexCacheOnTriangle(KFbxScene* pScene, KFbxNode* pTriangle, char* pSampleFileName)
{
KString lFBXAbsolutePath = KFbxFullPath(pSampleFileName);
KString lFPCAbsoluteDirectory;
lFPCAbsoluteDirectory = KFbxExtractDirectory(lFBXAbsolutePath);
lFPCAbsoluteDirectory += "/";
lFPCAbsoluteDirectory += KFbxExtractFileName(pSampleFileName, false);
lFPCAbsoluteDirectory += "_fpc";
lFPCAbsoluteDirectory = KFbxCleanPath(lFPCAbsoluteDirectory);
KString lAbsolutePCFileName = lFPCAbsoluteDirectory + KString("/") + pTriangle->GetName();
lAbsolutePCFileName += gExportVertexCacheMCFormat ? ".xml" : ".pc2";
KString lRelativePCFileName = KFbxGetRelativeFilePath(KFbxExtractDirectory(lFBXAbsolutePath)+"/", lAbsolutePCFileName);
if (!KFbxEnsureDirectoryExistance(lAbsolutePCFileName))
{
return;
}
KFbxCache* lCache = KFbxCache::Create(pScene, pTriangle->GetName());
lCache->SetCacheFileName(lRelativePCFileName, lAbsolutePCFileName);
lCache->SetCacheFileFormat(gExportVertexCacheMCFormat ? KFbxCache::eMC : KFbxCache::ePC2);
KFbxVertexCacheDeformer* lDeformer = KFbxVertexCacheDeformer::Create(pScene, pTriangle->GetName());
lDeformer->SetCache(lCache);
lDeformer->SetCacheChannel(pTriangle->GetName());
lDeformer->SetActive(true);
pTriangle->GetGeometry()->AddDeformer(lDeformer);
}
void SetCubeDefaultPosition(KFbxNode* pCube)
{
pCube->LclTranslation.Set(KFbxVector4(-75.0, -50.0, 0.0));
pCube->LclRotation.Set(KFbxVector4(0.0, 0.0, 0.0));
pCube->LclScaling.Set(KFbxVector4(1.0, 1.0, 1.0));
}
void SetPyramidDefaultPosition(KFbxNode* pPyramid)
{
pPyramid->LclTranslation.Set(KFbxVector4(75.0, -50.0, 0.0));
pPyramid->LclRotation.Set(KFbxVector4(0.0, 0.0, 0.0));
pPyramid->LclScaling.Set(KFbxVector4(1.0, 1.0, 1.0));
}
void SetTriangleDefaultPosition(KFbxNode* pTriangle)
{
pTriangle->LclTranslation.Set(KFbxVector4(200.0, -50.0, 0.0));
pTriangle->LclRotation.Set(KFbxVector4(0.0, 0.0, 0.0));
pTriangle->LclScaling.Set(KFbxVector4(1.0, 1.0, 1.0));
}
void SetMyKFbxMeshCubeDefaultPosition(KFbxNode* pMyKFbxCube)
{
pMyKFbxCube->LclTranslation.Set(KFbxVector4(-200.0, -25.0, 0.0));
pMyKFbxCube->LclRotation.Set(KFbxVector4(0.0, 0.0, 0.0));
pMyKFbxCube->LclScaling.Set(KFbxVector4(1.0, 1.0, 1.0));
}
void Animate(KFbxNode* pNode, KFbxAnimLayer* pAnimLayer)
{
KFbxAnimCurve* lCurve = NULL;
KTime lTime;
int lKeyIndex = 0;
lCurve = pNode->LclRotation.GetCurve<KFbxAnimCurve>(pAnimLayer, KFCURVENODE_R_Y, true);
if (lCurve)
{
lCurve->KeyModifyBegin();
lTime.SetSecondDouble(0.0);
lKeyIndex = lCurve->KeyAdd(lTime);
lCurve->KeySetValue(lKeyIndex, 0.0);
lCurve->KeySetInterpolation(lKeyIndex, KFbxAnimCurveDef::eINTERPOLATION_CUBIC);
lTime.SetSecondDouble(0.5);
lKeyIndex = lCurve->KeyAdd(lTime);
lCurve->KeySetValue(lKeyIndex, 90.0);
lCurve->KeySetInterpolation(lKeyIndex, KFbxAnimCurveDef::eINTERPOLATION_CUBIC);
lTime.SetSecondDouble(1.0);
lKeyIndex = lCurve->KeyAdd(lTime);
lCurve->KeySetValue(lKeyIndex, 180.0);
lCurve->KeySetInterpolation(lKeyIndex, KFbxAnimCurveDef::eINTERPOLATION_CUBIC);
lTime.SetSecondDouble(1.5);
lKeyIndex = lCurve->KeyAdd(lTime);
lCurve->KeySetValue(lKeyIndex, 270.0);
lCurve->KeySetInterpolation(lKeyIndex, KFbxAnimCurveDef::eINTERPOLATION_CUBIC);
lTime.SetSecondDouble(2.0);
lKeyIndex = lCurve->KeyAdd(lTime);
lCurve->KeySetValue(lKeyIndex, 360.0);
lCurve->KeySetInterpolation(lKeyIndex, KFbxAnimCurveDef::eINTERPOLATION_CUBIC);
lCurve->KeyModifyEnd();
}
lCurve = pNode->LclRotation.GetCurve<KFbxAnimCurve>(pAnimLayer, KFCURVENODE_R_X, true);
if (lCurve)
{
lCurve->KeyModifyBegin();
lTime.SetSecondDouble(2.0);
lKeyIndex = lCurve->KeyAdd(lTime);
lCurve->KeySetValue(lKeyIndex, 0.0);
lCurve->KeySetInterpolation(lKeyIndex, KFbxAnimCurveDef::eINTERPOLATION_CUBIC);
lTime.SetSecondDouble(2.5);
lKeyIndex = lCurve->KeyAdd(lTime);
lCurve->KeySetValue(lKeyIndex, 90.0);
lCurve->KeySetInterpolation(lKeyIndex, KFbxAnimCurveDef::eINTERPOLATION_CUBIC);
lTime.SetSecondDouble(3.5);
lKeyIndex = lCurve->KeyAdd(lTime);
lCurve->KeySetValue(lKeyIndex, -90.0);
lCurve->KeySetInterpolation(lKeyIndex, KFbxAnimCurveDef::eINTERPOLATION_CUBIC);
lTime.SetSecondDouble(4.0);
lKeyIndex = lCurve->KeyAdd(lTime);
lCurve->KeySetValue(lKeyIndex, 0.0);
lCurve->KeySetInterpolation(lKeyIndex, KFbxAnimCurveDef::eINTERPOLATION_CUBIC);
lCurve->KeyModifyEnd();
}
KFbxGeometry* lGeometry = (KFbxGeometry*) pNode->GetNodeAttribute();
lCurve = lGeometry->GetShapeChannel(0, 0, pAnimLayer, true);
if (lCurve)
{
lCurve->KeyModifyBegin();
lTime.SetSecondDouble(0.0);
lKeyIndex = lCurve->KeyAdd(lTime);
lCurve->KeySetValue(lKeyIndex, 0.0);
lCurve->KeySetInterpolation(lKeyIndex, KFbxAnimCurveDef::eINTERPOLATION_CUBIC);
lTime.SetSecondDouble(2.0);
lKeyIndex = lCurve->KeyAdd(lTime);
lCurve->KeySetValue(lKeyIndex, 100.0);
lCurve->KeySetInterpolation(lKeyIndex, KFbxAnimCurveDef::eINTERPOLATION_CUBIC);
lTime.SetSecondDouble(4.0);
lKeyIndex = lCurve->KeyAdd(lTime);
lCurve->KeySetValue(lKeyIndex, 0.0);
lCurve->KeySetInterpolation(lKeyIndex, KFbxAnimCurveDef::eINTERPOLATION_CUBIC);
lCurve->KeyModifyEnd();
}
}
void AnimateVertexCacheOnTriangleDoubleVertex(KFbxNode* pTriangle, double pFrameRate)
{
KFbxVertexCacheDeformer* lDeformer = static_cast<KFbxVertexCacheDeformer*>(pTriangle->GetGeometry()->GetDeformer(0, KFbxDeformer::eVERTEX_CACHE));
KFbxCache* lCache = lDeformer->GetCache();
bool lRet;
KTime lTimeIncrement, lCurrentTime, lStopTime;
lTimeIncrement.SetTime(0, 0, 0, 1);
lStopTime.SetTime(0, 0, 4);
unsigned int lFrameCount = (unsigned int)(lStopTime.Get()/lTimeIncrement.Get());
if (gExportVertexCacheMCFormat)
{
lRet = lCache->OpenFileForWrite(KFbxCache::eMC_ONE_FILE, pFrameRate, pTriangle->GetName());
}
else
{
lRet = lCache->OpenFileForWrite(0.0, pFrameRate, lFrameCount, 3);
}
if (!lRet)
{
printf("File open error: %s\n", lCache->GetError().GetLastErrorString());
return;
}
int lChannelIndex = lCache->GetChannelIndex(pTriangle->GetName());
unsigned int lCurrentFrame = 0;
while (lCurrentTime <= lStopTime)
{
double lVertices[3][3];
double lScaleFactor = 1.0-double(lCurrentTime.GetSecondDouble()/lStopTime.GetSecondDouble());
lVertices[0][0] = -50.0 * lScaleFactor;
lVertices[0][1] = 0.0;
lVertices[0][2] = 50.0 * lScaleFactor;
lVertices[1][0] = 50.0 * lScaleFactor;
lVertices[1][1] = 0.0;
lVertices[1][2] = 50.0 * lScaleFactor;
lVertices[2][0] = 0.0 * lScaleFactor;
lVertices[2][1] = 50.0 * lScaleFactor;
lVertices[2][2] = -50.0 * lScaleFactor;
if (gExportVertexCacheMCFormat)
{
lCache->Write(lChannelIndex, lCurrentTime, &lVertices[0][0], 3);
}
else
{
lCache->Write(lCurrentFrame, &lVertices[0][0]);
}
lCurrentTime += lTimeIncrement;
lCurrentFrame++;
}
if (!lCache->CloseFile())
{
printf("File open error: %s\n", lCache->GetError().GetLastErrorString());
}
}
void AnimateVertexCacheOnTriangleInt32(KFbxNode* pTriangle, double pFrameRate)
{
KFbxVertexCacheDeformer* lDeformer = static_cast<KFbxVertexCacheDeformer*>(pTriangle->GetGeometry()->GetDeformer(0, KFbxDeformer::eVERTEX_CACHE));
KFbxCache* lCache = lDeformer->GetCache();
bool lRet = false;
KTime lTimeIncrement, lCurrentTime, lStopTime;
lTimeIncrement.SetTime(0, 0, 0, 1);
lStopTime.SetTime(0, 0, 4);
unsigned int lFrameCount = (unsigned int)(lStopTime.Get()/lTimeIncrement.Get());
if (gExportVertexCacheMCFormat)
{
lRet = lCache->OpenFileForWrite(KFbxCache::eMC_ONE_FILE, pFrameRate, pTriangle->GetName(), KFbxCache::kInt32Array);
}
if (!lRet)
{
printf("File open error: %s\n", lCache->GetError().GetLastErrorString());
return;
}
int lChannelIndex = lCache->GetChannelIndex(pTriangle->GetName());
unsigned int lCurrentFrame = 0;
while (lCurrentTime <= lStopTime)
{
int v[2];
v[0] = -10 + lCurrentFrame;
v[1] = v[0]+1;
if (gExportVertexCacheMCFormat)
{
lCache->Write(lChannelIndex, lCurrentTime, &v[0], 2);
}
lCurrentTime += lTimeIncrement;
lCurrentFrame++;
}
if (!lCache->CloseFile())
{
printf("File open error: %s\n", lCache->GetError().GetLastErrorString());
}
if (gExportVertexCacheMCFormat)
{
lRet = lCache->OpenFileForRead();
}
if (!lRet)
{
printf("File open error: %s\n", lCache->GetError().GetLastErrorString());
return;
}
KTime lCurrentTime2;
lCurrentFrame = 0;
printf("Testing awCache int32 array read and write\n");
bool passTest = true;
while (lCurrentTime2 <= lStopTime)
{
int v[2];
if (gExportVertexCacheMCFormat)
{
lCache->Read(lChannelIndex, lCurrentTime2, &v[0], 2);
if ((v[0] != -10 + lCurrentFrame) || (v[0]+1 != v[1]) )
{
printf("awCache int32 array read/write mismatch\n");
passTest = false;
break;
}
}
lCurrentTime2 += lTimeIncrement;
lCurrentFrame++;
}
if (!lCache->CloseFile())
{
printf("File open error: %s\n", lCache->GetError().GetLastErrorString());
}
if (passTest)
{
;
printf("awCache int32 array read and write test passed\n");
}
}
void AnimateVertexCacheOnTriangleFloat(KFbxNode* pTriangle, double pFrameRate)
{
KFbxVertexCacheDeformer* lDeformer = static_cast<KFbxVertexCacheDeformer*>(pTriangle->GetGeometry()->GetDeformer(0, KFbxDeformer::eVERTEX_CACHE));
KFbxCache* lCache = lDeformer->GetCache();
bool lRet;
KTime lTimeIncrement, lCurrentTime, lStopTime;
lTimeIncrement.SetTime(0, 0, 0, 1);
lStopTime.SetTime(0, 0, 4);
unsigned int lFrameCount = (unsigned int)(lStopTime.Get()/lTimeIncrement.Get());
if (gExportVertexCacheMCFormat)
{
lRet = lCache->OpenFileForWrite(KFbxCache::eMC_ONE_FILE, pFrameRate, pTriangle->GetName(), KFbxCache::kFloatVectorArray);
}
else
{
lRet = false;
}
if (!lRet)
{
printf("File open error: %s\n", lCache->GetError().GetLastErrorString());
return;
}
int lChannelIndex = lCache->GetChannelIndex(pTriangle->GetName());
unsigned int lCurrentFrame = 0;
while (lCurrentTime <= lStopTime)
{
float lVertices[3][3];
float lScaleFactor = 1.0f-float(lCurrentTime.GetSecondDouble()/lStopTime.GetSecondDouble());
lVertices[0][0] = -50.0f * lScaleFactor;
lVertices[0][1] = 0.0f;
lVertices[0][2] = 50.0f * lScaleFactor;
lVertices[1][0] = 50.0f * lScaleFactor;
lVertices[1][1] = 0.0f;
lVertices[1][2] = 50.0f * lScaleFactor;
lVertices[2][0] = 0.0f * lScaleFactor;
lVertices[2][1] = 50.0f * lScaleFactor;
lVertices[2][2] = -50.0f * lScaleFactor;
if (gExportVertexCacheMCFormat)
{
lCache->Write(lChannelIndex, lCurrentTime, &lVertices[0][0], 3);
}
lCurrentTime += lTimeIncrement;
lCurrentFrame++;
}
if (!lCache->CloseFile())
{
printf("File open error: %s\n", lCache->GetError().GetLastErrorString());
}
if (gExportVertexCacheMCFormat)
{
lRet = lCache->OpenFileForRead();
}
if (!lRet)
{
printf("File open error: %s\n", lCache->GetError().GetLastErrorString());
return;
}
KTime lCurrentTime2;
lCurrentFrame = 0;
#define AbsFlt(a) (((a) < 0) ? -(a) : (a))
#define CmpFlt(a,b) (AbsFlt((a)-(b)) > 1e-5)
printf("Testing awCache Float3 array read and write\n");
bool passTest = true;
while (lCurrentTime2 <= lStopTime)
{
float lVertices[3][3];
float lScaleFactor = 1.0f-float(lCurrentTime2.GetSecondDouble()/lStopTime.GetSecondDouble());
if (gExportVertexCacheMCFormat)
{
lCache->Read(lChannelIndex, lCurrentTime2, &lVertices[0][0], 3);
if ((CmpFlt(lVertices[0][0], -50.0f * lScaleFactor) || CmpFlt(lVertices[0][1], 0.0f ) || CmpFlt(lVertices[0][2], 50.0f * lScaleFactor)) ||
(CmpFlt(lVertices[1][0], 50.0f * lScaleFactor) || CmpFlt(lVertices[1][1], 0.0f ) || CmpFlt(lVertices[1][2], 50.0f * lScaleFactor)) ||
(CmpFlt(lVertices[2][0], 0.0f * lScaleFactor) || CmpFlt(lVertices[2][1], 50.0f * lScaleFactor) || CmpFlt(lVertices[2][2],-50.0f * lScaleFactor)))
{
printf("awCache Float3 array read/write mismatch\n");
passTest = false;
break;
}
}
lCurrentTime2 += lTimeIncrement;
lCurrentFrame++;
}
#undef AbsFlt
#undef CmpFlt
if (!lCache->CloseFile())
{
printf("File open error: %s\n", lCache->GetError().GetLastErrorString());
}
if (passTest)
{
printf("awCache float3 array read and write test passed\n");
}
}