SwitchBinding/main.cxx

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

 Copyright (C) 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. 

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

// Switch binding from one cylinder to the other one.
// Please check out the animation to see difference.
// Illustrates how to:
// 1. Get skin deformer and cluster;
// 2. Detach skin;
// 3. Bind and create corresponding bindpose;
//
// Steps:
//  1. Initialize FBX SDK Manager and FBX Scene
//  2. Load the input file to scene
//  3. Access the two cylinders
//  4. Get the skin deformer and all clusters from the first cylinder
//  5. Remove clusters, skin deformer and bind pose from the first cylinder
//  6. Move joints to proper position for cylinder2
//  7. Update clusters, skin deformer and create a bind pose for the second cylinder
//  8. Save the scene to output file
//  9. Destroy the FBX SDK Manager and FBX Scene

#include <fbxsdk.h>
#include "../Common/Common.h"

const char * SAMPLE_FILENAME_BEFORE_SWITCH = "Bind_Before_Switch.fbx";
const char * SAMPLE_FILENAME_AFTER_SWITCH = "Bind_After_Switch.fbx";

void SwitchBinding( KFbxScene* pScene );

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

    // Prepare the FBX SDK.
    InitializeSdkObjects(lSdkManager, lScene);  
    // Load the scene. 
    if ( LoadScene(lSdkManager, lScene, SAMPLE_FILENAME_BEFORE_SWITCH) )
    {
        //Switch binding from the first cylinder to the second one.
        SwitchBinding( lScene );
        //Save the scene after switching binding.
        SaveScene(lSdkManager, lScene, SAMPLE_FILENAME_AFTER_SWITCH);
    }

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

void SwitchBinding(KFbxScene * pScene)
{
    //Get the two cylinders.
    KFbxMesh* lCylinder01 = (KFbxMesh*) pScene->GetSrcObject(FBX_TYPE(KFbxMesh), 0);
    KFbxMesh* lCylinder02 = (KFbxMesh*) pScene->GetSrcObject(FBX_TYPE(KFbxMesh), 1);

    //Get the skin deformer, which will be switched from the first cylinder to the the second one.
    KFbxSkin* lSkin = (KFbxSkin *) lCylinder01->GetDeformer(0, KFbxDeformer::eSKIN);
    
    // Get all clusters through the skin deformer, later they will be re-used for the second cylinder.
    KArrayTemplate<KFbxCluster*> lClusterArray;
    for (int lClusterIndex = 0; lClusterIndex < lSkin->GetClusterCount(); ++lClusterIndex)
    {
        KFbxCluster* lCluster=lSkin->GetCluster(lClusterIndex);
        lClusterArray.Add(lCluster);
    }

    // Detach the first cylinder.
    // Remove clusters from the skin deformer.
    for (int lClusterIndex = 0; lClusterIndex < lClusterArray.GetCount(); ++lClusterIndex)
        lSkin->RemoveCluster(lClusterArray[lClusterIndex]);
    // Remove the skin deformer from the first cylinder.
    // Currently, the only deformer connected to lCylinder01 is the skin deformer.
    lCylinder01->RemoveDeformer(0);
    // Remove the corresponding bindpose.
    pScene->RemovePose(0) ;

    // Bind the second cylinder and create the corresponding bind pose.
    // Move the joints into the second cylinder. Only need to move the root joint.
    lClusterArray[0]->GetLink()->LclTranslation.Set(fbxDouble3 ( -11.0322688253132,5.3883395780739,0) );

    // Prepare an array to collect nodes for bind pose, which is combined of the geometry and joints. 
    KArrayTemplate<KFbxNode*> lPoseNodeArray;

    // Add the second cylinder to node array of bind pose.
    lPoseNodeArray.Add(lCylinder02->GetNode());

    // Set proper transformation for each cluster and add them to the skin deformer.
    // ThansformMatrix is the global transformation of the mesh when the binding happens.
    // TransformLinkMatrix is the global transformation of the joint(Link) when the binding happens.
    KFbxXMatrix lTransformMatrix, lTransformLinkMatrix;
    lTransformMatrix = lCylinder02->GetNode()->EvaluateGlobalTransform();
    for(int lClusterIndex = 0; lClusterIndex < lClusterArray.GetCount(); lClusterIndex++)
    {
        // All joints have the same TransformMatrix.
        lClusterArray[lClusterIndex]->SetTransformMatrix(lTransformMatrix);

        // Compute global transformation of each joint and set it as TransformLinkMatrix.
        lTransformLinkMatrix = lClusterArray[lClusterIndex]->GetLink()->EvaluateGlobalTransform();
        lClusterArray[lClusterIndex]->SetTransformLinkMatrix(lTransformLinkMatrix);

        // Add cluster to the skin deformer.
        lSkin->AddCluster(lClusterArray[lClusterIndex]);

        // Add each joint(Link) to node array of bindpose.
        lPoseNodeArray.Add(lClusterArray[lClusterIndex]->GetLink());
    }

    // Add the skin deformer to the second cylinder.
    lCylinder02->AddDeformer(lSkin);

    // Create a pose by the node array and set it as bindpose.
    KFbxPose* lPose = KFbxPose::Create(pScene,lCylinder02->GetNode()->GetName());
    lPose->SetIsBindPose(true);
    for(int lPoseNodeIndex = 0; lPoseNodeIndex<lPoseNodeArray.GetCount(); ++lPoseNodeIndex)
    {
        KFbxMatrix lBindMatrix = lPoseNodeArray[lPoseNodeIndex]->EvaluateGlobalTransform();
        lPose->Add(lPoseNodeArray[lPoseNodeIndex], lBindMatrix);
    }

    // Add the bindpose to the scene.
    pScene->AddPose(lPose);
}