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 | SceneGraphNode *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 | if (SqrMagnitude(cross_a_b) <= 0.000001 * FLT_EPSILON * FLT_EPSILON)
|
---|
212 | {
|
---|
213 | // v0, v1 & v2 lies on a line (area == 0)
|
---|
214 | return false;
|
---|
215 | }
|
---|
216 |
|
---|
217 | return true;
|
---|
218 | }
|
---|
219 |
|
---|
220 |
|
---|
221 | bool ObjParser::ParseFile(const string filename,
|
---|
222 | SceneGraphNode *root,
|
---|
223 | const bool loadMeshes,
|
---|
224 | vector<FaceParentInfo> *parents)
|
---|
225 | {
|
---|
226 | FILE *file;
|
---|
227 | if ((file = fopen(filename.c_str(), "rt")) == NULL)
|
---|
228 | {
|
---|
229 | return false;
|
---|
230 | }
|
---|
231 |
|
---|
232 | map<int, Vector3> hashTable; // table associating indices with vectors
|
---|
233 | VertexContainer vertices; // table for vertices
|
---|
234 | FaceContainer faces;
|
---|
235 |
|
---|
236 | int line = 0;
|
---|
237 |
|
---|
238 | char str[100000];
|
---|
239 | int meshGrouping;
|
---|
240 | Environment::GetSingleton()->GetIntValue("ObjParser.meshGrouping", meshGrouping);
|
---|
241 |
|
---|
242 | int nMaxFaces = meshGrouping;
|
---|
243 |
|
---|
244 | while (fgets(str, 100000, file) != NULL)
|
---|
245 | {
|
---|
246 | switch (str[0])
|
---|
247 | {
|
---|
248 | case 'v': // vertex or normal
|
---|
249 | {
|
---|
250 | switch (str[1])
|
---|
251 | {
|
---|
252 | case 'n' :
|
---|
253 | // do nothing for normals
|
---|
254 | break;
|
---|
255 | case 't':
|
---|
256 | // do nothing for textures
|
---|
257 | break;
|
---|
258 | default:
|
---|
259 | float x, y, z; //cout << "v";
|
---|
260 | sscanf(str + 1, "%f %f %f", &x, &y, &z);
|
---|
261 | vertices.push_back(Vector3(x,y,z));
|
---|
262 |
|
---|
263 | }
|
---|
264 | break;
|
---|
265 | }
|
---|
266 | case 'f':
|
---|
267 | {
|
---|
268 | if (loadMeshes)
|
---|
269 | {
|
---|
270 | Face *face = LoadFace(str, vertices, hashTable);
|
---|
271 |
|
---|
272 | if (!face) break;
|
---|
273 |
|
---|
274 | faces.push_back(face);
|
---|
275 |
|
---|
276 | if ((int)faces.size() >= nMaxFaces)
|
---|
277 | {
|
---|
278 | ProcessMesh(faces, hashTable, root, parents);
|
---|
279 | }
|
---|
280 | }
|
---|
281 | else
|
---|
282 | {
|
---|
283 | ++ line;
|
---|
284 |
|
---|
285 | //////////
|
---|
286 | // construct the triangles given in the current line
|
---|
287 |
|
---|
288 | vector<Triangle3> triangles;
|
---|
289 | LoadTriangles(str, vertices, triangles, line);
|
---|
290 |
|
---|
291 | vector<Triangle3>::const_iterator tit, tit_end = triangles.end();
|
---|
292 |
|
---|
293 | for (tit = triangles.begin(); tit != tit_end; ++ tit)
|
---|
294 | {
|
---|
295 | // test if it is a valid triangle
|
---|
296 | if (0 && !TriangleValid(*tit)) continue;
|
---|
297 |
|
---|
298 | TriangleIntersectable *obj = new TriangleIntersectable(*tit);
|
---|
299 | root->mGeometry.push_back(obj);
|
---|
300 | }
|
---|
301 | }
|
---|
302 | break;
|
---|
303 | } // end face
|
---|
304 | default:
|
---|
305 | break;
|
---|
306 | }
|
---|
307 | }
|
---|
308 |
|
---|
309 | //cout << "\n**** " << root->mGeometry.size() << " " << " lines: " << line << endl;
|
---|
310 | if (loadMeshes)
|
---|
311 | {
|
---|
312 | // there could be faces remaining
|
---|
313 | if (!faces.empty())
|
---|
314 | {
|
---|
315 | ProcessMesh(faces, hashTable, root, parents);
|
---|
316 | }
|
---|
317 | }
|
---|
318 |
|
---|
319 | // reset tables
|
---|
320 | hashTable.clear();
|
---|
321 | faces.clear();
|
---|
322 | fclose(file);
|
---|
323 |
|
---|
324 | return true;
|
---|
325 | }
|
---|
326 |
|
---|
327 | }
|
---|