[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, " ");
|
---|
[2307] | 82 |
|
---|
[1328] | 83 | VertexIndexContainer indices;
|
---|
[2307] | 84 | int i = 0;
|
---|
[1328] | 85 | while (pch != NULL)
|
---|
[2307] | 86 | {++ i;
|
---|
[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];
|
---|
[2307] | 91 | if (index >= 0)
|
---|
[1328] | 92 | indices.push_back(index);
|
---|
| 93 |
|
---|
[2042] | 94 | pch = strtok(NULL, " ");
|
---|
| 95 |
|
---|
| 96 | // problem: don't know how intel ray tracer tesselates
|
---|
| 97 | if ((int)indices.size() > 2)
|
---|
[1972] | 98 | {
|
---|
[1978] | 99 | const int index_2 = (int)indices.size() - 2;
|
---|
| 100 | const int index_3 = (int)indices.size() - 1;
|
---|
[1972] | 101 |
|
---|
| 102 | triangles.push_back(Triangle3(vertices[indices[0]],
|
---|
[1979] | 103 | vertices[indices[index_2]],
|
---|
| 104 | vertices[indices[index_3]]));
|
---|
[1972] | 105 | }
|
---|
[1328] | 106 | }
|
---|
[1979] | 107 | //if (line == 451703)
|
---|
| 108 | // cout << "t: " << triangles.size() << endl;
|
---|
[1328] | 109 | }
|
---|
| 110 |
|
---|
| 111 |
|
---|
| 112 | static Mesh *CreateMesh(FaceContainer &faces,
|
---|
| 113 | const map<int, Vector3> &hashTable)
|
---|
| 114 | {
|
---|
[1221] | 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
|
---|
[2307] | 127 | const int index = *vit;
|
---|
[1221] | 128 | map<int, Vector3>::const_iterator hit = hashTable.find(index);
|
---|
| 129 |
|
---|
| 130 | // correct face index (nust be relative to start of verices)
|
---|
[1314] | 131 | (*vit) = (int)distance(hashTable.begin(), hit);
|
---|
[1221] | 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;
|
---|
[1292] | 146 | // can't do cleanup because coupling with kdf file for intel ray tracer
|
---|
| 147 | mesh->Preprocess(false);
|
---|
| 148 |
|
---|
[1221] | 149 | return mesh;
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 |
|
---|
| 153 | // HACK: associate mesh instances with triangles
|
---|
[1328] | 154 | static void AssociateFacesWithInstance(MeshInstance *mi,
|
---|
| 155 | vector<FaceParentInfo> &parents)
|
---|
[1221] | 156 | {
|
---|
[1344] | 157 | Mesh *mesh = mi->GetMesh();
|
---|
| 158 |
|
---|
| 159 | int i = 0;
|
---|
[1655] | 160 | FaceContainer::const_iterator fit, fit_end = mesh->mFaces.end();
|
---|
| 161 |
|
---|
[1344] | 162 | for (fit = mesh->mFaces.begin(); fit != fit_end; ++ fit, i++)
|
---|
[1221] | 163 | {
|
---|
[1344] | 164 | parents.push_back(FaceParentInfo(mi, i));
|
---|
[1221] | 165 | }
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 |
|
---|
[1328] | 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 |
|
---|
[1973] | 191 | bool TriangleValid(const Triangle3 &triangle)
|
---|
| 192 | {
|
---|
[1976] | 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);
|
---|
[1973] | 196 |
|
---|
[1976] | 197 | if (SqrMagnitude(cross_a_b) <= 0.000001 * FLT_EPSILON * FLT_EPSILON)
|
---|
| 198 | {
|
---|
[1978] | 199 | // v0, v1 & v2 lies on a line (area == 0)
|
---|
[1976] | 200 | return false;
|
---|
| 201 | }
|
---|
[1973] | 202 |
|
---|
[1976] | 203 | return true;
|
---|
[1973] | 204 | }
|
---|
| 205 |
|
---|
[1976] | 206 |
|
---|
[1221] | 207 | bool ObjParser::ParseFile(const string filename,
|
---|
[1344] | 208 | SceneGraphNode *root,
|
---|
[1379] | 209 | const bool loadMeshes,
|
---|
[1281] | 210 | vector<FaceParentInfo> *parents)
|
---|
[1221] | 211 | {
|
---|
| 212 | FILE *file;
|
---|
| 213 | if ((file = fopen(filename.c_str(), "rt")) == NULL)
|
---|
[1344] | 214 | {
|
---|
[1221] | 215 | return false;
|
---|
[1344] | 216 | }
|
---|
| 217 |
|
---|
[1221] | 218 | map<int, Vector3> hashTable; // table associating indices with vectors
|
---|
[1327] | 219 | VertexContainer vertices; // table for vertices
|
---|
[1221] | 220 | FaceContainer faces;
|
---|
| 221 |
|
---|
[1979] | 222 | int line = 0;
|
---|
| 223 |
|
---|
[2307] | 224 | char str[100000];
|
---|
[1221] | 225 | int meshGrouping;
|
---|
| 226 | Environment::GetSingleton()->GetIntValue("ObjParser.meshGrouping", meshGrouping);
|
---|
| 227 |
|
---|
[1292] | 228 | int nMaxFaces = meshGrouping;
|
---|
| 229 |
|
---|
[2307] | 230 | while (fgets(str, 100000, file) != NULL)
|
---|
[2119] | 231 | {
|
---|
[1876] | 232 | switch (str[0])
|
---|
[1221] | 233 | {
|
---|
[1876] | 234 | case 'v': // vertex or normal
|
---|
| 235 | {
|
---|
[2119] | 236 | switch (str[1])
|
---|
| 237 | {
|
---|
[1876] | 238 | case 'n' :
|
---|
[2119] | 239 | // normal do nothing
|
---|
| 240 | break;
|
---|
[1876] | 241 | default:
|
---|
[2119] | 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 |
|
---|
[1233] | 246 | }
|
---|
[1876] | 247 | break;
|
---|
| 248 | }
|
---|
[1221] | 249 | case 'f':
|
---|
| 250 | {
|
---|
[1421] | 251 | if (loadMeshes)
|
---|
| 252 | {
|
---|
| 253 | Face *face = LoadFace(str, vertices, hashTable);
|
---|
[2119] | 254 |
|
---|
[1421] | 255 | if (!face) break;
|
---|
[1344] | 256 |
|
---|
[1421] | 257 | faces.push_back(face);
|
---|
[1328] | 258 |
|
---|
[2307] | 259 | if ((int)faces.size() >= nMaxFaces)
|
---|
[1421] | 260 | {
|
---|
| 261 | ProcessMesh(faces, hashTable, root, parents);
|
---|
| 262 | }
|
---|
| 263 | }
|
---|
| 264 | else
|
---|
[1221] | 265 | {
|
---|
[2307] | 266 | ++ line;
|
---|
[1972] | 267 | vector<Triangle3> triangles;
|
---|
| 268 |
|
---|
[1979] | 269 | LoadTriangles(str, vertices, triangles, line);
|
---|
[1978] | 270 |
|
---|
[1972] | 271 | vector<Triangle3>::const_iterator tit, tit_end = triangles.end();
|
---|
[1344] | 272 |
|
---|
[1972] | 273 | for (tit = triangles.begin(); tit != tit_end; ++ tit)
|
---|
[1421] | 274 | {
|
---|
[1979] | 275 | if (0 && !TriangleValid(*tit)) continue;
|
---|
| 276 |
|
---|
[1972] | 277 | TriangleIntersectable *obj = new TriangleIntersectable(*tit);
|
---|
[1979] | 278 | root->mGeometry.push_back(obj);
|
---|
[1421] | 279 | }
|
---|
[1344] | 280 | }
|
---|
[1221] | 281 | break;
|
---|
[1314] | 282 | } // end face
|
---|
[1221] | 283 | default:
|
---|
| 284 | break;
|
---|
| 285 | }
|
---|
| 286 | }
|
---|
[2307] | 287 |
|
---|
| 288 | //cout << "\n**** " << root->mGeometry.size() << " " << " lines: " << line << endl;
|
---|
[1421] | 289 | if (loadMeshes)
|
---|
| 290 | {
|
---|
| 291 | // there could be faces remaining
|
---|
| 292 | if (!faces.empty())
|
---|
| 293 | {
|
---|
| 294 | ProcessMesh(faces, hashTable, root, parents);
|
---|
| 295 | }
|
---|
[1221] | 296 | }
|
---|
[1421] | 297 |
|
---|
[1344] | 298 | // reset tables
|
---|
| 299 | hashTable.clear();
|
---|
| 300 | faces.clear();
|
---|
[1221] | 301 | fclose(file);
|
---|
[1344] | 302 |
|
---|
[1221] | 303 | return true;
|
---|
| 304 | }
|
---|
| 305 |
|
---|
[1272] | 306 | }
|
---|