ExportScene02/main.cxx

/**************************************************************************************

 Copyright (C) 2001 - 2010 Autodesk, Inc. and/or its licensors.
 All Rights Reserved.

 The coded instructions, statements, computer programs, and/or related material 
 (collectively the "Data") in these files contain unpublished information 
 proprietary to Autodesk, Inc. and/or its licensors, which is protected by 
 Canada and United States of America federal copyright law and by international 
 treaties. 
 
 The Data may not be disclosed or distributed to third parties, in whole or in
 part, without the prior written consent of Autodesk, Inc. ("Autodesk").

 THE DATA IS PROVIDED "AS IS" AND WITHOUT WARRANTY.
 ALL WARRANTIES ARE EXPRESSLY EXCLUDED AND DISCLAIMED. AUTODESK MAKES NO
 WARRANTY OF ANY KIND WITH RESPECT TO THE DATA, EXPRESS, IMPLIED OR ARISING
 BY CUSTOM OR TRADE USAGE, AND DISCLAIMS ANY IMPLIED WARRANTIES OF TITLE, 
 NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE OR USE. 
 WITHOUT LIMITING THE FOREGOING, AUTODESK DOES NOT WARRANT THAT THE OPERATION
 OF THE DATA WILL BE UNINTERRUPTED OR ERROR FREE. 
 
 IN NO EVENT SHALL AUTODESK, ITS AFFILIATES, PARENT COMPANIES, LICENSORS
 OR SUPPLIERS ("AUTODESK GROUP") BE LIABLE FOR ANY LOSSES, DAMAGES OR EXPENSES
 OF ANY KIND (INCLUDING WITHOUT LIMITATION PUNITIVE OR MULTIPLE DAMAGES OR OTHER
 SPECIAL, DIRECT, INDIRECT, EXEMPLARY, INCIDENTAL, LOSS OF PROFITS, REVENUE
 OR DATA, COST OF COVER OR CONSEQUENTIAL LOSSES OR DAMAGES OF ANY KIND),
 HOWEVER CAUSED, AND REGARDLESS OF THE THEORY OF LIABILITY, WHETHER DERIVED
 FROM CONTRACT, TORT (INCLUDING, BUT NOT LIMITED TO, NEGLIGENCE), OR OTHERWISE,
 ARISING OUT OF OR RELATING TO THE DATA OR ITS USE OR ANY OTHER PERFORMANCE,
 WHETHER OR NOT AUTODESK HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH LOSS
 OR DAMAGE. 

**************************************************************************************/

//
// The scene created in this example is a sphere morphed by 2 shapes. A 
// animation stack shows the influence of the shapes over the sphere.
//
// The example illustrates how to:
//        1) create a nurbs
//        2) map a shape over a nurbs
//        3) map a texture over a nurbs on material channel Diffuse 
//           and Ambient
//        4) map a material over a nurbs
//        5) create an animation stack
//        6) export a scene in a .FBX file
//

#include <math.h>

#include <fbxsdk.h>

#include "../Common/Common.h"

#define SAMPLE_FILENAME "ExportScene02.fbx"


// Function prototypes.
bool CreateScene(KFbxScene* pScene);

KFbxNode* CreateNurbs(KFbxScene* pScene, char* pName);

void MapStretchedShape(KFbxScene* pScene, KFbxNode* pNurbs);
void MapBoxShape(KFbxScene* pScene, KFbxNode* pNurbs);
void MapShapesOnNurbs(KFbxScene* pScene, KFbxNode* pNurbs);
void MapTexture(KFbxScene* pScene, KFbxNode* pNurbs);
void MapMaterial(KFbxScene* pScene, KFbxNode* pNurbs);

void AnimateNurbs(KFbxNode* pNurbs, KFbxScene* pScene);


int main(int argc, char** argv)
{
    KFbxSdkManager* lSdkManager = NULL;
    KFbxScene* lScene = NULL;
    bool lResult;

    // Prepare the FBX SDK.
    InitializeSdkObjects(lSdkManager, lScene);

    // Create the scene.

    lResult = CreateScene(lScene);

    if(lResult == false)
    {
        printf("\n\nAn error occurred while creating the scene...\n");
        DestroySdkObjects(lSdkManager);
        return 0;
    }

    // Save the scene.

    // The example can take an output file name as an argument.
    if(argc > 1)
    {
        lResult = SaveScene(lSdkManager, lScene, argv[1]);
    }
    // A default output file name is given otherwise.
    else
    {
        lResult = SaveScene(lSdkManager, lScene, SAMPLE_FILENAME);
    }

    if(lResult == false)
    {
        printf("\n\nAn error occurred while saving the scene...\n");
        DestroySdkObjects(lSdkManager);
        return 0;
    }

    // Destroy all objects created by the FBX SDK.
    DestroySdkObjects(lSdkManager);

    return 0;
}

bool CreateScene(KFbxScene* pScene)
{
    KFbxNode* lNurbs = CreateNurbs(pScene, "Nurbs");

    MapShapesOnNurbs(pScene, lNurbs);
    MapMaterial(pScene, lNurbs);
    MapTexture(pScene, lNurbs);

    // Build the node tree.
    KFbxNode* lRootNode = pScene->GetRootNode();
    lRootNode->AddChild(lNurbs);

    AnimateNurbs(lNurbs, pScene);
    return true;
}

// Create a sphere. 
KFbxNode* CreateNurbs(KFbxScene* pScene, char* pName)
{
    KFbxNurb* lNurbs = KFbxNurb::Create(pScene,pName);

    // Set nurbs properties.
    lNurbs->SetOrder(4, 4);
    lNurbs->SetStep(2, 2);
    lNurbs->InitControlPoints(8, KFbxNurb::ePERIODIC, 7, KFbxNurb::eOPEN);

    double lUKnotVector[] = { -3.0, -2.0, -1.0, 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0 };
    memcpy(lNurbs->GetUKnotVector(), lUKnotVector, lNurbs->GetUKnotCount()*sizeof(double));

    double lVKnotVector[] = { 0.0, 0.0, 0.0, 0.0, 1.0, 2.0, 3.0, 4.0, 4.0, 4.0, 4.0 };
    memcpy(lNurbs->GetVKnotVector(), lVKnotVector, lNurbs->GetVKnotCount()*sizeof(double));

    KFbxVector4* lVector4 = lNurbs->GetControlPoints();
    int i, j;
    double lScale = 20.0;
    double lPi = 3.14159;
    double lYAngle[] = { 90.0, 90.0, 52.0, 0.0, -52.0, -90.0, -90.0 };
    double lRadius[] = { 0.0, 0.283, 0.872, 1.226, 0.872, 0.283, 0.0}; 

    for (i = 0; i < 7; i++) 
    {
        for (j = 0; j < 8; j++)
        {
            double lX = lScale * lRadius[i] * cos(lPi/4*j);
            double lY = lScale * sin(2*lPi/360*lYAngle[i]);
            double lZ = lScale * lRadius[i] * sin(lPi/4*j);
            double lWeight = 1.0;

            lVector4[8*i + j].Set(lX, lY, lZ, lWeight);
        }
    }


    KFbxNode* lNode = KFbxNode::Create(pScene,pName);

    lNode->SetNodeAttribute(lNurbs);

    return lNode;
}

// Map nurbs control points onto a stretched shape.
void MapStretchedShape(KFbxScene* pScene, KFbxBlendShapeChannel* lBlendShapeChannel)
{
    KFbxShape* lShape = KFbxShape::Create(pScene,"StretchedShape");

    KFbxVector4 lExtremeRight(-250.0, 0.0, 0.0);
    KFbxVector4 lExtremeLeft(250.0, 0.0, 0.0);

    lShape->InitControlPoints(8*7);

    KFbxVector4* lVector4 = lShape->GetControlPoints();

    int i, j;

    for (i = 0; i < 7; i++)
    {
        for (j = 0; j < 8; j++)
        {
            if (j < 3 || j > 6)
            {
                lVector4[8*i + j] = lExtremeLeft;
            }
            else
            {
                lVector4[8*i + j] = lExtremeRight;
            }
        }
    }

    lBlendShapeChannel->AddTargetShape(lShape);
}

// Map nurbs control points onto a box shape.
void MapBoxShape(KFbxScene* pScene, KFbxBlendShapeChannel* lBlendShapeChannel)
{
    KFbxShape* lShape = KFbxShape::Create(pScene,"BoxShape");

    lShape->InitControlPoints(8*7);

    KFbxVector4* lVector4 = lShape->GetControlPoints();

    int i, j;
    double lScale = 20.0;
    double lWeight = 1.0;
    double lX[] = { 0.9, 1.1, 0.0, -1.1, -0.9, -1.1, 0.0, 1.1 };
    double lZ[] = { 0.0, 1.1, 0.9, 1.1, 0.0, -1.1, -0.9, -1.1 };

    // Top control points.
    for (i = 0; i < 8; i++)
    {
        lVector4[i].Set(0.0, lScale, 0.0, lWeight);
    }

    // Middle control points.
    for (i = 1; i < 6; i++)
    {
        double lY = 1.0 - 0.5 * (i - 1);

        for (j = 0; j < 8; j++)
        {
            lVector4[8*i + j].Set(lScale*lX[j], lScale*lY, lScale*lZ[j], lWeight);
        }
    }

    // Bottom control points.
    for (i = 48; i < 56; i++)
    {
        lVector4[i].Set(0.0, -lScale, 0.0, lWeight);
    }

    lBlendShapeChannel->AddTargetShape(lShape);
}

void MapShapesOnNurbs(KFbxScene* pScene, KFbxNode* pNurbs)
{
    KFbxBlendShape* lBlendShape = KFbxBlendShape::Create(pScene,"MyBlendShape");
    KFbxBlendShapeChannel* lBlendShapeChannel01 = KFbxBlendShapeChannel::Create(pScene,"MyBlendShapeChannel01");
    KFbxBlendShapeChannel* lBlendShapeChannel02 = KFbxBlendShapeChannel::Create(pScene,"MyBlendShapeChannel02");

    //Create and add two target shapes on the lBlendShapeChannel.
    MapStretchedShape(pScene, lBlendShapeChannel01);
    MapBoxShape(pScene, lBlendShapeChannel01);  
    
    MapBoxShape(pScene, lBlendShapeChannel02);  

    //Set the lBlendShapeChannel on lBlendShape.
    lBlendShape->AddBlendShapeChannel(lBlendShapeChannel01);
    lBlendShape->AddBlendShapeChannel(lBlendShapeChannel02);

    //Set the lBlendShape on pNurbs.
    KFbxGeometry* lGeometry = pNurbs->GetGeometry();
    lGeometry->AddDeformer(lBlendShape);
};

// Map texture over sphere.
void MapTexture(KFbxScene* pScene, KFbxNode* pNurbs)
{
    KFbxFileTexture* lTexture = KFbxFileTexture::Create(pScene,"scene02.jpg");

    // The texture won't be displayed if node shading mode isn't set to KFbxNode::eTEXTURE_SHADING.
    pNurbs->SetShadingMode(KFbxNode::eTEXTURE_SHADING);

    // Set texture properties.
    lTexture->SetFileName("scene02.jpg"); // Resource file is in current directory.
    lTexture->SetTextureUse(KFbxTexture::eSTANDARD);
    lTexture->SetMappingType(KFbxTexture::eCYLINDRICAL);
    lTexture->SetMaterialUse(KFbxFileTexture::eMODEL_MATERIAL);
    lTexture->SetSwapUV(false);
    lTexture->SetTranslation(0.45, -0.05);
    lTexture->SetScale(4.0, 1.0);
    lTexture->SetRotation(0.0, 0.0);

    // we connect the texture to the material DiffuseColor property
    KFbxSurfacePhong* lMaterial = pNurbs->GetSrcObject<KFbxSurfacePhong>(0);
    if (lMaterial)
        lMaterial->Diffuse.ConnectSrcObject(lTexture);

    //now, we can try to map a texture on the AMBIENT channel of a material.

    //It is important to create a NEW texture and not to simply change the
    //properties of lTexture.

    //Set the Texture properties
    lTexture=KFbxFileTexture::Create(pScene, "grandient.jpg");
    lTexture->SetFileName("gradient.jpg");
    lTexture->SetTextureUse(KFbxTexture::eSTANDARD);
    lTexture->SetMappingType(KFbxTexture::eCYLINDRICAL);
    lTexture->SetMaterialUse(KFbxFileTexture::eMODEL_MATERIAL);
    lTexture->SetSwapUV(false);

    // we connect the texture to the material Ambient property
    if (lMaterial)
        lMaterial->Ambient.ConnectSrcObject(lTexture);

}

// Map material over sphere.
void MapMaterial(KFbxScene* pScene, KFbxNode* pNurbs)
{
    KFbxSurfacePhong* lMaterial = KFbxSurfacePhong::Create(pScene,"scene02");
    fbxDouble3 lBlue(0.0, 0.0, 1.0);
    fbxDouble3 lBlack(0.0, 0.0, 0.0);

    lMaterial->Emissive.Set(lBlue);
    lMaterial->Ambient.Set(lBlack);
    lMaterial->Specular.Set(lBlack);
    lMaterial->TransparencyFactor.Set(0.0);
    lMaterial->Shininess.Set(0.0);
    lMaterial->ReflectionFactor.Set(0.0);



    // Create GeometryElementMaterial
    KFbxNurb*                       lNurb                       = pNurbs->GetNurb();
    KFbxGeometryElementMaterial*    lGeometryElementMaterial    = lNurb->GetElementMaterial( 0);

    if (!lGeometryElementMaterial)
    {
        lGeometryElementMaterial = lNurb->CreateElementMaterial();
    }

    // The material is mapped to the whole Nurbs
    lGeometryElementMaterial->SetMappingMode(KFbxGeometryElement::eALL_SAME);

    // And the material is avalible in the Direct array
    lGeometryElementMaterial->SetReferenceMode(KFbxGeometryElement::eDIRECT);
    pNurbs->AddMaterial(lMaterial);
}

// Morph sphere into box shape.
void AnimateNurbs(KFbxNode* pNurbs, KFbxScene* pScene)
{
    KString lAnimStackName;
    KTime lTime;
    int lKeyIndex = 0;

    // First animation stack.
    lAnimStackName = "Morph sphere into box";
    KFbxAnimStack* lAnimStack = KFbxAnimStack::Create(pScene, lAnimStackName);

        // The animation nodes can only exist on AnimLayers therefore it is mandatory to
        // add at least one AnimLayer to the AnimStack. And for the purpose of this example,
        // one layer is all we need.
        KFbxAnimLayer* lAnimLayer = KFbxAnimLayer::Create(pScene, "Base Layer");
        lAnimStack->AddMember(lAnimLayer);

    KFbxGeometry* lNurbsAttribute = (KFbxGeometry*) pNurbs->GetNodeAttribute();

    // The stretched shape is at index 0 because it was added first to the nurbs.
    KFbxAnimCurve* lCurve = lNurbsAttribute->GetShapeChannel(0, 0, lAnimLayer, 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(1.0);
        lKeyIndex = lCurve->KeyAdd(lTime);
        lCurve->KeySetValue(lKeyIndex, 75.0);
        lCurve->KeySetInterpolation(lKeyIndex, KFbxAnimCurveDef::eINTERPOLATION_CUBIC);

        lTime.SetSecondDouble(1.25);
        lKeyIndex = lCurve->KeyAdd(lTime);
        lCurve->KeySetValue(lKeyIndex, 0.0);
        lCurve->KeySetInterpolation(lKeyIndex, KFbxAnimCurveDef::eINTERPOLATION_CUBIC);

        lCurve->KeyModifyEnd();
    }

    // The box shape is at index 1 because it was added second to the nurbs.
    lCurve = lNurbsAttribute->GetShapeChannel(0, 1, lAnimLayer, true);
    if (lCurve)
    {
        lCurve->KeyModifyBegin();

        lTime.SetSecondDouble(1.0);
        lKeyIndex = lCurve->KeyAdd(lTime);
        lCurve->KeySetValue(lKeyIndex, 0.0);
        lCurve->KeySetInterpolation(lKeyIndex, KFbxAnimCurveDef::eINTERPOLATION_CUBIC);

        lTime.SetSecondDouble(1.25);
        lKeyIndex = lCurve->KeyAdd(lTime);
        lCurve->KeySetValue(lKeyIndex, 100.0);
        lCurve->KeySetInterpolation(lKeyIndex, KFbxAnimCurveDef::eINTERPOLATION_CUBIC);

        lCurve->KeyModifyEnd();
    }
}