tools/toolpose/ortoolpose_tool.cxx

tools/toolpose/ortoolpose_tool.cxx
/***************************************************************************************
Autodesk(R) Open Reality(R) Samples
(C) 2009 Autodesk, Inc. and/or its licensors
All rights reserved.
AUTODESK SOFTWARE LICENSE AGREEMENT
Autodesk, Inc. licenses this Software to you only upon the condition that
you accept all of the terms contained in the Software License Agreement ("Agreement")
that is embedded in or that is delivered with this Software. By selecting
the "I ACCEPT" button at the end of the Agreement or by copying, installing,
uploading, accessing or using all or any portion of the Software you agree
to enter into the Agreement. A contract is then formed between Autodesk and
either you personally, if you acquire the Software for yourself, or the company
or other legal entity for which you are acquiring the software.
AUTODESK, INC., MAKES NO WARRANTY, EITHER EXPRESS OR IMPLIED, INCLUDING BUT
NOT LIMITED TO ANY IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR
PURPOSE REGARDING THESE MATERIALS, AND MAKES SUCH MATERIALS AVAILABLE SOLELY ON AN
"AS-IS" BASIS.
IN NO EVENT SHALL AUTODESK, INC., BE LIABLE TO ANYONE FOR SPECIAL, COLLATERAL,
INCIDENTAL, OR CONSEQUENTIAL DAMAGES IN CONNECTION WITH OR ARISING OUT OF PURCHASE
OR USE OF THESE MATERIALS. THE SOLE AND EXCLUSIVE LIABILITY TO AUTODESK, INC.,
REGARDLESS OF THE FORM OF ACTION, SHALL NOT EXCEED THE PURCHASE PRICE OF THE
MATERIALS DESCRIBED HEREIN.
Autodesk, Inc., reserves the right to revise and improve its products as it sees fit.
Autodesk and Open Reality are registered trademarks or trademarks of Autodesk, Inc.,
in the U.S.A. and/or other countries. All other brand names, product names, or
trademarks belong to their respective holders.
GOVERNMENT USE
Use, duplication, or disclosure by the U.S. Government is subject to restrictions as
set forth in FAR 12.212 (Commercial Computer Software-Restricted Rights) and
DFAR 227.7202 (Rights in Technical Data and Computer Software), as applicable.
Manufacturer is Autodesk, Inc., 10 Duke Street, Montreal, Quebec, Canada, H3C 2L7.
***************************************************************************************/
//--- Class declaration
#include "ortoolpose_tool.h"
//--- Registration defines
#define ORTOOLPOSE__CLASS ORTOOLPOSE__CLASSNAME
#define ORTOOLPOSE__LABEL "Pose"
#define ORTOOLPOSE__DESC "OR - Pose Description"
//--- FiLMBOX implementation and registration
FBToolImplementation( ORTOOLPOSE__CLASS );
FBRegisterTool ( ORTOOLPOSE__CLASS,
ORTOOLPOSE__LABEL,
ORTOOLPOSE__DESC,
FB_DEFAULT_SDK_ICON ); // Icon filename (default=Open Reality icon)
/************************************************
* Tool creation function.
************************************************/
bool ORToolPose::FBCreate()
{
StartSize[0] = 400;
StartSize[1] = 500;
// Create/reset/manage UI
UICreate ();
UIConfigure ();
EventRefresh( NULL, NULL );
return true;
}
/************************************************
* Tool destruction function.
************************************************/
void ORToolPose::FBDestroy()
{
}
/************************************************
* Create the UI (Assign regions & controls).
************************************************/
void ORToolPose::UICreate()
{
int lS = 10;
int lButtonW = 100;
int lButtonH = 18;
Region.X = 300;
Region.Y = 300;
Region.Width = 400;
Region.Height = 500;
AddRegion( "Tree", "Tree",
lS, kFBAttachLeft, "", 1.0,
lS, kFBAttachTop, "", 1.0,
250, kFBAttachNone, "", 1.0,
-lS, kFBAttachBottom, "", 1.0 );
AddRegion( "ButtonRefresh", "ButtonRefresh",
lS, kFBAttachRight, "Tree", 1.0,
lS, kFBAttachTop, "", 1.0,
lButtonW, kFBAttachNone, "", 1.0,
lButtonH, kFBAttachNone, "", 1.0 );
AddRegion( "ButtonClear", "ButtonClear",
lS, kFBAttachRight, "Tree", 1.0,
lS, kFBAttachBottom, "ButtonRefresh", 1.0,
lButtonW, kFBAttachNone, "", 1.0,
lButtonH, kFBAttachNone, "", 1.0 );
AddRegion( "ButtonGenerate", "ButtonGenerate",
lS, kFBAttachRight, "Tree", 1.0,
lS, kFBAttachBottom, "ButtonClear", 1.0,
lButtonW, kFBAttachNone, "", 1.0,
lButtonH, kFBAttachNone, "", 1.0 );
SetControl( "Tree", mTree );
SetControl( "ButtonRefresh", mButtonRefresh );
SetControl( "ButtonClear", mButtonClear );
SetControl( "ButtonGenerate", mButtonGenerate );
}
void ORToolPose::UIConfigure()
{
mButtonRefresh.Caption = "Refresh Pose List";
mButtonRefresh.OnClick.Add( this, (FBCallback) &ORToolPose::EventRefresh );
mButtonClear.Caption = "Delete All Poses";
mButtonClear.OnClick.Add( this, (FBCallback) &ORToolPose::EventClear );
mButtonGenerate.Caption = "Generate Poses";
mButtonGenerate.OnClick.Add( this, (FBCallback) &ORToolPose::EventGenerate );
EventRefresh( NULL, NULL );
}
static void CollectModels(FBModel* pParent, FBModelList &pAllModels)
{
for(int i = 0; i < pParent->Children.GetCount(); i++)
{
pAllModels.Add(pParent->Children[i]);
CollectModels(pParent->Children[i],pAllModels);
}
}
void ORToolPose::EventRefresh( HIRegister /*pSender*/, HKEvent /*pEvent*/ )
{
FBScene* lScene = mSystem.Scene;
//
// Show the list of Bind Pose information in the scene
//
mTree.Clear();
int lCount = lScene->Poses.GetCount();
char buffer[2048];
FBTreeNode* lNode;
FBTreeNode* lSubNode;
FBString lName;
FBMatrix lMatrix;
bool lLocal;
// character and bind poses
for( int i = 0; i < lCount; i++ )
{
lNode = mTree.InsertLast( mTree.GetRoot(), lScene->Poses[i]->Name );
int lNodeCount = lScene->Poses[i]->GetNodeCount();
for( int j = 0; j < lNodeCount; j++ )
{
lName = lScene->Poses[i]->GetNodeName( j );
lMatrix = lScene->Poses[i]->GetNodeMatrix( j );
lLocal = lScene->Poses[i]->IsNodeLocalMatrix( j );
lSubNode = mTree.InsertLast( lNode, lName );
for( int k = 0; k < 16; k++ )
{
sprintf( buffer, "[%d][%d]=%f", k/4, k%4, lMatrix(k/4,k%4) );
mTree.InsertLast( lSubNode, buffer );
}
}
}
//object poses
//currently object pose can return transformation values for specific modelname. So we simply collect all models from scene and check if pose have it stored.
FBModelList lAllModels;
CollectModels(lScene->RootModel,lAllModels);
lCount = lScene->ObjectPoses.GetCount();
FBObjectPose* lObjectPose;
FBMatrix lRM;
FBMatrix lSM;
FBSVector lS;
for(int i = 0; i < lCount; i++)
{
lObjectPose = lScene->ObjectPoses[i];
lNode = mTree.InsertLast( mTree.GetRoot(), lObjectPose->Name );
for(int j = 0; j < lAllModels.GetCount(); j++)
{
lName = lAllModels[j]->Name;
if(lObjectPose->IsTransformStored(lName,kFBPoseTransformGlobal))
{
lLocal = false;
}
else if(lObjectPose->IsTransformStored(lName,kFBPoseTransformLocal))
{
lLocal = true;
}
else //no transform stored, but see if we got Lcl Translation and/or Lcl Rotation stored
{
lSubNode = NULL;
if(lObjectPose->IsPropertyStored(lName,ANIMATIONNODE_TYPE_LOCAL_TRANSLATION))
{
lSubNode = mTree.InsertLast( lNode, (char *)lName );
lObjectPose->GetPropertyValue(lT.mValue,3,lName,ANIMATIONNODE_TYPE_LOCAL_TRANSLATION);
sprintf( buffer, "%s = (%f %f %f)", ANIMATIONNODE_TYPE_LOCAL_TRANSLATION, lT[0],lT[1],lT[2] );
mTree.InsertLast( lSubNode, buffer );
}
if(lObjectPose->IsPropertyStored(lName,ANIMATIONNODE_TYPE_LOCAL_ROTATION))
{
if(!lSubNode) lSubNode = mTree.InsertLast( lNode, (char *)lName );
lObjectPose->GetPropertyValue(lT.mValue,3,lName,ANIMATIONNODE_TYPE_LOCAL_ROTATION);
sprintf( buffer, "%s = (%f %f %f)", ANIMATIONNODE_TYPE_LOCAL_ROTATION, lT[0],lT[1],lT[2] );
mTree.InsertLast( lSubNode, buffer );
}
continue;
}
if(lObjectPose->GetTransform(lT,lRM,lSM,lName, lLocal ? kFBPoseTransformLocal : kFBPoseTransformGlobal))
{
lSubNode = mTree.InsertLast( lNode, (char *)lName );
FBTRSToMatrix(lMatrix,lT,lR,lS);
for( int k = 0; k < 16; k++ )
{
sprintf( buffer, "[%d][%d]=%f", k/4, k%4, lMatrix(k/4,k%4) );
mTree.InsertLast( lSubNode, buffer );
}
}
}
}
}
void ORToolPose::EventClear( HIRegister /*pSender*/, HKEvent /*pEvent*/ )
{
//
// Delete all the Bind Pose information in the scene
//
while( mSystem.Scene->Poses.GetCount() )
{
FBPose* lPose = mSystem.Scene->Poses[0];
mSystem.Scene->Poses.RemoveAt(0);
lPose->Destroy();
}
//
// Refresh the list of bind pose
//
EventRefresh( NULL, NULL );
}
void ORToolPose::EventGenerate( HIRegister /*pSender*/, HKEvent /*pEvent*/ )
{
//
// Delete all the previous bind poses
//
EventClear( NULL, NULL );
//
// Generate bind pose information for all the models
//
GenerateBindPoseNodes( mSystem.SceneRootModel, NULL );
//
// Refresh the list of bind pose
//
EventRefresh( NULL, NULL );
}
void ORToolPose::GenerateBindPoseNodes( FBModel* pModel, FBPose* pPose )
{
if( pModel )
{
FBPose* lPose = pPose;
//
// Make sure we have an FBPose.
// If not, create one and add it to the scene.
//
if( lPose == NULL )
{
lPose = new FBPose( "BindPoseInformation" );
mSystem.Scene->Poses.Add( lPose );
}
//
// Add a node with the matrix of this model.
// This will be the bind pose information for this model.
//
if( pModel != mSystem.SceneRootModel )
{
FBMatrix lMatrix;
pModel->GetMatrix( lMatrix );
lPose->AddNode( pModel, &lMatrix, false /*IsLocalMatrix*/ );
}
//
// Recursive call.
// Do the same thing for all our children.
//
int i;
for( i = 0; i < pModel->Children.GetCount(); i++ )
{
GenerateBindPoseNodes( pModel->Children[i], lPose );
}
}
}