#include <math.h>
#include <fbxsdk.h>
#include "../Common/Common.h"
#define SAMPLE_FILENAME "ExportScene04.fbx"
bool CreateScene(FbxScene* pScene);
FbxNode* CreateLightGroup(FbxScene* pScene, char* pName);
FbxNode* CreateLight(FbxScene* pScene, char* pName);
FbxNode* CreateMarker(FbxScene* pScene, char* pName);
FbxNode* CreateCamera(FbxScene* pScene, char* pName);
void SetCameraPointOfInterest(FbxNode* pCamera, FbxNode* pPointOfInterest);
void SetLightGroupDefaultPosition(FbxNode* pLightGroup);
void SetLightDefaultPosition(FbxNode* pLight, int pIndex);
void SetMarkerDefaultPosition(FbxNode* pMarker);
void SetCamera1DefaultPosition(FbxNode* pCamera);
void SetCamera2DefaultPosition(FbxNode* pCamera);
void AnimateLightGroup(FbxNode* pLightGroup, FbxAnimLayer* pAnimLayer);
void AnimateLight(FbxNode* pLight, int pIndex, FbxAnimLayer* pAnimLayer);
void AnimateCamera(FbxNode* pLightGroup, FbxAnimLayer* pAnimLayer);
int main(int argc, char** argv)
{
FbxManager* lSdkManager = NULL;
FbxScene* lScene = NULL;
bool lResult;
InitializeSdkObjects(lSdkManager, lScene);
lResult = CreateScene(lScene);
if(lResult == false)
{
FBXSDK_printf("\n\nAn error occurred while creating the scene...\n");
DestroySdkObjects(lSdkManager);
return 0;
}
if(argc > 1)
{
lResult = SaveScene(lSdkManager, lScene, argv[1]);
}
else
{
lResult = SaveScene(lSdkManager, lScene, SAMPLE_FILENAME);
}
if(lResult == false)
{
FBXSDK_printf("\n\nAn error occurred while saving the scene...\n");
DestroySdkObjects(lSdkManager);
return 0;
}
DestroySdkObjects(lSdkManager);
return 0;
}
bool CreateScene(FbxScene* pScene)
{
FbxNode* lLightGroup = CreateLightGroup(pScene, "LightGroup");
FbxNode* lMarker = CreateMarker(pScene, "Marker");
FbxNode* lCamera1 = CreateCamera(pScene, "Camera1");
FbxNode* lCamera2 = CreateCamera(pScene, "Camera2");
pScene->GetGlobalSettings().SetAmbientColor(FbxColor(1.0, 0.5, 0.2));
SetCameraPointOfInterest(lCamera1, lMarker);
SetCameraPointOfInterest(lCamera2, lCamera1);
SetLightGroupDefaultPosition(lLightGroup);
SetMarkerDefaultPosition(lMarker);
SetCamera1DefaultPosition(lCamera1);
SetCamera2DefaultPosition(lCamera2);
FbxAnimStack* lAnimStack = FbxAnimStack::Create(pScene, "Rotating lights");
FbxAnimLayer* lAnimLayer = FbxAnimLayer::Create(pScene, "Base Layer");
lAnimStack->AddMember(lAnimLayer);
FbxNode* lRootNode = pScene->GetRootNode();
lRootNode->AddChild(lLightGroup);
lRootNode->AddChild(lMarker);
lRootNode->AddChild(lCamera1);
lCamera1->AddChild(lCamera2);
pScene->GetGlobalSettings().SetDefaultCamera(PRODUCER_PERSPECTIVE);
AnimateLightGroup(lLightGroup, lAnimLayer);
AnimateCamera(lCamera1, lAnimLayer);
return true;
}
FbxNode* CreateLightGroup(FbxScene* pScene, char* pName)
{
FbxString lLightName;
FbxNode* lGroup = NULL;
FbxNode* lNode = NULL;
FbxLight* lLight = NULL;
int i;
lGroup = FbxNode::Create(pScene,pName);
for(i = 0; i < 6; i++)
{
lLightName = pName;
lLightName += "-Light";
lLightName += i;
lNode = CreateLight(pScene, lLightName.Buffer());
lGroup->AddChild(lNode);
}
for (i = 0; i < 6; i++)
{
lLight = (FbxLight*) lGroup->GetChild(i)->GetNodeAttribute();
lLight->FileName.Set("gobo.tif");
lLight->DrawGroundProjection.Set(true);
lLight->DrawVolumetricLight.Set(true);
lLight->DrawFrontFacingVolumetricLight.Set(false);
}
return lGroup;
}
FbxNode* CreateLight(FbxScene* pScene, char* pName)
{
FbxLight* lLight = FbxLight::Create(pScene,pName);
lLight->LightType.Set(FbxLight::eSpot);
lLight->CastLight.Set(true);
FbxNode* lNode = FbxNode::Create(pScene,pName);
lNode->SetNodeAttribute(lLight);
return lNode;
}
FbxNode* CreateMarker(FbxScene* pScene, char* pName)
{
FbxMarker* lMarker = FbxMarker::Create(pScene,pName);
FbxNode* lNode = FbxNode::Create(pScene,pName);
lNode->SetNodeAttribute(lMarker);
return lNode;
}
FbxNode* CreateCamera(FbxScene* pScene, char* pName)
{
FbxCamera* lCamera = FbxCamera::Create(pScene,pName);
lCamera->SetApertureMode(FbxCamera::eVertical);
lCamera->SetApertureWidth(0.816);
lCamera->SetApertureHeight(0.612);
lCamera->SetSqueezeRatio(0.5);
FbxNode* lNode = FbxNode::Create(pScene,pName);
lNode->SetNodeAttribute(lCamera);
return lNode;
}
void SetCameraPointOfInterest(FbxNode* pCamera, FbxNode* pPointOfInterest)
{
pCamera->SetTarget(pPointOfInterest);
}
void SetLightGroupDefaultPosition(FbxNode* pLightGroup)
{
int i;
for (i = 0; i < pLightGroup->GetChildCount(); i++)
{
SetLightDefaultPosition(pLightGroup->GetChild(i), i);
}
pLightGroup->LclTranslation.Set(FbxVector4(0.0, 15.0, 0.0));
pLightGroup->LclRotation.Set(FbxVector4(0.0, 0.0, 0.0));
pLightGroup->LclScaling.Set(FbxVector4(1.0, 1.0, 1.0));
}
void SetLightDefaultPosition(FbxNode* pLight, int pIndex)
{
pLight->LclTranslation.Set(FbxVector4((cos((double)pIndex) * 40.0), 0.0, (sin((double)pIndex) * 40.0)));
pLight->LclRotation.Set(FbxVector4(20.0, (90.0 - pIndex * 60.0), 0.0));
pLight->LclScaling.Set(FbxVector4(1.0, 1.0, 1.0));
FbxDouble3 lColor[6] =
{
FbxDouble3(1.0, 0.0, 0.0),
FbxDouble3(1.0, 1.0, 0.0),
FbxDouble3(0.0, 1.0, 0.0),
FbxDouble3(0.0, 1.0, 1.0),
FbxDouble3(0.0, 0.0, 1.0),
FbxDouble3(1.0, 0.0, 1.0)
};
FbxLight* light = pLight->GetLight();
if (light)
{
light->Color.Set(lColor[pIndex % 6]);
light->Intensity.Set(33.0);
light->OuterAngle.Set(90.0);
light->Fog.Set(100.0);
}
}
void SetMarkerDefaultPosition(FbxNode* pMarker)
{
pMarker->LclTranslation.Set(FbxVector4(0.0, 0.0, 0.0));
pMarker->LclRotation.Set(FbxVector4(0.0, 0.0, 0.0));
pMarker->LclScaling.Set(FbxVector4(1.0, 1.0, 1.0));
}
void SetCamera1DefaultPosition(FbxNode* pCamera)
{
FbxVector4 lCameraLocation(0.0, 100.0, -300.0);
FbxVector4 lDefaultPointOfInterest(1.0, 100.0, -300.0);
FbxVector4 lNewPointOfInterest(0, 0, 0);
FbxVector4 lRotation;
FbxVector4 lScaling(1.0, 1.0, 1.0);
FbxVector4::AxisAlignmentInEulerAngle(lCameraLocation, lDefaultPointOfInterest, lNewPointOfInterest, lRotation);
pCamera->LclTranslation.Set(lCameraLocation);
pCamera->LclRotation.Set(lRotation);
pCamera->LclScaling.Set(lScaling);
}
void SetCamera2DefaultPosition(FbxNode* pCamera)
{
pCamera->LclTranslation.Set(FbxVector4(-150.0, 0.0, 75.0));
}
void AnimateLightGroup(FbxNode* pLightGroup, FbxAnimLayer* pAnimLayer)
{
FbxAnimCurve* lCurve = NULL;
FbxTime lTime;
int i;
int lKeyIndex = 0;
for (i = 0; i < pLightGroup->GetChildCount(); i++)
{
AnimateLight(pLightGroup->GetChild(i), i, pAnimLayer);
}
pLightGroup->LclRotation.GetCurveNode(pAnimLayer, true);
pLightGroup->LclTranslation.GetCurveNode(pAnimLayer, true);
lCurve = pLightGroup->LclRotation.GetCurve<FbxAnimCurve>(pAnimLayer, FBXSDK_CURVENODE_COMPONENT_Y, true);
if (lCurve)
{
lCurve->KeyModifyBegin();
lTime.SetSecondDouble(0.0);
lKeyIndex = lCurve->KeyAdd(lTime);
lCurve->KeySetValue(lKeyIndex, 0.0);
lCurve->KeySetInterpolation(lKeyIndex, FbxAnimCurveDef::eInterpolationLinear);
lTime.SetSecondDouble(10.0);
lKeyIndex = lCurve->KeyAdd(lTime);
lCurve->KeySetValue(lKeyIndex, 5*360.0);
lCurve->KeyModifyEnd();
}
lCurve = pLightGroup->LclTranslation.GetCurve<FbxAnimCurve>(pAnimLayer, FBXSDK_CURVENODE_COMPONENT_Y, true);
if (lCurve)
{
lCurve->KeyModifyBegin();
lTime.SetSecondDouble(0.0);
lKeyIndex = lCurve->KeyAdd(lTime);
lCurve->KeySetValue(lKeyIndex, 15.0);
lCurve->KeySetInterpolation(lKeyIndex, FbxAnimCurveDef::eInterpolationCubic);
lTime.SetSecondDouble(5.0);
lKeyIndex = lCurve->KeyAdd(lTime);
lCurve->KeySetValue(lKeyIndex, 200.0);
lCurve->KeyModifyEnd();
}
}
void AnimateLight(FbxNode* pLight, int pIndex, FbxAnimLayer* pAnimLayer)
{
FbxAnimCurve* lCurve = NULL;
FbxTime lTime;
int i, j;
int lKeyIndex = 0;
FbxLight* light = pLight->GetLight();
light->Intensity.GetCurveNode(pAnimLayer, true);
lCurve = light->Intensity.GetCurve<FbxAnimCurve>(pAnimLayer, true);
if (lCurve)
{
lCurve->KeyModifyBegin();
lTime.SetSecondDouble(0.0);
lKeyIndex = lCurve->KeyAdd(lTime);
lCurve->KeySetValue(lKeyIndex, 0.0);
lCurve->KeySetInterpolation(lKeyIndex, FbxAnimCurveDef::eInterpolationCubic);
lTime.SetSecondDouble(3.0);
lKeyIndex = lCurve->KeyAdd(lTime);
lCurve->KeySetValue(lKeyIndex, 33.0);
lCurve->KeySetInterpolation(lKeyIndex, FbxAnimCurveDef::eInterpolationLinear);
lTime.SetSecondDouble(7.0);
lKeyIndex = lCurve->KeyAdd(lTime);
lCurve->KeySetValue(lKeyIndex, 33.0);
lCurve->KeySetInterpolation(lKeyIndex, FbxAnimCurveDef::eInterpolationCubic);
lTime.SetSecondDouble(10.0);
lKeyIndex = lCurve->KeyAdd(lTime);
lCurve->KeySetValue(lKeyIndex, 0.0);
lCurve->KeyModifyEnd();
}
light->Fog.GetCurveNode(pAnimLayer, true);
lCurve = light->Fog.GetCurve<FbxAnimCurve>(pAnimLayer, true);
if (lCurve)
{
lCurve->KeyModifyBegin();
lTime.SetSecondDouble(0.0);
lKeyIndex = lCurve->KeyAdd(lTime);
lCurve->KeySetValue(lKeyIndex, 0.0);
lCurve->KeySetInterpolation(lKeyIndex, FbxAnimCurveDef::eInterpolationCubic);
lTime.SetSecondDouble(3.0);
lKeyIndex = lCurve->KeyAdd(lTime);
lCurve->KeySetValue(lKeyIndex, 33.0);
lCurve->KeySetInterpolation(lKeyIndex, FbxAnimCurveDef::eInterpolationLinear);
lTime.SetSecondDouble(7.0);
lKeyIndex = lCurve->KeyAdd(lTime);
lCurve->KeySetValue(lKeyIndex, 33.0);
lCurve->KeySetInterpolation(lKeyIndex, FbxAnimCurveDef::eInterpolationCubic);
lTime.SetSecondDouble(10.0);
lKeyIndex = lCurve->KeyAdd(lTime);
lCurve->KeySetValue(lKeyIndex, 0.0);
lCurve->KeyModifyEnd();
}
{
pLight->LclRotation.GetCurveNode(pAnimLayer, true);
light->OuterAngle.GetCurveNode(pAnimLayer, true);
lCurve = pLight->LclRotation.GetCurve<FbxAnimCurve>(pAnimLayer, FBXSDK_CURVENODE_COMPONENT_X, true);
FbxAnimCurve* lConeCurve = light->OuterAngle.GetCurve<FbxAnimCurve>(pAnimLayer,true);
double lValue;
lCurve->KeyModifyBegin();
lConeCurve->KeyModifyBegin();
for (i = 0; i < 8; i++)
{
lTime.SetSecondDouble((double)i * 0.833333);
lValue = cos((((double)i) + (((double)pIndex) * 60.0)) * 72.0);
lKeyIndex = lCurve->KeyAdd(lTime);
lCurve->KeySetValue(lKeyIndex, float((lValue - 0.4) * 30.0));
lCurve->KeySetInterpolation(lKeyIndex, FbxAnimCurveDef::eInterpolationCubic);
lKeyIndex = lConeCurve->KeyAdd(lTime);
lConeCurve->KeySetValue(lKeyIndex, float((2.0 - (lValue + 1.0)) * 45.0));
lConeCurve->KeySetInterpolation(lKeyIndex, FbxAnimCurveDef::eInterpolationLinear);
}
lTime.SetSecondDouble(10.0);
lKeyIndex = lCurve->KeyAdd(lTime);
lCurve->KeySetValue(lKeyIndex, -90.0);
lCurve->KeySetInterpolation(lKeyIndex, FbxAnimCurveDef::eInterpolationCubic);
lKeyIndex = lConeCurve->KeyAdd(lTime);
lConeCurve->KeySetValue(lKeyIndex, 180.0);
lConeCurve->KeySetInterpolation(lKeyIndex, FbxAnimCurveDef::eInterpolationLinear);
lCurve->KeyModifyEnd();
lConeCurve->KeyModifyEnd();
}
{
FbxDouble3 lColor[6] =
{
FbxDouble3(1.0, 0.0, 0.0),
FbxDouble3(1.0, 1.0, 0.0),
FbxDouble3(0.0, 1.0, 0.0),
FbxDouble3(0.0, 1.0, 1.0),
FbxDouble3(0.0, 0.0, 1.0),
FbxDouble3(1.0, 0.0, 1.0)
};
FbxAnimCurve* lCurve[3];
light->Color.GetCurveNode(pAnimLayer, true);
lCurve[0] = light->Color.GetCurve<FbxAnimCurve>(pAnimLayer,FBXSDK_CURVENODE_COLOR_RED, true);
lCurve[1] = light->Color.GetCurve<FbxAnimCurve>(pAnimLayer,FBXSDK_CURVENODE_COLOR_GREEN, true);
lCurve[2] = light->Color.GetCurve<FbxAnimCurve>(pAnimLayer,FBXSDK_CURVENODE_COLOR_BLUE, true);
if (lCurve[0] && lCurve[1] && lCurve[2])
{
lCurve[0]->KeyModifyBegin();
lCurve[1]->KeyModifyBegin();
lCurve[2]->KeyModifyBegin();
for (i = 0; i < 24; i++)
{
j = i + pIndex;
while (j > 5)
{
j -= 6;
}
lTime.SetSecondDouble((double)i * 0.4166666);
lKeyIndex = lCurve[0]->KeyAdd(lTime);
lCurve[0]->KeySetValue(lKeyIndex, (float)lColor[j][0]);
lCurve[0]->KeySetInterpolation(lKeyIndex, FbxAnimCurveDef::eInterpolationCubic);
lKeyIndex = lCurve[1]->KeyAdd(lTime);
lCurve[1]->KeySetValue(lKeyIndex, (float)lColor[j][1]);
lCurve[1]->KeySetInterpolation(lKeyIndex, FbxAnimCurveDef::eInterpolationCubic);
lKeyIndex = lCurve[2]->KeyAdd(lTime);
lCurve[2]->KeySetValue(lKeyIndex, (float)lColor[j][2]);
lCurve[2]->KeySetInterpolation(lKeyIndex, FbxAnimCurveDef::eInterpolationCubic);
}
lCurve[0]->KeyModifyEnd();
lCurve[1]->KeyModifyEnd();
lCurve[2]->KeyModifyEnd();
}
}
}
void AnimateCamera(FbxNode* pCamera, FbxAnimLayer* pAnimLayer)
{
FbxAnimCurve* lCurve = NULL;
FbxTime lTime;
int lKeyIndex = 0;
pCamera->LclTranslation.GetCurveNode(pAnimLayer, true);
lCurve = pCamera->LclTranslation.GetCurve<FbxAnimCurve>(pAnimLayer, FBXSDK_CURVENODE_COMPONENT_X, true);
if (lCurve)
{
lCurve->KeyModifyBegin();
lTime.SetSecondDouble(0.0);
lKeyIndex = lCurve->KeyAdd(lTime);
lCurve->KeySet(lKeyIndex, lTime, 0.0, FbxAnimCurveDef::eInterpolationLinear);
lTime.SetSecondDouble(10.0);
lKeyIndex = lCurve->KeyAdd(lTime);
lCurve->KeySet(lKeyIndex, lTime, 200.0);
lCurve->KeyModifyEnd();
}
lCurve = pCamera->LclTranslation.GetCurve<FbxAnimCurve>(pAnimLayer, FBXSDK_CURVENODE_COMPONENT_Y, true);
if (lCurve)
{
lCurve->KeyModifyBegin();
lTime.SetSecondDouble(0.0);
lKeyIndex = lCurve->KeyAdd(lTime);
lCurve->KeySet(lKeyIndex, lTime, 0.0, FbxAnimCurveDef::eInterpolationLinear);
lTime.SetSecondDouble(10.0);
lKeyIndex = lCurve->KeyAdd(lTime);
lCurve->KeySet(lKeyIndex, lTime, 300.0);
lCurve->KeyModifyEnd();
}
FbxCamera* cam = pCamera->GetCamera();
cam->Roll.GetCurveNode(pAnimLayer, true);
lCurve = cam->Roll.GetCurve<FbxAnimCurve>(pAnimLayer, true);
if (lCurve)
{
lCurve->KeyModifyBegin();
lTime.SetSecondDouble (0.0);
lKeyIndex = lCurve->KeyAdd(lTime);
lCurve->KeySet(lKeyIndex, lTime, 0.0, FbxAnimCurveDef::eInterpolationLinear);
lTime.SetSecondDouble(10.0);
lKeyIndex = lCurve->KeyAdd(lTime);
lCurve->KeySet(lKeyIndex, lTime, 2*360.0);
lCurve->KeyModifyEnd();
}
}