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 | char *pch = strtok(str + 1, " ");
|
---|
82 |
|
---|
83 | VertexIndexContainer indices;
|
---|
84 | int i = 0;
|
---|
85 | while (pch != NULL)
|
---|
86 | {++ i;
|
---|
87 | const int index = (int)strtol(pch, NULL, 10) - 1;
|
---|
88 |
|
---|
89 | // store vertex in hash table
|
---|
90 | //hashTable[index] = vertices[index];
|
---|
91 | if (index >= 0)
|
---|
92 | indices.push_back(index);
|
---|
93 |
|
---|
94 | pch = strtok(NULL, " ");
|
---|
95 |
|
---|
96 | // problem: don't know how intel ray tracer tesselates
|
---|
97 | if ((int)indices.size() > 2)
|
---|
98 | {
|
---|
99 | const int index_2 = (int)indices.size() - 2;
|
---|
100 | const int index_3 = (int)indices.size() - 1;
|
---|
101 |
|
---|
102 | triangles.push_back(Triangle3(vertices[indices[0]],
|
---|
103 | vertices[indices[index_2]],
|
---|
104 | vertices[indices[index_3]]));
|
---|
105 | }
|
---|
106 | }
|
---|
107 | //if (line == 451703)
|
---|
108 | // cout << "t: " << triangles.size() << endl;
|
---|
109 | }
|
---|
110 |
|
---|
111 |
|
---|
112 | static Mesh *CreateMesh(FaceContainer &faces,
|
---|
113 | const map<int, Vector3> &hashTable)
|
---|
114 | {
|
---|
115 | Mesh *mesh = MeshManager::GetSingleton()->CreateResource();
|
---|
116 |
|
---|
117 | FaceContainer::const_iterator fit, fit_end = faces.end();
|
---|
118 |
|
---|
119 | for (fit = faces.begin(); fit != fit_end; ++ fit)
|
---|
120 | {
|
---|
121 | Face *face = *fit;
|
---|
122 | VertexIndexContainer::iterator vit, vit_end = face->mVertexIndices.end();
|
---|
123 |
|
---|
124 | for (vit = face->mVertexIndices.begin(); vit != vit_end; ++ vit)
|
---|
125 | {
|
---|
126 | // go through indices
|
---|
127 | const int index = *vit;
|
---|
128 | map<int, Vector3>::const_iterator hit = hashTable.find(index);
|
---|
129 |
|
---|
130 | // correct face index (nust be relative to start of verices)
|
---|
131 | (*vit) = (int)distance(hashTable.begin(), hit);
|
---|
132 | }
|
---|
133 | }
|
---|
134 |
|
---|
135 | VertexContainer vertices;
|
---|
136 |
|
---|
137 | map<int, Vector3>::const_iterator hit, hit_end = hashTable.end();
|
---|
138 |
|
---|
139 | // store vertices in given order
|
---|
140 | for (hit = hashTable.begin(); hit != hit_end; ++ hit)
|
---|
141 | {
|
---|
142 | mesh->mVertices.push_back((*hit).second);
|
---|
143 | }
|
---|
144 |
|
---|
145 | mesh->mFaces = faces;
|
---|
146 | // can't do cleanup because coupling with kdf file for intel ray tracer
|
---|
147 | mesh->Preprocess(false);
|
---|
148 |
|
---|
149 | return mesh;
|
---|
150 | }
|
---|
151 |
|
---|
152 |
|
---|
153 | // HACK: associate mesh instances with triangles
|
---|
154 | static void AssociateFacesWithInstance(MeshInstance *mi,
|
---|
155 | vector<FaceParentInfo> &parents)
|
---|
156 | {
|
---|
157 | Mesh *mesh = mi->GetMesh();
|
---|
158 |
|
---|
159 | int i = 0;
|
---|
160 | FaceContainer::const_iterator fit, fit_end = mesh->mFaces.end();
|
---|
161 |
|
---|
162 | for (fit = mesh->mFaces.begin(); fit != fit_end; ++ fit, i++)
|
---|
163 | {
|
---|
164 | parents.push_back(FaceParentInfo(mi, i));
|
---|
165 | }
|
---|
166 | }
|
---|
167 |
|
---|
168 |
|
---|
169 | static void ProcessMesh(FaceContainer &faces,
|
---|
170 | map<int, Vector3> &hashTable,
|
---|
171 | SceneGraphNode *root,
|
---|
172 | vector<FaceParentInfo> *parents)
|
---|
173 | {
|
---|
174 | Mesh *mesh = CreateMesh(faces, hashTable);
|
---|
175 | // make an instance of this mesh
|
---|
176 | MeshInstance *mi = new MeshInstance(mesh);
|
---|
177 |
|
---|
178 | if (parents)
|
---|
179 | {
|
---|
180 | AssociateFacesWithInstance(mi, *parents);
|
---|
181 | }
|
---|
182 |
|
---|
183 | root->mGeometry.push_back(mi);
|
---|
184 |
|
---|
185 | // reset tables
|
---|
186 | hashTable.clear();
|
---|
187 | faces.clear();
|
---|
188 | }
|
---|
189 |
|
---|
190 |
|
---|
191 | bool TriangleValid(const Triangle3 &triangle)
|
---|
192 | {
|
---|
193 | const Vector3 a = triangle.mVertices[0] - triangle.mVertices[1];
|
---|
194 | const Vector3 b = triangle.mVertices[0] - triangle.mVertices[2];
|
---|
195 | const Vector3 cross_a_b = CrossProd(a, b);
|
---|
196 |
|
---|
197 | if (SqrMagnitude(cross_a_b) <= 0.000001 * FLT_EPSILON * FLT_EPSILON)
|
---|
198 | {
|
---|
199 | // v0, v1 & v2 lies on a line (area == 0)
|
---|
200 | return false;
|
---|
201 | }
|
---|
202 |
|
---|
203 | return true;
|
---|
204 | }
|
---|
205 |
|
---|
206 |
|
---|
207 | bool ObjParser::ParseFile(const string filename,
|
---|
208 | SceneGraphNode *root,
|
---|
209 | const bool loadMeshes,
|
---|
210 | vector<FaceParentInfo> *parents)
|
---|
211 | {
|
---|
212 | FILE *file;
|
---|
213 | if ((file = fopen(filename.c_str(), "rt")) == NULL)
|
---|
214 | {
|
---|
215 | return false;
|
---|
216 | }
|
---|
217 |
|
---|
218 | map<int, Vector3> hashTable; // table associating indices with vectors
|
---|
219 | VertexContainer vertices; // table for vertices
|
---|
220 | FaceContainer faces;
|
---|
221 |
|
---|
222 | int line = 0;
|
---|
223 |
|
---|
224 | char str[100000];
|
---|
225 | int meshGrouping;
|
---|
226 | Environment::GetSingleton()->GetIntValue("ObjParser.meshGrouping", meshGrouping);
|
---|
227 |
|
---|
228 | int nMaxFaces = meshGrouping;
|
---|
229 |
|
---|
230 | while (fgets(str, 100000, file) != NULL)
|
---|
231 | {
|
---|
232 | switch (str[0])
|
---|
233 | {
|
---|
234 | case 'v': // vertex or normal
|
---|
235 | {
|
---|
236 | switch (str[1])
|
---|
237 | {
|
---|
238 | case 'n' :
|
---|
239 | // normal do nothing
|
---|
240 | break;
|
---|
241 | default:
|
---|
242 | float x, y, z; //cout << "v";
|
---|
243 | sscanf(str + 1, "%f %f %f", &x, &y, &z);
|
---|
244 | vertices.push_back(Vector3(x,y,z));
|
---|
245 |
|
---|
246 | }
|
---|
247 | break;
|
---|
248 | }
|
---|
249 | case 'f':
|
---|
250 | {
|
---|
251 | if (loadMeshes)
|
---|
252 | {
|
---|
253 | Face *face = LoadFace(str, vertices, hashTable);
|
---|
254 |
|
---|
255 | if (!face) break;
|
---|
256 |
|
---|
257 | faces.push_back(face);
|
---|
258 |
|
---|
259 | if ((int)faces.size() >= nMaxFaces)
|
---|
260 | {
|
---|
261 | ProcessMesh(faces, hashTable, root, parents);
|
---|
262 | }
|
---|
263 | }
|
---|
264 | else
|
---|
265 | {
|
---|
266 | ++ line;
|
---|
267 | vector<Triangle3> triangles;
|
---|
268 |
|
---|
269 | LoadTriangles(str, vertices, triangles, line);
|
---|
270 |
|
---|
271 | vector<Triangle3>::const_iterator tit, tit_end = triangles.end();
|
---|
272 |
|
---|
273 | for (tit = triangles.begin(); tit != tit_end; ++ tit)
|
---|
274 | {
|
---|
275 | if (0 && !TriangleValid(*tit)) continue;
|
---|
276 |
|
---|
277 | TriangleIntersectable *obj = new TriangleIntersectable(*tit);
|
---|
278 | root->mGeometry.push_back(obj);
|
---|
279 | }
|
---|
280 | }
|
---|
281 | break;
|
---|
282 | } // end face
|
---|
283 | default:
|
---|
284 | break;
|
---|
285 | }
|
---|
286 | }
|
---|
287 |
|
---|
288 | //cout << "\n**** " << root->mGeometry.size() << " " << " lines: " << line << endl;
|
---|
289 | if (loadMeshes)
|
---|
290 | {
|
---|
291 | // there could be faces remaining
|
---|
292 | if (!faces.empty())
|
---|
293 | {
|
---|
294 | ProcessMesh(faces, hashTable, root, parents);
|
---|
295 | }
|
---|
296 | }
|
---|
297 |
|
---|
298 | // reset tables
|
---|
299 | hashTable.clear();
|
---|
300 | faces.clear();
|
---|
301 | fclose(file);
|
---|
302 |
|
---|
303 | return true;
|
---|
304 | }
|
---|
305 |
|
---|
306 | }
|
---|