1 | #include <stdlib.h>
|
---|
2 | #include <iostream>
|
---|
3 | #include <list>
|
---|
4 | #include <map>
|
---|
5 | using namespace std;
|
---|
6 |
|
---|
7 | #include "Vector3.h"
|
---|
8 | #include "Mesh.h"
|
---|
9 | #include "SceneGraph.h"
|
---|
10 |
|
---|
11 | #include "UnigraphicsParser.h"
|
---|
12 | #include "Material.h"
|
---|
13 | #include "Environment.h"
|
---|
14 | #include "ResourceManager.h"
|
---|
15 | #include "Triangle3.h"
|
---|
16 | #include "IntersectableWrapper.h"
|
---|
17 | #include "Polygon3.h"
|
---|
18 |
|
---|
19 |
|
---|
20 | namespace GtpVisibilityPreprocessor {
|
---|
21 |
|
---|
22 | #define ROTATE_SCENE 0
|
---|
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 |
|
---|
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,
|
---|
48 | SceneGraphNode *root,
|
---|
49 | const bool loadMeshes,
|
---|
50 | vector<FaceParentInfo> *parents)
|
---|
51 | {
|
---|
52 | map<string, Vector3, ltstr> vht; // hash table for vectors
|
---|
53 | map<string, Vector3, ltstr> cht; // hash table for colors
|
---|
54 |
|
---|
55 | FILE *file;
|
---|
56 | char str[80];
|
---|
57 | Face *face;
|
---|
58 |
|
---|
59 | float x,y,z;
|
---|
60 | //int i;
|
---|
61 |
|
---|
62 | if ((file = fopen(filename.c_str(),"rt")) == NULL)
|
---|
63 | return false;
|
---|
64 |
|
---|
65 | int meshGrouping;
|
---|
66 | Environment::GetSingleton()->GetIntValue("Unigraphics.meshGrouping",
|
---|
67 | meshGrouping);
|
---|
68 |
|
---|
69 | Mesh *currentMesh = MeshManager::GetSingleton()->CreateResource();
|
---|
70 |
|
---|
71 | while(fscanf(file,"%s",str)!=EOF)
|
---|
72 | {
|
---|
73 | switch (str[0])
|
---|
74 | {
|
---|
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);
|
---|
83 |
|
---|
84 | cht[str] = Vector3(x,y,z); // swap the file coordinates
|
---|
85 | }
|
---|
86 | break;
|
---|
87 |
|
---|
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 << "+";
|
---|
102 | VertexContainer vertices;
|
---|
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())
|
---|
155 | {
|
---|
156 | mat->mDiffuseColor = RgbColor((*c).second.x, (*c).second.y, (*c).second.z);
|
---|
157 | currentMesh->mMaterial = mat;
|
---|
158 | }
|
---|
159 | else
|
---|
160 | {
|
---|
161 | currentMesh->AssignRandomMaterial();
|
---|
162 | }
|
---|
163 | }
|
---|
164 | }
|
---|
165 |
|
---|
166 | currentMesh->AddFace(face);
|
---|
167 |
|
---|
168 | if (loadMeshes &&
|
---|
169 | (meshGrouping != 0 && currentMesh->mFaces.size() >= meshGrouping))
|
---|
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 | }
|
---|
182 | }
|
---|
183 | break;
|
---|
184 | } /* end face */
|
---|
185 |
|
---|
186 | // get the rest of the line
|
---|
187 | fgets(str,80,file);
|
---|
188 | }
|
---|
189 |
|
---|
190 | if (!currentMesh->mVertices.empty())
|
---|
191 | {
|
---|
192 | if (ROTATE_SCENE)
|
---|
193 | {
|
---|
194 | RotateMesh(currentMesh);
|
---|
195 | }
|
---|
196 |
|
---|
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 | }
|
---|
238 | }
|
---|
239 |
|
---|
240 | fclose(file);
|
---|
241 | return true;
|
---|
242 | }
|
---|
243 |
|
---|
244 | }
|
---|