[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 | {
|
---|
[2539] | 81 | vector<char *> substrings;
|
---|
| 82 |
|
---|
| 83 | // extract the triples of the form v/t/n v/t/n ...
|
---|
[1328] | 84 | char *pch = strtok(str + 1, " ");
|
---|
[2539] | 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 |
|
---|
[1328] | 95 | VertexIndexContainer indices;
|
---|
[2539] | 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)
|
---|
[1972] | 114 | {
|
---|
[2539] | 115 | const int idx2 = (int)indices.size() - 2;
|
---|
| 116 | const int idx3 = (int)indices.size() - 1;
|
---|
[1972] | 117 |
|
---|
| 118 | triangles.push_back(Triangle3(vertices[indices[0]],
|
---|
[2539] | 119 | vertices[indices[idx2]],
|
---|
| 120 | vertices[indices[idx3]]));
|
---|
[1972] | 121 | }
|
---|
[1328] | 122 | }
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 |
|
---|
| 126 | static Mesh *CreateMesh(FaceContainer &faces,
|
---|
| 127 | const map<int, Vector3> &hashTable)
|
---|
| 128 | {
|
---|
[1221] | 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
|
---|
[2307] | 141 | const int index = *vit;
|
---|
[1221] | 142 | map<int, Vector3>::const_iterator hit = hashTable.find(index);
|
---|
| 143 |
|
---|
| 144 | // correct face index (nust be relative to start of verices)
|
---|
[1314] | 145 | (*vit) = (int)distance(hashTable.begin(), hit);
|
---|
[1221] | 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;
|
---|
[1292] | 160 | // can't do cleanup because coupling with kdf file for intel ray tracer
|
---|
| 161 | mesh->Preprocess(false);
|
---|
| 162 |
|
---|
[1221] | 163 | return mesh;
|
---|
| 164 | }
|
---|
| 165 |
|
---|
| 166 |
|
---|
| 167 | // HACK: associate mesh instances with triangles
|
---|
[1328] | 168 | static void AssociateFacesWithInstance(MeshInstance *mi,
|
---|
| 169 | vector<FaceParentInfo> &parents)
|
---|
[1221] | 170 | {
|
---|
[1344] | 171 | Mesh *mesh = mi->GetMesh();
|
---|
| 172 |
|
---|
| 173 | int i = 0;
|
---|
[1655] | 174 | FaceContainer::const_iterator fit, fit_end = mesh->mFaces.end();
|
---|
| 175 |
|
---|
[1344] | 176 | for (fit = mesh->mFaces.begin(); fit != fit_end; ++ fit, i++)
|
---|
[1221] | 177 | {
|
---|
[1344] | 178 | parents.push_back(FaceParentInfo(mi, i));
|
---|
[1221] | 179 | }
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 |
|
---|
[1328] | 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 |
|
---|
[1973] | 205 | bool TriangleValid(const Triangle3 &triangle)
|
---|
| 206 | {
|
---|
[1976] | 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);
|
---|
[1973] | 210 |
|
---|
[1976] | 211 | if (SqrMagnitude(cross_a_b) <= 0.000001 * FLT_EPSILON * FLT_EPSILON)
|
---|
| 212 | {
|
---|
[1978] | 213 | // v0, v1 & v2 lies on a line (area == 0)
|
---|
[1976] | 214 | return false;
|
---|
| 215 | }
|
---|
[1973] | 216 |
|
---|
[1976] | 217 | return true;
|
---|
[1973] | 218 | }
|
---|
| 219 |
|
---|
[1976] | 220 |
|
---|
[1221] | 221 | bool ObjParser::ParseFile(const string filename,
|
---|
[1344] | 222 | SceneGraphNode *root,
|
---|
[1379] | 223 | const bool loadMeshes,
|
---|
[1281] | 224 | vector<FaceParentInfo> *parents)
|
---|
[1221] | 225 | {
|
---|
| 226 | FILE *file;
|
---|
| 227 | if ((file = fopen(filename.c_str(), "rt")) == NULL)
|
---|
[1344] | 228 | {
|
---|
[1221] | 229 | return false;
|
---|
[1344] | 230 | }
|
---|
| 231 |
|
---|
[1221] | 232 | map<int, Vector3> hashTable; // table associating indices with vectors
|
---|
[1327] | 233 | VertexContainer vertices; // table for vertices
|
---|
[1221] | 234 | FaceContainer faces;
|
---|
| 235 |
|
---|
[1979] | 236 | int line = 0;
|
---|
| 237 |
|
---|
[2307] | 238 | char str[100000];
|
---|
[1221] | 239 | int meshGrouping;
|
---|
| 240 | Environment::GetSingleton()->GetIntValue("ObjParser.meshGrouping", meshGrouping);
|
---|
| 241 |
|
---|
[1292] | 242 | int nMaxFaces = meshGrouping;
|
---|
| 243 |
|
---|
[2307] | 244 | while (fgets(str, 100000, file) != NULL)
|
---|
[2119] | 245 | {
|
---|
[2539] | 246 | switch (str[0])
|
---|
[1221] | 247 | {
|
---|
[1876] | 248 | case 'v': // vertex or normal
|
---|
[2119] | 249 | {
|
---|
[2539] | 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 | }
|
---|
[2119] | 264 | break;
|
---|
[1233] | 265 | }
|
---|
[1221] | 266 | case 'f':
|
---|
| 267 | {
|
---|
[1421] | 268 | if (loadMeshes)
|
---|
| 269 | {
|
---|
| 270 | Face *face = LoadFace(str, vertices, hashTable);
|
---|
[2119] | 271 |
|
---|
[1421] | 272 | if (!face) break;
|
---|
[2539] | 273 |
|
---|
[1421] | 274 | faces.push_back(face);
|
---|
[1328] | 275 |
|
---|
[2307] | 276 | if ((int)faces.size() >= nMaxFaces)
|
---|
[1421] | 277 | {
|
---|
| 278 | ProcessMesh(faces, hashTable, root, parents);
|
---|
| 279 | }
|
---|
| 280 | }
|
---|
| 281 | else
|
---|
[1221] | 282 | {
|
---|
[2307] | 283 | ++ line;
|
---|
[2539] | 284 |
|
---|
| 285 | //////////
|
---|
| 286 | // construct the triangles given in the current line
|
---|
| 287 |
|
---|
[1972] | 288 | vector<Triangle3> triangles;
|
---|
[1979] | 289 | LoadTriangles(str, vertices, triangles, line);
|
---|
[1978] | 290 |
|
---|
[1972] | 291 | vector<Triangle3>::const_iterator tit, tit_end = triangles.end();
|
---|
[1344] | 292 |
|
---|
[1972] | 293 | for (tit = triangles.begin(); tit != tit_end; ++ tit)
|
---|
[1421] | 294 | {
|
---|
[2539] | 295 | // test if it is a valid triangle
|
---|
[1979] | 296 | if (0 && !TriangleValid(*tit)) continue;
|
---|
| 297 |
|
---|
[1972] | 298 | TriangleIntersectable *obj = new TriangleIntersectable(*tit);
|
---|
[2539] | 299 | root->mGeometry.push_back(obj);
|
---|
[1421] | 300 | }
|
---|
[1344] | 301 | }
|
---|
[1221] | 302 | break;
|
---|
[1314] | 303 | } // end face
|
---|
[1221] | 304 | default:
|
---|
| 305 | break;
|
---|
| 306 | }
|
---|
| 307 | }
|
---|
[2539] | 308 |
|
---|
[2307] | 309 | //cout << "\n**** " << root->mGeometry.size() << " " << " lines: " << line << endl;
|
---|
[1421] | 310 | if (loadMeshes)
|
---|
| 311 | {
|
---|
| 312 | // there could be faces remaining
|
---|
| 313 | if (!faces.empty())
|
---|
| 314 | {
|
---|
| 315 | ProcessMesh(faces, hashTable, root, parents);
|
---|
| 316 | }
|
---|
[1221] | 317 | }
|
---|
[1421] | 318 |
|
---|
[1344] | 319 | // reset tables
|
---|
| 320 | hashTable.clear();
|
---|
| 321 | faces.clear();
|
---|
[1221] | 322 | fclose(file);
|
---|
[1344] | 323 |
|
---|
[1221] | 324 | return true;
|
---|
| 325 | }
|
---|
| 326 |
|
---|
[1272] | 327 | }
|
---|