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