1 | #include <stdlib.h>
|
---|
2 | #include <iostream>
|
---|
3 | #include <list>
|
---|
4 | #include <map>
|
---|
5 | #include <math.h>
|
---|
6 |
|
---|
7 | #include "Vector3.h"
|
---|
8 | #include "Mesh.h"
|
---|
9 | #include "SceneGraph.h"
|
---|
10 |
|
---|
11 | #include "ObjParser.h"
|
---|
12 | //#include "Material.h"
|
---|
13 | #include "Triangle3.h"
|
---|
14 | #include "Environment.h"
|
---|
15 | #include "ResourceManager.h"
|
---|
16 | #include "IntersectableWrapper.h"
|
---|
17 |
|
---|
18 |
|
---|
19 | using namespace std;
|
---|
20 |
|
---|
21 | namespace GtpVisibilityPreprocessor {
|
---|
22 |
|
---|
23 | #define CONNECT_SEQUENTIAL_FACES 0
|
---|
24 | #define ROTATE_SCENE 0
|
---|
25 |
|
---|
26 | // hack: define this as in the intel ray tracer
|
---|
27 | #define FLT_EPSILON 1.192092896e-07f
|
---|
28 |
|
---|
29 | // HACK
|
---|
30 | static void RotateMesh(Mesh *mesh)
|
---|
31 | {
|
---|
32 | VertexContainer::iterator it, it_end = mesh->mVertices.end();
|
---|
33 |
|
---|
34 | const float angle = 30.0f * PI / 180.0f;
|
---|
35 | const Matrix4x4 rot = RotationYMatrix(angle);
|
---|
36 |
|
---|
37 | for (it = mesh->mVertices.begin(); it != it_end; ++ it)
|
---|
38 | {
|
---|
39 | (*it) = rot * (*it);
|
---|
40 | }
|
---|
41 | }
|
---|
42 |
|
---|
43 |
|
---|
44 | struct ltstr
|
---|
45 | {
|
---|
46 | bool operator()(const string s1, const string s2) const
|
---|
47 | {
|
---|
48 | return s1 < s2;
|
---|
49 | }
|
---|
50 | };
|
---|
51 |
|
---|
52 |
|
---|
53 | static Face *LoadFace(char *str,
|
---|
54 | const VertexContainer &vertices,
|
---|
55 | map<int, Vector3> &hashTable)
|
---|
56 | {
|
---|
57 | char *pch = strtok(str + 1, " ");
|
---|
58 |
|
---|
59 | VertexIndexContainer indices;
|
---|
60 | while (pch != NULL)
|
---|
61 | {
|
---|
62 | const int index = (int)strtol(pch, NULL, 10) - 1;
|
---|
63 | //Debug << index << " x ";
|
---|
64 |
|
---|
65 | // store vertex in hash table
|
---|
66 | hashTable[index] = vertices[index];
|
---|
67 | indices.push_back(index);
|
---|
68 |
|
---|
69 | pch = strtok(NULL, " ");
|
---|
70 | }
|
---|
71 |
|
---|
72 | return new Face(indices);
|
---|
73 | }
|
---|
74 |
|
---|
75 |
|
---|
76 | static void LoadTriangles(char *str,
|
---|
77 | const VertexContainer &vertices,
|
---|
78 | vector<Triangle3> &triangles,
|
---|
79 | const int line)
|
---|
80 | {
|
---|
81 | vector<char *> substrings;
|
---|
82 |
|
---|
83 | // extract the triples of the form v/t/n v/t/n ...
|
---|
84 | char *pch = strtok(str + 1, " ");
|
---|
85 |
|
---|
86 | while (pch != NULL)
|
---|
87 | {
|
---|
88 | substrings.push_back(pch);
|
---|
89 | pch = strtok(NULL, " ");
|
---|
90 | }
|
---|
91 |
|
---|
92 | //for (int i = 0; i < substrings.size(); ++ i)
|
---|
93 | // cout << "sub: " << substrings[i] << " ";
|
---|
94 |
|
---|
95 | VertexIndexContainer indices;
|
---|
96 |
|
---|
97 | for (int i = 0; i < substrings.size(); ++ i)
|
---|
98 | {
|
---|
99 | // discard normal and texture indices
|
---|
100 | // and only keep first index
|
---|
101 |
|
---|
102 | char *str = strtok(substrings[i], "/");
|
---|
103 | const int index = (int)strtol(str, NULL, 10) - 1;
|
---|
104 |
|
---|
105 | // store indices
|
---|
106 | if (index >= 0)
|
---|
107 | indices.push_back(index);
|
---|
108 |
|
---|
109 | //cout << "i " << index << " ";
|
---|
110 |
|
---|
111 | // new triangle found
|
---|
112 | // problem: don't know how intel ray tracer tesselates
|
---|
113 | if ((int)indices.size() > 2)
|
---|
114 | {
|
---|
115 | const int idx2 = (int)indices.size() - 2;
|
---|
116 | const int idx3 = (int)indices.size() - 1;
|
---|
117 |
|
---|
118 | triangles.push_back(Triangle3(vertices[indices[0]],
|
---|
119 | vertices[indices[idx2]],
|
---|
120 | vertices[indices[idx3]]));
|
---|
121 | }
|
---|
122 | }
|
---|
123 | }
|
---|
124 |
|
---|
125 |
|
---|
126 | static Mesh *CreateMesh(FaceContainer &faces,
|
---|
127 | const map<int, Vector3> &hashTable)
|
---|
128 | {
|
---|
129 | Mesh *mesh = MeshManager::GetSingleton()->CreateResource();
|
---|
130 |
|
---|
131 | FaceContainer::const_iterator fit, fit_end = faces.end();
|
---|
132 |
|
---|
133 | for (fit = faces.begin(); fit != fit_end; ++ fit)
|
---|
134 | {
|
---|
135 | Face *face = *fit;
|
---|
136 | VertexIndexContainer::iterator vit, vit_end = face->mVertexIndices.end();
|
---|
137 |
|
---|
138 | for (vit = face->mVertexIndices.begin(); vit != vit_end; ++ vit)
|
---|
139 | {
|
---|
140 | // go through indices
|
---|
141 | const int index = *vit;
|
---|
142 | map<int, Vector3>::const_iterator hit = hashTable.find(index);
|
---|
143 |
|
---|
144 | // correct face index (nust be relative to start of verices)
|
---|
145 | (*vit) = (int)distance(hashTable.begin(), hit);
|
---|
146 | }
|
---|
147 | }
|
---|
148 |
|
---|
149 | VertexContainer vertices;
|
---|
150 |
|
---|
151 | map<int, Vector3>::const_iterator hit, hit_end = hashTable.end();
|
---|
152 |
|
---|
153 | // store vertices in given order
|
---|
154 | for (hit = hashTable.begin(); hit != hit_end; ++ hit)
|
---|
155 | {
|
---|
156 | mesh->mVertices.push_back((*hit).second);
|
---|
157 | }
|
---|
158 |
|
---|
159 | mesh->mFaces = faces;
|
---|
160 | // can't do cleanup because coupling with kdf file for intel ray tracer
|
---|
161 | mesh->Preprocess(false);
|
---|
162 |
|
---|
163 | return mesh;
|
---|
164 | }
|
---|
165 |
|
---|
166 |
|
---|
167 | // HACK: associate mesh instances with triangles
|
---|
168 | static void AssociateFacesWithInstance(MeshInstance *mi,
|
---|
169 | vector<FaceParentInfo> &parents)
|
---|
170 | {
|
---|
171 | Mesh *mesh = mi->GetMesh();
|
---|
172 |
|
---|
173 | int i = 0;
|
---|
174 | FaceContainer::const_iterator fit, fit_end = mesh->mFaces.end();
|
---|
175 |
|
---|
176 | for (fit = mesh->mFaces.begin(); fit != fit_end; ++ fit, i++)
|
---|
177 | {
|
---|
178 | parents.push_back(FaceParentInfo(mi, i));
|
---|
179 | }
|
---|
180 | }
|
---|
181 |
|
---|
182 |
|
---|
183 | static void ProcessMesh(FaceContainer &faces,
|
---|
184 | map<int, Vector3> &hashTable,
|
---|
185 | SceneGraphLeaf *root,
|
---|
186 | vector<FaceParentInfo> *parents)
|
---|
187 | {
|
---|
188 | Mesh *mesh = CreateMesh(faces, hashTable);
|
---|
189 | // make an instance of this mesh
|
---|
190 | MeshInstance *mi = new MeshInstance(mesh);
|
---|
191 |
|
---|
192 | if (parents)
|
---|
193 | {
|
---|
194 | AssociateFacesWithInstance(mi, *parents);
|
---|
195 | }
|
---|
196 |
|
---|
197 | root->mGeometry.push_back(mi);
|
---|
198 |
|
---|
199 | // reset tables
|
---|
200 | hashTable.clear();
|
---|
201 | faces.clear();
|
---|
202 | }
|
---|
203 |
|
---|
204 |
|
---|
205 | bool TriangleValid(const Triangle3 &triangle)
|
---|
206 | {
|
---|
207 | const Vector3 a = triangle.mVertices[0] - triangle.mVertices[1];
|
---|
208 | const Vector3 b = triangle.mVertices[0] - triangle.mVertices[2];
|
---|
209 | const Vector3 cross_a_b = CrossProd(a, b);
|
---|
210 |
|
---|
211 | #define SMALL_FLT 1e-5
|
---|
212 |
|
---|
213 | if (SqrMagnitude(cross_a_b) <= 0.0001 * SMALL_FLT * SMALL_FLT)
|
---|
214 | {
|
---|
215 | // v0, v1 & v2 lies on a line (area == 0)
|
---|
216 | return false;
|
---|
217 | }
|
---|
218 |
|
---|
219 | return true;
|
---|
220 | }
|
---|
221 |
|
---|
222 |
|
---|
223 | bool ObjParser::ParseFile(const string filename,
|
---|
224 | SceneGraphLeaf *root,
|
---|
225 | const bool loadMeshes,
|
---|
226 | vector<FaceParentInfo> *parents)
|
---|
227 | {
|
---|
228 | FILE *file;
|
---|
229 | if ((file = fopen(filename.c_str(), "rt")) == NULL)
|
---|
230 | {
|
---|
231 | return false;
|
---|
232 | }
|
---|
233 |
|
---|
234 | map<int, Vector3> hashTable; // table associating indices with vectors
|
---|
235 | VertexContainer vertices; // table for vertices
|
---|
236 | FaceContainer faces;
|
---|
237 |
|
---|
238 | int line = 0;
|
---|
239 |
|
---|
240 | char str[100000];
|
---|
241 | int meshGrouping;
|
---|
242 | Environment::GetSingleton()->GetIntValue("ObjParser.meshGrouping", meshGrouping);
|
---|
243 |
|
---|
244 | int nMaxFaces = meshGrouping;
|
---|
245 |
|
---|
246 | while (fgets(str, 100000, file) != NULL)
|
---|
247 | {
|
---|
248 | switch (str[0])
|
---|
249 | {
|
---|
250 | case 'v': // vertex or normal
|
---|
251 | {
|
---|
252 | switch (str[1])
|
---|
253 | {
|
---|
254 | case 'n' :
|
---|
255 | // do nothing for normals
|
---|
256 | break;
|
---|
257 | case 't':
|
---|
258 | // do nothing for textures
|
---|
259 | break;
|
---|
260 | default:
|
---|
261 | float x, y, z; //cout << "v";
|
---|
262 | sscanf(str + 1, "%f %f %f", &x, &y, &z);
|
---|
263 | vertices.push_back(Vector3(x,y,z));
|
---|
264 |
|
---|
265 | }
|
---|
266 | break;
|
---|
267 | }
|
---|
268 | case 'f':
|
---|
269 | {
|
---|
270 | if (loadMeshes)
|
---|
271 | {
|
---|
272 | Face *face = LoadFace(str, vertices, hashTable);
|
---|
273 |
|
---|
274 | if (!face) break;
|
---|
275 |
|
---|
276 | faces.push_back(face);
|
---|
277 |
|
---|
278 | if ((int)faces.size() >= nMaxFaces)
|
---|
279 | {
|
---|
280 | ProcessMesh(faces, hashTable, root, parents);
|
---|
281 | }
|
---|
282 | }
|
---|
283 | else
|
---|
284 | {
|
---|
285 | ++ line;
|
---|
286 |
|
---|
287 | //////////
|
---|
288 | // construct the triangles given in the current line
|
---|
289 |
|
---|
290 | vector<Triangle3> triangles;
|
---|
291 | LoadTriangles(str, vertices, triangles, line);
|
---|
292 |
|
---|
293 | vector<Triangle3>::const_iterator tit, tit_end = triangles.end();
|
---|
294 |
|
---|
295 | for (tit = triangles.begin(); tit != tit_end; ++ tit)
|
---|
296 | {
|
---|
297 | // test if it is a valid triangle
|
---|
298 | if (!TriangleValid(*tit))
|
---|
299 | continue;
|
---|
300 |
|
---|
301 | TriangleIntersectable *obj = new TriangleIntersectable(*tit);
|
---|
302 | root->mGeometry.push_back(obj);
|
---|
303 | }
|
---|
304 | }
|
---|
305 | break;
|
---|
306 | } // end face
|
---|
307 | default:
|
---|
308 | break;
|
---|
309 | }
|
---|
310 | }
|
---|
311 |
|
---|
312 | //cout << "\n**** " << root->mGeometry.size() << " " << " lines: " << line << endl;
|
---|
313 | if (loadMeshes)
|
---|
314 | {
|
---|
315 | // there could be faces remaining
|
---|
316 | if (!faces.empty())
|
---|
317 | {
|
---|
318 | ProcessMesh(faces, hashTable, root, parents);
|
---|
319 | }
|
---|
320 | }
|
---|
321 |
|
---|
322 | // reset tables
|
---|
323 | hashTable.clear();
|
---|
324 | faces.clear();
|
---|
325 | fclose(file);
|
---|
326 |
|
---|
327 | return true;
|
---|
328 | }
|
---|
329 |
|
---|
330 | }
|
---|