#include #include #include #include using namespace std; #include "Vector3.h" #include "Mesh.h" #include "SceneGraph.h" #include "UnigraphicsParser.h" #include "Material.h" #include "Environment.h" #include "ResourceManager.h" namespace GtpVisibilityPreprocessor { #define ROTATE_SCENE 0 // HACK static void RotateMesh(Mesh *mesh) { VertexContainer::iterator it, it_end = mesh->mVertices.end(); const float angle = 30.0f * PI / 180.0f; const Matrix4x4 rot = RotationYMatrix(angle); for (it = mesh->mVertices.begin(); it != it_end; ++ it) { (*it) = rot * (*it); } } struct ltstr { bool operator()(const string s1, const string s2) const { return s1 < s2; } }; bool UnigraphicsParser::ParseFile(const string filename, SceneGraphNode **proot, const bool loadPolygonsAsMeshese) { map vht; // hash table for vectors map cht; // hash table for colors FILE *file; char str[80]; Face *face; //Vector3 *vptr; float x,y,z; int i; if ((file = fopen(filename.c_str(),"rt")) == NULL) return false; SceneGraphNode *root = new SceneGraphNode; Mesh *currentMesh = MeshManager::GetSingleton()->CreateResource(); int meshGrouping; environment->GetIntValue("Unigraphics.meshGrouping", meshGrouping); while(fscanf(file,"%s",str)!=EOF) { switch (str[0]) { case 'c': if (strcmp(str,"c_rgb")==0) { cout<<"c"; cout.flush(); /* vertex */ fscanf(file,"%s",str); // label fscanf(file,"%f %f %f",&x,&y,&z); cht[str] = Vector3(x,y,z); // swap the file coordinates } break; case 'v': // is it v ? cout<<"."; cout.flush(); /* vertex */ fscanf(file,"%s",str); // label fscanf(file,"%f %f %f",&x,&y,&z); vht[str] = Vector3(x,z,-y); // swap the file coordinates // vptr = new Vector3C(x,y,z); // swap the file coordinates break; case 'f': { cout<<"+"; vector vertices; VertexIndexContainer vertexIndices; /* face */ fscanf(file,"%s",str); // label may be ? if (str[0]!='(') fscanf(file,"%s",str); i = 0; // index of the vertex map::const_iterator c = vht.find(&str[1]); if (c == vht.end()) { cout<<"error reading file : "<< filename<<" ---missing vertex:"<mVertices.size(); for (i=0; i < vertices.size(); i++, index++) { currentMesh->mVertices.push_back(vertices[i]); vertexIndices.push_back(index); } face = new Face(vertexIndices); fscanf(file,"%s",str); // get the next vertex label if (str[0]!=';') { c = cht.find(str); if (currentMesh->mMaterial == NULL) { Material *mat = MaterialManager::GetSingleton()->CreateResource(); if (c != cht.end()) { mat->mDiffuseColor = RgbColor((*c).second.x, (*c).second.y, (*c).second.z); currentMesh->mMaterial = mat; } else currentMesh->AssignRandomMaterial(); } } currentMesh->AddFace(face); if (meshGrouping != 0 && currentMesh->mFaces.size() >= meshGrouping) { if (ROTATE_SCENE) RotateMesh(currentMesh); currentMesh->Preprocess(); // make an instance of this mesh MeshInstance *mi = new MeshInstance(currentMesh); root->mGeometry.push_back(mi); currentMesh = MeshManager::GetSingleton()->CreateResource(); } } break; } /* end face */ // get the rest of the line fgets(str,80,file); } if (currentMesh->mVertices.size()) { if (1) RotateMesh(currentMesh); currentMesh->Preprocess(); MeshInstance *mi = new MeshInstance(currentMesh); root->mGeometry.push_back(mi); } fclose(file); *proot = root; return true; } }