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