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