xFile_Loader.cpp

00001 #include "xFile_Loader.h"
00002 
00006 xFile_Loader::xFile_Loader()
00007 {
00008         FileName = NULL;
00009         root = NULL;
00010         rootFrame = NULL;
00011         verbose = true;
00012         initialise();
00013 }
00014 
00018 xFile_Loader::~xFile_Loader()
00019 {
00020         cleanUp();
00021 }
00022 
00027 bool xFile_Loader::initialise()
00028 {
00029         back1 = "";
00030         back2 ="";
00031         current = "";
00032         return true;
00033 }
00034 
00039 bool xFile_Loader::cleanUp()
00040 {       
00041         if(rootFrame)delete rootFrame;
00042         parents.empty();
00043         clearHierarchy(root);
00044         return true;
00045 }
00046 
00051 bool xFile_Loader::reset()
00052 {
00053         cleanUp();
00054         initialise();
00055 
00056         return true;
00057 }
00058 
00065 bool xFile_Loader::load(LPCSTR fileName,bool verboseOutput)
00066 {       
00067 
00068         reset();
00069 
00070         verbose = verboseOutput;
00071 
00072         // Make sure filename is specified
00073         if(!fileName)
00074         {
00075                 debug("xFile_Loader::loadXFile : FileName Not Specified");
00076                 return false;
00077         }
00078 
00079         FileName = strdup(fileName);    
00080         oFile.open(FileName,ifstream::in);
00081 
00082         // Check that we opened the file okay
00083         if(!oFile.is_open())
00084         {
00085                 debug("Error - Cannot open file.");
00086                 return false;
00087         }
00088 
00089         if(!oFile.good())
00090         {
00091                 debug("Error - File is corrupt!");
00092                 return false;
00093         }
00094 
00095 
00096         // Get the very first line of the .x file
00097         bool checkHeader = false;
00098         if(checkHeader)
00099         {
00100                 char header[100];
00101                 oFile.getline(header,sizeof(header));
00102                 strcat(header,"\0");
00103                 cout << header << "\n";
00104                 if( strcmp("xof 0303txt 0032",header) != 0 )
00105                 {
00106                         debug("Error - This file is not a compliant .x text file");
00107                         return false;
00108                 }
00109         }
00110 
00111 
00112         //Push the a root onto the parents stack (this will be the parent of everything)
00113         root = new stTemplate();
00114         root->Name = "ROOT";
00115         root->type = getType("Header");
00116         parents.push(root);
00117 
00118         //Initialise the root Frame.
00119         rootFrame = new Frame();
00120         rootFrame->Name = "ROOT_FRAME";
00121         rootFrame->transform = &worldTransform;
00122 
00123         processBlocks();
00124 
00125         oFile.close();
00126 
00127         return true;
00128 }
00129 
00130 
00131 
00136 void xFile_Loader::outputTemplateHierarchyAsText()
00137 {
00138         displayHierarchy(root);
00139 }
00140 
00145 void xFile_Loader::displayHierarchy(stTemplate* parent)
00146 {
00147         if(!parent)
00148                 return;
00149 
00150         static int depth = 0;
00151         char p = 192;
00152         char line = '-';
00153         for(int i = 0; i < depth; i ++)
00154         {
00155                 if(i == 0)
00156                         cout << p;
00157 
00158                 cout << line;
00159         }
00160         cout   << parent->Name << " [" << parent->type->str << "]"<<"\n";
00161 
00162         depth ++;
00163         for(UINT i = 0; i < parent->children.size(); i++)
00164         {
00165                 displayHierarchy(parent->children[i]);
00166         }
00167         depth --;
00168 }
00169 
00174 void xFile_Loader::clearHierarchy(stTemplate* parent)
00175 {
00176         if(!parent)
00177                 return;
00178 
00179         for(UINT i = 0; i < parent->children.size(); i++)
00180         {
00181                 clearHierarchy(parent->children[i]);
00182         }
00183 
00184         delete parent; 
00185         parent = NULL;
00186 }
00187 
00195 const stXDefines* xFile_Loader::getType(LPCSTR str) 
00196 {
00197         // Use Templates list as look up table
00198         int iTemplates = sizeof(DirectX_FileTemplates)/sizeof(stXDefines);
00199         int iTemplateIndex = 0;
00200 
00201         for(iTemplateIndex =0; iTemplateIndex<iTemplates; iTemplateIndex++)
00202         {
00203                 if( strcmp(DirectX_FileTemplates[iTemplateIndex].str, str) == 0 )
00204                 {
00205                         return &DirectX_FileTemplates[iTemplateIndex];                                          // We have a match, return the current template.
00206                 }
00207         }
00208 
00209         return &DirectX_FileTemplates[0];                                                                                       // No match return UNKNOWN.          
00210 }
00211 
00216 void xFile_Loader::outputFrameHierarchyAsText()
00217 {
00218         displayFrameHierachy(rootFrame);
00219 }
00220 
00221 
00227 void xFile_Loader::displayFrameHierachy(Frame *parent)
00228 {
00229         if(!parent)
00230                 return;
00231 
00232         static int depth = 0;
00233 
00234         char p = 192;
00235 
00236         for(int i = 0; i < depth; i ++)
00237         {
00238                 if(i == 0)
00239                         cout << p;
00240                 cout << "-";
00241         }
00242         cout << parent->Name << "\n";
00243 
00244         /*
00245         for(UINT i = 0; i < parent->meshes.size(); i++)
00246         {
00247                 cout << p <<"--" << parent->meshes[i]->Name << "\n";
00248 
00249                 for(UINT j = 0; j < parent->meshes[i]->materialList->materials.size(); j++)
00250                 {
00251                         cout << p <<"---" << parent->meshes[i]->materialList->materials[j]->name << "\n";
00252 
00253                         for(UINT k = 0; k < parent->meshes[i]->materialList->materials[j]->textureFileNames.size(); k++)
00254                         {
00255                                 cout << p <<"----" << parent->meshes[i]->materialList->materials[j]->textureFileNames[k]->szString << "\n";
00256                         }
00257                 }
00258         }
00259 */
00260         depth ++;
00261         for(UINT i = 0; i < parent->childFrames.size(); i++)
00262         {
00263                 displayFrameHierachy(parent->childFrames[i]);
00264         }
00265         depth --;
00266 }
00267 
00273 bool xFile_Loader::processBlocks()
00274 {
00275         while(!(oFile.eof() && current == back1 && back1 == back2))                                     // If we have reached the end of the file and back1 and back2 have overlapped to contain current, we are done.
00276         {
00277 
00278                 getNext();      // Read next piece of data
00279 
00280                 static int depth = 0;
00281 
00282                 if(current == "{")                                                                                                              // We have reached the Start of a new Block.
00283                 {
00284                         const stXDefines* currentTemplate = NULL;                                                       // Stores the Template type of the current Block being processed.
00285 
00286                         if((currentTemplate = getType(back2.c_str()))->ID == X_UNSUPPORTED)     // If the Block is named, its type will be back2 from the '{'
00287                         {
00288                                 currentTemplate = getType(back1.c_str());                                               // Otherwise its type will be back 1 from the '{'
00289                         }
00290 
00291                         newTemplate =  new stTemplate();
00292                         newTemplate->Name = back1;                                                                                      // The Name will either be the one from the file or the type name
00293                         newTemplate->type = currentTemplate;                                                            // The Type we got earlier, this may be X_UNSUPPORTED
00294                         newTemplate->heirachyDepth = depth;                                                                     // Store the hierarchical depth of this Block
00295 
00296                         if(parents.size() > 0)
00297                         {
00298                                 newTemplate->parent = parents.top();                                                    // This Blocks parent is on the top of the parents stack.
00299                                 newTemplate->parent->children.push_back(newTemplate);                   // Add this Block to the children of its parent (double linked list).
00300                         }
00301 
00302                         parents.push(newTemplate);                                                                                      // push this Block onto the top of the parents stack.
00303                         depth ++;                                                                                                                       // increment the depth.
00304 
00305                         switch(currentTemplate->ID)                                                                                     // Call the relevant function to extract the data from supported Block types.
00306                         {
00307                         case(X_FRAME):
00308                                 {
00309                                         newTemplate->pdata = processFrame();
00310                                 }break; 
00311                         case(X_FRAMETRANSFORMMATRIX):
00312                                 {
00313                                         newTemplate->pdata = processFrameTransformationMatrix();
00314                                 }break;
00315                         case(X_MESH):
00316                                 {
00317                                         newTemplate->pdata = processMesh();
00318                                 }break;
00319                         case(X_MESHNORMALS):
00320                                 {
00321                                         newTemplate->pdata = processMeshNormals();
00322                                 }break;
00323                         case(X_MESHTEXTURECOORDS):
00324                                 {
00325                                         newTemplate->pdata = processMeshTextureCoords();
00326                                 }break;
00327                         case(X_MESHMATERIALLIST):
00328                                 {
00329                                         newTemplate->pdata = processMeshMaterilalList();
00330                                 }break;
00331                         case(X_MATERIAL):
00332                                 {
00333                                         newTemplate->pdata = processMaterial();
00334                                 }break;
00335                         case(X_TEXTUREFILENAME):
00336                                 {
00337                                         newTemplate->pdata = processTextureFileName();
00338                                 }break;
00339                         case(X_EFFECTINSTANCE):
00340                                 {
00341                                         newTemplate->pdata = processEffectInstance();
00342                                 }break;
00343                         case(X_EFFECTPARAMDWORD):
00344                                 {
00345                                         newTemplate->pdata = processEffectParamDword();
00346                                 }break;
00347                         case(X_EFFECTPARAMSTRING):
00348                                 {
00349                                         newTemplate->pdata = processEffectPramString();
00350                                 }break;
00351                         case(X_EFFECTPARAMFLOATS):
00352                                 {
00353                                         newTemplate->pdata = processEffectParamFloats();
00354                                 }break;
00355                         case(X_ANIMATIONSET):
00356                                 {
00357                                         newTemplate->pdata = processAnimationSet();
00358                                 }break;
00359                         case(X_ANIMATION):
00360                                 {
00361                                         newTemplate->pdata = processAnimation();
00362                                 }break;
00363                         case(X_ANIMATIONKEY):
00364                                 {
00365                                         newTemplate->pdata = processAnimationKey();
00366                                 }break;
00367                         case(X_ANIMATIONOPTIONS):
00368                                 {
00369                                         newTemplate->pdata = processAnimationOptions();
00370                                 }break;
00371                         }               
00372                 }
00373                 else if(current == "}")                                                                                                         // We have reached the end of a block.
00374                 {
00375                         if(parents.size()> 0)                                                                                           
00376                                 parents.pop();                                                                                                          // Pop this block off the parents stack.
00377                         depth --;                                                                                                                               // Decrement the depth.
00378                 }
00379 
00380         }
00381 
00382         return true;
00383 }
00384 
00389 Mesh* xFile_Loader::processMesh()
00390 {
00391 
00392         Mesh* mesh = new Mesh();
00393         meshes.push_back(mesh);
00394 
00395         //if the parent template is a frame, add this mesh to it
00396         if(newTemplate->parent && newTemplate->parent->type && newTemplate->parent->type->ID == X_FRAME)
00397         {
00398                 Frame* parantFrame = (Frame*)newTemplate->parent->pdata;
00399                 parantFrame->nMeshes ++;
00400                 parantFrame->meshes.push_back(mesh);
00401 
00402                 mesh->pFrame = parantFrame;
00403 
00404         }
00405 
00406         // Set the meshes Name.
00407         mesh->Name = newTemplate->Name;
00408 
00409         // get Next Piece of data
00410         getNext();
00411 
00412         //Read the Number of Vertices.
00413         mesh->nVertices = atoi(current.c_str());
00414 
00415         if(verbose)
00416         {
00417                 cout << "Num Vertices: " << mesh->nVertices << "\n";
00418         }
00419 
00420         //Fill array of n Vertices.
00421         mesh->vertices = new Vector[mesh->nVertices];
00422 
00423         //Temp Values used to Calculate the Radius of the mesh (the furthest vertex from the origin in model space)
00424         float tempRadius = 0.0f;
00425 
00426         //Read Vertices
00427         for(DWORD i = 0; i < mesh->nVertices; i++)
00428         {
00429                 static float vecValues[3] = {0.0f,0.0f,0.0f};
00430                 int currentVecIndex = 0;
00431                 string currentVal = "";
00432                 char currentChar;
00433 
00434                 //get Next Piece of data
00435                 getNext();
00436 
00437                 //Seperate each line of the array into a vector.
00438                 for(UINT j = 0; j < current.length(); j ++)
00439                 {
00440                         currentChar = current.c_str()[j];
00441 
00442                         //We are at the next vector in the array.
00443                         if(currentChar == ',')
00444                         {
00445                                 currentVecIndex = 0;
00446                                 break;
00447                         }
00448                         //We are at the next value in the vector.
00449                         if(currentChar == ';')
00450                         {
00451                                 vecValues[currentVecIndex] = (float)atof(currentVal.c_str());
00452                                 currentVecIndex ++;
00453                                 currentVal = "";
00454                         }else
00455                         {
00456                                 //Concatinate the current value.
00457                                 currentVal += currentChar;
00458                         }
00459                 }
00460 
00461                 mesh->vertices[i] = Vector(vecValues[0],vecValues[1],vecValues[2]);
00462 
00463                 //Radius = absolute maximum x or y or z
00464                 tempRadius = (max(max(fabs(mesh->vertices[i].x),fabs(mesh->vertices[i].y)),fabs(mesh->vertices[i].z)));
00465                 if(tempRadius > mesh->radius)
00466                         mesh->radius = tempRadius;
00467 
00468                 //Bounding Box Lowest & Highest Values on the X,Y and Z axes.
00469                 if(mesh->vertices[i].x < mesh->minX) mesh->minX = mesh->vertices[i].x; 
00470                 if(mesh->vertices[i].y < mesh->minY) mesh->minY = mesh->vertices[i].y; 
00471                 if(mesh->vertices[i].z < mesh->minZ) mesh->minZ = mesh->vertices[i].z;
00472                 if(mesh->vertices[i].x > mesh->maxX) mesh->maxX = mesh->vertices[i].x;
00473                 if(mesh->vertices[i].y > mesh->maxY) mesh->maxY = mesh->vertices[i].y;
00474                 if(mesh->vertices[i].z > mesh->maxZ) mesh->maxZ = mesh->vertices[i].z;
00475 
00476                 if(verbose)
00477                 {
00478                 cout <<"        Vertex ["<< i << "] : " << mesh->vertices[i].x << " , " << mesh->vertices[i].y << " , " << mesh->vertices[i].z << "\n";
00479                 }
00480         }
00481 
00482         if(verbose)
00483         {
00484                 cout << "Mesh Radius: " << mesh->radius << "\n";
00485                 cout << "Mesh Bounding Box Min: " << mesh->minX << "," << mesh->minY << "," << mesh->minZ << "\n";
00486                 cout << "Mesh Bounding Box Max: " << mesh->maxX << "," << mesh->maxY << "," << mesh->maxZ << "\n";
00487                 cout << "Mesh Size (untransformed):" << mesh->maxX - mesh->minX << "," << mesh->maxY - mesh->minY << "," << mesh->maxZ - mesh->minZ << "\n";
00488         }
00489 
00490         getNext();              // Sync
00491 
00492         //Read the Number of Faces 
00493         mesh->nFaces = atoi(current.c_str());
00494         
00495         if(verbose)
00496         {
00497                 cout << "Num Faces: " << mesh->nFaces << "\n";
00498         }
00499 
00500         //Fill Array of n Faces.
00501         mesh->faces = new MeshFace[mesh->nFaces];
00502         for(DWORD i = 0; i < mesh->nFaces; i++)
00503         {
00504                 getNext();
00505 
00506                 //Read Indices
00507                 string currentVal = "";
00508                 char currentChar = ' ';
00509 
00510                 //Get the First Value (before the first semi-colon) this is the number of face Indices
00511                 UINT j = 0;
00512                 int colonCount = 0;
00513                 int currentFaceIndex = 0;
00514                 for(j; j < current.length(); j ++)
00515                 {
00516                         if(currentChar == ';' && colonCount == 0)
00517                         {
00518                                 //Store the number of indices and create an array of DWORDS of that scale.
00519                                 mesh->faces[i].numFaceVertexIndices = (DWORD)atoi(currentVal.c_str());
00520                                 mesh->faces[i].faceVertexIndices =  new DWORD[mesh->faces[i].numFaceVertexIndices];
00521                                 currentVal = "";
00522                                 colonCount ++;
00523                         }
00524                         else if(currentChar == ',' || (currentChar == ';' && colonCount > 0))
00525                         {
00526                                 //Store each index
00527                                 mesh->faces[i].faceVertexIndices[currentFaceIndex] = (DWORD)atoi(currentVal.c_str());
00528                                 currentFaceIndex++;
00529                                 currentVal = "";
00530                         }else
00531                         {
00532                                 currentVal += currentChar;
00533                         }
00534 
00535                         currentChar = current.c_str()[j];
00536 
00537                 }
00538 
00539                 // Output data
00540                 if(verbose)
00541                 {
00542                         cout << "       Num Face Indices " << mesh->faces[i].numFaceVertexIndices << "\n";
00543                         for(DWORD p = 0; p < mesh->faces[i].numFaceVertexIndices; p ++)
00544                         {
00545                                 cout << "               Index [" << p << "] : " << mesh->faces[i].faceVertexIndices[p] << "\n";
00546                         }
00547                 }
00548 
00549 
00550         }
00551 
00552 
00553         return mesh;
00554 }
00555 
00561 MeshNormals* xFile_Loader::processMeshNormals()
00562 {
00563 
00564         MeshNormals* meshNormals = new MeshNormals();
00565 
00566         //if the parent template is a frame, add this mesh to it
00567         if(newTemplate->parent && newTemplate->parent->type && newTemplate->parent->type->ID == X_MESH)
00568         {
00569                 Mesh* parantMesh = (Mesh*)newTemplate->parent->pdata;
00570                 if(!parantMesh->normals)
00571                 {
00572                         parantMesh->normals = meshNormals;
00573                 }else
00574                 {
00575                         debug("Error - Mesh has more than one set of normals.");
00576                         return NULL;
00577                 }
00578         
00579         }else{
00580                 debug("Error - meshNormals block found outside Mesh block.");
00581                 return NULL;
00582         }
00583 
00584         // get Next Piece of data
00585         getNext();
00586 
00587         //Read the Number of Vertices.
00588         meshNormals->nNormals = atoi(current.c_str());
00589         if(verbose)
00590         {
00591                 cout << "Num MeshNormals: " << meshNormals->nNormals << "\n";
00592         }
00593 
00594         //Fill array of n Normals.
00595         meshNormals->normals = new Vector[meshNormals->nNormals];
00596 
00597         //Read Vertices
00598         for(DWORD i = 0; i < meshNormals->nNormals; i++)
00599         {
00600                 getNext();
00601 
00602                 static float vecValues[3] = {0.0f,0.0f,0.0f};
00603                 int currentVecIndex = 0;
00604                 string currentVal = "";
00605                 char currentChar;
00606 
00607                 //Seperate each line of the array into a vector.
00608                 for(UINT j = 0; j < current.length(); j ++)
00609                 {
00610                         currentChar = current.c_str()[j];
00611 
00612                         //We are at the next vector in the array.
00613                         if(currentChar == ',')
00614                         {
00615                                 currentVecIndex = 0;
00616                                 break;
00617                         }
00618                         //We are at the next value in the vector.
00619                         if(currentChar == ';')
00620                         {
00621                                 vecValues[currentVecIndex] = (float)atof(currentVal.c_str());
00622                                 currentVecIndex ++;
00623                                 currentVal = "";
00624                         }else
00625                         {
00626                                 //Concatinate the current value.
00627                                 currentVal += currentChar;
00628                         }
00629                 }
00630 
00631                 meshNormals->normals[i] = Vector(vecValues[0],vecValues[1],vecValues[2]);
00632 
00633                 if(verbose)
00634                 {
00635                         cout << "       Normal [" << i << "] : " << meshNormals->normals[i].x << "," << meshNormals->normals[i].y << "," << meshNormals->normals[i].z  <<"\n";
00636                 }
00637         }
00638 
00639 
00640         getNext();              // Sync
00641 
00642         //Read the Number of Faces 
00643         meshNormals->nFaceNormals = atoi(current.c_str());
00644 
00645         if(verbose)
00646         {
00647                 cout << "Num Face Normals: " << meshNormals->nFaceNormals << "\n";
00648         }
00649         
00650         //Fill Array of n Faces.
00651         meshNormals->facenormals = new MeshFace[meshNormals->nFaceNormals];
00652         for(DWORD i = 0; i < meshNormals->nFaceNormals; i++)
00653         {
00654                 //Get next piece of data
00655                 getNext();
00656 
00657                 //Read Indices
00658                 string currentVal = "";
00659                 char currentChar = ' ';
00660 
00661                 //Get the First Value (before the first semi-colon) this is the number of face Indices
00662                 UINT j = 0;
00663                 int colonCount = 0;
00664                 int currentFaceIndex = 0;
00665                 for(j; j < current.length(); j ++)
00666                 {
00667                         if(currentChar == ';' && colonCount == 0)
00668                         {
00669                                 //Store the number of indices and create an array of DWORDS of that scale.
00670                                 meshNormals->facenormals[i].numFaceVertexIndices = (DWORD)atoi(currentVal.c_str());
00671                                 meshNormals->facenormals[i].faceVertexIndices =  new DWORD[meshNormals->facenormals[i].numFaceVertexIndices];
00672                                 currentVal = "";
00673                                 colonCount ++;
00674                         }
00675                         else if(currentChar == ',' || (currentChar == ';' && colonCount > 0))
00676                         {
00677                                 //Store each index
00678                                 meshNormals->facenormals[i].faceVertexIndices[currentFaceIndex] = (DWORD)atoi(currentVal.c_str());
00679                                 currentFaceIndex++;
00680                                 currentVal = "";
00681                         }else
00682                         {
00683                                 currentVal += currentChar;
00684                         }
00685 
00686                         currentChar = current.c_str()[j];
00687 
00688                 }
00689 
00690                 if(verbose)
00691                 {
00692                         cout << "       Face Normal [" << i << "] \n" << "              Num Normal Indices " << meshNormals->facenormals[i].numFaceVertexIndices << "\n";
00693                         for(DWORD p = 0; p < meshNormals->facenormals[i].numFaceVertexIndices; p ++)
00694                         {
00695                                 cout << "                       Index [" << p << "] : " << meshNormals->facenormals[i].faceVertexIndices[p] << "\n";
00696                         }
00697                 }
00698         }
00699 
00700 
00701         return meshNormals;
00702 }
00703 
00704 
00710 MeshTextureCoords* xFile_Loader::processMeshTextureCoords()
00711 {
00712 
00713         MeshTextureCoords* meshTextureCoords = new MeshTextureCoords();
00714 
00715 
00716         //if the parent template is a frame, add this mesh to it
00717         if(newTemplate->parent && newTemplate->parent->type && newTemplate->parent->type->ID == X_MESH)
00718         {
00719                 Mesh* parantMesh = (Mesh*)newTemplate->parent->pdata;
00720                 if(!parantMesh->textureCoords)
00721                 {
00722                         parantMesh->textureCoords = meshTextureCoords;
00723                 }else
00724                 {
00725                         debug("Error - Mesh has more than one set of Texture Coordinates.");
00726                         return NULL;
00727                 }
00728         }else{
00729                 debug("Error - MeshTextureCoords block found outside Mesh block.");
00730                 return NULL;
00731         }
00732 
00733 
00734         // Get Next Piece of data
00735         getNext();
00736 
00737         //Read the Number of TextureCoords.
00738         meshTextureCoords->nTextCoords = atoi(current.c_str());
00739 
00740         if(verbose)
00741         {
00742                 cout << "Num MeshTextureCoords: " << meshTextureCoords->nTextCoords << "\n";
00743         }
00744 
00745 
00746         //Fill array of n TextureCoords.
00747         meshTextureCoords->textCoords = new Coords2d[meshTextureCoords->nTextCoords];
00748 
00749         for(DWORD i = 0; i < meshTextureCoords->nTextCoords; i ++)
00750         {
00751                 // Get Next Piece of data
00752                 getNext();
00753 
00754                 static float vecValues[2] = {0.0f,0.0f};
00755                 int currentVecIndex = 0;
00756                 string currentVal = "";
00757                 char currentChar;
00758 
00759                 //Seperate each line of the array into a vector.
00760                 for(UINT j = 0; j < current.length(); j ++)
00761                 {
00762                         currentChar = current.c_str()[j];
00763 
00764                         //We are at the next vector in the array.
00765                         if(currentChar == ',')
00766                         {
00767                                 currentVecIndex = 0;
00768                                 break;
00769                         }
00770                         //We are at the next value in the vector.
00771                         if(currentChar == ';')
00772                         {
00773                                 vecValues[currentVecIndex] = (float)atof(currentVal.c_str());
00774                                 currentVecIndex ++;
00775                                 currentVal = "";
00776                         }else
00777                         {
00778                                 //Concatinate the current value.
00779                                 currentVal += currentChar;
00780                         }
00781                 }
00782 
00783                 meshTextureCoords->textCoords[i] = Coords2d(vecValues[0],vecValues[1]);
00784 
00785                 if(verbose)
00786                 {
00787                         cout << "       TextureCoord [" << i << "] : " << meshTextureCoords->textCoords[i].u << "," << meshTextureCoords->textCoords[i].v  <<"\n";
00788                 }
00789         }
00790         return meshTextureCoords;
00791 }
00792 
00798 FrameTransformMatrix* xFile_Loader::processFrameTransformationMatrix()
00799 {
00800         static float vecValues[16] = {0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f};
00801         int currentVecIndex = 0;
00802         string currentVal = "";
00803         char currentChar;
00804 
00805         // Get Next Piece of data
00806         getNext();
00807 
00808         //Seperate each line of the array into a vector.
00809         for(UINT j = 0; j <= current.length(); j ++)
00810         {
00811                 currentChar = current.c_str()[j];
00812 
00813                 //We are at the next value in the vector.
00814                 if(currentChar == ',' || currentChar == ';')
00815                 {
00816                         vecValues[currentVecIndex] = (float)atof(currentVal.c_str());
00817                         currentVecIndex ++;
00818                         currentVal = "";
00819                 }else
00820                 {
00821                         //Concatinate the current value.
00822                         currentVal += currentChar;
00823                 }
00824         }
00825 
00826         FrameTransformMatrix * frameTransformationMatrix = new FrameTransformMatrix(vecValues[0],vecValues[1],vecValues[2],vecValues[3],
00827                 vecValues[4],vecValues[5],vecValues[6],vecValues[7],
00828                 vecValues[8],vecValues[9],vecValues[10],vecValues[11],
00829                 vecValues[12],vecValues[13],vecValues[14],vecValues[15]);
00830 
00831         //if the parent template is a frame, add this transformation to it
00832         if(newTemplate->parent && newTemplate->parent->type && newTemplate->parent->type->ID == X_FRAME)
00833         {
00834                 Frame* parantFrame = (Frame*)newTemplate->parent->pdata;
00835                 if(!parantFrame->transform)
00836                 {
00837                         parantFrame->transform = frameTransformationMatrix;
00838                 }else
00839                 {
00840                         debug("Error - More than one FrameTransformationMatrix found in the Frame: ");
00841                         debug((char*)newTemplate->parent->Name.c_str());
00842                         return NULL;
00843                 }
00844         }
00845         else
00846         {
00847                 debug("Error - FrameTransformationMatrix found outside a Frame Block");
00848                 return NULL;
00849         }
00850 
00851         if(verbose)
00852         {
00853         #ifdef _WIN32
00854                 cout << "FrameTransformationMatrix :\n" 
00855                         <<"     "<< frameTransformationMatrix->frameMatrix.x1 << "," << frameTransformationMatrix->frameMatrix.x2 << "," <<frameTransformationMatrix->frameMatrix.x3 << "," <<frameTransformationMatrix->frameMatrix.x4 << "\n"
00856                         <<"     "<< frameTransformationMatrix->frameMatrix.x5 << "," <<frameTransformationMatrix->frameMatrix.x6 << "," <<frameTransformationMatrix->frameMatrix.x7 << ","<< frameTransformationMatrix->frameMatrix.x8 << "\n"
00857                         <<"     "<< frameTransformationMatrix->frameMatrix.x9 << "," <<frameTransformationMatrix->frameMatrix.x10 << "," <<frameTransformationMatrix->frameMatrix.x11 << "," <<frameTransformationMatrix->frameMatrix.x12 << "\n"
00858                         <<"     "<< frameTransformationMatrix->frameMatrix.x13 << "," <<frameTransformationMatrix->frameMatrix.x14 << "," <<frameTransformationMatrix->frameMatrix.x15 << "," <<frameTransformationMatrix->frameMatrix.x16 <<  "\n";
00859         #else
00860 
00861                 cout << "FrameTransformationMatrix :\n" 
00862                         <<"     "<< frameTransformationMatrix->frameMatrix.elem[0][0] << "," << frameTransformationMatrix->frameMatrix.elem[0][1] << "," << frameTransformationMatrix->frameMatrix.elem[0][2]<< "," << frameTransformationMatrix->frameMatrix.elem[0][3] << "\n"
00863                         <<"     "<< frameTransformationMatrix->frameMatrix.elem[1][0] << "," << frameTransformationMatrix->frameMatrix.elem[1][1] << "," << frameTransformationMatrix->frameMatrix.elem[1][2]<< "," << frameTransformationMatrix->frameMatrix.elem[1][3] << "\n"
00864                         <<"     "<< frameTransformationMatrix->frameMatrix.elem[2][0] << "," << frameTransformationMatrix->frameMatrix.elem[2][1] << "," << frameTransformationMatrix->frameMatrix.elem[2][2]<< "," << frameTransformationMatrix->frameMatrix.elem[2][3] << "\n"
00865                         <<"     "<< frameTransformationMatrix->frameMatrix.elem[3][0] << "," << frameTransformationMatrix->frameMatrix.elem[3][1] << "," << frameTransformationMatrix->frameMatrix.elem[3][2]<< "," << frameTransformationMatrix->frameMatrix.elem[3][3] << "\n";
00866         #endif
00867         }
00868 
00869 
00870 
00871         return frameTransformationMatrix;
00872 }
00873 
00878 Frame* xFile_Loader::processFrame()
00879 {
00880         Frame* frame = new Frame;
00881         frame->Name = newTemplate->Name;
00882         frames.push_back(frame);
00883 
00884         //if the parent is a frame, add this material list to it
00885         if(newTemplate->parent && newTemplate->parent->type && newTemplate->parent->type->ID == X_FRAME)
00886         {
00887                 Frame* frameParent = (Frame*) newTemplate->parent->pdata;
00888 
00889                 if(!frameParent)
00890                 {
00891                         frameParent = rootFrame;                // If the frame has no parent, use the root frame.
00892                 }
00893 
00894                 frame->parentFrame = frameParent;
00895                 frameParent->childFrames.push_back(frame);
00896 
00897         }else
00898         {
00899                 frame->parentFrame = rootFrame;
00900                 rootFrame->childFrames.push_back(frame);
00901         }
00902 
00903         return frame;
00904 }
00905 
00911 MeshMaterialList* xFile_Loader::processMeshMaterilalList()
00912 {
00913         MeshMaterialList* materialList = new MeshMaterialList();
00914 
00915         //if the parent is a mesh, add this material list to it
00916         if(newTemplate->parent && newTemplate->parent->type && newTemplate->parent->type->ID == X_MESH)
00917         {
00918                 Mesh* meshParent = (Mesh*) newTemplate->parent->pdata;
00919                 if(meshParent->materialList)
00920                 {
00921                         debug("Error - Mesh has more than one MeshMaterialList.");
00922                         return NULL;
00923                 }else
00924                 {
00925                         meshParent->materialList = materialList;
00926                 }
00927         }
00928         else
00929         {
00930                 debug("Error - MeshMaterialList block found outside Mesh block.");
00931                 return NULL;
00932         }
00933 
00934         //Get number of materials
00935         getNext();
00936         materialList->nMaterials = (DWORD)atol(current.c_str());
00937 
00938         //get the number of face indices
00939         getNext();
00940         materialList->nFaceIndexes = (DWORD)atol(current.c_str());
00941 
00942         if(verbose)
00943         {
00944                 cout << "MeshMaterialList \n";
00945                 cout << "       Num Materials:" << materialList->nMaterials << "\n";
00946                 cout << "       Num Indices:" << materialList->nFaceIndexes << "\n";
00947         }
00948 
00949         //Read in face Indices
00950         materialList->faceIndex = new DWORD[materialList->nFaceIndexes];
00951         for(DWORD i = 0; i < materialList->nFaceIndexes; i++)
00952         {
00953                 getNext();
00954                 materialList->faceIndex[i] = (DWORD)atol(current.c_str());
00955 
00956                 if(verbose)
00957                 {
00958                         cout << "               Index [" << i << "] : " << materialList->faceIndex[i] << "\n";
00959                 }
00960         }
00961 
00962         return materialList;
00963 }
00964 
00970 Material* xFile_Loader::processMaterial()
00971 {
00972         Material* material = new Material();
00973         material->name = newTemplate->Name.c_str();
00974         materials.push_back(material);
00975 
00976         // if the parent is a material list, add a pointer to this material to its list.
00977         if(newTemplate->parent && newTemplate->parent->type && newTemplate->parent->type->ID == X_MESHMATERIALLIST)
00978         {
00979                 MeshMaterialList* meshMaterialListParent = (MeshMaterialList*) newTemplate->parent->pdata;
00980 
00981                 if(!meshMaterialListParent)
00982                 {
00983                         debug("Error - Material Found but its MeshMaterialList has no data.");
00984                         return NULL;
00985                 }else
00986                 {               
00987                         meshMaterialListParent->materials.push_back(material);
00988                 }
00989 
00990         }       
00991         else
00992         {
00993                 debug("Error - Material block found outside MeshMaterialList block.");
00994                 return NULL;
00995         }
00996 
00997         getNext();
00998 
00999         // Read the diffuse colour
01000         static float vecValues[4] = {1.0f,1.0f,1.0f,1.0f};
01001         int currentVecIndex = 0;
01002         string currentVal = "";
01003         char currentChar;
01004 
01005 
01006         //Seperate each line of the array into a vector.
01007         for(UINT j = 0; j < current.length() -1; j ++)
01008         {
01009                 currentChar = current.c_str()[j];
01010 
01011 
01012                 //We are at the next value in the vector.
01013                 if(currentChar == ';')
01014                 {
01015                         vecValues[currentVecIndex] = (float)atof(currentVal.c_str());
01016                         currentVecIndex ++;
01017                         currentVal = "";
01018                 }else
01019                 {
01020                         //Concatinate the current value.
01021                         currentVal += currentChar;
01022                 }
01023         }
01024         currentVecIndex = 0;
01025         currentVal = "";
01026 
01027         material->faceColour = ColorRGBA(vecValues[0],vecValues[1],vecValues[2],vecValues[3]);
01028 
01029         //Read the Power Value
01030         getNext();
01031         material->power = (float)atof(current.c_str());
01032 
01033         getNext();
01034 
01035         //Seperate each line of the array into a vector.
01036         for(UINT j = 0; j < current.length() -1; j ++)
01037         {
01038                 currentChar = current.c_str()[j];
01039 
01040                 //We are at the next value in the vector.
01041                 if(currentChar == ';')
01042                 {
01043                         vecValues[currentVecIndex] = (float)atof(currentVal.c_str());
01044                         currentVecIndex ++;
01045                         currentVal = "";
01046                 }else
01047                 {
01048                         //Concatinate the current value.
01049                         currentVal += currentChar;
01050                 }
01051         }
01052         currentVecIndex = 0;
01053         currentVal = "";
01054 
01055         material->specularColour = ColorRGB(vecValues[0],vecValues[1],vecValues[2]);
01056 
01057         getNext();
01058 
01059         //Seperate each line of the array into a vector.
01060         for(UINT j = 0; j < current.length()-1; j ++)
01061         {
01062                 currentChar = current.c_str()[j];
01063 
01064                 //We are at the next value in the vector.
01065                 if(currentChar == ';')
01066                 {
01067                         vecValues[currentVecIndex] = (float)atof(currentVal.c_str());
01068                         currentVecIndex ++;
01069                         currentVal = "";
01070                 }else
01071                 {
01072                         //Concatinate the current value.
01073                         currentVal += currentChar;
01074                 }
01075         }
01076         currentVal = "";
01077         currentVecIndex = 0;
01078 
01079         material->emissiveColour = ColorRGB(vecValues[0],vecValues[1],vecValues[2]);
01080 
01081         if(verbose)
01082         {
01083                 cout << "       Material: "<< material->name << "\n";
01084                 cout << "               Diffuse:" << material->faceColour.red << "," << material->faceColour.green << "," << material->faceColour.blue << "," << material->faceColour.alpha << "\n";   
01085                 cout << "               Power:" << material->power << "\n";   
01086                 cout << "               Specular:" << material->specularColour.red << "," << material->specularColour.green << "," << material->specularColour.blue << "\n";  
01087                 cout << "               Emissive:" << material->emissiveColour.red << "," << material->emissiveColour.green << "," << material->emissiveColour.blue <<  "\n";   
01088         }
01089 
01090         return material;
01091 }
01092 
01098 TextureFilename* xFile_Loader::processTextureFileName()
01099 {       
01100         TextureFilename* textureFileName= new TextureFilename();
01101         textureFileNames.push_back(textureFileName);
01102 
01103         //Link the Texture file name to its parent material.
01104         if(newTemplate->parent && newTemplate->parent->type && newTemplate->parent->type->ID == X_MATERIAL)
01105         {
01106                 Material* materialParent = (Material*) newTemplate->parent->pdata;
01107 
01108                 if(!materialParent)
01109                 {
01110                         debug("Error - TextureFileName Found but its Material has no data.");
01111                         return NULL;
01112                 }else
01113                 {               
01114                         materialParent->nTexturesFileNames++;
01115                         materialParent->textureFileNames.push_back(textureFileName);
01116                 }
01117         }
01118         else
01119         {
01120                 debug("Error - TextureFilename block found outside Material block.");
01121                 return NULL;
01122         }
01123 
01124 
01125         //Get the texture file name
01126         getNext();
01127         textureFileName->szString = trimSpeechMarkString(current).c_str();
01128 
01129         if(verbose)
01130         {
01131                 cout << "               TextureFilename:\n"
01132                         << "                    "<< textureFileName->szString <<  "\n";   
01133         }
01134         //return the TextureFileName
01135         return textureFileName;
01136 }
01137 
01143 EffectInstance* xFile_Loader::processEffectInstance()
01144 {
01145         EffectInstance* effectInstance = new EffectInstance();
01146         effectInstances.push_back(effectInstance);
01147 
01148         //If the parent block is a material add this effect to it
01149         //Link the Texture file name to its parent material.
01150         if(newTemplate->parent && newTemplate->parent->type && newTemplate->parent->type->ID == X_MATERIAL)
01151         {
01152                 Material* materialParent = (Material*) newTemplate->parent->pdata;
01153 
01154                 if(!materialParent)
01155                 {
01156                         debug("Error - EffectInstance Found but its Material has no data.");
01157                         return NULL;
01158                 }else
01159                 {               
01160                         materialParent->nEffectInstances++;
01161                         materialParent->effectInstances.push_back(effectInstance);
01162                 }
01163         }
01164         else
01165         {
01166                 debug("Error - EffectInstance block found outside Material block.");
01167                 return NULL;
01168         }
01169 
01170 
01171         //Get the effect file name
01172         getNext();
01173         effectInstance->EffectFilename= trimSpeechMarkString(current).c_str();
01174 
01175         if(verbose)
01176         {
01177                 cout << "Effect Instance:\n"
01178                         << "    "<< effectInstance->EffectFilename <<  "\n";   
01179         }
01180 
01181         //return the TextureFileName
01182         return effectInstance;
01183 }
01184 
01190 EffectParamDword* xFile_Loader::processEffectParamDword()
01191 {
01192         EffectParamDword* effectParamDword = new EffectParamDword();
01193 
01194         //If the parent block is a material add this effect to it
01195         //Link the Texture file name to its parent material.
01196         if(newTemplate->parent && newTemplate->parent->type && newTemplate->parent->type->ID == X_EFFECTINSTANCE)
01197         {
01198                 EffectInstance* effectInstanceParent = (EffectInstance*) newTemplate->parent->pdata;
01199 
01200                 if(!effectInstanceParent)
01201                 {
01202                         debug("Error - EffectParamDword Found outside EffectInstance block.");
01203                         return NULL;
01204                 }else
01205                 {               
01206                         effectInstanceParent->dwordParams.push_back(effectParamDword);
01207                 }
01208         }
01209         else
01210         {
01211                 debug("Error - EffectParamDword block found outside EffectInstance block.");
01212                 return NULL;
01213         }
01214 
01215         //Get the parameter name
01216         getNext();
01217         effectParamDword->ParamName= current.c_str();
01218 
01219         //Get the value
01220         getNext();
01221         effectParamDword->value = atol(current.c_str());
01222 
01223         if(verbose)
01224         {
01225                 cout << "       EffectParamDword :\n"
01226                         << "            Parameter Name:"<< effectParamDword->ParamName <<  "\n"
01227                         << "            Parameter Value:"<< effectParamDword->value <<  "\n";   
01228         }
01229         //return the EffectParamDword
01230         return effectParamDword;
01231 }
01232 
01238 EffectParamFloats* xFile_Loader::processEffectParamFloats()
01239 {
01240         EffectParamFloats* effectParamFloats = new EffectParamFloats();
01241 
01242         //If the parent block is a material add this effect to it
01243         //Link the Texture file name to its parent material.
01244         if(newTemplate->parent && newTemplate->parent->type && newTemplate->parent->type->ID == X_EFFECTINSTANCE)
01245         {
01246                 EffectInstance* effectInstanceParent = (EffectInstance*) newTemplate->parent->pdata;
01247 
01248                 if(!effectInstanceParent)
01249                 {
01250                         debug("Error - EffectParamFloats Found outside EffectInstance block.");
01251                         return NULL;
01252                 }else
01253                 {               
01254                         effectInstanceParent->floatParams.push_back(effectParamFloats);
01255                 }
01256         }
01257         else
01258         {
01259                 debug("Error - EffectParamFloats block found outside EffectInstance block.");
01260                 return NULL;
01261         }
01262 
01263         //Get the parameter name
01264         getNext();
01265         effectParamFloats->ParamName= current.c_str();
01266 
01267         //Get the value
01268         getNext();
01269         effectParamFloats->nFloats      = atol(current.c_str());
01270         effectParamFloats->Floats = new FLOAT[effectParamFloats->nFloats];
01271 
01272         if(verbose)
01273         {
01274                 cout << "       EffectParamFloats :\n"
01275                         << "            Parameter Name:"<< effectParamFloats->ParamName <<  "\n"
01276                         << "            NumFloats:"<< effectParamFloats->nFloats <<  "\n";   
01277         }
01278 
01279         for(DWORD i = 0; i < effectParamFloats->nFloats; i ++)
01280         {
01281                 // Get Next Piece of data
01282                 getNext();
01283 
01284                 int currentVecIndex = 0;
01285                 string currentVal = "";
01286                 char currentChar;
01287 
01288                 //Seperate each line of the array into a vector.
01289                 for(UINT j = 0; j < current.length(); j ++)
01290                 {
01291                         currentChar = current.c_str()[j];
01292                         //We are at the next vector in the array.
01293                         if(currentChar == ',')
01294                         {
01295                                 effectParamFloats->Floats[currentVecIndex] = (float)atof(currentVal.c_str());
01296                                 currentVecIndex ++;
01297                                 currentVal = "";
01298 
01299                                 if(verbose)
01300                                 {
01301                                         cout << "               Float [" << currentVecIndex << "] : " << effectParamFloats->Floats[currentVecIndex] << "\n";
01302                                 }
01303 
01304                         }
01305                         //We are at the end of the list.
01306                         if(currentChar == ';')
01307                         {
01308                                 currentVecIndex = 0;
01309                                 currentVal = "";
01310                                 break;
01311                         }else
01312                         {
01313                                 //Concatinate the current value.
01314                                 currentVal += currentChar;
01315                         }
01316                 }
01317         }
01318 
01319         //return the EffectParamDword
01320         return effectParamFloats;
01321 }
01322 
01328 EffectParamString* xFile_Loader::processEffectPramString()
01329 {
01330         EffectParamString* effectParamString = new EffectParamString();
01331 
01332         //If the parent block is a material add this effect to it
01333         //Link the Texture file name to its parent material.
01334         if(newTemplate->parent && newTemplate->parent->type && newTemplate->parent->type->ID == X_EFFECTINSTANCE)
01335         {
01336                 EffectInstance* effectInstanceParent = (EffectInstance*) newTemplate->parent->pdata;
01337 
01338                 if(!effectInstanceParent)
01339                 {
01340                         debug("Error - EffectParamString Found outside EffectInstance block.");
01341                         return NULL;
01342                 }else
01343                 {               
01344                         effectInstanceParent->stringPrarams.push_back(effectParamString);
01345                 }
01346         }
01347         else
01348         {
01349                 debug("Error - EffectParamString block found outside EffectInstance block.");
01350                 return NULL;
01351         }
01352 
01353         //Get the parameter name
01354         getNext();
01355 
01356         effectParamString->ParamName= current.c_str();
01357 
01358         //Get the value
01359         getNext();
01360         effectParamString->value= current;
01361 
01362         if(verbose)
01363         {
01364                 cout << "       EffectParamString :\n"
01365                         << "            Parameter Name:"<< effectParamString->ParamName <<  "\n"
01366                         << "            Parameter Value:"<< effectParamString->value <<  "\n";   
01367         }
01368 
01369         //return the EffectParamDword
01370         return effectParamString;
01371 }
01372 
01378 AnimationSet* xFile_Loader::processAnimationSet()
01379 {
01380         AnimationSet* animationSet = new AnimationSet();
01381         
01382         animationSet->Name = newTemplate->Name;
01383 
01384         if(verbose)
01385         {
01386                 cout << "AnimationSet :\n"
01387                         << "    Name:"<< animationSet->Name <<  "\n";
01388         }
01389 
01390         animationSets.push_back(animationSet);
01391 
01392         //return the AnimationSet
01393         return animationSet;
01394 }
01395 
01401 Animation* xFile_Loader::processAnimation()
01402 {
01403 
01404         Animation* animation = new Animation();
01405 
01406         animation->Name = newTemplate->Name;
01407 
01408         //Get The Bone Name
01409         getNext();
01410         getNext();
01411         animation->BoneName = current;
01412         getNext();
01413 
01414         //If the parent block is an animation set add this animation to it
01415         //Animations must be in an animation set.
01416         if(newTemplate->parent && newTemplate->parent->type && newTemplate->parent->type->ID == X_ANIMATIONSET)
01417         {
01418                 AnimationSet* animationSetParent = (AnimationSet*) newTemplate->parent->pdata;
01419 
01420                 if(!animationSetParent)
01421                 {
01422                         debug("Error - Animation Found inside non-processed AnimationSet block.");
01423                         return NULL;
01424                 }else
01425                 {               
01426                         animationSetParent->Animations.push_back(animation);
01427                         animationSetParent->nAnimations ++;
01428                 }
01429         }
01430         else
01431         {
01432                 debug("Error - Animation Found outside AnimationSet block.");
01433                 return NULL;
01434         }
01435 
01436         if(verbose)
01437         {
01438                 cout << "       Animation :\n"
01439                         << "            Name:"<< animation->Name <<  "\n"
01440                         << "            BoneName:"<< animation->BoneName <<  "\n";
01441         }
01442 
01443         return animation;
01444 }
01445 
01451 AnimationOptions* xFile_Loader::processAnimationOptions()
01452 {
01453         AnimationOptions* animationOptions = new AnimationOptions();
01454 
01455         //If the parent block is an animation add these animation options to it
01456         //AnimationsOptions must be in an animation block.
01457         if(newTemplate->parent && newTemplate->parent->type && newTemplate->parent->type->ID == X_ANIMATION)
01458         {
01459                 Animation* animationParent = (Animation*) newTemplate->parent->pdata;
01460 
01461                 if(!animationParent)
01462                 {
01463                         debug("Error - AnimationOptions Found in non-processed Animation block.");
01464                         return NULL;
01465                 }else
01466                 {               
01467                         animationParent->options = animationOptions;
01468                 }
01469         }
01470         else
01471         {
01472                 debug("Error - AnimationOptions Found outside Animation block.");
01473                 return NULL;
01474         }
01475 
01476 
01477         getNext();
01478         animationOptions->opencloased = atoi(current.c_str());
01479         getNext();
01480         animationOptions->positionquality = atoi(current.c_str());
01481 
01482         if(verbose)
01483         {
01484                 cout << "               AnimationOptions :\n"
01485                         << "                    OpenOrCloased:"<< animationOptions->opencloased <<  "\n"
01486                         << "                    PositionQuality:"<< animationOptions->positionquality <<  "\n";
01487         }
01488 
01489         return animationOptions;
01490 }
01491 
01497 AnimationKey* xFile_Loader::processAnimationKey()
01498 {
01499         AnimationKey* animationKey = new AnimationKey();
01500 
01501         //If the parent block is an animation add this AnimationKey to it
01502         //AnimationKeys must be in an animation block.
01503         if(newTemplate->parent && newTemplate->parent->type && newTemplate->parent->type->ID == X_ANIMATION)
01504         {
01505                 Animation* animationParent = (Animation*) newTemplate->parent->pdata;
01506 
01507                 if(!animationParent)
01508                 {
01509                         debug("Error - AnimationKey Found in non-processed Animation block.");
01510                         return NULL;
01511                 }else
01512                 {               
01513                         animationParent->AnimationKeys.push_back(animationKey);
01514                         animationParent->nAnimationKeys ++;
01515                 }
01516         }
01517         else
01518         {
01519                 debug("Error - AnimationKey Found outside Animation block.");
01520                 return NULL;
01521         }
01522 
01523         // Get the Grand Parent AnimationSet
01524         AnimationSet* animationSetGrandParent = NULL;
01525         if(newTemplate->parent->parent && newTemplate->parent->parent->type && newTemplate->parent->parent->type->ID == X_ANIMATIONSET)
01526         {
01527                 animationSetGrandParent = (AnimationSet*) newTemplate->parent->parent->pdata;
01528 
01529                 if(!animationSetGrandParent)
01530                 {
01531                         debug("Error - AnimationKey Found in non-processed AnimationSet block.");
01532                         return NULL;
01533                 }
01534         }
01535         else
01536         {
01537                 debug("Error - AnimationKey Found outside AnimationSet block.");
01538                 return NULL;
01539         }
01540 
01541 
01542         getNext();
01543         animationKey->keyType = atoi(current.c_str());
01544 
01545         if(animationKey->keyType < 0 || animationKey->keyType > 4 || animationKey->keyType == 3)
01546         {
01547                 debug("Error - Unknown KeyType.");
01548                 return NULL;
01549         }
01550 
01551         getNext();
01552         animationKey->nKeys = atoi(current.c_str());
01553 
01554         if(verbose)
01555         {
01556                 cout << "                       animationKey :\n"
01557                         << "                            KeyType:"<< animationKey->keyType <<  "\n"
01558                         << "                            numKeys:"<< animationKey->nKeys <<  "\n";
01559         }
01560 
01561         // Loop through all the timedAnimationKeys
01562         for(int i = 0; i < animationKey->nKeys; i ++)
01563         {
01564 
01565                 TimedFloatKeys* timedFloatKey = new TimedFloatKeys();                           // Create a timed float key
01566                 FloatKeys* floatKeys = timedFloatKey->tfkeys = new FloatKeys();         // Create a float key
01567 
01568                 getNext();
01569 
01570                 string value = "";
01571                 char currentChar =' ';
01572                 int j = 0;
01573 
01574                 for(j; j < current.length(); j++)                                                                       // Get the Time value (the value before the first ';')
01575                 {
01576                         currentChar = current.c_str()[j];
01577                         value += currentChar;
01578                         if(currentChar == ';')
01579                                 break;
01580                 }
01581 
01582                 timedFloatKey->time = atoi(value.c_str());
01583                 j++;
01584                 value = "";
01585                 for(j; j < current.length(); j++)                                                               // Get the Number of values ( the next value after the first ';' and before the second).
01586                 {
01587                         currentChar = current.c_str()[j];
01588                         value += currentChar;
01589                         if(currentChar == ';')
01590                                 break;
01591                 }
01592                 
01593                 floatKeys->nValues = atoi(value.c_str()); 
01594 
01595                 if(verbose)
01596                 {
01597                         cout << "                               TimedFloatKeys :\n"
01598                                 << "                                    Time:"<< timedFloatKey->time <<  "\n"
01599                                 << "                                            floatKeys :\n"
01600                                 << "                                                    numValues:"<< floatKeys->nValues <<  "\n"
01601                                 << "--Values:\n";
01602                 }
01603 
01604                 j++;
01605                 value = "";
01606 
01607                 floatKeys->value = new FLOAT[floatKeys->nValues];
01608                 int index = 0;
01609 
01610                 for(j; j < current.length(); j++)                                                                       // Continue parsing through the string to find each individual value separated by ','
01611                 {
01612                         currentChar = current.c_str()[j];
01613                         value += currentChar;
01614 
01615                         if(currentChar == ',' || currentChar == ';')
01616                         {
01617                                 floatKeys->value[index] = (float)atof(value.c_str());
01618                                 
01619                                 if(verbose)
01620                                 {
01621                                         cout << floatKeys->value[index];
01622                                         
01623                                         if(((index + 1) %4) == 0)
01624                                         {
01625                                                 cout << "\n";
01626                                         }else
01627                                         {
01628                                                 cout << " ";
01629                                         }
01630                                 }
01631                                 index ++;
01632                                 value = "";
01633 
01634                                 if(currentChar == ';')
01635                                         break;
01636                         }
01637                 }
01638 
01639                 switch(animationKey->keyType)
01640                 {
01641                         case(0):{
01642                                 //Quaternion Rotation Key
01643                                         }break;
01644                         case(1):{
01645                                         // Scale Vector Key
01646                                         floatKeys->valueMatrix = Matrix4x4( floatKeys->value[0],0,0,0,
01647                                                                                                                 0,floatKeys->value[1],0,0,
01648                                                                                                                 0,0,floatKeys->value[2],0,
01649                                                                                                                 0,0,0,1);
01650 
01651                                         }break;
01652                         case(2):
01653                                 {
01654                                         // Translation Vector
01655                                         floatKeys->valueMatrix = Matrix4x4( 1,0,0,0,
01656                                                                                                                 0,1,0,0,
01657                                                                                                                 0,0,1,0,
01658                                                                                                                 floatKeys->value[0],floatKeys->value[1],floatKeys->value[2],1);
01659 
01660                                 }break;
01661 
01662                         case(4):
01663                                 {
01664                                         //Matrix Key
01665                                         floatKeys->valueMatrix = Matrix4x4(floatKeys->value[0],floatKeys->value[1],floatKeys->value[2],floatKeys->value[3],
01666                                                                                                                 floatKeys->value[4],floatKeys->value[5],floatKeys->value[6],floatKeys->value[7],
01667                                                                                                                 floatKeys->value[8],floatKeys->value[9],floatKeys->value[10],floatKeys->value[11],
01668                                                                                                                 floatKeys->value[12],floatKeys->value[13],floatKeys->value[14],floatKeys->value[15]);
01669                                 }break;
01670                 }
01671 
01672 
01673                 //Accumulate length property of animationSet
01674                 if(timedFloatKey->time > animationSetGrandParent->Length)
01675                 {
01676                         animationSetGrandParent->Length = timedFloatKey->time;
01677                 }
01678 
01679                 // Add the current tunedFloatKey to the animation Key.
01680                 animationKey->keys.push_back(timedFloatKey);
01681 
01682 
01683         }
01684 
01685         return animationKey;
01686 }
01687 
01691 void xFile_Loader::getNext()
01692 {
01693         back2 = back1;
01694         back1 = current;
01695         oFile >> current;
01696 }
01697 
01701 void xFile_Loader::debug(char* message)
01702 {
01703         if(message)
01704         {
01705 #ifdef DEBUG_X
01706                 printf(message);
01707                 printf("\n");
01708 #endif
01709         }
01710 }
01711 
01717 string xFile_Loader::trimSpeechMarkString(string instr)
01718 {
01719         if(instr.at(0) == '\'' || instr.at(0) == '"')
01720         {
01721                 instr.erase(0,1);
01722         }
01723         if(instr.at(instr.size()-1) == ';')
01724         {
01725                 instr.erase(instr.size()-2,instr.size()-1);
01726         }
01727         if(instr.at(instr.size()-1) == '\'' || instr.at(instr.size()-1) == '"')
01728         {
01729                 instr.erase(instr.size()-2,instr.size()-1);
01730         }
01731 
01732         return instr;
01733 }

Generated on Sun May 18 21:45:09 2008 for PS2X by  doxygen 1.5.4