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