#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" #include "Triangle3.h" #include "IntersectableWrapper.h" #include "Polygon3.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 *root, const bool loadMeshes, vector *parents) { map vht; // hash table for vectors map cht; // hash table for colors FILE *file; char str[80]; Face *face; float x,y,z; //int i; if ((file = fopen(filename.c_str(),"rt")) == NULL) return false; int meshGrouping; Environment::GetSingleton()->GetIntValue("Unigraphics.meshGrouping", meshGrouping); Mesh *currentMesh = MeshManager::GetSingleton()->CreateResource(); 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 << "+"; VertexContainer vertices; VertexIndexContainer vertexIndices; // face fscanf(file,"%s",str); // label may be ? if (str[0]!='(') fscanf(file,"%s",str); map::const_iterator c = vht.find(&str[1]); if (c == vht.end()) { cout<<"error reading file : "<< filename <<" ---missing vertex:"<mVertices.size(); for (int i=0; i < (int)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 (loadMeshes && (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.empty()) { if (ROTATE_SCENE) { RotateMesh(currentMesh); } if (!loadMeshes) { // hack: just triangulate current mesh FaceContainer::const_iterator fit, fit_begin = currentMesh->mFaces.begin(), fit_end = currentMesh->mFaces.end(); for (fit = fit_begin; fit != fit_end; ++ fit) { // triangulate the faces Face *face = *fit; vector triangles; Polygon3 poly(face, currentMesh); poly.Triangulate(triangles); vector::const_iterator tit, tit_end = triangles.end(); for (tit = triangles.begin(); tit != tit_end; ++ tit) { if ((*tit).CheckValidity()) { // create new triangle intersectable TriangleIntersectable *ti = new TriangleIntersectable(*tit); //cout << "t: " << (*tit) << endl; root->mGeometry.push_back(ti); } else { cout << "error tri:\n" << (*tit) << endl; } } } // not needed anymore MeshManager::GetSingleton()->DestroyEntry(currentMesh->GetId()); } else { currentMesh->Preprocess(); MeshInstance *mi = new MeshInstance(currentMesh); root->mGeometry.push_back(mi); } } fclose(file); return true; } }