Advertisement
Guest User

ASEModel.cpp

a guest
May 27th, 2011
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 20.62 KB | None | 0 0
  1. /*
  2. ===============================================================================
  3. File:       ASEModel.cpp
  4. Author:     Clinton Freeman
  5. Created:    May 8, 2011
  6. ===============================================================================
  7. */
  8.  
  9. #include <headers/Common.h>
  10. #include <headers/ASEModel.h>
  11.  
  12. #include <iostream>
  13. #include <fstream>
  14. #include <vector>
  15. #include <iterator>
  16.  
  17. using namespace E3D;
  18.  
  19. using std::vector;
  20. using std::string;
  21. using std::iterator;
  22.  
  23. static void split(string input, vector<string>& tokens);
  24. static float parseFloat(vector<string>::iterator& tokenItr);
  25. static unsigned int parseUInt(vector<string>::iterator& tokenItr);
  26. static string& parseString(vector<string>::iterator& tokenItr);
  27.  
  28. // more complicated structures require passing the structure in to avoid
  29. // having to allocate a new one in the free store
  30. static void parseRGB(vector<string>::iterator& tokenItr, RGB& rgb);
  31. static void parseVector3(vector<string>::iterator& tokenItr, Vector3& v);
  32.  
  33. //=============================================================================
  34. // ASEModel
  35. //=============================================================================
  36.  
  37. ASEModel::ASEModel(string filename)
  38. {
  39.     string curLine, token;
  40.     vector<string> tokens;
  41.     vector<string>::iterator tokenItr;
  42.  
  43.     // attempt to read the file, parse it into tokens based on whitespace
  44.     std::ifstream file(filename.c_str());
  45.     if(file.is_open())
  46.     {
  47.         while(file.good())
  48.         {
  49.             getline(file, curLine);
  50.             split(curLine, tokens);
  51.         }
  52.     }
  53.     else
  54.     {
  55.         printf("Unable to open file \"%s\".\n", filename.c_str());
  56.         return;
  57.     }
  58.  
  59.     tokenItr = tokens.begin();
  60.     lastToken = tokens.end();
  61.     parse(tokenItr);
  62.  
  63.     file.close();
  64. }
  65.  
  66. void ASEModel::parse(vector<string>::iterator& tokenItr)
  67. {
  68.     exportVersion = parseUInt(tokenItr);
  69.     comment = parseString(tokenItr);
  70.  
  71.     scene.parse(tokenItr);
  72.     materialList.parse(tokenItr);
  73.     while(tokenItr != lastToken)
  74.     {
  75.         if(*tokenItr == "*GEOMOBJECT")
  76.         {
  77.             ASEGeomObject go;
  78.             go.parse(tokenItr);
  79.             geomObjects.push_back(go);
  80.         }
  81.         else if(*tokenItr == "*LIGHTOBJECT")
  82.         {
  83.             // do something!
  84.         }
  85.     }
  86. }
  87.  
  88. //=============================================================================
  89. // ASEScene
  90. //=============================================================================
  91.  
  92. void ASEScene::parse(vector<string>::iterator& tokenItr)
  93. {
  94.     // consume *SCENE and {
  95.     tokenItr += 2;
  96.  
  97.     filename = parseString(tokenItr);
  98.     firstFrame = parseUInt(tokenItr);
  99.     lastFrame = parseUInt(tokenItr);
  100.     frameSpeed = parseUInt(tokenItr);
  101.     ticksPerFrame = parseUInt(tokenItr);
  102.  
  103.     parseRGB(tokenItr, backgroundStatic);
  104.     parseRGB(tokenItr, ambientStatic);
  105.  
  106.     // consume }
  107.     tokenItr++;
  108. }
  109.  
  110. //=============================================================================
  111. // ASEMaterialList
  112. //=============================================================================
  113.  
  114. void ASEMaterialList::parse(vector<string>::iterator& tokenItr)
  115. {
  116.     // consume *MATERIAL_LIST and {
  117.     tokenItr += 2;
  118.  
  119.     materialCount = parseUInt(tokenItr);
  120.  
  121.     for(unsigned int i = 0; i < this->materialCount; i++)
  122.     {
  123.         ASEMaterial m;
  124.         m.parse(tokenItr);
  125.         materials.push_back(m);
  126.     }
  127.  
  128.     // consume }
  129.     tokenItr++;
  130. }
  131.  
  132. //=============================================================================
  133. // ASEMaterial
  134. //=============================================================================
  135.  
  136. void ASEMaterial::parse(vector<string>::iterator& tokenItr)
  137. {
  138.     // consume *MATERIAL or *SUBMATERIAL and # and {
  139.     tokenItr += 3;
  140.  
  141.     name = parseString(tokenItr);
  142.     matClass = parseString(tokenItr);
  143.  
  144.     parseRGB(tokenItr, ambient);
  145.     parseRGB(tokenItr, diffuse);
  146.     parseRGB(tokenItr, specular);
  147.  
  148.     shine = parseFloat(tokenItr);
  149.     shineStrength = parseFloat(tokenItr);
  150.     transparency = parseFloat(tokenItr);
  151.     wireSize = parseFloat(tokenItr);
  152.  
  153.     // potentially parse submaterials
  154.     if(*tokenItr == "*NUMSUBMTLS")
  155.     {
  156.         numSubMtls = parseUInt(tokenItr);
  157.  
  158.         for(unsigned int j = 0;  j < numSubMtls; j++)
  159.         {
  160.             ASEMaterial sm;
  161.             sm.parse(tokenItr);
  162.             subMaterials.push_back(sm);
  163.         }
  164.  
  165.         shading = "n/a";
  166.         xpFalloff = 0.0;
  167.         selfIllum = 0.0;
  168.         falloff = "n/a";
  169.         xpType = "n/a";
  170.     }
  171.     else
  172.     {
  173.         numSubMtls = 0;
  174.         shading = parseString(tokenItr);
  175.         xpFalloff = parseFloat(tokenItr);
  176.         selfIllum = parseFloat(tokenItr);
  177.         falloff = parseString(tokenItr);
  178.         xpType = parseString(tokenItr);
  179.  
  180.         // this is a little bit trickier than objects in the ASE file that
  181.         // tell you the count ahead of time. since the maps are located last
  182.         // in the material, we can use the presence of a } after parsing
  183.         // a map as a signal that we've gone through all of them.
  184.         for(;;)
  185.         {
  186.             ASEMap m;
  187.             m.parse(tokenItr);
  188.             maps.push_back(m);
  189.  
  190.             if(*tokenItr == "}")
  191.                 break;
  192.         }
  193.     }
  194.  
  195.     // consume }
  196.     tokenItr++;
  197. }
  198.  
  199. //=============================================================================
  200. // ASEMap
  201. //=============================================================================
  202.  
  203. void ASEMap::parse(vector<string>::iterator& tokenItr)
  204. {
  205.     if(*tokenItr == "*MAP_DIFFUSE")
  206.         mapFunc = MAP_DIFFUSE;
  207.     else if(*tokenItr == "*MAP_SPECULAR")
  208.         mapFunc = MAP_SPECULAR;
  209.     else if(*tokenItr == "*MAP_SHINE")
  210.         mapFunc = MAP_SHINE;
  211.     else if(*tokenItr == "*MAP_SHINESTRENGTH")
  212.         mapFunc = MAP_SHINESTRENGTH;
  213.     else if(*tokenItr == "*MAP_SELFILLUM")
  214.         mapFunc = MAP_SELFILLUM;
  215.     else if(*tokenItr == "*MAP_OPACITY")
  216.         mapFunc = MAP_OPACITY;
  217.     else if(*tokenItr == "*MAP_FILTERCOLOR")
  218.         mapFunc = MAP_FILTERCOLOR;
  219.     else if(*tokenItr == "*MAP_BUMP")
  220.         mapFunc = MAP_BUMP;
  221.     else if(*tokenItr == "*MAP_REFLECT")
  222.         mapFunc = MAP_REFLECT;
  223.     else if(*tokenItr == "*MAP_REFRACT")
  224.         mapFunc = MAP_REFRACT;
  225.  
  226.     // consume *MAP_____ and {
  227.     tokenItr += 2;
  228.  
  229.     name = parseString(tokenItr);
  230.     mapClass = parseString(tokenItr);
  231.     subno = parseUInt(tokenItr);
  232.     amount = parseFloat(tokenItr);
  233.     bitmap = parseString(tokenItr);
  234.     type = parseString(tokenItr);
  235.     uvwUOffset = parseFloat(tokenItr);
  236.     uvwVOffset = parseFloat(tokenItr);
  237.     uvwUTiling = parseFloat(tokenItr);
  238.     uvwVTiling = parseFloat(tokenItr);
  239.     uvwAngle = parseFloat(tokenItr);
  240.     uvwBlur = parseFloat(tokenItr);
  241.     uvwBlurOffset = parseFloat(tokenItr);
  242.     uvwNoiseAmt = parseFloat(tokenItr);
  243.     uvwNoiseSize = parseFloat(tokenItr);
  244.     uvwNoiseLevel = parseUInt(tokenItr);
  245.     uvwNoisePhase = parseFloat(tokenItr);
  246.     bitmapFilter = parseString(tokenItr);
  247.  
  248.     // consume }
  249.     tokenItr++;
  250. }
  251.  
  252. //=============================================================================
  253. // ASEGeomObject
  254. //=============================================================================
  255.  
  256. void ASEGeomObject::parse(vector<string>::iterator& tokenItr)
  257. {
  258.     // consume *GEOMOBJECT and {
  259.     tokenItr += 2;
  260.  
  261.     nodeName = parseString(tokenItr);
  262.  
  263.     nodeTM.parse(tokenItr);
  264.     mesh.parse(tokenItr);
  265.  
  266.     propMotionBlur = parseUInt(tokenItr);
  267.     propCastShadow = parseUInt(tokenItr);
  268.     propRecvShadow = parseUInt(tokenItr);
  269.     materialRef = parseUInt(tokenItr);
  270.  
  271.     // consume }
  272.     tokenItr++;
  273. }
  274.  
  275. //=============================================================================
  276. // ASEMesh
  277. //=============================================================================
  278.  
  279. void ASEMesh::parse(vector<string>::iterator& tokenItr)
  280. {
  281.     // consume *MESH and {
  282.     tokenItr += 2;
  283.  
  284.     timeValue = parseUInt(tokenItr);
  285.     numVertex = parseUInt(tokenItr);
  286.     numFaces = parseUInt(tokenItr);
  287.  
  288.     // consume *MESH_VERTEX_LIST and {
  289.     tokenItr += 2;
  290.     for(unsigned int i = 0; i < numVertex; i++)
  291.     {
  292.         ASEVertex v;
  293.         v.parse(tokenItr);
  294.         vertexList.push_back(v);
  295.     }
  296.     // consume }
  297.     tokenItr++;
  298.  
  299.     // consume *MESH_FACE_LIST and {
  300.     tokenItr += 2;
  301.     for(unsigned int i = 0; i < numFaces; i++)
  302.     {
  303.         ASEFace f;
  304.         f.parse(tokenItr);
  305.         faceList.push_back(f);
  306.     }
  307.     // consume }
  308.     tokenItr++;
  309.  
  310.     numTVertex = parseUInt(tokenItr);
  311.  
  312.     // consume *MESH_TVERTLIST and {
  313.     tokenItr += 2;
  314.     for(unsigned int i = 0; i < numTVertex; i++)
  315.     {
  316.         ASETVertex v;
  317.         v.parse(tokenItr);
  318.         tVertexList.push_back(v);
  319.     }
  320.     // consume }
  321.     tokenItr++;
  322.  
  323.     numTVFaces = parseUInt(tokenItr);
  324.  
  325.     // consume *MESH_TFACELIST and {
  326.     tokenItr += 2;
  327.     for(unsigned int i = 0; i < numTVFaces; i++)
  328.     {
  329.         ASETFace f;
  330.         f.parse(tokenItr);
  331.         tFaceList.push_back(f);
  332.     }
  333.     // consume }
  334.     tokenItr++;
  335.  
  336.     numCVertex = parseUInt(tokenItr);
  337.  
  338.     if(numCVertex > 0)
  339.     {
  340.         // consume *MESH_CVERTLIST and {
  341.         tokenItr += 2;
  342.         for(unsigned int i = 0; i < numCVertex; i++)
  343.         {
  344.             ASECVertex c;
  345.             c.parse(tokenItr);
  346.             cVertexList.push_back(c);
  347.         }
  348.         // consume }
  349.         tokenItr++;
  350.  
  351.         numCVFaces = parseUInt(tokenItr);
  352.  
  353.         // consume *MESH_CVERTLIST and {
  354.         tokenItr += 2;
  355.         for(unsigned int i = 0; i < numCVFaces; i++)
  356.         {
  357.             ASECFace c;
  358.             c.parse(tokenItr);
  359.             cFaceList.push_back(c);
  360.         }
  361.         // consume }
  362.         tokenItr++;
  363.     }
  364.  
  365.     // consume *MESH_NORMALS and {
  366.     tokenItr += 2;
  367.     for(unsigned int i = 0; i < numFaces; i++)
  368.     {
  369.         ASENormal fNorm;
  370.         fNorm.parse(tokenItr);
  371.         faceNormals.push_back(fNorm);
  372.  
  373.         for(int j = 0; j < 3; j++)
  374.         {
  375.             ASENormal vNorm;
  376.             vNorm.parse(tokenItr);
  377.             vertexNormals.push_back(vNorm);
  378.         }
  379.     }
  380.     // consume }
  381.     tokenItr++;
  382.  
  383.     // consume }
  384.     tokenItr++;
  385. }
  386.  
  387. //=============================================================================
  388. // ASENodeTM
  389. //=============================================================================
  390.  
  391. void ASENodeTM::parse(vector<string>::iterator& tokenItr)
  392. {
  393.     // consume *NODE_TM and {
  394.     tokenItr += 2;
  395.  
  396.     nodeName = parseString(tokenItr);
  397.  
  398.     parseVector3(tokenItr, inheritPos);
  399.     parseVector3(tokenItr, inheritRot);
  400.     parseVector3(tokenItr, inheritScl);
  401.     parseVector3(tokenItr, tmRow0);
  402.     parseVector3(tokenItr, tmRow1);
  403.     parseVector3(tokenItr, tmRow2);
  404.     parseVector3(tokenItr, tmRow3);
  405.     parseVector3(tokenItr, tmPos);
  406.     parseVector3(tokenItr, tmRotAxis);
  407.  
  408.     tmRotAngle = parseFloat(tokenItr);
  409.  
  410.     parseVector3(tokenItr, tmScale);
  411.     parseVector3(tokenItr, tmScaleAxis);
  412.  
  413.     tmScaleAxisAng = parseFloat(tokenItr);
  414.  
  415.     // consume }
  416.     tokenItr++;
  417. }
  418.  
  419. //=============================================================================
  420. // ASEFace
  421. //=============================================================================
  422.  
  423. void ASEFace::parse(vector<string>::iterator& tokenItr)
  424. {
  425.     // consume *MESH_FACE
  426.     tokenItr++;
  427.  
  428.     // the id ends with a colon, remove that
  429.     id = atoi(tokenItr->substr(0, tokenItr->size()-1).c_str());
  430.     tokenItr++;
  431.  
  432.     A = parseUInt(tokenItr);
  433.     B = parseUInt(tokenItr);
  434.     C = parseUInt(tokenItr);
  435.     AB = parseUInt(tokenItr);
  436.     BC = parseUInt(tokenItr);
  437.     CA = parseUInt(tokenItr);
  438.  
  439.     // if the face doesn't have a smoothing group it will be empty (not 0)
  440.     tokenItr++;
  441.     if(*tokenItr != "*MESH_MTLID")
  442.     {
  443.         meshSmoothing = atoi(tokenItr->c_str());
  444.         tokenItr++;
  445.     }
  446.     else
  447.         meshSmoothing = -1; // flag that it's empty
  448.  
  449.     meshMtlID = parseUInt(tokenItr);
  450. }
  451.  
  452. //=============================================================================
  453. // ASETFace
  454. //=============================================================================
  455.  
  456. void ASETFace::parse(vector<string>::iterator& tokenItr)
  457. {
  458.     // consume *MESH_TFACE or *MESH_CFACE
  459.     tokenItr++;
  460.  
  461.     id = atoi(tokenItr->c_str());
  462.     tokenItr++;
  463.  
  464.     A = atoi(tokenItr->c_str());
  465.     tokenItr++;
  466.  
  467.     B = atoi(tokenItr->c_str());
  468.     tokenItr++;
  469.  
  470.     C = atoi(tokenItr->c_str());
  471.     tokenItr++;
  472. }
  473.  
  474. //=============================================================================
  475. // ASEVertex
  476. //=============================================================================
  477.  
  478. void ASEVertex::parse(vector<string>::iterator& tokenItr)
  479. {
  480.     // consume *MESH_VERTEX or TVERTEX or FACENORMAL, etc.
  481.     tokenItr++;
  482.  
  483.     id = atoi(tokenItr->c_str());
  484.     tokenItr++;
  485.  
  486.     coords.x = atof(tokenItr->c_str());
  487.     tokenItr++;
  488.  
  489.     coords.y = atof(tokenItr->c_str());
  490.     tokenItr++;
  491.  
  492.     coords.z = atof(tokenItr->c_str());
  493.     tokenItr++;
  494. }
  495.  
  496. //=============================================================================
  497. // << Operators
  498. //=============================================================================
  499.  
  500. namespace E3D
  501. {
  502.  
  503. std::ostream& operator <<(std::ostream& o, const ASEModel& m)
  504. {
  505.     o << "ASEModel ========================================================\n";
  506.     o << "Export Version: " << m.exportVersion << "\n";
  507.     o << "Comment: " << m.comment << "\n";
  508.     o << m.scene;
  509.     o << m.materialList;
  510.  
  511.     for(vector<ASEGeomObject>::const_iterator it = m.geomObjects.begin();
  512.         it != m.geomObjects.end(); ++it)
  513.     {
  514.         o << *it;
  515.     }
  516.  
  517.     return o;
  518. }
  519.  
  520. std::ostream& operator <<(std::ostream& o, const ASEScene& s)
  521. {
  522.     o << "ASEScene ========================================================\n";
  523.     o << "Filename: " << s.filename << "\n";
  524.     o << "First Frame: " << s.firstFrame << "\n";
  525.     o << "Last Frame: " << s.lastFrame << "\n";
  526.     o << "Frame Speed: " << s.frameSpeed << "\n";
  527.     o << "Ticks Per Frame: " << s.ticksPerFrame << "\n";
  528.     o << "Background Static: " << s.backgroundStatic << "\n";
  529.     o << "Ambient Static: " << s.ambientStatic << "\n";
  530.  
  531.     return o;
  532. }
  533.  
  534. std::ostream& operator <<(std::ostream& o, const ASEMaterialList& m)
  535. {
  536.     o << "ASEMaterialList =================================================\n";
  537.     o << "Material Count: " << m.materialCount << "\n";
  538.  
  539.     for(vector<ASEMaterial>::const_iterator it = m.materials.begin();
  540.         it != m.materials.end(); ++it)
  541.     {
  542.         o << *it;
  543.     }
  544.  
  545.     return o;
  546. }
  547.  
  548. std::ostream& operator <<(std::ostream& o, const ASEMaterial& m)
  549. {
  550.     o << "ASEMaterial =====================================================\n";
  551.     o << "Name: %s\n" << m.name << "\n";
  552.     o << "Class: %s\n" << m.matClass << "\n";
  553.     o << "Ambient: " << m.ambient << "\n";
  554.     o << "Diffuse: " << m.diffuse << "\n";
  555.     o << "Specular: " << m.specular << "\n";
  556.     o << "Shine: " << m.shine << "\n";
  557.     o << "Shine Strength: " << m.shineStrength << "\n";
  558.     o << "Transparency: " << m.transparency << "\n";
  559.     o << "Wire Size: " << m.wireSize << "\n";
  560.     o << "Submaterials ====================================================\n";
  561.     o << "Number of Submaterials: " << m.numSubMtls << "\n";
  562.     for(vector<ASEMaterial>::const_iterator it = m.subMaterials.begin();
  563.         it != m.subMaterials.end(); ++it)
  564.     {
  565.         o << *it;
  566.     }
  567.     o << "=================================================================\n";
  568.     o << "Shading: " << m.shading << "\n";
  569.     o << "XP Falloff: " << m.xpFalloff << "\n";
  570.     o << "Self Illumination; " << m.selfIllum << "\n";
  571.     o << "Falloff: " << m.falloff << "\n";
  572.     o << "XP Type: " << m.xpType << "\n";
  573.     o << "Maps ============================================================\n";
  574.     for(vector<ASEMap>::const_iterator it = m.maps.begin();
  575.         it != m.maps.end(); ++it)
  576.     {
  577.         o << *it;
  578.     }
  579.     o << "=================================================================\n";
  580.  
  581.     return o;
  582. }
  583.  
  584. std::ostream& operator <<(std::ostream& o, const ASEMap& m)
  585. {
  586.     o << "ASEMap ==========================================================\n";
  587.     o << "Map Func: " << m.mapFunc << "\n";
  588.     o << "Name: " << m.name << "\n";
  589.     o << "Class: " << m.mapClass << "\n";
  590.     o << "Subno: " << m.subno << "\n";
  591.     o << "Amount: " << m.amount << "\n";
  592.     o << "Bitmap: " << m.bitmap << "\n";
  593.     o << "Type: " << m.type << "\n";
  594.     o << "uvw U Offset: " << m.uvwUOffset << "\n";
  595.     o << "uvw V Offset: " << m.uvwVOffset << "\n";
  596.     o << "uvw U Tiling: " << m.uvwUTiling << "\n";
  597.     o << "uvw V Tiling: " << m.uvwVTiling << "\n";
  598.     o << "uvw Angle: " << m.uvwAngle << "\n";
  599.     o << "uvw Blur: " << m.uvwBlur << "\n";
  600.     o << "uvw Blur Offset: " << m.uvwBlurOffset << "\n";
  601.     o << "uvw Noise Amt: " << m.uvwNoiseAmt << "\n";
  602.     o << "uvw Noise Size: " << m.uvwNoiseSize << "\n";
  603.     o << "uvw Noise Level: " << m.uvwNoiseLevel << "\n";
  604.     o << "uvw Noise Phase: " << m.uvwNoisePhase << "\n";
  605.     o << "Bitmap Filter: " << m.bitmapFilter << "\n";
  606.  
  607.     return o;
  608. }
  609.  
  610. std::ostream& operator <<(std::ostream& o, const ASEGeomObject& go)
  611. {
  612.     o << "ASEGeomObject ===================================================\n";
  613.     o << "Node Name: " << go.nodeName << "\n";
  614.     o << go.nodeTM;
  615.     o << go.mesh;
  616.     o << "Motion Blur: " << go.propMotionBlur << "\n";
  617.     o << "Cast Shadow: " << go.propCastShadow << "\n";
  618.     o << "Recieve Shadow: " << go.propRecvShadow << "\n";
  619.     o << "Material Reference: " << go.materialRef << "\n";
  620.  
  621.     return o;
  622. }
  623.  
  624. std::ostream& operator <<(std::ostream& o, const ASENodeTM& n)
  625. {
  626.     o << "ASENodeTM =======================================================\n";
  627.     o << "Node Name: " << n.nodeName << "\n";
  628.     o << "Inherit Position: " << n.inheritPos << "\n";
  629.     o << "Inherit Rotation: " << n.inheritRot << "\n";
  630.     o << "Inherit Scale: " << n.inheritScl << "\n";
  631.     o << "TM Row0: " << n.tmRow0 << "\n";
  632.     o << "TM Row1: " << n.tmRow1 << "\n";
  633.     o << "TM Row2: " << n.tmRow2 << "\n";
  634.     o << "TM Row3: " << n.tmRow3 << "\n";
  635.     o << "TM Pos: " << n.tmPos << "\n";
  636.     o << "TM Rot Axis: " << n.tmRotAxis << "\n";
  637.     o << "TM Rot Angle: " << n.tmRotAngle << "\n";
  638.     o << "TM Scale: " << n.tmScale << "\n";
  639.     o << "TM Scale Axis: " << n.tmScaleAxis << "\n";
  640.     o << "TM Scale Axis Angle: " << n.tmScaleAxisAng << "\n";
  641.  
  642.     return o;
  643. }
  644.  
  645. std::ostream& operator <<(std::ostream& o, const ASEMesh& m)
  646. {
  647.     o << "ASEMesh =========================================================\n";
  648.     o << "Time Value: " << m.timeValue << "\n";
  649.     o << "Number of Vertices: " << m.numVertex << "\n";
  650.     for(vector<ASEVertex>::const_iterator it = m.vertexList.begin();
  651.         it != m.vertexList.end(); ++it)
  652.     {
  653.         o << *it << "\n";
  654.     }
  655.     o << "Number of Faces: " << m.numFaces << "\n";
  656.     for(vector<ASEFace>::const_iterator it = m.faceList.begin();
  657.         it != m.faceList.end(); ++it)
  658.     {
  659.         o << *it << "\n";
  660.     }
  661.     o << "Number of TVertices: " << m.numTVertex << "\n";
  662.     for(vector<ASETVertex>::const_iterator it = m.tVertexList.begin();
  663.         it != m.tVertexList.end(); ++it)
  664.     {
  665.         o << *it << "\n";
  666.     }
  667.     o << "Number of TFaces: %d\n" << m.numTVFaces << "\n";
  668.     for(vector<ASETFace>::const_iterator it = m.tFaceList.begin();
  669.         it != m.tFaceList.end(); ++it)
  670.     {
  671.         o << *it << "\n";
  672.     }
  673.     o << "Number of CVertices: " << m.numCVertex << "\n";
  674.     // TODO: print cvertex list and cface list
  675.     o << "Normals:\n";
  676.     // TODO: iterators?
  677.     for(unsigned int i = 0; i < m.numFaces; i++)
  678.     {
  679.         o << "Face Normal ";
  680.         o << m.faceNormals.at(i) << "\n";
  681.  
  682.         for(int j = 0; j < 3; j++)
  683.         {
  684.             o << "Vertex Normal ";
  685.             o << m.vertexNormals.at(i*3+j) << "\n";
  686.         }
  687.     }
  688.  
  689.     return o;
  690. }
  691.  
  692. std::ostream& operator <<(std::ostream& o, const ASETFace& tf)
  693. {
  694.     o << tf.id << " " << tf.A << " " << tf.B << " " << tf.C;
  695.     return o;
  696. }
  697.  
  698. std::ostream& operator <<(std::ostream& o, const ASEFace& f)
  699. {
  700.     o << f.id << " A: " << f.A << " B: " << f.B << " C: " << f.C << " ";
  701.     o << "AB: " << f.AB << " BC: " << f.BC << " CA: " << f.CA << " ";
  702.     o << "Smoothing Group: " << f.meshSmoothing << " ";
  703.     o << "Material ID: " << f.meshMtlID;
  704.     return o;
  705. }
  706.  
  707. std::ostream& operator <<(std::ostream& o, const ASEVertex& v)
  708. {
  709.     o << v.id << " " << v.coords;
  710.     return o;
  711. }
  712.  
  713. }
  714.  
  715. //=============================================================================
  716. // Parsing helper functions
  717. //=============================================================================
  718.  
  719. /*
  720.  * Convenience function to split an input string into tokens based on
  721.  * whitespace. Perserves quoted string literals.
  722.  */
  723. static void split(string input, vector<string>& tokens)
  724. {
  725.     string token;
  726.     std::istringstream iss(input);
  727.  
  728.     while(iss >> token)
  729.     {
  730.         // if this is the beginning of a quoted string that does not end with
  731.         // a matching quote
  732.         if((token.at(0) == '\"') && (token.at(token.size()-1) != '\"'))
  733.         {
  734.             // create a temp string to accumulate the full quoted string
  735.             string quotedStr(token);
  736.  
  737.             // begin grabbing subsequent tokens until we find one that has the
  738.             // matching end quote
  739.             while(iss >> token)
  740.             {
  741.                 // append a space because presumably that is what caused the
  742.                 // break
  743.                 quotedStr.append(" ");
  744.                 quotedStr.append(token);
  745.  
  746.                 if(token.at(token.size()-1) == '\"')
  747.                     break;
  748.             }
  749.  
  750.             tokens.push_back(quotedStr);
  751.             continue;
  752.         }
  753.  
  754.         tokens.push_back(token);
  755.     }
  756. }
  757.  
  758. static float parseFloat(vector<string>::iterator& tokenItr)
  759. {
  760.     tokenItr++;
  761.     float val = atof(tokenItr->c_str());
  762.     tokenItr++;
  763.  
  764.     return val;
  765. }
  766.  
  767. static unsigned int parseUInt(vector<string>::iterator& tokenItr)
  768. {
  769.     tokenItr++;
  770.     unsigned int val = atoi(tokenItr->c_str());
  771.     tokenItr++;
  772.  
  773.     return val;
  774. }
  775.  
  776. static string& parseString(vector<string>::iterator& tokenItr)
  777. {
  778.     tokenItr++;
  779.     string& s = *tokenItr;
  780.     tokenItr++;
  781.  
  782.     return s;
  783. }
  784.  
  785. static void parseRGB(vector<string>::iterator& tokenItr, RGB& rgb)
  786. {
  787.     tokenItr++;
  788.     rgb.r = atof(tokenItr->c_str()); tokenItr++;
  789.     rgb.g = atof(tokenItr->c_str()); tokenItr++;
  790.     rgb.b = atof(tokenItr->c_str()); tokenItr++;
  791. }
  792.  
  793. static void parseVector3(vector<string>::iterator& tokenItr, Vector3& v)
  794. {
  795.     tokenItr++;
  796.     v.x = atof(tokenItr->c_str()); tokenItr++;
  797.     v.y = atof(tokenItr->c_str()); tokenItr++;
  798.     v.z = atof(tokenItr->c_str()); tokenItr++;
  799. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement