[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;
|
---|
| 83 | //Debug << index << " x ";
|
---|
| 84 |
|
---|
| 85 | // store vertex in hash table
|
---|
| 86 | hashTable[index] = vertices[index];
|
---|
| 87 | indices.push_back(index);
|
---|
| 88 |
|
---|
| 89 | pch = strtok(NULL, " ");
|
---|
| 90 | }
|
---|
| 91 | //if (indices.size() > 4) return NULL;
|
---|
| 92 |
|
---|
[1344] | 93 | return Triangle3(vertices[indices[0]], vertices[indices[1]], vertices[indices[2]]);
|
---|
[1328] | 94 | }
|
---|
| 95 |
|
---|
| 96 |
|
---|
| 97 | static Mesh *CreateMesh(FaceContainer &faces,
|
---|
| 98 | const map<int, Vector3> &hashTable)
|
---|
| 99 | {
|
---|
[1221] | 100 | Mesh *mesh = MeshManager::GetSingleton()->CreateResource();
|
---|
| 101 |
|
---|
| 102 | FaceContainer::const_iterator fit, fit_end = faces.end();
|
---|
| 103 |
|
---|
| 104 | for (fit = faces.begin(); fit != fit_end; ++ fit)
|
---|
| 105 | {
|
---|
| 106 | Face *face = *fit;
|
---|
| 107 | VertexIndexContainer::iterator vit, vit_end = face->mVertexIndices.end();
|
---|
| 108 |
|
---|
| 109 | for (vit = face->mVertexIndices.begin(); vit != vit_end; ++ vit)
|
---|
| 110 | {
|
---|
| 111 | // go through indices
|
---|
| 112 | const int index = *vit;
|
---|
| 113 | //Debug << "old idx: " << (*vit) << endl;
|
---|
| 114 | map<int, Vector3>::const_iterator hit = hashTable.find(index);
|
---|
| 115 |
|
---|
| 116 | // correct face index (nust be relative to start of verices)
|
---|
[1314] | 117 | (*vit) = (int)distance(hashTable.begin(), hit);
|
---|
[1221] | 118 | //Debug << "new idx: " << (*vit) << endl;
|
---|
| 119 | }
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | VertexContainer vertices;
|
---|
| 123 |
|
---|
| 124 | map<int, Vector3>::const_iterator hit, hit_end = hashTable.end();
|
---|
| 125 |
|
---|
| 126 | // store vertices in given order
|
---|
| 127 | for (hit = hashTable.begin(); hit != hit_end; ++ hit)
|
---|
| 128 | {
|
---|
| 129 | mesh->mVertices.push_back((*hit).second);
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | mesh->mFaces = faces;
|
---|
[1292] | 133 | // can't do cleanup because coupling with kdf file for intel ray tracer
|
---|
| 134 | mesh->Preprocess(false);
|
---|
| 135 |
|
---|
[1221] | 136 | return mesh;
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 |
|
---|
| 140 | // HACK: associate mesh instances with triangles
|
---|
[1328] | 141 | static void AssociateFacesWithInstance(MeshInstance *mi,
|
---|
| 142 | vector<FaceParentInfo> &parents)
|
---|
[1221] | 143 | {
|
---|
[1344] | 144 | Mesh *mesh = mi->GetMesh();
|
---|
| 145 |
|
---|
| 146 | FaceContainer::const_iterator fit, fit_end = mesh->mFaces.end();
|
---|
| 147 | int i = 0;
|
---|
| 148 | for (fit = mesh->mFaces.begin(); fit != fit_end; ++ fit, i++)
|
---|
[1221] | 149 | {
|
---|
[1344] | 150 | parents.push_back(FaceParentInfo(mi, i));
|
---|
[1221] | 151 | }
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 |
|
---|
[1328] | 155 | static void ProcessMesh(FaceContainer &faces,
|
---|
| 156 | map<int, Vector3> &hashTable,
|
---|
| 157 | SceneGraphNode *root,
|
---|
| 158 | vector<FaceParentInfo> *parents)
|
---|
| 159 | {
|
---|
| 160 | Mesh *mesh = CreateMesh(faces, hashTable);
|
---|
| 161 | // make an instance of this mesh
|
---|
| 162 | MeshInstance *mi = new MeshInstance(mesh);
|
---|
| 163 |
|
---|
| 164 | if (parents)
|
---|
| 165 | {
|
---|
| 166 | AssociateFacesWithInstance(mi, *parents);
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 | root->mGeometry.push_back(mi);
|
---|
| 170 |
|
---|
| 171 | // reset tables
|
---|
| 172 | hashTable.clear();
|
---|
| 173 | faces.clear();
|
---|
| 174 | }
|
---|
| 175 |
|
---|
| 176 |
|
---|
[1221] | 177 | bool ObjParser::ParseFile(const string filename,
|
---|
[1344] | 178 | SceneGraphNode *root,
|
---|
[1379] | 179 | const bool loadMeshes,
|
---|
[1281] | 180 | vector<FaceParentInfo> *parents)
|
---|
[1221] | 181 | {
|
---|
| 182 | FILE *file;
|
---|
| 183 | if ((file = fopen(filename.c_str(), "rt")) == NULL)
|
---|
[1344] | 184 | {
|
---|
[1221] | 185 | return false;
|
---|
[1344] | 186 | }
|
---|
| 187 |
|
---|
[1221] | 188 | map<int, Vector3> hashTable; // table associating indices with vectors
|
---|
[1327] | 189 | VertexContainer vertices; // table for vertices
|
---|
[1221] | 190 | FaceContainer faces;
|
---|
| 191 |
|
---|
[1327] | 192 | char str[100];
|
---|
[1221] | 193 | int meshGrouping;
|
---|
| 194 | Environment::GetSingleton()->GetIntValue("ObjParser.meshGrouping", meshGrouping);
|
---|
| 195 |
|
---|
[1292] | 196 | int nMaxFaces = meshGrouping;
|
---|
| 197 |
|
---|
[1221] | 198 | while (fgets(str, 80, file) != NULL)
|
---|
| 199 | {
|
---|
| 200 | switch (str[0])
|
---|
| 201 | {
|
---|
[1344] | 202 | case 'v': // vertex
|
---|
[1233] | 203 | {
|
---|
[1344] | 204 | float x, y, z; //cout << "v";
|
---|
[1233] | 205 | sscanf(str + 1, "%f %f %f", &x, &y, &z);
|
---|
| 206 | vertices.push_back(Vector3(x,y,z));
|
---|
| 207 | //Debug << "vertex: " << vertices.back() << endl;
|
---|
| 208 | break;
|
---|
| 209 | }
|
---|
[1221] | 210 | case 'f':
|
---|
| 211 | {
|
---|
[1454] | 212 | // cout << "f";
|
---|
[1421] | 213 |
|
---|
| 214 | if (loadMeshes)
|
---|
| 215 | {
|
---|
| 216 | Face *face = LoadFace(str, vertices, hashTable);
|
---|
| 217 | if (!face) break;
|
---|
[1344] | 218 |
|
---|
[1421] | 219 | faces.push_back(face);
|
---|
[1328] | 220 |
|
---|
[1421] | 221 | if (faces.size() >= nMaxFaces)
|
---|
| 222 | {
|
---|
| 223 | ProcessMesh(faces, hashTable, root, parents);
|
---|
| 224 | }
|
---|
| 225 | }
|
---|
| 226 | else
|
---|
[1221] | 227 | {
|
---|
[1421] | 228 | Triangle3 triangle = LoadTriangle(str, vertices, hashTable);
|
---|
[1328] | 229 |
|
---|
[1421] | 230 | TriangleIntersectable *obj = new TriangleIntersectable(triangle);
|
---|
| 231 | root->mGeometry.push_back(obj);
|
---|
[1344] | 232 |
|
---|
[1421] | 233 | // matt: we don't really need to keep an additional data structure
|
---|
| 234 | // if working with triangles => remove this
|
---|
| 235 | if (parents)
|
---|
| 236 | {
|
---|
| 237 | FaceParentInfo info(obj, 0);
|
---|
| 238 | parents->push_back(info);
|
---|
| 239 | }
|
---|
[1344] | 240 | }
|
---|
[1221] | 241 | break;
|
---|
[1314] | 242 | } // end face
|
---|
[1221] | 243 | default:
|
---|
| 244 | break;
|
---|
| 245 | }
|
---|
| 246 | }
|
---|
| 247 |
|
---|
[1421] | 248 | if (loadMeshes)
|
---|
| 249 | {
|
---|
| 250 | // there could be faces remaining
|
---|
| 251 | if (!faces.empty())
|
---|
| 252 | {
|
---|
| 253 | ProcessMesh(faces, hashTable, root, parents);
|
---|
| 254 | }
|
---|
[1221] | 255 | }
|
---|
[1421] | 256 |
|
---|
[1344] | 257 | // reset tables
|
---|
| 258 | hashTable.clear();
|
---|
| 259 | faces.clear();
|
---|
[1221] | 260 | fclose(file);
|
---|
[1344] | 261 |
|
---|
[1221] | 262 | return true;
|
---|
| 263 | }
|
---|
| 264 |
|
---|
[1272] | 265 | }
|
---|