uiacceltk/hitchcock/coretoolkit/src/huim3gmesh.cpp
changeset 0 15bf7259bb7c
equal deleted inserted replaced
-1:000000000000 0:15bf7259bb7c
       
     1 /*
       
     2 * Copyright (c) 2007 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:   Definition of CHuiM3GMesh, an interface enabling usage of M3G
       
    15  * 				 scene graphs within Hitchcock UI Toolkit user interfaces.
       
    16  *
       
    17 */
       
    18 
       
    19 
       
    20 
       
    21 #include <uiacceltk/huim3gmesh.h>
       
    22 #include <uiacceltk/HuiStatic.h>
       
    23 #include <uiacceltk/HuiEnv.h>
       
    24 
       
    25 // Force softfp linkage until included header has its own definitions
       
    26 #if defined(__ARMCC_VERSION)
       
    27 #pragma push
       
    28 #pragma softfp_linkage
       
    29 #endif
       
    30 #include <M3G/m3g_core.h>
       
    31 #if defined(__ARMCC_VERSION)
       
    32 #pragma pop
       
    33 #endif
       
    34 
       
    35 #include <s32file.h>
       
    36 #include <e32math.h>
       
    37 #include <cstack.h>
       
    38 
       
    39 // ======== LOCAL FUNCTIONS ========
       
    40 
       
    41 // ---------------------------------------------------------------------------
       
    42 // A static function to destroy an M3G object if associated clean up item
       
    43 // is popped from the cleanup stack.
       
    44 // ---------------------------------------------------------------------------
       
    45 //
       
    46 void DestroyM3GObject(TAny* aObject)
       
    47     {
       
    48     m3gDeleteObject( (M3GObject)aObject );
       
    49     }
       
    50 
       
    51 // ======== MEMBER FUNCTIONS ========
       
    52 
       
    53 // ---------------------------------------------------------------------------
       
    54 // Default constructor.
       
    55 // ---------------------------------------------------------------------------
       
    56 //
       
    57 EXPORT_C CHuiM3GMesh::CHuiM3GMesh()
       
    58 : CHuiMesh(EHuiMeshTypeM3G)
       
    59     {
       
    60     }
       
    61 
       
    62 // ---------------------------------------------------------------------------
       
    63 // 2nd phase constructor.
       
    64 // ---------------------------------------------------------------------------
       
    65 //
       
    66 EXPORT_C void CHuiM3GMesh::ConstructL()
       
    67     {
       
    68     iCamRotation[0] = 0;
       
    69     iCamRotation[1] = 0;
       
    70     iCamRotation[2] = 0;
       
    71     iCamRotation[3] = 0;
       
    72     iCamTranslation[0] = 0;
       
    73     iCamTranslation[1] = 0;
       
    74     iCamTranslation[2] = 0;
       
    75     iCamTranslation[3] = 0;
       
    76     }
       
    77 
       
    78 // ---------------------------------------------------------------------------
       
    79 // Load scene from file.
       
    80 // ---------------------------------------------------------------------------
       
    81 //
       
    82 EXPORT_C void CHuiM3GMesh::LoadSceneL(const TDesC& /*aFileName*/)
       
    83     {
       
    84     }
       
    85 
       
    86 EXPORT_C void CHuiM3GMesh::LoadSceneL(const TDesC& aFileName, M3GInterface aInterface)
       
    87     {
       
    88     // Release the previously loaded M3G scene graph
       
    89     ReleaseScene();
       
    90 
       
    91     // Load the new scene graph
       
    92     TRAPD( err, DoLoadSceneL( aFileName, aInterface ) );
       
    93 
       
    94     // If an error occured during the scene loading release the scene and
       
    95     // re-leave.
       
    96     if( err != KErrNone )
       
    97         {
       
    98         ReleaseScene();
       
    99         User::Leave( err );
       
   100         }
       
   101     }
       
   102 
       
   103 void CHuiM3GMesh::DoLoadSceneL(const TDesC& aFileName, M3GInterface aInterface)
       
   104     {
       
   105     // Load M3G content from same path as images
       
   106     TFileName fileName;
       
   107     fileName = CHuiStatic::Env().TextureManager().ImagePath();
       
   108     fileName += aFileName;
       
   109 
       
   110     // Create M3G loader to load the scene from memory buffer.
       
   111     M3GLoader loader = m3gCreateLoader(aInterface);
       
   112     if (loader == NULL)
       
   113         {
       
   114         User::Leave(KErrNoMemory);
       
   115         }
       
   116     TCleanupItem loaderCleaner( &DestroyM3GObject, loader);
       
   117     CleanupStack::PushL(loaderCleaner);
       
   118 
       
   119     // Read the file contents to the memory buffer    
       
   120     RFs fs;
       
   121     CleanupClosePushL(fs);
       
   122     RFileReadStream reader;
       
   123     CleanupClosePushL(reader);
       
   124     User::LeaveIfError(fs.Connect() );
       
   125     TInt errorCode = reader.Open(fs, fileName, EFileRead);
       
   126     if (errorCode != KErrNone)
       
   127         {
       
   128         User::Leave(errorCode);
       
   129         }
       
   130 
       
   131     // Read the contents of the file to a buffer.
       
   132     TInt sceneSize = reader.Source()->SizeL();
       
   133     TUint chunkSize = 1024;
       
   134     TUint8 *buf = new (ELeave) TUint8[chunkSize];
       
   135     CleanupStack::PushL(buf);
       
   136     TInt i = 0;
       
   137     M3Gsizei reqBytes = 1;
       
   138     while (i < sceneSize && reqBytes > 0 ){
       
   139     TInt j = 0;
       
   140     for( j = 0; j < chunkSize && i < sceneSize; j++ )
       
   141         {
       
   142         buf[j] = reader.ReadUint8L();
       
   143         i++;
       
   144         }
       
   145     reqBytes = m3gDecodeData( loader, j, buf );
       
   146     }
       
   147 
       
   148 // Get number of loaded objects
       
   149 iNumObjects = m3gGetLoadedObjects( loader, NULL );
       
   150 
       
   151 // The input buffer from the file is not needed anymore
       
   152 CleanupStack::PopAndDestroy( buf );
       
   153 CleanupStack::PopAndDestroy( &reader );
       
   154 CleanupStack::PopAndDestroy( &fs );
       
   155 
       
   156 // Leave with KErrCorrupt if there were no M3G objects within the file (corrupted file).
       
   157 if( iNumObjects == 0 )
       
   158     {
       
   159     User::Leave( KErrCorrupt );
       
   160     }
       
   161 
       
   162 // Add loaded objects to an object array.
       
   163 iObjects = new (ELeave) M3GObject[iNumObjects];
       
   164 m3gGetLoadedObjects( loader, iObjects );
       
   165 
       
   166 // Add references and find the first world object
       
   167 for( TInt ii = 0; ii < iNumObjects; ii++ )
       
   168     {
       
   169     m3gAddRef( iObjects[ii] );
       
   170     if( m3gGetClass( iObjects[ii] ) == M3G_CLASS_WORLD && !iWorld )
       
   171         {
       
   172         iWorld = (M3GWorld)iObjects[ii];
       
   173         break;
       
   174         }
       
   175     }
       
   176 
       
   177 // Fetch animation controllers from world
       
   178 PopulateAnimationControllerArrayL( (M3GObject)iWorld );
       
   179 
       
   180 iUpdateCamera = EFalse;
       
   181 iCamera = m3gGetActiveCamera(iWorld);
       
   182 if ( !iCamera )
       
   183     {
       
   184     // Create camera if it doesn't exist
       
   185     // The camera is updated according to hitchcock projection and model view matrices
       
   186     iUpdateCamera = ETrue;
       
   187     iCamera = m3gCreateCamera(aInterface);
       
   188     if( iCamera == NULL )
       
   189         {
       
   190         User::Leave( KErrNoMemory );
       
   191         }
       
   192     m3gAddChild( (M3GGroup)iWorld, (M3GNode)iCamera );
       
   193     m3gSetActiveCamera( iWorld, iCamera );
       
   194     M3GMatrix identity;
       
   195     m3gIdentityMatrix( &identity );
       
   196     m3gSetTransform( (M3GTransformable)iCamera, &identity );
       
   197     m3gTranslate( (M3GTransformable)iCamera, 0.f, 0.f, 1.f );
       
   198     m3gSetPerspective( iCamera,90.f, 1.f, 0.1f, 100.f );
       
   199     }
       
   200 
       
   201 // Init background
       
   202 M3GBackground bg = m3gGetBackground( iWorld );
       
   203 if (!bg)
       
   204     {
       
   205     bg = m3gCreateBackground( aInterface );
       
   206     if( bg == NULL )
       
   207         {
       
   208         User::Leave( KErrNoMemory );
       
   209         }
       
   210     m3gSetBackground( iWorld, bg );
       
   211     }
       
   212 m3gSetBgEnable( bg, M3G_SETGET_COLORCLEAR, M3G_FALSE );
       
   213 m3gSetBgEnable( bg, M3G_SETGET_DEPTHCLEAR, M3G_FALSE );
       
   214 
       
   215 // Release the loader
       
   216 CleanupStack::PopAndDestroy( loader );
       
   217 }
       
   218 
       
   219 // ---------------------------------------------------------------------------
       
   220 // Object destructor.
       
   221 // ---------------------------------------------------------------------------
       
   222 //    
       
   223 EXPORT_C CHuiM3GMesh::~CHuiM3GMesh()
       
   224     {
       
   225     ReleaseScene();
       
   226     iControllers.Close();
       
   227     }
       
   228 
       
   229 // ---------------------------------------------------------------------------
       
   230 // Set viewport rect
       
   231 // ---------------------------------------------------------------------------
       
   232 //    
       
   233 EXPORT_C void CHuiM3GMesh::SetViewportRect( const THuiRealRect& aRect )
       
   234     {
       
   235     iViewportRect = aRect;
       
   236     }
       
   237 
       
   238 // ---------------------------------------------------------------------------
       
   239 // Draw scene
       
   240 // ---------------------------------------------------------------------------
       
   241 //    
       
   242 EXPORT_C void CHuiM3GMesh::Draw(CHuiGc& /*aGc*/, const THuiImage* /*aImage*/,
       
   243         const THuiImage* /*aSecondaryImage*/,
       
   244         TReal32 /*aSecondaryAlpha*/) const __SOFTFP
       
   245     {
       
   246     }
       
   247 
       
   248 // ---------------------------------------------------------------------------
       
   249 // Release scene
       
   250 // ---------------------------------------------------------------------------
       
   251 //    
       
   252 EXPORT_C void CHuiM3GMesh::ReleaseScene()
       
   253     {
       
   254     if (iObjects)
       
   255         {
       
   256         for (TInt i = 0; i < iNumObjects; i++)
       
   257             {
       
   258             m3gDeleteRef(iObjects[i]);
       
   259             iObjects[i] = NULL;
       
   260             }
       
   261         delete [] iObjects;
       
   262         iNumObjects = 0;
       
   263         iObjects = NULL;
       
   264         iWorld = NULL;
       
   265         }
       
   266     iControllers.Reset();
       
   267     }
       
   268 
       
   269 // ---------------------------------------------------------------------------
       
   270 // Rotate world around Y axis
       
   271 // ---------------------------------------------------------------------------
       
   272 //    
       
   273 EXPORT_C void CHuiM3GMesh::RotateYaw(TReal32 aAngle) __SOFTFP
       
   274     {
       
   275     RotateObjects(aAngle, 0.0f, 1.0f, 0.0f);
       
   276     }
       
   277 
       
   278 // ---------------------------------------------------------------------------
       
   279 // Rotate world around X axis
       
   280 // ---------------------------------------------------------------------------
       
   281 //    
       
   282 EXPORT_C void CHuiM3GMesh::RotatePitch(TReal32 aAngle) __SOFTFP
       
   283     {
       
   284     RotateObjects(aAngle, 1.0f, 0.0f, 0.0f);
       
   285     }
       
   286 
       
   287 // ---------------------------------------------------------------------------
       
   288 // Find the center point of the group
       
   289 // ---------------------------------------------------------------------------
       
   290 //    
       
   291 void CHuiM3GMesh::FindObjectCenter(M3GGroup aGroup, TReal32 aTrans[4],
       
   292         TBool aFirst)
       
   293     {
       
   294     TReal32 objectTrans[4];
       
   295     TInt numChildren = m3gGetChildCount(aGroup);
       
   296     for (TInt i = 0; i < numChildren; ++i)
       
   297         {
       
   298         M3GNode child = m3gGetChild((M3GGroup)aGroup, i);
       
   299         M3GClass m3gClass = m3gGetClass((M3GObject)child);
       
   300         M3GMesh mesh = (M3GMesh)child;
       
   301 
       
   302         if (m3gClass == M3G_CLASS_SKINNED_MESH || m3gClass == M3G_CLASS_MESH
       
   303                 || m3gClass == M3G_CLASS_MORPHING_MESH)
       
   304             {
       
   305 
       
   306             // Set blending and culling
       
   307             M3GAppearance hApp = m3gGetAppearance(mesh, 0);
       
   308 
       
   309             /*
       
   310              M3GCompositingMode cm = m3gGetCompositingMode(hApp);
       
   311              m3gSetBlending(cm, M3G_REPLACE);
       
   312              m3gSetAlphaWriteEnable(cm, 0);
       
   313              m3gSetCompositingMode(hApp, cm);
       
   314              m3gSetAppearance( mesh, 0, hApp );
       
   315              */
       
   316 
       
   317             M3GPolygonMode pm = m3gGetPolygonMode(hApp);
       
   318             m3gSetCulling(pm, M3G_CULL_NONE);
       
   319             m3gSetPolygonMode(hApp, pm);
       
   320 
       
   321             m3gGetTranslation((M3GTransformable)mesh, objectTrans);
       
   322             if (!aFirst)
       
   323                 {
       
   324                 aTrans[0] = (aTrans[0] + objectTrans[0]) / 2;
       
   325                 aTrans[1] = (aTrans[1] + objectTrans[1]) / 2;
       
   326                 aTrans[2] = (aTrans[2] + objectTrans[2]) / 2;
       
   327                 aTrans[3] = (aTrans[3] + objectTrans[3]) / 2;
       
   328                 }
       
   329             else
       
   330                 {
       
   331                 aTrans[0] = objectTrans[0];
       
   332                 aTrans[1] = objectTrans[1];
       
   333                 aTrans[2] = objectTrans[2];
       
   334                 aTrans[3] = objectTrans[3];
       
   335                 aFirst = EFalse;
       
   336                 }
       
   337             }
       
   338         else
       
   339             if (m3gClass == M3G_CLASS_GROUP || m3gClass == M3G_CLASS_WORLD)
       
   340                 {
       
   341                 m3gGetTranslation((M3GTransformable)mesh, objectTrans);
       
   342                 if (!aFirst)
       
   343                     {
       
   344                     aTrans[0] = (aTrans[0] + objectTrans[0]) / 2;
       
   345                     aTrans[1] = (aTrans[1] + objectTrans[1]) / 2;
       
   346                     aTrans[2] = (aTrans[2] + objectTrans[2]) / 2;
       
   347                     aTrans[3] = (aTrans[3] + objectTrans[3]) / 2;
       
   348                     }
       
   349                 else
       
   350                     {
       
   351                     aTrans[0] = objectTrans[0];
       
   352                     aTrans[1] = objectTrans[1];
       
   353                     aTrans[2] = objectTrans[2];
       
   354                     aTrans[3] = objectTrans[3];
       
   355                     aFirst = EFalse;
       
   356                     }
       
   357                 FindObjectCenter((M3GGroup)child, aTrans, aFirst);
       
   358                 }
       
   359             else
       
   360                 {
       
   361                 // For PC-lint
       
   362                 }
       
   363         }
       
   364 
       
   365     }
       
   366 
       
   367 // ---------------------------------------------------------------------------
       
   368 // Rotates camera around worlds objects.
       
   369 // ---------------------------------------------------------------------------
       
   370 //    
       
   371 void CHuiM3GMesh::RotateObjects(TReal32 aAngle, TReal32 aAxisX,
       
   372         TReal32 aAxisY, TReal32 aAxisZ)
       
   373 __SOFTFP        {
       
   374         if(iWorld != 0)
       
   375             {
       
   376 
       
   377             // Find the center of the objects
       
   378             TReal32 objectTrans[4];
       
   379             objectTrans[0] = 0;
       
   380             objectTrans[1] = 0;
       
   381             objectTrans[2] = 0;
       
   382             objectTrans[3] = 0;
       
   383             FindObjectCenter((M3GGroup)iWorld,objectTrans);
       
   384 
       
   385             // Get camera position
       
   386             M3GCamera cam = iCamera;
       
   387             TReal32 trans[4];
       
   388             m3gGetTranslation((M3GTransformable)cam, trans);
       
   389 
       
   390             // Calculate distance of objects center point and camera
       
   391             objectTrans[0] = objectTrans[0]-trans[0];
       
   392             objectTrans[1] = objectTrans[1]-trans[1];
       
   393             objectTrans[2] = objectTrans[2]-trans[2];
       
   394             objectTrans[3] = objectTrans[3]-trans[3];
       
   395             TReal dist;
       
   396             Math().Sqrt(dist,(TReal)objectTrans[0]*objectTrans[0]+objectTrans[1]*objectTrans[1]+objectTrans[2]*objectTrans[2]);
       
   397 
       
   398             // Get orientation of camera
       
   399             TReal32 rot[4];
       
   400             m3gGetOrientation((M3GTransformable)cam, rot);
       
   401 
       
   402             // Create new camera vector
       
   403             M3GVec4 camZ;
       
   404             camZ.x = 0;
       
   405             camZ.y = 0;
       
   406             camZ.z = -dist;
       
   407             camZ.w = 1;
       
   408 
       
   409             // Create rotation matrix for camera (old position)
       
   410             M3GMatrix rotmtx;
       
   411             m3gIdentityMatrix(&rotmtx);
       
   412             m3gPostRotateMatrix(&rotmtx, rot[0], rot[1], rot[2], rot[3]);
       
   413 
       
   414             // Create rotation matrix for camera (new position)
       
   415             M3GMatrix mtx;
       
   416             m3gIdentityMatrix(&mtx);
       
   417             m3gPostRotateMatrix(&mtx, aAngle, aAxisX, aAxisY, aAxisZ);
       
   418 
       
   419             // Translate camera vector
       
   420             m3gTransformVec4(&rotmtx,&camZ);
       
   421 
       
   422             // Store this position
       
   423             M3GVec4 trans2;
       
   424             trans2.x = camZ.x;
       
   425             trans2.y = camZ.y;
       
   426             trans2.z = camZ.z;
       
   427             trans2.w = camZ.w;
       
   428 
       
   429             // create new camera vector
       
   430             camZ.x = 0;
       
   431             camZ.y = 0;
       
   432             camZ.z = -dist;
       
   433             camZ.w = 1;
       
   434 
       
   435             // transform this vector with new position matrix
       
   436             m3gTransformVec4(&mtx,&camZ);
       
   437 
       
   438             // transform with old position matrix
       
   439             m3gTransformVec4(&rotmtx,&camZ);
       
   440 
       
   441             // move and rotate camerea based on these calculations
       
   442             m3gPostRotate((M3GTransformable)cam,aAngle, aAxisX, aAxisY, aAxisZ);
       
   443             m3gTranslate((M3GTransformable)cam, -camZ.x+trans2.x, -camZ.y+trans2.y, -camZ.z+trans2.z);
       
   444 
       
   445             m3gAlignNode((M3GNode)cam,NULL);
       
   446             }
       
   447         }
       
   448 
       
   449     // ---------------------------------------------------------------------------
       
   450     // Store camera transformations
       
   451     // ---------------------------------------------------------------------------
       
   452     //    
       
   453 EXPORT_C void CHuiM3GMesh::StoreCamera()
       
   454     {
       
   455     if(iWorld != 0)
       
   456         {
       
   457         m3gGetOrientation((M3GTransformable)iCamera, iCamRotation);
       
   458         m3gGetTranslation((M3GTransformable)iCamera, iCamTranslation);
       
   459         }
       
   460     }
       
   461 
       
   462 // ---------------------------------------------------------------------------
       
   463 // Restore camera transformations
       
   464 // ---------------------------------------------------------------------------
       
   465 //    	
       
   466 EXPORT_C void CHuiM3GMesh::RestoreCamera()
       
   467     {
       
   468     if(iWorld != 0)
       
   469         {
       
   470         m3gSetOrientation((M3GTransformable)iCamera, iCamRotation[0],iCamRotation[1],iCamRotation[2],iCamRotation[3]);
       
   471         m3gSetTranslation((M3GTransformable)iCamera, iCamTranslation[0],iCamTranslation[1],iCamTranslation[2]);
       
   472         }
       
   473     }
       
   474 
       
   475 // ---------------------------------------------------------------------------
       
   476 // Fetch animation controllers from any M3G object (recursively).
       
   477 // ---------------------------------------------------------------------------
       
   478 //   
       
   479 void CHuiM3GMesh::PopulateAnimationControllerArrayL(const M3GObject aObject)
       
   480     {
       
   481     // Array for storing the already visited M3G object pointers.
       
   482     RPointerArray<M3GObjectImpl> visited;
       
   483     CleanupClosePushL(visited);
       
   484 
       
   485     // Stack for iteratively traversing through the object reference net.
       
   486     CStack<M3GObjectImpl, EFalse>* stack = new ( ELeave ) CStack<M3GObjectImpl, EFalse>();
       
   487     CleanupStack::PushL(stack);
       
   488 
       
   489     // Push the initial M3G object to the iteration stack
       
   490     stack->PushL(aObject);
       
   491 
       
   492     // Iterate through all objects in the iteration stack
       
   493     while ( !stack->IsEmpty() )
       
   494         {
       
   495         // Pop the current M3G object
       
   496         M3GObject current = stack->Pop();
       
   497 
       
   498         // If animation controller, add to array
       
   499         M3GClass m3gClass = m3gGetClass(current);
       
   500         if (m3gClass == M3G_CLASS_ANIMATION_CONTROLLER)
       
   501             {
       
   502             M3GAnimationController controller =
       
   503                     (M3GAnimationController)current;
       
   504 
       
   505             THuiM3GMeshAnimationController newController;
       
   506             newController.iController = controller;
       
   507             newController.iPosition = THuiTimedValue( 0,
       
   508                     EHuiTimedValueStyleLinear);
       
   509             newController.iAnimating = EFalse;
       
   510             newController.iTimeOffset = 0;
       
   511             newController.iPreviousTime
       
   512                     = CHuiStatic::MilliSecondsSinceStart();
       
   513             iControllers.AppendL(newController);
       
   514             }
       
   515 
       
   516         // Traverse graph
       
   517         const TInt count = m3gGetReferences(current, NULL, 0);
       
   518         if (count)
       
   519             {
       
   520             visited.ReserveL(visited.Count() + count);
       
   521             M3GObject* refs = new M3GObject[count];
       
   522             User::LeaveIfNull(refs);
       
   523             CleanupArrayDeletePushL(refs);
       
   524 
       
   525             m3gGetReferences(current, refs, count);
       
   526             for (TInt i = 0; i < count; i++)
       
   527                 {
       
   528                 TInt index = 0;
       
   529                 TInt err = visited.FindInAddressOrder(refs[i], index);
       
   530                 if (err != KErrNone)
       
   531                     {
       
   532                     visited.InsertL(refs[i], index);
       
   533                     stack->PushL(refs[i]);
       
   534                     }
       
   535                 }
       
   536 
       
   537             CleanupStack::PopAndDestroy(refs);
       
   538             }
       
   539         }
       
   540 
       
   541     CleanupStack::PopAndDestroy(stack);
       
   542     CleanupStack::PopAndDestroy( &visited);
       
   543     }
       
   544 
       
   545 // ---------------------------------------------------------------------------
       
   546 // Returns number of (hitchcock) animation controllers
       
   547 // ---------------------------------------------------------------------------
       
   548 //    
       
   549 EXPORT_C TInt CHuiM3GMesh::AnimationControllerCount() const
       
   550     {
       
   551     TInt baseAnimations = CHuiMesh::AnimationControllerCount();
       
   552     TInt count = iControllers.Count();
       
   553     return baseAnimations + count;
       
   554     }
       
   555 
       
   556 // ---------------------------------------------------------------------------
       
   557 // Set position of animation in animation controller.		
       
   558 // ---------------------------------------------------------------------------
       
   559 //    
       
   560 EXPORT_C void CHuiM3GMesh::SetAnimationPosition(TInt aControllerId, TReal32 aTarget, TInt aTime) __SOFTFP
       
   561     {
       
   562     if(iWorld != 0)
       
   563         {
       
   564         // Animate every controller
       
   565         if(aControllerId == KHuiM3GAnimateAllControllers )
       
   566             {
       
   567             for(int i = 0; i < iControllers.Count(); i++)
       
   568                 {
       
   569                 ((iControllers[i]).iPosition).Set(aTarget-((iControllers[i]).iTimeOffset), aTime);
       
   570                 }
       
   571             }
       
   572         else // Animate only aControllerId:s controller
       
   573 
       
   574             {
       
   575             for(int i = 0; i < iControllers.Count(); i++)
       
   576                 {
       
   577                 if(aControllerId == m3gGetUserID((M3GObject)(iControllers[i].iController)))
       
   578                     {
       
   579                     ((iControllers[i]).iPosition).Set(aTarget-((iControllers[i]).iTimeOffset), aTime);
       
   580                     }
       
   581                 }
       
   582             }
       
   583         }
       
   584     }
       
   585 
       
   586 // ---------------------------------------------------------------------------
       
   587 // Go through animation controllers and does animations.		
       
   588 // ---------------------------------------------------------------------------
       
   589 //    
       
   590 EXPORT_C void CHuiM3GMesh::AnimateControllers()
       
   591     {
       
   592     if(iWorld != 0)
       
   593         {
       
   594         TInt currentTime = CHuiStatic::MilliSecondsSinceStart();
       
   595         for(TInt i = 0; i < iControllers.Count(); i++)
       
   596             {
       
   597             M3GAnimationController controller = (iControllers[i]).iController;
       
   598             if( ( (iControllers[i]).iPosition).Changed())
       
   599                 {
       
   600                 ((iControllers[i]).iPosition).ClearChanged();
       
   601                 }
       
   602             else
       
   603                 {
       
   604                 if ( (iControllers[i]).iAnimating )
       
   605                     {
       
   606                     TInt timeChanged = currentTime-((iControllers[i]).iPreviousTime);
       
   607                     ((iControllers[i]).iTimeOffset) += timeChanged;
       
   608                     }
       
   609                 }
       
   610             ((iControllers[i]).iPreviousTime) = currentTime;
       
   611             m3gSetPosition(controller, ((iControllers[i]).iPosition).Now() + ((iControllers[i]).iTimeOffset), currentTime);
       
   612             m3gSetActiveInterval(controller, currentTime, currentTime);
       
   613             }
       
   614         m3gAnimate((M3GObject)iWorld, currentTime);
       
   615         }
       
   616     }
       
   617 
       
   618 // ---------------------------------------------------------------------------
       
   619 // Start animation of the controller
       
   620 // ---------------------------------------------------------------------------
       
   621 //    
       
   622 EXPORT_C void CHuiM3GMesh::StartAnimationController(TInt aControllerId)
       
   623     {
       
   624     if(iWorld != 0)
       
   625         {
       
   626         // Start every animation controller
       
   627         if(aControllerId == KHuiM3GAnimateAllControllers )
       
   628             {
       
   629             for(int i = 0; i < iControllers.Count(); i++)
       
   630                 {
       
   631                 iControllers[i].iAnimating = ETrue;
       
   632                 TInt currentTime = CHuiStatic::MilliSecondsSinceStart();
       
   633                 m3gSetActiveInterval((iControllers[i]).iController, currentTime, currentTime);
       
   634                 }
       
   635             }
       
   636         else // Start only aControllerId:s controller
       
   637 
       
   638             {
       
   639             for(int i = 0; i < iControllers.Count(); i++)
       
   640                 {
       
   641                 if(aControllerId == m3gGetUserID((M3GObject)(iControllers[i].iController)))
       
   642                     {
       
   643                     iControllers[i].iAnimating = ETrue;
       
   644                     TInt currentTime = CHuiStatic::MilliSecondsSinceStart();
       
   645                     m3gSetActiveInterval((iControllers[i]).iController, currentTime, currentTime);
       
   646                     }
       
   647                 }
       
   648             }
       
   649         }
       
   650     }
       
   651 
       
   652 // ---------------------------------------------------------------------------
       
   653 // Stop animation of the controller
       
   654 // ---------------------------------------------------------------------------
       
   655 //    
       
   656 EXPORT_C void CHuiM3GMesh::StopAnimationController(TInt aControllerId)
       
   657     {
       
   658     if(iWorld != 0)
       
   659         {
       
   660         // Stop every animation controller
       
   661         if(aControllerId == KHuiM3GAnimateAllControllers )
       
   662             {
       
   663             for(int i = 0; i < iControllers.Count(); i++)
       
   664                 {
       
   665                 iControllers[i].iAnimating = EFalse;
       
   666                 TInt currentTime = CHuiStatic::MilliSecondsSinceStart();
       
   667                 m3gSetActiveInterval((iControllers[i]).iController, currentTime-2, currentTime-1);
       
   668                 }
       
   669             }
       
   670         else // Stop only aControllerId:s controller
       
   671 
       
   672             {
       
   673             for(int i = 0; i < iControllers.Count(); i++)
       
   674                 {
       
   675                 if(aControllerId == m3gGetUserID((M3GObject)(iControllers[i].iController)))
       
   676                     {
       
   677                     iControllers[i].iAnimating = EFalse;
       
   678                     TInt currentTime = CHuiStatic::MilliSecondsSinceStart();
       
   679                     m3gSetActiveInterval((iControllers[i]).iController, currentTime-2, currentTime-1);
       
   680                     }
       
   681                 }
       
   682             }
       
   683         }
       
   684     }
       
   685 
       
   686 EXPORT_C void CHuiM3GMesh::M3GMeshExtension(const TUid& /*aExtensionUid*/, TAny** aExtensionParams)
       
   687     {
       
   688     // If no extension with given UID was found, indicate it by returning null
       
   689     *aExtensionParams = NULL;
       
   690     }
       
   691