importexport/impexptool/orimpexptool_engine.cxx

importexport/impexptool/orimpexptool_engine.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.
***************************************************************************************/
#include "orimpexptool_engine.h"
#include <math.h>
/************************************************
* File importation function
* - to be modified to import your own custom file
* - format. For examples, see the scene creation
* - functions.
************************************************/
bool ORImportExportEngine::ImportFile( const char* pFileName )
{
// if successful importation, return true
return true;
}
/************************************************
* Import the fabricated (hard-coded) scene.
* Creates a flag & a cube, attached
* together (cube is parent) and adds a texture
* onto the flag.
************************************************/
void ORImportExportEngine::ImportScene()
{
// BUILD MODELS
FBModel* lRoot = NULL;
FBModel* lFlag = NULL;
// Create models
lFlag = CreateTexturedMesh("flag");
lRoot = CreateCube("cube");
// Select flag
lFlag->Selected = true;
// Hierarchy
// cubeA parent is the parent of surface
lFlag->Children.Add(lRoot);
// SET VECTORS
FBVector3d lPos,lRot,lSca;
// Set initial TRS values
lPos[0]=100.0;
lPos[1]=0.0;
lPos[2]=0.0;
lRot[0]=lRot[1]=lRot[2]=0.0;
lSca[0]=lSca[1]=lSca[2]=1.0;
lRoot->SetVector( lPos, kModelTranslation, false );
lRoot->SetVector( lRot, kModelRotation, true );
lRoot->SetVector( lSca, kModelScaling, true );
lPos[0]=-100.0;
lPos[1]=0.0;
lPos[2]=0.0;
lRot[0]=lRot[1]=lRot[2]=0.0;
lSca[0]=lSca[1]=lSca[2]=1.0;
lFlag->SetVector( lPos, kModelTranslation, false );
lFlag->SetVector( lRot, kModelRotation, true );
lFlag->SetVector( lSca, kModelScaling, true );
// GET NODES
// animation
FBFCurve* lCurveX;
FBFCurve* lCurveY;
FBFCurve* lCurveZ;
FBAnimationNode* lNodeTranslation;
FBAnimationNode* lNodeTrans_X;
FBAnimationNode* lNodeTrans_Y;
FBAnimationNode* lNodeTrans_Z;
// Get animation node for lRoot
lRoot->Translation.SetAnimated(true);
lNodeTranslation = FindAnimationNode(lRoot->AnimationNode, ANIMATIONNODE_TYPE_LOCAL_TRANSLATION);
lNodeTrans_X = FindAnimationNode( lNodeTranslation, "X");
lNodeTrans_Y = FindAnimationNode( lNodeTranslation, "Y");
lNodeTrans_Z = FindAnimationNode( lNodeTranslation, "Z");
// Get FCurves
lCurveX = (FBFCurve*) lNodeTrans_X->FCurve;
lCurveY = (FBFCurve*) lNodeTrans_Y->FCurve;
lCurveZ = (FBFCurve*) lNodeTrans_Z->FCurve;
// Create if necessary
if(!lCurveX) lCurveX = new FBFCurve;
else lCurveX->Keys.RemoveAll();
if(!lCurveY) lCurveY = new FBFCurve;
else lCurveY->Keys.RemoveAll();
if(!lCurveZ) lCurveZ = new FBFCurve;
else lCurveZ->Keys.RemoveAll();
// ADD KEYS
FBTime lStart,lStop;
// Set start & stop times
lStart.SetMilliSeconds(0);
lStop.SetMilliSeconds(1000);
// Add keys
lCurveX->KeyAdd(lStart,0);
lCurveX->KeyAdd(lStop,100);
lCurveY->KeyAdd(lStart,0);
lCurveY->KeyAdd(lStop,100);
lCurveZ->KeyAdd(lStart,0);
lCurveZ->KeyAdd(lStop,100);
int count;
count=lCurveZ->Keys.GetCount();
// put back curves onto nodes.
lNodeTrans_X->FCurve = lCurveX;
lNodeTrans_Y->FCurve = lCurveY;
lNodeTrans_Z->FCurve = lCurveZ;
// make model visible
lRoot->Show = true;
}
void ORImportExportEngine::ImportAnimation()
{
}
void ORImportExportEngine::ImportOptical()
{
// Create the optical model
FBModelOptical* opticalModel = CreateOpticalModel();
FBAnimationNode* lNodeTranslation;
FBAnimationNode* lNodeTrans_X;
FBAnimationNode* lNodeTrans_Y;
FBAnimationNode* lNodeTrans_Z;
// Get its animation node
opticalModel->Translation.SetAnimated(true);
lNodeTranslation = FindAnimationNode( opticalModel->AnimationNode, ANIMATIONNODE_TYPE_LOCAL_TRANSLATION );
lNodeTrans_X = FindAnimationNode( lNodeTranslation, "X" );
lNodeTrans_Y = FindAnimationNode( lNodeTranslation, "Y" );
lNodeTrans_Z = FindAnimationNode( lNodeTranslation, "Z" );
// Import animation FCurves
ImportFCurve( lNodeTrans_X );
ImportFCurve( lNodeTrans_Y );
ImportFCurve( lNodeTrans_Z );
ImportOpticalModelAnimation( opticalModel );
opticalModel->Show = true;
}
/************************************************
* Create the cube model.
* Description: Creates a textured cube with materials.
************************************************/
FBModel *ORImportExportEngine::CreateCube( const char* pName )
{
FBModel* lModel = new FBModel( pName );
FBMesh* lMesh = new FBMesh("Cube");
lModel->Geometry = lMesh;
// Play with material values for the mesh.
FBMaterial* lMaterial = new FBMaterial( "OR - Material" );
//FBColor Diffuse (0.5,0.3,1.0);
FBColor Diffuse (1.0,0.0,0.0);
FBColor Ambient ( Diffuse[0]-0.15,Diffuse[1]-0.15,Diffuse[2]-0.15);
FBColor Specular(0.0,0.0,0.0);
if (Ambient[0]<0) Ambient[0]=0;
if (Ambient[1]<0) Ambient[1]=0;
if (Ambient[2]<0) Ambient[2]=0;
lMaterial->Ambient = Ambient;
lMaterial->Diffuse = Diffuse;
lMaterial->Specular = Specular;
// Add materials to model.
lModel->Materials.Add( lMaterial );
// Add a new texture
FBTexture* lTexture = new FBTexture( FBString(mSystem.PathImages)+"/openreality_tooldemo-l.png" );
// Connect texture to material
lMaterial->Diffuse.ConnectSrc( lTexture );
double len = 10.0;//unit length
// Object creation
int P0a,P0e,P0d;
int P1a,P1b,P1e;
int P2a,P2b,P2c;
int P3a,P3c,P3d;
int P4d,P4e,P4f;
int P5b,P5e,P5f;
int P6b,P6c,P6f;
int P7c,P7d,P7f;
lMesh->GeometryBegin();
// Step 1: Create vertices
// Because FiLMBOX doesn't yet support multiple UV or multiple Normals per vertex,
// the cube's vertices need to be duplicated
lMesh->VertexInit(24, false, false);
// Surface A: Y=0 N=(0,-1,0)
lMesh->VertexNormalSet(0.0,-1.0,0.0);
lMesh->VertexUVSet(0.0,0.0);
P0a = lMesh->VertexAdd( 0.0, 0.0, 0.0 );
lMesh->VertexUVSet(0.0,1.0);
P1a = lMesh->VertexAdd( 0.0, 0.0, len );
lMesh->VertexUVSet(1.0,1.0);
P2a = lMesh->VertexAdd( len, 0.0, len );
lMesh->VertexUVSet(1.0,0.0);
P3a = lMesh->VertexAdd( len, 0.0, 0.0 );
// Surface B: Z=1, N=(0,0,1)
lMesh->VertexNormalSet(0,0,1);
lMesh->VertexUVSet(0.0,0.0);
P1b = lMesh->VertexAdd( 0.0, 0.0, len );
lMesh->VertexUVSet(1.0,0.0);
P2b = lMesh->VertexAdd( len, 0.0, len );
lMesh->VertexUVSet(0.0,1.0);
P5b = lMesh->VertexAdd( 0.0, len, len );
lMesh->VertexUVSet(1.0,1.0);
P6b = lMesh->VertexAdd( len, len, len );
// Surface C: X=1 N=(1,0,0)
lMesh->VertexNormalSet(1,0,0);
P2c = lMesh->VertexAdd( len, 0.0, len );
P3c = lMesh->VertexAdd( len, 0.0, 0.0 );
P6c = lMesh->VertexAdd( len, len, len );
P7c = lMesh->VertexAdd( len, len, 0.0 );
// Surface D: Z=0 N=(0,0,-1)
lMesh->VertexNormalSet(0,0,-1);
P0d = lMesh->VertexAdd( 0.0, 0.0, 0.0 );
P3d = lMesh->VertexAdd( len, 0.0, 0.0 );
P7d = lMesh->VertexAdd( len, len, 0.0 );
P4d = lMesh->VertexAdd( 0.0, len, 0.0 );
// Surface E: X=0 N=(-1,0,0)
lMesh->VertexNormalSet(-1,0,0);
P0e = lMesh->VertexAdd( 0.0, 0.0, 0.0 );
P1e = lMesh->VertexAdd( 0.0, 0.0, len );
P4e = lMesh->VertexAdd( 0.0, len, 0.0 );
P5e = lMesh->VertexAdd( 0.0, len, len );
// Surface F: Y=1 N=(0,1,0)
lMesh->VertexNormalSet(0,1,0);
P4f = lMesh->VertexAdd( 0.0, len, 0.0 );
P5f = lMesh->VertexAdd( 0.0, len, len );
P6f = lMesh->VertexAdd( len, len, len );
P7f = lMesh->VertexAdd( len, len, 0.0 );
// Step 2: Create polygons
lMesh->PolygonBegin();
lMesh->PolygonVertexAdd(P0a);
lMesh->PolygonVertexAdd(P3a);
lMesh->PolygonVertexAdd(P2a);
lMesh->PolygonVertexAdd(P1a);
lMesh->PolygonEnd();
lMesh->PolygonBegin();
lMesh->PolygonVertexAdd(P1b);
lMesh->PolygonVertexAdd(P2b);
lMesh->PolygonVertexAdd(P6b);
lMesh->PolygonVertexAdd(P5b);
lMesh->PolygonEnd();
lMesh->PolygonBegin();
lMesh->PolygonVertexAdd(P2c);
lMesh->PolygonVertexAdd(P3c);
lMesh->PolygonVertexAdd(P7c);
lMesh->PolygonVertexAdd(P6c);
lMesh->PolygonEnd();
lMesh->PolygonBegin();
lMesh->PolygonVertexAdd(P0d);
lMesh->PolygonVertexAdd(P4d);
lMesh->PolygonVertexAdd(P7d);
lMesh->PolygonVertexAdd(P3d);
lMesh->PolygonEnd();
lMesh->PolygonBegin();
lMesh->PolygonVertexAdd(P1e);
lMesh->PolygonVertexAdd(P5e);
lMesh->PolygonVertexAdd(P4e);
lMesh->PolygonVertexAdd(P0e);
lMesh->PolygonEnd();
lMesh->PolygonBegin();
lMesh->PolygonVertexAdd(P4f);
lMesh->PolygonVertexAdd(P5f);
lMesh->PolygonVertexAdd(P6f);
lMesh->PolygonVertexAdd(P7f);
lMesh->PolygonEnd();
lMesh->GeometryEnd();
// make visible
lModel->Show = true;
// Adjust the shading mode.
lModel->ShadingMode = kFBModelShadingTexture;
return lModel;
}
/************************************************
* Create textured mesh.
* Description: Creates a textures flat polygon mesh
************************************************/
FBModel *ORImportExportEngine::CreateTexturedMesh( const char* pName )
{
int x,y;
FBModel* lModel = new FBModel( pName );
// Retrieves the polygon mesh surface from the model.
FBMesh* lMesh = new FBMesh("Mesh");
lModel->Geometry = lMesh;
lModel->ShadingMode = kFBModelShadingTexture;
FBTexture* lTexture = new FBTexture( FBString(mSystem.PathImages)+"/openreality_tooldemo-l.png" );
FBMaterial* lMaterial = new FBMaterial( "SDK material" );
lMaterial->Diffuse.ConnectSrc(lTexture);
lModel->Materials.Add(lMaterial);
FBColor Diffuse (1.0,0.0,0.0);
FBColor Ambient ( Diffuse[0]-0.15,Diffuse[1]-0.15,Diffuse[2]-0.15);
FBColor Specular(0.0,0.0,0.0);
if (Ambient[0]<0) Ambient[0]=0;
if (Ambient[1]<0) Ambient[1]=0;
if (Ambient[2]<0) Ambient[2]=0;
lMaterial->Ambient = Ambient;
lMaterial->Diffuse = Diffuse;
lMaterial->Specular = Specular;
// Object creation
lMesh->GeometryBegin();
// Init Position/Normal/UV array size.
lMesh->VertexInit(MESH_STEP_X * MESH_STEP_Y, false, true);
// Step 1: Create vertices
lMesh->VertexNormalSet(0.0,0.0,1.0);
float len = 100.0;//unit length
float stepX =(float)len/(float) MESH_STEP_X;
float stepY =(float)len/(float) MESH_STEP_Y;
float stepU =(float)1.0/(float) MESH_STEP_X;
float stepV =(float)1.0/(float) MESH_STEP_Y;
for(x=0;x<=MESH_STEP_X;x++)
{
for(y=0;y<=MESH_STEP_Y;y++)
{
lMesh->VertexUVSet(x*stepU,y*stepV);
V[x][y] = lMesh->VertexAdd(x*stepX,y*stepY, 0.0 );
}
}
for(x=0;x<MESH_STEP_X;x++)
{
for(y=0;y<MESH_STEP_Y;y++)
{
lMesh->PolygonBegin();
lMesh->PolygonVertexAdd(V[x][y]);
lMesh->PolygonVertexAdd(V[x+1][y]);
lMesh->PolygonVertexAdd(V[x+1][y+1]);
lMesh->PolygonVertexAdd(V[x][y+1]);
lMesh->PolygonEnd();
}
}
lMesh->GeometryEnd();
// make visible
lModel->Show = true;
return lModel;
}
/************************************************
* Export Animation Node.
************************************************/
bool ORImportExportEngine::ExportAnimationNode( FILE* pFile, FBAnimationNode* pNode, const char* pParentName )
{
FBString timeString;
FBTime time;
FBFCurve* curve = NULL;
int i;
FBString name;
name = pParentName;
name += "/";
name += pNode->Name;
if(pNode->Nodes.GetCount()==0)
{
//leaf node: export animation data
fprintf(pFile,"Node name:%s UserName:%s\n",(const char *)name, (const char *)pNode->UserName);
curve = pNode->FCurve;
// test to see if there is recorded data
if(curve)
{
int keyCount = curve->Keys.GetCount();
fprintf(pFile,"%d Key Frames\n", keyCount);
for(int i=0;i<keyCount;i++)
{
time = curve->Keys[i].Time;
// Write time values in a format (SMPTE or frame number)
// according to current FB settings in the transport control
timeString = (char *)time.GetTimeString();
fprintf(pFile,"KeyIndex[%d]: Time[%s] Val[%lf] Deriv[%.2lf,%.2lf] TCB[%.2lf,%.2lf,%.2lf]\n",
i,
(char *)timeString,
(double)curve->Keys[i].Value,
(double)curve->Keys[i].LeftDerivative,
(double)curve->Keys[i].RightDerivative,
(double)curve->Keys[i].Tension,
(double)curve->Keys[i].Continuity,
(double)curve->Keys[i].Bias
);
}
fprintf(pFile,"\n");
}
else
{
fprintf(pFile,"Node has no recorded data\n");
}
}
// recurse through children
for(i=0;i<pNode->Nodes.GetCount();i++)
{
ExportAnimationNode(pFile, pNode->Nodes[i], (char *)name) ;
}
return true;
}
/************************************************
* Export Optical model.
************************************************/
bool ORImportExportEngine::ExportOpticalModel( FILE *pFile, FBModelOptical* pModel )
{
FBTime start,end,period;
FBModelMarkerOptical* marker = NULL;
double x,y,z,o;
int i,f,frameCount;
start = pModel->SamplingStart;
end = pModel->SamplingStop;
period = pModel->SamplingPeriod;
fprintf(pFile,"\nAnimation: start:%s end:%s period: %lf (%lf Hz)\n",
(char *)start.GetTimeString(),
(char *)end.GetTimeString(),
(double)period.GetMilliSeconds(),
1.0/(double)period.GetMilliSeconds());
if(pModel->ExportSetup())
{
for(i=0;i< pModel->Children.GetCount();i++)
{
marker = (FBModelMarkerOptical*)pModel->Children[i];
frameCount = marker->ExportBegin();
fprintf(pFile," Marker[%d]: name:%s length:%d frames\n",i,(const char *)marker->Name,frameCount);
for(f=0;f< frameCount;f++)
{
if(marker->ExportKey( &x,&y,&z,&o))
{
fprintf(pFile," frame[%d]= [%.2lf,%.2lf,%.2lf,%.2lf]\n",f, x,y,z,o);
}
else
{
fprintf(pFile,"------------Error at frame %d\n",f);
}
}
marker->ExportEnd();
}
}
return true;
}
/************************************************
* Export Voice reality information.
************************************************/
bool ORImportExportEngine::ExportVoice( const char* pFileName )
{
// Description:
// This function is called by EventButtonExportVoiceClick() called by
// pressing the "Export Voice" button in the interface.
FILE *pf=NULL;
pf=fopen(pFileName,"wt");
// Scan through the list of devices to find the Voice
for( int c = 0; c<mSystem.Scene->Devices.GetCount(); c++ )
{
FBDevice* voiceDevice;
voiceDevice = mSystem.Scene->Devices[c];
if (strstr(voiceDevice->FbxGetObjectSubType(),"Voice")!=0)
{
fprintf(pf,"Voice device found\n");
FBAnimationNode* node = voiceDevice->AnimationNodeOutGet();
ExportAnimationNode(pf, node, "Voice");
break;
}
}
fclose(pf);
return true;//success
}
/************************************************
* Export selected to file.
************************************************/
bool ORImportExportEngine::ExportSelected( const char* pFileName )
{
FBModel* lRootModel;
FILE* lFile = NULL;
// Get root model
lRootModel = mSystem.SceneRootModel;
// Open file
lFile = fopen(pFileName,"wt");
// Export information
ExportHierarchy( lFile, lRootModel, true );
// Close file
fclose( lFile );
return true;//success
}
/************************************************
* Export object hierarchy.
************************************************/
bool ORImportExportEngine::ExportHierarchy( FILE* pFile, FBModel* pModel, bool pIfSelected )
{
int i;
// If getting all models | getting selected & this model is selected
if( !pIfSelected || ( pModel->Selected && pIfSelected ) )
{
// If optical model
if( pModel->Is( FBModelOptical::TypeInfo ) )
{
ExportOpticalModel( pFile, (FBModelOptical*)pModel );
}
// otherwise
else
{
ExportModel( pFile, pModel );
ExportAnimationNode( pFile, pModel->AnimationNode, pModel->Name.AsString() );
}
}
// Recurse through children
for(i=0;i<pModel->Children.GetCount();i++)
{
ExportHierarchy(pFile, pModel->Children[i], pIfSelected);
}
return true;//success
}
/************************************************
* Export model.
************************************************/
bool ORImportExportEngine::ExportModel( FILE *pFile, FBModel* pModel )
{
int i;
int polyCount=0,vertexCount=0,lMaterialCount=0,lTextureCount=0;
int matId =0;
FBNormal normal;
FBVertex vertex;
FBUV lUV;
// Get the mesh from the model
FBMesh* lMesh = pModel->TessellatedMesh;
polyCount = lMesh->PolygonCount();
vertexCount = lMesh->VertexCount();
lMaterialCount = pModel->Materials.GetCount();
//
//Model Type
//
FBString name;
name = pModel->Name;
fprintf(pFile,"______________________________________________________________________________________\n");
fprintf(pFile,"Model Name: %s (typeId=%d) [%d polygons, %d vertices, %d materials, %d textures]\n",
(char *)name, pModel->TypeInfo, polyCount,vertexCount,lMaterialCount,lTextureCount);
if(pModel->Is(FBModel::TypeInfo))
fprintf(pFile,"Derives from class FBModel\n");
if(pModel->Is(FBModelOptical::TypeInfo))
fprintf(pFile,"Derives from class FBModelOptical\n");
if(pModel->Is(FBModelMarker::TypeInfo))
fprintf(pFile,"Derives from class FBModelMarker\n");
if(pModel->Is(FBModelMarkerOptical::TypeInfo))
fprintf(pFile,"Derives from class FBModelMarkerOptical\n");
if(pModel->Is(FBModelNull::TypeInfo))
fprintf(pFile,"Derives from class FBModelNull\n");
if(pModel->Is(FBModelRoot::TypeInfo))
fprintf(pFile,"Derives from class FBModelRoot\n");
if(pModel->Is(FBCamera::TypeInfo))
fprintf(pFile,"Derives from class FBCamera\n");
if(pModel->Is(FBLight::TypeInfo))
fprintf(pFile,"Derives from class FBLight\n");
//
//Transformation
//
FBVector3d GT,GR,GS;
pModel->GetVector(GT,kModelTranslation,true) ;
pModel->GetVector(GR,kModelRotation,true) ;
pModel->GetVector(GS,kModelScaling,true);
fprintf(pFile,"\nTransformation:\n");
fprintf(pFile," Global TRS: T=[%.2lf,%.2lf,%.2lf] R=[%.2lf,%.2lf,%.2lf] S=[%.2lf,%.2lf,%.2lf]\n",
GT[0],GT[1],GT[2],
GR[0],GR[1],GR[2],
GS[0],GS[1],GS[2]);
//
//Materials
//
fprintf(pFile,"\nMaterials: count=%d\n",lMaterialCount);
fprintf(pFile," Material mapping mode:");
switch(lMesh->MaterialMappingMode)
{
case kFBGeometryMapping_NONE: fprintf(pFile,"[Non Material]\n"); break;
case kFBGeometryMapping_BY_CONTROL_POINT: fprintf(pFile,"[By Control Point]\n"); break;
case kFBGeometryMapping_BY_POLYGON_VERTEX: fprintf(pFile,"[By Polygon Vertex]\n"); break;
case kFBGeometryMapping_BY_POLYGON: fprintf(pFile,"[By Polygon]\n"); break;
case kFBGeometryMapping_BY_EDGE: fprintf(pFile,"[By Edge]\n"); break;
case kFBGeometryMapping_ALL_SAME: fprintf(pFile,"[All Same]\n"); break;
default: fprintf(pFile,"[Unknown]\n"); break;
}
for(i=0;i< lMaterialCount;i++)
{
FBMaterial* lMaterial = pModel->Materials[i];
fprintf(pFile," ");
fprintf(pFile,"Material[%s] ",(const char *)(lMaterial->LongName));
fprintf(pFile,"Id[%d]\n",i);
FBColor lPropColorValue;
float lPropDoubleValue;
FBTexture* lPropTex;
lPropColorValue = lMaterial->Ambient;
lPropTex = lMaterial->GetTexture(kFBMaterialTextureAmbient);
fprintf(pFile,"AmbientValue=[%.2lf,%.2lf,%.2lf] AmbientTex=[%s]\n", lPropColorValue[0], lPropColorValue[1],lPropColorValue[2], lPropTex? (const char*)(lPropTex->LongName) : NULL);
lPropDoubleValue = lMaterial->AmbientFactor;
lPropTex = lMaterial->GetTexture(kFBMaterialTextureAmbientFactor);
fprintf(pFile,"AmbientFactorValue=[%.2lf] AmbientTex=[%s]\n", lPropDoubleValue, lPropTex? (const char*)(lPropTex->LongName) : NULL);
lPropColorValue = lMaterial->Emissive;
lPropTex = lMaterial->GetTexture(kFBMaterialTextureEmissive);
fprintf(pFile,"EmissiveValue=[%.2lf,%.2lf,%.2lf] EmissiveTex=[%s]\n", lPropColorValue[0], lPropColorValue[1],lPropColorValue[2], lPropTex? (const char*)(lPropTex->LongName) : NULL);
lPropDoubleValue = lMaterial->EmissiveFactor;
lPropTex = lMaterial->GetTexture(kFBMaterialTextureEmissiveFactor);
fprintf(pFile,"EmissiveFactorValue=[%.2lf] EmissiveTex=[%s]\n", lPropDoubleValue, lPropTex? (const char*)(lPropTex->LongName) : NULL);
lPropColorValue = lMaterial->Diffuse;
lPropTex = lMaterial->GetTexture(kFBMaterialTextureDiffuse);
fprintf(pFile,"DiffuseValue=[%.2lf,%.2lf,%.2lf] DiffuseTex=[%s]\n", lPropColorValue[0], lPropColorValue[1],lPropColorValue[2], lPropTex? (const char*)(lPropTex->LongName) : NULL);
lPropDoubleValue = lMaterial->DiffuseFactor;
lPropTex = lMaterial->GetTexture(kFBMaterialTextureDiffuseFactor);
fprintf(pFile,"DiffuseFactorValue=[%.2lf] DiffuseFactorTex=[%s]\n", lPropDoubleValue, lPropTex? (const char*)(lPropTex->LongName) : NULL);
lPropColorValue = lMaterial->TransparentColor;
lPropTex = lMaterial->GetTexture(kFBMaterialTextureTransparent);
fprintf(pFile,"TransparentColorValue=[%.2lf,%.2lf,%.2lf] TransparentColorTex=[%s]\n", lPropColorValue[0], lPropColorValue[1],lPropColorValue[2], lPropTex? (const char*)(lPropTex->LongName) : NULL);
lPropDoubleValue = lMaterial->TransparencyFactor;
lPropTex = lMaterial->GetTexture(kFBMaterialTextureTransparentFactor);
fprintf(pFile,"TransparencyFactorValue=[%.2lf] TransparencyFactorTex=[%s]\n", lPropDoubleValue, lPropTex? (const char*)(lPropTex->LongName) : NULL);
lPropColorValue = lMaterial->Bump;
lPropTex = lMaterial->GetTexture(kFBMaterialTextureBump);
fprintf(pFile,"BumpValue=[%.2lf,%.2lf,%.2lf] BumpTex=[%s]\n", lPropColorValue[0], lPropColorValue[1],lPropColorValue[2], lPropTex? (const char*)(lPropTex->LongName) : NULL);
lPropColorValue = lMaterial->NormalMap;
lPropTex = lMaterial->GetTexture(kFBMaterialTextureNormalMap);
fprintf(pFile,"NormalMapValue=[%.2lf,%.2lf,%.2lf] NormalMapTex=[%s]\n", lPropColorValue[0], lPropColorValue[1],lPropColorValue[2], lPropTex? (const char*)(lPropTex->LongName) : NULL);
lPropColorValue = lMaterial->Specular;
lPropTex = lMaterial->GetTexture(kFBMaterialTextureSpecular);
fprintf(pFile,"SpecularValue=[%.2lf,%.2lf,%.2lf] SpecularTex=[%s]\n", lPropColorValue[0], lPropColorValue[1],lPropColorValue[2], lPropTex? (const char*)(lPropTex->LongName) : NULL);
lPropDoubleValue = lMaterial->SpecularFactor;
lPropTex = lMaterial->GetTexture(kFBMaterialTextureSpecularFactor);
fprintf(pFile,"SpecularFactorValue=[%.2lf] SpecularFactorTex=[%s]\n", lPropDoubleValue, lPropTex? (const char*)(lPropTex->LongName) : NULL);
lPropDoubleValue = lMaterial->Shininess;
lPropTex = lMaterial->GetTexture(kFBMaterialTextureShiness);
fprintf(pFile,"ShininessValue=[%.2lf] ShininessTex=[%s]\n", lPropDoubleValue, lPropTex? (const char*)(lPropTex->LongName) : NULL);
lPropColorValue = lMaterial->Reflection;
lPropTex = lMaterial->GetTexture(kFBMaterialTextureReflection);
fprintf(pFile,"ReflectionValue=[%.2lf,%.2lf,%.2lf] ReflectionTex=[%s]\n", lPropColorValue[0], lPropColorValue[1],lPropColorValue[2], lPropTex? (const char*)(lPropTex->LongName) : NULL);
lPropDoubleValue = lMaterial->ReflectionFactor;
lPropTex = lMaterial->GetTexture(kFBMaterialTextureReflectionFactor);
fprintf(pFile,"ReflectionFactorValue=[%.2lf] ReflectionFactorTex=[%s]\n", lPropDoubleValue, lPropTex? (const char*)(lPropTex->LongName) : NULL);
fprintf(pFile,"\n\n");
}
//
//Vertices
//
fprintf(pFile,"\nVertices:\n");
for(i=0;i< vertexCount;i++)
{
vertex = lMesh->VertexGet(i);
normal = lMesh->VertexNormalGet(i);
lUV = lMesh->VertexUVGet(i);
fprintf(pFile," Vertex[%3d] P=[%.2lf,%.2lf,%.2lf] N=[%.2lf,%.2lf,%.2lf] Mat=[%d] UV=[%.2lf,%.2lf]\n",
i,
vertex[0],vertex[1],vertex[2],
normal[0],normal[1],normal[2],
matId,
lUV[0],lUV[1]);
}
//
//Polygons
//
fprintf(pFile,"\nPolygons:\n");
for(i=0;i< polyCount;i++)
{
fprintf(pFile," Poly[%d] Vert=[",i);
for(int j=0;j<lMesh->PolygonVertexCount(i);j++)
{
fprintf(pFile,"%d ",lMesh->PolygonVertexIndex(i,j));
}
fprintf(pFile,"] ");
fprintf(pFile,"MatId=[%d] ",lMesh->PolygonMaterialIdGet(i));
}
return true;
}
/************************************************
* Import FCurves for animation node.
************************************************/
void ORImportExportEngine::ImportFCurve(FBAnimationNode* pNode)
{
// Get FCurve, create if needed.
FBFCurve* fcurve = (FBFCurve*)pNode->FCurve;
FBTime oneFrame (0,0,0,1); // one frame
FBTime time;
double freq = 0.01;
double amplitude = 10.0;
int keyIndex;
double tmpl,tmpr;
if(!fcurve) fcurve = new FBFCurve;
else fcurve->Keys.RemoveAll();
time = 0;
// For each optical sample
for(int i=0;i<200;i++)
{
// Switch frames
time = time + oneFrame;
// Add value (getting key index)
keyIndex = fcurve->KeyAdd( time, amplitude*sin(freq*(double)time.GetMilliSeconds()) );
// Play with curve interpolation
tmpl = fcurve->Keys[keyIndex].LeftDerivative;
tmpr = fcurve->Keys[keyIndex].RightDerivative;
fcurve->Keys[keyIndex].LeftDerivative = 0.0;
fcurve->Keys[keyIndex].RightDerivative = 0.0;
fcurve->Keys[keyIndex].Tension = 1.0;
fcurve->Keys[keyIndex].Continuity = 1.0;
fcurve->Keys[keyIndex].Bias = 1.0;
}
// To debug
int count;
count=fcurve->Keys.GetCount();
// Copy curve
pNode->FCurve = fcurve;
}
/************************************************
* Import animation for optical model.
************************************************/
void ORImportExportEngine::ImportOpticalModelAnimation(FBModelOptical* pModel)
{
FBTime start = 0; //hh,mm,ss,ff
FBTime stop; //hh,mm,ss,ff
FBTime oneFrame (0,0,0,1);
// Set stop time
stop.SetMilliSeconds(200*oneFrame.GetMilliSeconds());
// Set sampling characteristics
pModel->SamplingStart = start;
pModel->SamplingStop = stop;
pModel->SamplingPeriod = oneFrame;
// Import setup
pModel->ImportSetup(); //start,stop, period
FBTime time;
time = 0;
double freq = 0.01;
double amplitude = 10.0;
double lPos[3];
FBModelMarkerOptical* marker = NULL;
int sampleCount;
// For each child
for(int m=0;m<pModel->Children.GetCount();m++)
{
// Get marker
marker = (FBModelMarkerOptical*) pModel->Children[m];
// Import data for marker
sampleCount = marker->ImportBegin();
// For each sample
for(int i=0;i<sampleCount;i++)
{
// adust frame
time = time + oneFrame;
// Get pos
lPos[0] = 1.3*amplitude*sin(0.567*freq*(double)time.GetMilliSeconds());
lPos[1] = 0.7*amplitude*sin(1.6*freq*(double)time.GetMilliSeconds());
lPos[2] = amplitude*sin(freq*(double)time.GetMilliSeconds());
// Import key
marker->ImportKey(lPos[0],lPos[1],lPos[2]);
}
marker->ImportEnd();
}
}
/************************************************
* Create optical model.
************************************************/
FBModelOptical* ORImportExportEngine::CreateOpticalModel()
{
char name[256];
// Create optical model with 1 marker
FBModelOptical* model = new FBModelOptical( "test" );
// Marker size
model->MarkerSize =10.0;
// Add to scene
model->Show = true;
FBModelMarkerOptical* opticalMarker;
int markerCount = 2;
// For number of markers
for(int i=0;i<markerCount;i++)
{
// Create and child new marker
sprintf(name,"m%d", i);
opticalMarker = new FBModelMarkerOptical (name);
opticalMarker->Show = true;
model->Children.Add(opticalMarker);
}
FBVector3d lPos,lRot,lSca;
//model->Selected = true;
// Set initial TRS values
lPos[0]=100.0;//X
lPos[1]=0.0;//Y
lPos[2]=0.0;//Z
lRot[2]=lRot[1]=lRot[0]=0.0;
lSca[2]=lSca[1]=lSca[0]=1.0;
// Set model position
model->SetVector(lPos, kModelTranslation, false /*true if global, false if local*/);
model->SetVector(lRot, kModelRotation, true /*global*/ );
model->SetVector(lSca, kModelScaling, true /*global*/ );
return model;
}
/************************************************
* Find an animation node from a given root.
************************************************/
FBAnimationNode* ORImportExportEngine::FindAnimationNode( FBAnimationNode* pNode, const char* pName )
{
const char* lName;
// For the number of child nodes on pNode.
for(int i=0;i < pNode->Nodes.GetCount();i++)
{
lName = pNode->Nodes[i]->Name;
// If the same, return the node.
if(strcmp(lName,pName)==0)
{
return pNode->Nodes[i];
}
}
return NULL;
}