[162] | 1 | #include <stdlib.h>
|
---|
| 2 | #include <iostream>
|
---|
| 3 | #include <list>
|
---|
| 4 | #include <map>
|
---|
| 5 | using namespace std;
|
---|
| 6 |
|
---|
[167] | 7 | #include "Vector3.h"
|
---|
| 8 | #include "Mesh.h"
|
---|
[162] | 9 | #include "SceneGraph.h"
|
---|
| 10 |
|
---|
| 11 | #include "UnigraphicsParser.h"
|
---|
| 12 | #include "Material.h"
|
---|
| 13 | #include "Environment.h"
|
---|
[1001] | 14 | #include "ResourceManager.h"
|
---|
[1627] | 15 | #include "Triangle3.h"
|
---|
| 16 | #include "IntersectableWrapper.h"
|
---|
| 17 | #include "Polygon3.h"
|
---|
[162] | 18 |
|
---|
[1001] | 19 |
|
---|
[863] | 20 | namespace GtpVisibilityPreprocessor {
|
---|
[162] | 21 |
|
---|
[704] | 22 | #define ROTATE_SCENE 0
|
---|
[694] | 23 | // HACK
|
---|
| 24 | static void RotateMesh(Mesh *mesh)
|
---|
| 25 | {
|
---|
| 26 | VertexContainer::iterator it, it_end = mesh->mVertices.end();
|
---|
| 27 |
|
---|
| 28 | const float angle = 30.0f * PI / 180.0f;
|
---|
| 29 | const Matrix4x4 rot = RotationYMatrix(angle);
|
---|
| 30 |
|
---|
| 31 | for (it = mesh->mVertices.begin(); it != it_end; ++ it)
|
---|
| 32 | {
|
---|
| 33 | (*it) = rot * (*it);
|
---|
| 34 | }
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 |
|
---|
[162] | 38 | struct ltstr
|
---|
| 39 | {
|
---|
| 40 | bool operator()(const string s1, const string s2) const
|
---|
| 41 | {
|
---|
| 42 | return s1 < s2;
|
---|
| 43 | }
|
---|
| 44 | };
|
---|
| 45 |
|
---|
| 46 | bool
|
---|
| 47 | UnigraphicsParser::ParseFile(const string filename,
|
---|
[1344] | 48 | SceneGraphNode *root,
|
---|
[1379] | 49 | const bool loadMeshes,
|
---|
[1281] | 50 | vector<FaceParentInfo> *parents)
|
---|
[162] | 51 | {
|
---|
| 52 | map<string, Vector3, ltstr> vht; // hash table for vectors
|
---|
| 53 | map<string, Vector3, ltstr> cht; // hash table for colors
|
---|
| 54 |
|
---|
[1577] | 55 | FILE *file;
|
---|
[162] | 56 | char str[80];
|
---|
[1577] | 57 | Face *face;
|
---|
| 58 |
|
---|
[162] | 59 | float x,y,z;
|
---|
[1577] | 60 | //int i;
|
---|
[162] | 61 |
|
---|
| 62 | if ((file = fopen(filename.c_str(),"rt")) == NULL)
|
---|
| 63 | return false;
|
---|
| 64 |
|
---|
| 65 | int meshGrouping;
|
---|
[1004] | 66 | Environment::GetSingleton()->GetIntValue("Unigraphics.meshGrouping",
|
---|
[162] | 67 | meshGrouping);
|
---|
| 68 |
|
---|
[1627] | 69 | Mesh *currentMesh = MeshManager::GetSingleton()->CreateResource();
|
---|
| 70 |
|
---|
[1577] | 71 | while(fscanf(file,"%s",str)!=EOF)
|
---|
| 72 | {
|
---|
| 73 | switch (str[0])
|
---|
[162] | 74 | {
|
---|
[1577] | 75 | case 'c':
|
---|
| 76 | if (strcmp(str,"c_rgb")==0)
|
---|
| 77 | {
|
---|
| 78 | cout<<"c";
|
---|
| 79 | cout.flush();
|
---|
| 80 | // vertex
|
---|
| 81 | fscanf(file,"%s",str); // label
|
---|
| 82 | fscanf(file,"%f %f %f",&x,&y,&z);
|
---|
[162] | 83 |
|
---|
[1577] | 84 | cht[str] = Vector3(x,y,z); // swap the file coordinates
|
---|
| 85 | }
|
---|
| 86 | break;
|
---|
[1002] | 87 |
|
---|
[1577] | 88 | case 'v': // is it v ?
|
---|
| 89 | cout<<".";
|
---|
| 90 | cout.flush();
|
---|
| 91 | // vertex
|
---|
| 92 | fscanf(file,"%s",str); // label
|
---|
| 93 | fscanf(file,"%f %f %f",&x,&y,&z);
|
---|
| 94 |
|
---|
| 95 | vht[str] = Vector3(x,z,-y); // swap the file coordinates
|
---|
| 96 | // vptr = new Vector3C(x,y,z); // swap the file coordinates
|
---|
| 97 | break;
|
---|
| 98 |
|
---|
| 99 | case 'f':
|
---|
| 100 | {
|
---|
| 101 | cout << "+";
|
---|
[1627] | 102 | VertexContainer vertices;
|
---|
[1577] | 103 | VertexIndexContainer vertexIndices;
|
---|
| 104 |
|
---|
| 105 | // face
|
---|
| 106 | fscanf(file,"%s",str); // label may be ?
|
---|
| 107 | if (str[0]!='(')
|
---|
| 108 | fscanf(file,"%s",str);
|
---|
| 109 |
|
---|
| 110 | map<string, Vector3, ltstr>::const_iterator c = vht.find(&str[1]);
|
---|
| 111 |
|
---|
| 112 | if (c == vht.end())
|
---|
| 113 | {
|
---|
| 114 | cout<<"error reading file : "<< filename
|
---|
| 115 | <<" ---missing vertex:"<<str<<endl; exit(1);
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | vertices.push_back((*c).second);
|
---|
| 119 |
|
---|
| 120 | fscanf(file,"%s",str); // get the vertex label
|
---|
| 121 |
|
---|
| 122 | while(str[0]!=')')
|
---|
| 123 | {
|
---|
| 124 | c = vht.find(str);
|
---|
| 125 | if (c == vht.end())
|
---|
| 126 | {
|
---|
| 127 | cout << "error reading file : " << filename << " -- missing vertex\n";
|
---|
| 128 | exit(1);
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | vertices.push_back((*c).second);
|
---|
| 132 | fscanf(file,"%s",str); // get the next vertex label
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | // add vertices to the mesh vertex list
|
---|
| 136 | int index = (int)currentMesh->mVertices.size();
|
---|
| 137 |
|
---|
| 138 | for (int i=0; i < (int)vertices.size(); ++ i, ++ index)
|
---|
| 139 | {
|
---|
| 140 | currentMesh->mVertices.push_back(vertices[i]);
|
---|
| 141 | vertexIndices.push_back(index);
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | face = new Face(vertexIndices);
|
---|
| 145 |
|
---|
| 146 | fscanf(file,"%s",str); // get the next vertex label
|
---|
| 147 |
|
---|
| 148 | if (str[0]!=';')
|
---|
| 149 | {
|
---|
| 150 | c = cht.find(str);
|
---|
| 151 | if (currentMesh->mMaterial == NULL)
|
---|
| 152 | {
|
---|
| 153 | Material *mat = MaterialManager::GetSingleton()->CreateResource();
|
---|
| 154 | if (c != cht.end())
|
---|
[1627] | 155 | {
|
---|
| 156 | mat->mDiffuseColor = RgbColor((*c).second.x, (*c).second.y, (*c).second.z);
|
---|
| 157 | currentMesh->mMaterial = mat;
|
---|
| 158 | }
|
---|
[1577] | 159 | else
|
---|
[1627] | 160 | {
|
---|
| 161 | currentMesh->AssignRandomMaterial();
|
---|
| 162 | }
|
---|
[1577] | 163 | }
|
---|
| 164 | }
|
---|
| 165 |
|
---|
| 166 | currentMesh->AddFace(face);
|
---|
[1627] | 167 |
|
---|
| 168 | if (loadMeshes &&
|
---|
| 169 | (meshGrouping != 0 && currentMesh->mFaces.size() >= meshGrouping))
|
---|
[1577] | 170 | {
|
---|
| 171 | if (ROTATE_SCENE)
|
---|
| 172 | {
|
---|
| 173 | RotateMesh(currentMesh);
|
---|
| 174 | }
|
---|
| 175 |
|
---|
| 176 | currentMesh->Preprocess();
|
---|
| 177 | // make an instance of this mesh
|
---|
| 178 | MeshInstance *mi = new MeshInstance(currentMesh);
|
---|
| 179 | root->mGeometry.push_back(mi);
|
---|
| 180 | currentMesh = MeshManager::GetSingleton()->CreateResource();
|
---|
| 181 | }
|
---|
[1627] | 182 | }
|
---|
[1577] | 183 | break;
|
---|
| 184 | } /* end face */
|
---|
| 185 |
|
---|
| 186 | // get the rest of the line
|
---|
| 187 | fgets(str,80,file);
|
---|
[162] | 188 | }
|
---|
[1577] | 189 |
|
---|
| 190 | if (!currentMesh->mVertices.empty())
|
---|
[1486] | 191 | {
|
---|
[1577] | 192 | if (ROTATE_SCENE)
|
---|
| 193 | {
|
---|
| 194 | RotateMesh(currentMesh);
|
---|
| 195 | }
|
---|
| 196 |
|
---|
[1627] | 197 | if (!loadMeshes)
|
---|
| 198 | {
|
---|
| 199 | // hack: just triangulate current mesh
|
---|
| 200 | FaceContainer::const_iterator fit, fit_begin = currentMesh->mFaces.begin(),
|
---|
| 201 | fit_end = currentMesh->mFaces.end();
|
---|
| 202 |
|
---|
| 203 | for (fit = fit_begin; fit != fit_end; ++ fit)
|
---|
| 204 | {
|
---|
| 205 | // triangulate the faces
|
---|
| 206 | Face *face = *fit;
|
---|
| 207 | vector<Triangle3> triangles;
|
---|
| 208 |
|
---|
| 209 | Polygon3 poly(face, currentMesh);
|
---|
| 210 | poly.Triangulate(triangles);
|
---|
| 211 |
|
---|
| 212 | vector<Triangle3>::const_iterator tit, tit_end = triangles.end();
|
---|
| 213 |
|
---|
| 214 | for (tit = triangles.begin(); tit != tit_end; ++ tit)
|
---|
| 215 | {
|
---|
| 216 | if ((*tit).CheckValidity())
|
---|
| 217 | {
|
---|
| 218 | // create new triangle intersectable
|
---|
| 219 | TriangleIntersectable *ti = new TriangleIntersectable(*tit);
|
---|
| 220 | //cout << "t: " << (*tit) << endl;
|
---|
| 221 | root->mGeometry.push_back(ti);
|
---|
| 222 | }
|
---|
| 223 | else
|
---|
| 224 | {
|
---|
| 225 | cout << "error tri:\n" << (*tit) << endl;
|
---|
| 226 | }
|
---|
| 227 | }
|
---|
| 228 | }
|
---|
| 229 | // not needed anymore
|
---|
| 230 | MeshManager::GetSingleton()->DestroyEntry(currentMesh->GetId());
|
---|
| 231 | }
|
---|
| 232 | else
|
---|
| 233 | {
|
---|
| 234 | currentMesh->Preprocess();
|
---|
| 235 | MeshInstance *mi = new MeshInstance(currentMesh);
|
---|
| 236 | root->mGeometry.push_back(mi);
|
---|
| 237 | }
|
---|
[162] | 238 | }
|
---|
[1577] | 239 |
|
---|
[162] | 240 | fclose(file);
|
---|
| 241 | return true;
|
---|
| 242 | }
|
---|
| 243 |
|
---|
[1281] | 244 | }
|
---|