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 "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 | namespace GtpVisibilityPreprocessor {
|
---|
20 |
|
---|
21 | #define CONNECT_SEQUENTIAL_FACES 0
|
---|
22 | #define ROTATE_SCENE 0
|
---|
23 |
|
---|
24 | // HACK
|
---|
25 | static void RotateMesh(Mesh *mesh)
|
---|
26 | {
|
---|
27 | VertexContainer::iterator it, it_end = mesh->mVertices.end();
|
---|
28 |
|
---|
29 | const float angle = 30.0f * PI / 180.0f;
|
---|
30 | const Matrix4x4 rot = RotationYMatrix(angle);
|
---|
31 |
|
---|
32 | for (it = mesh->mVertices.begin(); it != it_end; ++ it)
|
---|
33 | {
|
---|
34 | (*it) = rot * (*it);
|
---|
35 | }
|
---|
36 | }
|
---|
37 |
|
---|
38 |
|
---|
39 | struct ltstr
|
---|
40 | {
|
---|
41 | bool operator()(const string s1, const string s2) const
|
---|
42 | {
|
---|
43 | return s1 < s2;
|
---|
44 | }
|
---|
45 | };
|
---|
46 |
|
---|
47 |
|
---|
48 | static Face *LoadFace(char *str,
|
---|
49 | const VertexContainer &vertices,
|
---|
50 | map<int, Vector3> &hashTable)
|
---|
51 | {
|
---|
52 | char *pch = strtok(str + 1, " ");
|
---|
53 |
|
---|
54 | VertexIndexContainer indices;
|
---|
55 | while (pch != NULL)
|
---|
56 | {
|
---|
57 | const int index = (int)strtol(pch, NULL, 10) - 1;
|
---|
58 | //Debug << index << " x ";
|
---|
59 |
|
---|
60 | // store vertex in hash table
|
---|
61 | hashTable[index] = vertices[index];
|
---|
62 | indices.push_back(index);
|
---|
63 |
|
---|
64 | pch = strtok(NULL, " ");
|
---|
65 | }
|
---|
66 | //if (indices.size() > 4) return NULL;
|
---|
67 |
|
---|
68 | return new Face(indices);
|
---|
69 | }
|
---|
70 |
|
---|
71 |
|
---|
72 | static Triangle3 LoadTriangle(char *str,
|
---|
73 | const VertexContainer &vertices,
|
---|
74 | map<int, Vector3> &hashTable)
|
---|
75 | {
|
---|
76 | char *pch = strtok(str + 1, " ");
|
---|
77 |
|
---|
78 | VertexIndexContainer indices;
|
---|
79 |
|
---|
80 | while (pch != NULL)
|
---|
81 | {
|
---|
82 | const int index = (int)strtol(pch, NULL, 10) - 1;
|
---|
83 |
|
---|
84 | // store vertex in hash table
|
---|
85 | hashTable[index] = vertices[index];
|
---|
86 | indices.push_back(index);
|
---|
87 |
|
---|
88 | pch = strtok(NULL, " ");
|
---|
89 | }
|
---|
90 |
|
---|
91 | return Triangle3(vertices[indices[0]], vertices[indices[1]], vertices[indices[2]]);
|
---|
92 | }
|
---|
93 |
|
---|
94 |
|
---|
95 | static Mesh *CreateMesh(FaceContainer &faces,
|
---|
96 | const map<int, Vector3> &hashTable)
|
---|
97 | {
|
---|
98 | Mesh *mesh = MeshManager::GetSingleton()->CreateResource();
|
---|
99 |
|
---|
100 | FaceContainer::const_iterator fit, fit_end = faces.end();
|
---|
101 |
|
---|
102 | for (fit = faces.begin(); fit != fit_end; ++ fit)
|
---|
103 | {
|
---|
104 | Face *face = *fit;
|
---|
105 | VertexIndexContainer::iterator vit, vit_end = face->mVertexIndices.end();
|
---|
106 |
|
---|
107 | for (vit = face->mVertexIndices.begin(); vit != vit_end; ++ vit)
|
---|
108 | {
|
---|
109 | // go through indices
|
---|
110 | const int index = *vit;
|
---|
111 | //Debug << "old idx: " << (*vit) << endl;
|
---|
112 | map<int, Vector3>::const_iterator hit = hashTable.find(index);
|
---|
113 |
|
---|
114 | // correct face index (nust be relative to start of verices)
|
---|
115 | (*vit) = (int)distance(hashTable.begin(), hit);
|
---|
116 | //Debug << "new idx: " << (*vit) << endl;
|
---|
117 | }
|
---|
118 | }
|
---|
119 |
|
---|
120 | VertexContainer vertices;
|
---|
121 |
|
---|
122 | map<int, Vector3>::const_iterator hit, hit_end = hashTable.end();
|
---|
123 |
|
---|
124 | // store vertices in given order
|
---|
125 | for (hit = hashTable.begin(); hit != hit_end; ++ hit)
|
---|
126 | {
|
---|
127 | mesh->mVertices.push_back((*hit).second);
|
---|
128 | }
|
---|
129 |
|
---|
130 | mesh->mFaces = faces;
|
---|
131 | // can't do cleanup because coupling with kdf file for intel ray tracer
|
---|
132 | mesh->Preprocess(false);
|
---|
133 |
|
---|
134 | return mesh;
|
---|
135 | }
|
---|
136 |
|
---|
137 |
|
---|
138 | // HACK: associate mesh instances with triangles
|
---|
139 | static void AssociateFacesWithInstance(MeshInstance *mi,
|
---|
140 | vector<FaceParentInfo> &parents)
|
---|
141 | {
|
---|
142 | Mesh *mesh = mi->GetMesh();
|
---|
143 |
|
---|
144 | int i = 0;
|
---|
145 | FaceContainer::const_iterator fit, fit_end = mesh->mFaces.end();
|
---|
146 |
|
---|
147 | for (fit = mesh->mFaces.begin(); fit != fit_end; ++ fit, i++)
|
---|
148 | {
|
---|
149 | parents.push_back(FaceParentInfo(mi, i));
|
---|
150 | }
|
---|
151 | }
|
---|
152 |
|
---|
153 |
|
---|
154 | static void ProcessMesh(FaceContainer &faces,
|
---|
155 | map<int, Vector3> &hashTable,
|
---|
156 | SceneGraphNode *root,
|
---|
157 | vector<FaceParentInfo> *parents)
|
---|
158 | {
|
---|
159 | Mesh *mesh = CreateMesh(faces, hashTable);
|
---|
160 | // make an instance of this mesh
|
---|
161 | MeshInstance *mi = new MeshInstance(mesh);
|
---|
162 |
|
---|
163 | if (parents)
|
---|
164 | {
|
---|
165 | AssociateFacesWithInstance(mi, *parents);
|
---|
166 | }
|
---|
167 |
|
---|
168 | root->mGeometry.push_back(mi);
|
---|
169 |
|
---|
170 | // reset tables
|
---|
171 | hashTable.clear();
|
---|
172 | faces.clear();
|
---|
173 | }
|
---|
174 |
|
---|
175 |
|
---|
176 | bool ObjParser::ParseFile(const string filename,
|
---|
177 | SceneGraphNode *root,
|
---|
178 | const bool loadMeshes,
|
---|
179 | vector<FaceParentInfo> *parents)
|
---|
180 | {
|
---|
181 | FILE *file;
|
---|
182 | if ((file = fopen(filename.c_str(), "rt")) == NULL)
|
---|
183 | {
|
---|
184 | return false;
|
---|
185 | }
|
---|
186 |
|
---|
187 | map<int, Vector3> hashTable; // table associating indices with vectors
|
---|
188 | VertexContainer vertices; // table for vertices
|
---|
189 | FaceContainer faces;
|
---|
190 |
|
---|
191 | char str[100];
|
---|
192 | int meshGrouping;
|
---|
193 | Environment::GetSingleton()->GetIntValue("ObjParser.meshGrouping", meshGrouping);
|
---|
194 |
|
---|
195 | int nMaxFaces = meshGrouping;
|
---|
196 |
|
---|
197 | while (fgets(str, 80, file) != NULL)
|
---|
198 | {
|
---|
199 | switch (str[0])
|
---|
200 | {
|
---|
201 | case 'v': // vertex
|
---|
202 | {
|
---|
203 | float x, y, z; //cout << "v";
|
---|
204 | sscanf(str + 1, "%f %f %f", &x, &y, &z);
|
---|
205 | vertices.push_back(Vector3(x,y,z));
|
---|
206 | //Debug << "vertex: " << vertices.back() << endl;
|
---|
207 | break;
|
---|
208 | }
|
---|
209 | case 'f':
|
---|
210 | {
|
---|
211 | // cout << "f";
|
---|
212 | if (loadMeshes)
|
---|
213 | {
|
---|
214 | Face *face = LoadFace(str, vertices, hashTable);
|
---|
215 | if (!face) break;
|
---|
216 |
|
---|
217 | faces.push_back(face);
|
---|
218 |
|
---|
219 | if (faces.size() >= nMaxFaces)
|
---|
220 | {
|
---|
221 | ProcessMesh(faces, hashTable, root, parents);
|
---|
222 | }
|
---|
223 | }
|
---|
224 | else
|
---|
225 | {
|
---|
226 | Triangle3 triangle = LoadTriangle(str, vertices, hashTable);
|
---|
227 |
|
---|
228 | TriangleIntersectable *obj = new TriangleIntersectable(triangle);
|
---|
229 | root->mGeometry.push_back(obj);
|
---|
230 |
|
---|
231 | // matt: we don't really need to keep an additional data structure
|
---|
232 | // if working with triangles => remove this
|
---|
233 | if (parents)
|
---|
234 | {
|
---|
235 | FaceParentInfo info(obj, 0);
|
---|
236 | parents->push_back(info);
|
---|
237 | }
|
---|
238 | }
|
---|
239 | break;
|
---|
240 | } // end face
|
---|
241 | default:
|
---|
242 | break;
|
---|
243 | }
|
---|
244 | }
|
---|
245 |
|
---|
246 | if (loadMeshes)
|
---|
247 | {
|
---|
248 | // there could be faces remaining
|
---|
249 | if (!faces.empty())
|
---|
250 | {
|
---|
251 | ProcessMesh(faces, hashTable, root, parents);
|
---|
252 | }
|
---|
253 | }
|
---|
254 |
|
---|
255 | // reset tables
|
---|
256 | hashTable.clear();
|
---|
257 | faces.clear();
|
---|
258 | fclose(file);
|
---|
259 |
|
---|
260 | return true;
|
---|
261 | }
|
---|
262 |
|
---|
263 | }
|
---|