#include <fbxsdk.h>
#include "../Common/Common.h"
#define SAMPLE_FILENAME "Layers.fbx"
#define BACKGROUND_IMAGE_NAME "Spotty"
#define BACKGROUND_IMAGE "spotty.jpg"
#define GEO1_IMAGE_NAME "One"
#define GEO1_IMAGE "1.jpg"
#define GEO2_IMAGE_NAME "Waffle"
#define GEO2_IMAGE "waffle.jpg"
typedef double Vector4[4];
typedef double Vector2[2];
KFbxNode* CreateCube(KFbxScene* pScene, char* pName);
KFbxTexture* CreateTexture(KFbxScene* pScene, char* name, char* filename);
int main(int argc, char** argv)
{
KFbxSdkManager* lSdkManager = NULL;
KFbxScene* lScene = NULL;
bool lResult;
InitializeSdkObjects(lSdkManager, lScene);
KFbxNode* lCube = CreateCube(lScene, "Cube");
KFbxNode* lRootNode = lScene->GetRootNode();
lRootNode->AddChild(lCube);
if(argc > 1)
{
lResult = SaveScene(lSdkManager, lScene, argv[1]);
}
else
{
lResult = SaveScene(lSdkManager, lScene, SAMPLE_FILENAME);
}
if(lResult == false)
{
printf("\n\nAn error occurred while saving the scene...\n");
DestroySdkObjects(lSdkManager);
return 0;
}
DestroySdkObjects(lSdkManager);
return 0;
}
KFbxNode* CreateCube(KFbxScene* pScene, char* pName)
{
static int vtxId[24] = {
0,1,2,3,
1,5,6,2,
5,4,7,6,
4,0,3,7,
0,4,5,1,
3,2,6,7
};
static Vector4 lControlPoints[8] = {
{ -50.0, 0.0, 50.0, 1.0}, { 50.0, 0.0, 50.0, 1.0}, { 50.0,100.0, 50.0, 1.0}, { -50.0,100.0, 50.0, 1.0},
{ -50.0, 0.0, -50.0, 1.0}, { 50.0, 0.0, -50.0, 1.0}, { 50.0,100.0, -50.0, 1.0}, { -50.0,100.0, -50.0, 1.0}
};
static Vector4 lNormals[8] = {
{-0.577350258827209,-0.577350258827209, 0.577350258827209, 1.0},
{ 0.577350258827209,-0.577350258827209, 0.577350258827209, 1.0},
{ 0.577350258827209, 0.577350258827209, 0.577350258827209, 1.0},
{-0.577350258827209, 0.577350258827209, 0.577350258827209, 1.0},
{-0.577350258827209,-0.577350258827209,-0.577350258827209, 1.0},
{ 0.577350258827209,-0.577350258827209,-0.577350258827209, 1.0},
{ 0.577350258827209, 0.577350258827209,-0.577350258827209, 1.0},
{-0.577350258827209, 0.577350258827209,-0.577350258827209, 1.0}
};
static Vector2 lUVs0[14] = {
{ 0.0, 0.0},
{ 1.0, 0.0},
{ 0.0, 1.0},
{ 1.0, 1.0},
{ 0.0, 2.0},
{ 1.0, 2.0},
{ 0.0, 3.0},
{ 1.0, 3.0},
{ 0.0, 4.0},
{ 1.0, 4.0},
{ 2.0, 0.0},
{ 2.0, 1.0},
{-1.0, 0.0},
{-1.0, 1.0}
};
static Vector2 lUVs1[14] = {
{ 0.0, 1.0},
{ 1.0, 0.0},
{ 0.0, 0.0},
{ 1.0, 1.0}
};
static int uvsId[24] = {
0,1,3,2,2,3,5,4,4,5,7,6,6,7,9,8,1,10,11,3,12,0,2,13
};
static Vector4 lColors[8] = {
{1.0, 1.0, 1.0, 1.0},
{1.0, 1.0, 0.0, 1.0},
{1.0, 0.0, 1.0, 1.0},
{0.0, 1.0, 1.0, 1.0},
{0.0, 0.0, 1.0, 1.0},
{1.0, 0.0, 0.0, 1.0},
{0.0, 1.0, 0.0, 1.0},
{0.0, 0.0, 0.0, 1.0},
};
KFbxMesh* lMesh = KFbxMesh::Create(pScene,"");
lMesh->InitControlPoints(8);
KFbxVector4* vertex = lMesh->GetControlPoints();
memcpy((void*)vertex, (void*)lControlPoints, 8*sizeof(KFbxVector4));
KFbxLayerContainer* lLayerContainer = (KFbxLayerContainer*)lMesh;
KFbxGeometryElementMaterial* lMaterialElement = lMesh->CreateElementMaterial();
lMaterialElement->SetMappingMode(KFbxGeometryElement::eBY_POLYGON);
lMaterialElement->SetReferenceMode(KFbxGeometryElement::eINDEX_TO_DIRECT);
int vId = 0;
for (int f=0; f<6; f++)
{
lMesh->BeginPolygon(f);
for (int v=0; v<4; v++)
lMesh->AddPolygon(vtxId[vId++]);
lMesh->EndPolygon();
}
KFbxGeometryElementNormal* lNormalElement = lMesh->CreateElementNormal();
lNormalElement->SetMappingMode(KFbxGeometryElement::eBY_CONTROL_POINT);
lNormalElement->SetReferenceMode(KFbxGeometryElement::eDIRECT);
for (int n=0; n<8; n++)
lNormalElement->GetDirectArray().Add(KFbxVector4(lNormals[n][0], lNormals[n][1], lNormals[n][2]));
KFbxGeometryElementVertexColor* lVertexColorElement = lMesh->CreateElementVertexColor();
lVertexColorElement->SetMappingMode(KFbxGeometryElement::eBY_CONTROL_POINT);
lVertexColorElement->SetReferenceMode(KFbxGeometryElement::eDIRECT);
for (int c=0; c<8; c++)
lVertexColorElement->GetDirectArray().Add(KFbxColor(lColors[c][0]*.8, lColors[c][1]*0.8, lColors[c][2]*.8, lColors[c][3]*.8));
KFbxGeometryElementPolygonGroup* lPolygonGroupElement = lMesh->CreateElementPolygonGroup();
lPolygonGroupElement->SetMappingMode(KFbxGeometryElement::eBY_POLYGON);
lPolygonGroupElement->SetReferenceMode(KFbxGeometryElement::eINDEX);
lPolygonGroupElement->GetIndexArray().Add(0);
lPolygonGroupElement->GetIndexArray().Add(0);
lPolygonGroupElement->GetIndexArray().Add(0);
lPolygonGroupElement->GetIndexArray().Add(0);
lPolygonGroupElement->GetIndexArray().Add(1);
lPolygonGroupElement->GetIndexArray().Add(1);
KFbxTexture* lTexture[3];
KFbxLayeredTexture::EBlendMode lBlendMode[3];
KFbxGeometryElementUV* lUVElement0 = lMesh->CreateElementUV( BACKGROUND_IMAGE_NAME);
K_ASSERT( lUVElement0 != NULL);
lUVElement0->SetMappingMode(KFbxGeometryElement::eBY_POLYGON_VERTEX);
lUVElement0->SetReferenceMode(KFbxGeometryElement::eINDEX_TO_DIRECT);
int i;
for (i = 0; i<14; i++)
lUVElement0->GetDirectArray().Add(KFbxVector2(lUVs0[i][0], lUVs0[i][1]));
for (i = 0; i<24; i++)
lUVElement0->GetIndexArray().Add(uvsId[i]);
KFbxNode* lNode = KFbxNode::Create(pScene,pName);
lNode->SetNodeAttribute(lMesh);
lNode->SetShadingMode(KFbxNode::eTEXTURE_SHADING);
KFbxSurfacePhong* lMaterial[6];
for (i = 0; i < 6; i++ )
{
KString lMaterialName = "material";
lMaterialName += i;
lMaterial[i] = KFbxSurfacePhong::Create(pScene,lMaterialName.Buffer());
lMaterial[i]->Emissive.Set(fbxDouble3(0.0, 0.0, 0.0));
lMaterial[i]->Ambient.Set(fbxDouble3(lColors[i][0], lColors[i][1], lColors[i][2]));
lMaterial[i]->Diffuse.Set(fbxDouble3(1.0, 1.0, 1.0));
lMaterial[i]->Specular.Set(fbxDouble3(0.0, 0.0, 0.0));
lMaterial[i]->TransparencyFactor.Set(0.0);
lMaterial[i]->Shininess.Set(0.5);
lMaterial[i]->ShadingModel.Set(KString("phong"));
lNode->AddMaterial(lMaterial[i]);
}
lTexture[0] = CreateTexture(pScene, BACKGROUND_IMAGE_NAME, BACKGROUND_IMAGE);
lBlendMode[0] = KFbxLayeredTexture::eTRANSLUCENT;
KFbxGeometryElementUV* lUVElement1 = lMesh->CreateElementUV( GEO1_IMAGE_NAME);
K_ASSERT( lUVElement1 != NULL);
lUVElement1->SetMappingMode(KFbxGeometryElement::eBY_POLYGON_VERTEX);
lUVElement1->SetReferenceMode(KFbxGeometryElement::eINDEX_TO_DIRECT);
for (i = 0; i <4; i++)
lUVElement1->GetDirectArray().Add(KFbxVector2(lUVs1[i][0], lUVs1[i][1]));
for (i = 0; i<24; i++)
lUVElement1->GetIndexArray().Add(uvsId[i%4]);
lTexture[1] = CreateTexture(pScene, GEO1_IMAGE_NAME, GEO1_IMAGE);
lBlendMode[1] = KFbxLayeredTexture::eMODULATE;
KFbxGeometryElementUV* lUVElement2 = lMesh->CreateElementUV( GEO2_IMAGE_NAME);
K_ASSERT( lUVElement2 != NULL);
*lUVElement2 = *lUVElement0;
lTexture[2] = CreateTexture(pScene, GEO2_IMAGE_NAME, GEO2_IMAGE);
lBlendMode[2] = KFbxLayeredTexture::eMODULATE;
KFbxLayeredTexture* lLayeredTexture[2];
lLayeredTexture[0] = KFbxLayeredTexture::Create(pScene, "layeredTexture0");
lLayeredTexture[1] = KFbxLayeredTexture::Create(pScene, "layeredTexture1");
for (i = 0; i < 3; i++)
{
lLayeredTexture[0]->ConnectSrcObject(lTexture[i]);
lLayeredTexture[0]->SetTextureBlendMode(i, lBlendMode[i]);
if (i < 2)
{
lLayeredTexture[1]->ConnectSrcObject(lTexture[i]);
lLayeredTexture[1]->SetTextureBlendMode(i, lBlendMode[i]);
}
}
lMaterial[0]->Diffuse.ConnectSrcObject(lLayeredTexture[0]);
for (i = 1; i < 6; i++)
lMaterial[i]->Diffuse.ConnectSrcObject(lLayeredTexture[1]);
return lNode;
}
KFbxTexture* CreateTexture(KFbxScene* pScene, char* name, char* filename)
{
KFbxFileTexture* lTexture = KFbxFileTexture::Create(pScene,name);
lTexture->SetFileName(filename);
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);
return lTexture;
}