void Unreal3DExport::WriteTracking()
{
    Tab<Point3> Loc;
    Tab<Quat> Quat;
    Tab<Point3> Euler;

    Loc.SetCount(FrameCount);
    Quat.SetCount(FrameCount);
    Euler.SetCount(FrameCount);

    for( int n=0; n<TrackedNodes.Count(); ++n )
    {
        IGameNode* node = TrackedNodes[n];

        for( int t=0; t<FrameCount; ++t )
        {            
            // Progress
            CheckCancel();
            
            // Set frame
            int curframe = FrameStart + t;
            pScene->SetStaticFrame(curframe);

            // Write tracking
            GMatrix objTM = node->GetWorldTM();
            Loc[t] = objTM.Translation();
            Quat[t] = objTM.Rotation();

            float eu[3];
            QuatToEuler(Quat[t],eu);
            Euler[t]=Point3(eu[0],eu[1],eu[2]);
            Euler[t] *= 180.0f/pi;

            eu[1] *= -1;
            EulerToQuat(eu,Quat[t],EULERTYPE_YXZ);
        }
        
        for( int t=0; t<FrameCount; ++t )
        {    
            _ftprintf( fLog, _T("%sLoc[%d]=(X=%f,Y=%f,Z=%f)\n"), node->GetName(), t, Loc[t].x, Loc[t].y, Loc[t].z );
        }
        
        for( int t=0; t<FrameCount; ++t )
        {    
            _ftprintf( fLog, _T("%sQuat[%d]=(W=%f,X=%f,Y=%f,Z=%f)\n"), node->GetName(), t, Quat[t].w, Quat[t].x, Quat[t].y, Quat[t].z ); 
        }
        
        for( int t=0; t<FrameCount; ++t )
        {    
            _ftprintf( fLog, _T("%sEuler[%d]=(X=%f,Y=%f,Z=%f)\n"), node->GetName(), t, Euler[t].x, Euler[t].y, Euler[t].z ); 
        }
    }
}
void Unreal3DExport::GetAnim()
{
    
    // Export vertex animation
    int lastvert = 0;
    FMeshVert nullvert = FMeshVert();
    Points.SetCount(VertsPerFrame*FrameCount,TRUE);
    for( int t=0; t<FrameCount; ++t )
    {            
        // Progress
        CheckCancel();
        ProgressMsg.printf(GetString(IDS_INFO_ANIM),t+1,FrameCount);
        pInt->ProgressUpdate(Progress+((float)t/FrameCount*U3D_PROGRESS_ANIM), FALSE, ProgressMsg.data());
        
        // Set frame
        int frameverts = 0;
        int curframe = FrameStart + t;
        pScene->SetStaticFrame(curframe);
        
        // Write mesh verts
        for( int n=0; n<Nodes.Count(); ++n )
        {
            CheckCancel();

            IGameMesh * mesh = (IGameMesh*)Nodes[n]->GetIGameObject();          
            if( mesh->InitializeData() )
            {
                int vertcount = mesh->GetNumberOfVerts();
                for( int i=0; i<vertcount; ++i )
                {
                    Point3 p;
                    if( mesh->GetVertex(i,p) )
                    {
                        Points[lastvert++] = p;
                    }
                }
                frameverts += vertcount;
            }
            Nodes[n]->ReleaseIGameObject();
        }

        // Check number of verts in this frame
        if( frameverts != VertsPerFrame )
        {
            ProgressMsg.printf(GetString(IDS_ERR_NOVERTS),curframe,frameverts,VertsPerFrame);
            throw MAXException(ProgressMsg.data());
        }
    }
    Progress += U3D_PROGRESS_ANIM;

}
int DoExport( const MCHAR* name, ExpInterface* ei, Interface* i, BOOL suppressPrompts, DWORD options )
{
	UserCoord exporterCoord =
	{
		1,		// right-handed coordinate
		1,		// X axis goes right
		2,		// Y axis goes up
		5,		// Z axis goes out
		1,		// U goes right
		0,		// V goes up
	};

	//// d3d setup
	//UserCoord exporterCoord =
	//{
	//	0,		// left-handed coordinate
	//	0,		// X axis goes left
	//	2,		// Y axis goes up
	//	5,		// Z axis goes out
	//	1,		// U goes right
	//	1,		// V goes down
	//};

	IGameScene*		igScene = GetIGameInterface();

	IGameConversionManager* cm = GetConversionManager();
	cm->SetUserCoordSystem(exporterCoord);

	igScene->InitialiseIGame();

	// export the very first frame
	igScene->SetStaticFrame( 0 );

	RootExporter rootExporter;

	// go through all top nodes
	for ( int i=0; i<igScene->GetTopLevelNodeCount(); i++ )
	{
		IGameNode* gameNode = igScene->GetTopLevelNode( i );
		rootExporter.AddNode( gameNode );
	}

	rootExporter.SaveToFile( name );

	// done
	igScene->ReleaseIGame();

	return TRUE;
}
void Unreal3DExport::Init()
{
    // Init
    CheckCancel();
    pScene = GetIGameInterface();
    GetConversionManager()->SetUserCoordSystem(UnrealCoords);
    if( bExportSelected )
    {
        Tab<INode*> selnodes;;
        for( int i=0; i<pInt->GetSelNodeCount(); ++i )
        {
            INode* n = pInt->GetSelNode(i);
            selnodes.Append(1,&n);
        }
        if( !pScene->InitialiseIGame(selnodes,false)  )
            throw MAXException(GetString(IDS_ERR_IGAME));
    }
    else
    {
        if( !pScene->InitialiseIGame() )
            throw MAXException(GetString(IDS_ERR_IGAME));
    }


    // Enumerate scene
    NodeCount = pScene->GetTotalNodeCount();
    for( int i=0; i<pScene->GetTopLevelNodeCount(); ++i )
    {
        IGameNode * n = pScene->GetTopLevelNode(i);
        ExportNode(n);
    }
    Progress += U3D_PROGRESS_ENUM;


    // Get animation info
    FrameStart = pScene->GetSceneStartTime() / pScene->GetSceneTicks();
    FrameEnd = pScene->GetSceneEndTime() / pScene->GetSceneTicks();
    FrameCount = FrameEnd - FrameStart+1;
    if( FrameCount <= 0 || FrameEnd < FrameStart ) 
    {
        ProgressMsg.printf(GetString(IDS_ERR_FRAMERANGE),FrameStart,FrameEnd);
        throw MAXException(ProgressMsg.data());
    }
    pScene->SetStaticFrame(FrameStart);
}