[1233] | 1 | #include <stdlib.h>
|
---|
[1221] | 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"
|
---|
[1328] | 13 | #include "Triangle3.h"
|
---|
[1221] | 14 | #include "Environment.h"
|
---|
| 15 | #include "ResourceManager.h"
|
---|
[1315] | 16 | #include "IntersectableWrapper.h"
|
---|
[1221] | 17 |
|
---|
| 18 |
|
---|
| 19 | namespace GtpVisibilityPreprocessor {
|
---|
| 20 |
|
---|
[1344] | 21 | #define CONNECT_SEQUENTIAL_FACES 0
|
---|
[1221] | 22 | #define ROTATE_SCENE 0
|
---|
[1328] | 23 |
|
---|
[1221] | 24 | // HACK
|
---|
[1328] | 25 | static void RotateMesh(Mesh *mesh)
|
---|
| 26 | {
|
---|
| 27 | VertexContainer::iterator it, it_end = mesh->mVertices.end();
|
---|
[1221] | 28 |
|
---|
[1328] | 29 | const float angle = 30.0f * PI / 180.0f;
|
---|
| 30 | const Matrix4x4 rot = RotationYMatrix(angle);
|
---|
[1221] | 31 |
|
---|
[1328] | 32 | for (it = mesh->mVertices.begin(); it != it_end; ++ it)
|
---|
| 33 | {
|
---|
| 34 | (*it) = rot * (*it);
|
---|
[1221] | 35 | }
|
---|
[1328] | 36 | }
|
---|
[1221] | 37 |
|
---|
[1344] | 38 |
|
---|
[1328] | 39 | struct ltstr
|
---|
| 40 | {
|
---|
| 41 | bool operator()(const string s1, const string s2) const
|
---|
[1221] | 42 | {
|
---|
[1328] | 43 | return s1 < s2;
|
---|
| 44 | }
|
---|
| 45 | };
|
---|
[1221] | 46 |
|
---|
| 47 |
|
---|
[1328] | 48 | static Face *LoadFace(char *str,
|
---|
| 49 | const VertexContainer &vertices,
|
---|
| 50 | map<int, Vector3> &hashTable)
|
---|
[1221] | 51 | {
|
---|
[1328] | 52 | char *pch = strtok(str + 1, " ");
|
---|
[1221] | 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 |
|
---|
[1344] | 72 | static Triangle3 LoadTriangle(char *str,
|
---|
| 73 | const VertexContainer &vertices,
|
---|
| 74 | map<int, Vector3> &hashTable)
|
---|
[1221] | 75 | {
|
---|
[1328] | 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;
|
---|
[1655] | 83 |
|
---|
[1328] | 84 | // store vertex in hash table
|
---|
| 85 | hashTable[index] = vertices[index];
|
---|
| 86 | indices.push_back(index);
|
---|
| 87 |
|
---|
| 88 | pch = strtok(NULL, " ");
|
---|
| 89 | }
|
---|
[1655] | 90 |
|
---|
[1344] | 91 | return Triangle3(vertices[indices[0]], vertices[indices[1]], vertices[indices[2]]);
|
---|
[1328] | 92 | }
|
---|
| 93 |
|
---|
| 94 |
|
---|
| 95 | static Mesh *CreateMesh(FaceContainer &faces,
|
---|
| 96 | const map<int, Vector3> &hashTable)
|
---|
| 97 | {
|
---|
[1221] | 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)
|
---|
[1314] | 115 | (*vit) = (int)distance(hashTable.begin(), hit);
|
---|
[1221] | 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;
|
---|
[1292] | 131 | // can't do cleanup because coupling with kdf file for intel ray tracer
|
---|
| 132 | mesh->Preprocess(false);
|
---|
| 133 |
|
---|
[1221] | 134 | return mesh;
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 |
|
---|
| 138 | // HACK: associate mesh instances with triangles
|
---|
[1328] | 139 | static void AssociateFacesWithInstance(MeshInstance *mi,
|
---|
| 140 | vector<FaceParentInfo> &parents)
|
---|
[1221] | 141 | {
|
---|
[1344] | 142 | Mesh *mesh = mi->GetMesh();
|
---|
| 143 |
|
---|
| 144 | int i = 0;
|
---|
[1655] | 145 | FaceContainer::const_iterator fit, fit_end = mesh->mFaces.end();
|
---|
| 146 |
|
---|
[1344] | 147 | for (fit = mesh->mFaces.begin(); fit != fit_end; ++ fit, i++)
|
---|
[1221] | 148 | {
|
---|
[1344] | 149 | parents.push_back(FaceParentInfo(mi, i));
|
---|
[1221] | 150 | }
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 |
|
---|
[1328] | 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 |
|
---|
[1221] | 176 | bool ObjParser::ParseFile(const string filename,
|
---|
[1344] | 177 | SceneGraphNode *root,
|
---|
[1379] | 178 | const bool loadMeshes,
|
---|
[1281] | 179 | vector<FaceParentInfo> *parents)
|
---|
[1221] | 180 | {
|
---|
| 181 | FILE *file;
|
---|
| 182 | if ((file = fopen(filename.c_str(), "rt")) == NULL)
|
---|
[1344] | 183 | {
|
---|
[1221] | 184 | return false;
|
---|
[1344] | 185 | }
|
---|
| 186 |
|
---|
[1221] | 187 | map<int, Vector3> hashTable; // table associating indices with vectors
|
---|
[1327] | 188 | VertexContainer vertices; // table for vertices
|
---|
[1221] | 189 | FaceContainer faces;
|
---|
| 190 |
|
---|
[1327] | 191 | char str[100];
|
---|
[1221] | 192 | int meshGrouping;
|
---|
| 193 | Environment::GetSingleton()->GetIntValue("ObjParser.meshGrouping", meshGrouping);
|
---|
| 194 |
|
---|
[1292] | 195 | int nMaxFaces = meshGrouping;
|
---|
| 196 |
|
---|
[1221] | 197 | while (fgets(str, 80, file) != NULL)
|
---|
| 198 | {
|
---|
[1876] | 199 | switch (str[0])
|
---|
[1221] | 200 | {
|
---|
[1876] | 201 | case 'v': // vertex or normal
|
---|
| 202 | {
|
---|
| 203 | switch (str[1]) {
|
---|
| 204 | case 'n' :
|
---|
| 205 | // normal do nothing
|
---|
| 206 | break;
|
---|
| 207 | default:
|
---|
| 208 | float x, y, z; //cout << "v";
|
---|
| 209 | sscanf(str + 1, "%f %f %f", &x, &y, &z);
|
---|
| 210 | vertices.push_back(Vector3(x,y,z));
|
---|
| 211 | //cout << "vertex: " << vertices.back() << endl;
|
---|
[1233] | 212 | }
|
---|
[1876] | 213 | break;
|
---|
| 214 | }
|
---|
[1221] | 215 | case 'f':
|
---|
| 216 | {
|
---|
[1655] | 217 | // cout << "f";
|
---|
[1421] | 218 | if (loadMeshes)
|
---|
| 219 | {
|
---|
| 220 | Face *face = LoadFace(str, vertices, hashTable);
|
---|
| 221 | if (!face) break;
|
---|
[1344] | 222 |
|
---|
[1421] | 223 | faces.push_back(face);
|
---|
[1328] | 224 |
|
---|
[1421] | 225 | if (faces.size() >= nMaxFaces)
|
---|
| 226 | {
|
---|
| 227 | ProcessMesh(faces, hashTable, root, parents);
|
---|
| 228 | }
|
---|
| 229 | }
|
---|
| 230 | else
|
---|
[1221] | 231 | {
|
---|
[1421] | 232 | Triangle3 triangle = LoadTriangle(str, vertices, hashTable);
|
---|
[1328] | 233 |
|
---|
[1421] | 234 | TriangleIntersectable *obj = new TriangleIntersectable(triangle);
|
---|
| 235 | root->mGeometry.push_back(obj);
|
---|
[1344] | 236 |
|
---|
[1421] | 237 | // matt: we don't really need to keep an additional data structure
|
---|
| 238 | // if working with triangles => remove this
|
---|
| 239 | if (parents)
|
---|
| 240 | {
|
---|
| 241 | FaceParentInfo info(obj, 0);
|
---|
| 242 | parents->push_back(info);
|
---|
| 243 | }
|
---|
[1344] | 244 | }
|
---|
[1221] | 245 | break;
|
---|
[1314] | 246 | } // end face
|
---|
[1221] | 247 | default:
|
---|
| 248 | break;
|
---|
| 249 | }
|
---|
| 250 | }
|
---|
| 251 |
|
---|
[1421] | 252 | if (loadMeshes)
|
---|
| 253 | {
|
---|
| 254 | // there could be faces remaining
|
---|
| 255 | if (!faces.empty())
|
---|
| 256 | {
|
---|
| 257 | ProcessMesh(faces, hashTable, root, parents);
|
---|
| 258 | }
|
---|
[1221] | 259 | }
|
---|
[1421] | 260 |
|
---|
[1344] | 261 | // reset tables
|
---|
| 262 | hashTable.clear();
|
---|
| 263 | faces.clear();
|
---|
[1221] | 264 | fclose(file);
|
---|
[1344] | 265 |
|
---|
[1221] | 266 | return true;
|
---|
| 267 | }
|
---|
| 268 |
|
---|
[1272] | 269 | }
|
---|