[2961] | 1 | #include "ObjConverter.h"
|
---|
| 2 | #include "gzstream.h"
|
---|
| 3 | #include <iostream>
|
---|
| 4 |
|
---|
| 5 |
|
---|
| 6 | using namespace std;
|
---|
| 7 |
|
---|
| 8 |
|
---|
| 9 | static void LoadIndices(char *str,
|
---|
| 10 | const VertexArray &vertices,
|
---|
| 11 | const VertexArray &normals,
|
---|
| 12 | const vector<pair<float, float> > &texcoords,
|
---|
| 13 | VertexArray &faceVertices,
|
---|
| 14 | VertexArray &faceNormals,
|
---|
| 15 | vector<Texcoord> &faceTexcoords
|
---|
| 16 | )
|
---|
| 17 | {
|
---|
| 18 | vector<char *> substrings;
|
---|
| 19 |
|
---|
| 20 | char *next_token;
|
---|
| 21 |
|
---|
| 22 | // extract the triples of the form v/t/n v/t/n ...
|
---|
| 23 | char *pch = strtok_s(str + 1, " ", &next_token);
|
---|
| 24 |
|
---|
| 25 | while (pch != NULL)
|
---|
| 26 | {
|
---|
| 27 | substrings.push_back(pch);
|
---|
| 28 | pch = strtok_s(NULL, " ", &next_token);
|
---|
| 29 | }
|
---|
| 30 |
|
---|
| 31 | vector<int> indices;
|
---|
| 32 | vector<int> nIndices;
|
---|
| 33 | vector<int> tIndices;
|
---|
| 34 |
|
---|
| 35 | for (size_t i = 0; i < substrings.size(); ++ i)
|
---|
| 36 | {
|
---|
| 37 | // vertex, normal and texture indices
|
---|
| 38 | char *str = strtok_s(substrings[i], "/", &next_token);
|
---|
| 39 | int index = (int)strtol(str, NULL, 10) - 1;
|
---|
| 40 |
|
---|
[2963] | 41 | int tIndex = index;
|
---|
| 42 | int nIndex = index;
|
---|
| 43 |
|
---|
| 44 | /*str = strtok_s(substrings[i], "/", &next_token);
|
---|
[2961] | 45 | int tIndex = (int)strtol(str, NULL, 10) - 1;
|
---|
| 46 |
|
---|
| 47 | str = strtok_s(substrings[i], "/", &next_token);
|
---|
| 48 | int nIndex = (int)strtol(str, NULL, 10) - 1;
|
---|
[2963] | 49 | */
|
---|
[2961] | 50 | // store indices
|
---|
| 51 | if (index >= 0)
|
---|
| 52 | {
|
---|
| 53 | indices.push_back(index);
|
---|
| 54 | nIndices.push_back(nIndex);
|
---|
| 55 | tIndices.push_back(tIndex);
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | // new triangle found
|
---|
| 59 | if (indices.size() > 2)
|
---|
| 60 | {
|
---|
| 61 | #if 1
|
---|
| 62 | int idx1 = 0;
|
---|
| 63 | int idx2 = (int)indices.size() - 2;
|
---|
| 64 | int idx3 = (int)indices.size() - 1;
|
---|
| 65 | #else
|
---|
| 66 | int idx3 = 0;
|
---|
| 67 | int idx2 = (int)indices.size() - 2;
|
---|
| 68 | int idx1 = (int)indices.size() - 1;
|
---|
| 69 | #endif
|
---|
| 70 | faceVertices.push_back(vertices[indices[idx1]]);
|
---|
| 71 | faceVertices.push_back(vertices[indices[idx2]]);
|
---|
| 72 | faceVertices.push_back(vertices[indices[idx3]]);
|
---|
[2963] | 73 |
|
---|
| 74 | Vector3 dummy(1, 0, 0);
|
---|
| 75 | faceNormals.push_back(dummy);
|
---|
| 76 | faceNormals.push_back(dummy);
|
---|
| 77 | faceNormals.push_back(dummy);
|
---|
| 78 |
|
---|
| 79 | /*
|
---|
| 80 | const Vector3 v2 = mVertices[2] - mVertices[1];
|
---|
| 81 | Normalize(CrossProd(v2, v1));
|
---|
| 82 |
|
---|
[2961] | 83 | faceNormals.push_back(normals[nIndices[idx1]]);
|
---|
| 84 | faceNormals.push_back(normals[nIndices[idx2]]);
|
---|
| 85 | faceNormals.push_back(normals[nIndices[idx3]]);
|
---|
| 86 |
|
---|
| 87 | faceTexcoords.push_back(texcoords[tIndices[idx1]]);
|
---|
| 88 | faceTexcoords.push_back(texcoords[tIndices[idx2]]);
|
---|
| 89 | faceTexcoords.push_back(texcoords[tIndices[idx3]]);
|
---|
[2963] | 90 | */
|
---|
[2961] | 91 | }
|
---|
| 92 | }
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 |
|
---|
| 96 | ObjConverter::ObjConverter()
|
---|
| 97 | {}
|
---|
| 98 |
|
---|
| 99 |
|
---|
| 100 | ObjConverter::~ObjConverter()
|
---|
| 101 | {
|
---|
| 102 | for (size_t i = 0; i < mGeometry.size(); ++ i)
|
---|
| 103 | {
|
---|
| 104 | delete [] mGeometry[i]->mVertices;
|
---|
| 105 | delete [] mGeometry[i]->mNormals;
|
---|
| 106 | delete [] mGeometry[i]->mTexcoords;
|
---|
| 107 |
|
---|
| 108 | delete mGeometry[i];
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | mGeometry.clear();
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 |
|
---|
| 115 | void ObjConverter::LoadShape(const VertexArray &faceVertices,
|
---|
| 116 | const VertexArray &faceNormals,
|
---|
| 117 | const vector<Texcoord> &faceTexcoords)
|
---|
| 118 | {
|
---|
| 119 | int numElements = (int)faceVertices.size();
|
---|
| 120 | Geometry *geom = new Geometry();
|
---|
| 121 |
|
---|
| 122 | // convert the triangles to geometry
|
---|
| 123 | geom->mVertices = new Vector3[numElements];
|
---|
| 124 | geom->mNormals = new Vector3[numElements];
|
---|
| 125 | geom->mTexcoords = new Texcoord[numElements];
|
---|
| 126 |
|
---|
| 127 | geom->mVertexCount = numElements;
|
---|
| 128 | geom->mTexcoordCount = (int)faceTexcoords.size();
|
---|
| 129 |
|
---|
| 130 | cout << "number of vertices=" << numElements << endl;
|
---|
| 131 |
|
---|
| 132 | for (int i = 0; i < numElements; ++ i)
|
---|
| 133 | {
|
---|
| 134 | geom->mVertices[i].x = faceVertices[i].x;
|
---|
| 135 | geom->mVertices[i].y = -faceVertices[i].z;
|
---|
| 136 | geom->mVertices[i].z = faceVertices[i].y;
|
---|
| 137 |
|
---|
| 138 | geom->mNormals[i].x = faceNormals[i].x;
|
---|
| 139 | geom->mNormals[i].y = -faceNormals[i].z;
|
---|
| 140 | geom->mNormals[i].z = faceNormals[i].y;
|
---|
| 141 |
|
---|
[2965] | 142 | if (i < geom->mTexcoordCount)
|
---|
[2963] | 143 | {
|
---|
| 144 | geom->mTexcoords[i].first = faceTexcoords[i].first;
|
---|
| 145 | geom->mTexcoords[i].second = faceTexcoords[i].second;
|
---|
| 146 | }
|
---|
[2961] | 147 | }
|
---|
| 148 |
|
---|
| 149 | mGeometry.push_back(geom);
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 |
|
---|
| 153 | bool ObjConverter::Convert(const string &filename, const std::string &outputFilename)
|
---|
| 154 | {
|
---|
| 155 | mNumShapes = 0;
|
---|
| 156 |
|
---|
| 157 | for (size_t i = 0; i < mGeometry.size(); ++ i)
|
---|
| 158 | {
|
---|
| 159 | delete [] mGeometry[i]->mVertices;
|
---|
| 160 | delete [] mGeometry[i]->mNormals;
|
---|
| 161 | delete [] mGeometry[i]->mTexcoords;
|
---|
| 162 |
|
---|
| 163 | delete mGeometry[i];
|
---|
| 164 | }
|
---|
| 165 |
|
---|
| 166 | mGeometry.clear();
|
---|
| 167 |
|
---|
| 168 | if (!ReadFile(filename))
|
---|
| 169 | {
|
---|
| 170 | cerr << "could not read file" << endl;
|
---|
| 171 | return false;
|
---|
| 172 | }
|
---|
| 173 |
|
---|
| 174 | if (!WriteFile(outputFilename))
|
---|
| 175 | {
|
---|
| 176 | cerr << "could not write file" << endl;
|
---|
| 177 | return false;
|
---|
| 178 | }
|
---|
| 179 |
|
---|
| 180 |
|
---|
| 181 | return true;
|
---|
| 182 | }
|
---|
| 183 |
|
---|
| 184 |
|
---|
| 185 | bool ObjConverter::ReadFile(const string &filename)
|
---|
| 186 | {
|
---|
| 187 | FILE *file;
|
---|
| 188 |
|
---|
| 189 | if ((file = fopen(filename.c_str(), "r")) == NULL)
|
---|
| 190 | {
|
---|
| 191 | return false;
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 | VertexArray faceVertices;
|
---|
| 195 | VertexArray faceNormals;
|
---|
| 196 | vector<Texcoord> faceTexcoords;
|
---|
| 197 |
|
---|
| 198 | VertexArray vertices;
|
---|
| 199 | VertexArray normals;
|
---|
| 200 | vector<Texcoord> texcoords;
|
---|
| 201 |
|
---|
| 202 | vector<int> indices;
|
---|
| 203 |
|
---|
| 204 | int line = 0;
|
---|
| 205 |
|
---|
| 206 | const int len = 10000;
|
---|
| 207 | char str[len];
|
---|
| 208 |
|
---|
| 209 | while (fgets(str, len, file) != NULL)
|
---|
| 210 | {
|
---|
| 211 | //cout << str;
|
---|
| 212 | switch (str[0])
|
---|
| 213 | {
|
---|
| 214 | case 'v': // vertex or normal
|
---|
| 215 | {
|
---|
| 216 | float x, y, z;
|
---|
| 217 |
|
---|
| 218 | switch (str[1])
|
---|
| 219 | {
|
---|
| 220 | case 'n' :
|
---|
| 221 | sscanf(str + 2, "%f %f %f", &x, &y, &z);
|
---|
| 222 | normals.push_back(Vector3(x, y, z));
|
---|
| 223 | break;
|
---|
| 224 | case 't':
|
---|
| 225 | sscanf(str + 2, "%f %f", &x, &y);
|
---|
| 226 | texcoords.push_back(pair<float, float>(x, y));
|
---|
| 227 | break;
|
---|
| 228 | default:
|
---|
| 229 | sscanf(str + 1, "%f %f %f", &x, &y, &z);
|
---|
| 230 | vertices.push_back(Vector3(x, y, z));
|
---|
| 231 | //cout <<"v " << x << " " << y << " "<< z << " ";
|
---|
| 232 | }
|
---|
| 233 | break;
|
---|
| 234 | }
|
---|
| 235 | case 'f':
|
---|
| 236 | {
|
---|
| 237 | //////////
|
---|
| 238 | //-- indices in the current line
|
---|
| 239 |
|
---|
| 240 | LoadIndices(str,
|
---|
| 241 | vertices, normals, texcoords,
|
---|
| 242 | faceVertices, faceNormals, faceTexcoords);
|
---|
| 243 |
|
---|
[2965] | 244 | if (((line % 1000) == 999) &&
|
---|
[2963] | 245 | !faceVertices.empty())
|
---|
| 246 | {
|
---|
| 247 | ++ mNumShapes;
|
---|
| 248 |
|
---|
| 249 | LoadShape(faceVertices, faceNormals, faceTexcoords);
|
---|
| 250 |
|
---|
| 251 | faceVertices.clear();
|
---|
| 252 | faceNormals.clear();
|
---|
| 253 | faceTexcoords.clear();
|
---|
| 254 | }
|
---|
| 255 |
|
---|
[2961] | 256 | break;
|
---|
| 257 | } // end face
|
---|
| 258 | case 'g':
|
---|
| 259 | {/*
|
---|
| 260 | if (!faceVertices.empty())
|
---|
| 261 | {
|
---|
| 262 | ++ mNumShapes;
|
---|
| 263 |
|
---|
| 264 | LoadShape(faceVertices, faceNormals, faceTexcoords);
|
---|
| 265 |
|
---|
| 266 | faceVertices.clear();
|
---|
| 267 | faceNormals.clear();
|
---|
| 268 | faceTexcoords.clear();
|
---|
| 269 | }*/
|
---|
| 270 | }
|
---|
| 271 | break;
|
---|
| 272 | default:
|
---|
| 273 | break;
|
---|
| 274 | }
|
---|
| 275 |
|
---|
| 276 | ++ line;
|
---|
| 277 | }
|
---|
| 278 |
|
---|
| 279 | if (!faceVertices.empty())
|
---|
| 280 | {
|
---|
| 281 | ++ mNumShapes;
|
---|
| 282 |
|
---|
| 283 | LoadShape(faceVertices, faceNormals, faceTexcoords);
|
---|
| 284 |
|
---|
| 285 | faceVertices.clear();
|
---|
| 286 | faceNormals.clear();
|
---|
| 287 | faceTexcoords.clear();
|
---|
| 288 | }
|
---|
| 289 |
|
---|
| 290 | fclose(file);
|
---|
| 291 |
|
---|
| 292 | return true;
|
---|
| 293 | }
|
---|
| 294 |
|
---|
| 295 |
|
---|
| 296 | void ObjConverter::WriteGeometry(ogzstream &str, Geometry *geom)
|
---|
| 297 | {
|
---|
| 298 | int vertexCount = geom->mVertexCount;
|
---|
| 299 | str.write(reinterpret_cast<char *>(&vertexCount), sizeof(int));
|
---|
| 300 |
|
---|
| 301 | str.write(reinterpret_cast<char *>(geom->mVertices), sizeof(Vector3) * vertexCount);
|
---|
| 302 | str.write(reinterpret_cast<char *>(geom->mNormals), sizeof(Vector3) * vertexCount);
|
---|
| 303 |
|
---|
| 304 | int texCoordCount = geom->mTexcoordCount;
|
---|
| 305 | str.write(reinterpret_cast<char *>(&texCoordCount), sizeof(int));
|
---|
| 306 |
|
---|
| 307 | if (texCoordCount)
|
---|
| 308 | str.write(reinterpret_cast<char *>(geom->mTexcoords), sizeof(float) * texCoordCount * 2);
|
---|
| 309 |
|
---|
| 310 |
|
---|
| 311 | ///////
|
---|
| 312 | //-- texture
|
---|
| 313 |
|
---|
[2963] | 314 | int texId = -1;
|
---|
| 315 | //int texId = 0;
|
---|
[2961] | 316 | str.write(reinterpret_cast<char *>(&texId), sizeof(int));
|
---|
| 317 |
|
---|
| 318 | bool alphaTestEnabled = false;
|
---|
| 319 | bool cullFaceEnabled = false;
|
---|
| 320 | //bool cullFaceEnabled = true;
|
---|
| 321 |
|
---|
| 322 | str.write(reinterpret_cast<char *>(&alphaTestEnabled), sizeof(bool));
|
---|
| 323 | str.write(reinterpret_cast<char *>(&cullFaceEnabled), sizeof(bool));
|
---|
| 324 |
|
---|
| 325 | // material
|
---|
| 326 | //bool hasMaterial = true;
|
---|
| 327 | bool hasMaterial = false;
|
---|
| 328 | str.write(reinterpret_cast<char *>(&hasMaterial), sizeof(bool));
|
---|
| 329 |
|
---|
| 330 | if (hasMaterial)
|
---|
| 331 | {
|
---|
| 332 | Vector3 ambient, diffuse, black;
|
---|
| 333 |
|
---|
| 334 | ambient.x = ambient.y = ambient.z = 0.2f;
|
---|
| 335 | diffuse.x = diffuse.y = diffuse.z = 1.0f;
|
---|
| 336 | black.x = black.y = black.z = .0f;
|
---|
| 337 |
|
---|
| 338 | // only write rgb part of the material
|
---|
| 339 | str.write(reinterpret_cast<char *>(&ambient), sizeof(Vector3));
|
---|
| 340 | str.write(reinterpret_cast<char *>(&diffuse), sizeof(Vector3));
|
---|
| 341 | str.write(reinterpret_cast<char *>(&black), sizeof(Vector3));
|
---|
| 342 | str.write(reinterpret_cast<char *>(&black), sizeof(Vector3));
|
---|
| 343 | }
|
---|
| 344 | }
|
---|
| 345 |
|
---|
| 346 |
|
---|
| 347 | bool ObjConverter::WriteFile(const string &filename)
|
---|
| 348 | {
|
---|
| 349 | ogzstream ofile(filename.c_str());
|
---|
| 350 |
|
---|
| 351 | if (!ofile.is_open())
|
---|
| 352 | return false;
|
---|
| 353 |
|
---|
| 354 |
|
---|
| 355 | /////////
|
---|
| 356 | //-- write textures
|
---|
| 357 |
|
---|
[2963] | 358 | //int textureCount = 1;
|
---|
| 359 | int textureCount = 0;
|
---|
[2961] | 360 |
|
---|
| 361 | ofile.write(reinterpret_cast<char *>(&textureCount), sizeof(int));
|
---|
| 362 |
|
---|
| 363 | if (textureCount > 0)
|
---|
| 364 | {
|
---|
| 365 | const string texName("wood.jpg");
|
---|
| 366 |
|
---|
| 367 | int texnameSize = (int)texName.length() + 1;
|
---|
| 368 | ofile.write(reinterpret_cast<char *>(&texnameSize), sizeof(int));
|
---|
| 369 |
|
---|
| 370 | ofile.write(texName.c_str(), sizeof(char) * texnameSize);
|
---|
| 371 |
|
---|
| 372 | int boundS = 1;
|
---|
| 373 | int boundT = 1;
|
---|
| 374 |
|
---|
| 375 | ofile.write(reinterpret_cast<char *>(&boundS), sizeof(int));
|
---|
| 376 | ofile.write(reinterpret_cast<char *>(&boundT), sizeof(int));
|
---|
| 377 | }
|
---|
| 378 |
|
---|
| 379 |
|
---|
| 380 | ///////////
|
---|
| 381 | //-- write shapes
|
---|
| 382 |
|
---|
| 383 | ofile.write(reinterpret_cast<char *>(&mNumShapes), sizeof(int));
|
---|
| 384 |
|
---|
| 385 | vector<Geometry *>::const_iterator it, it_end = mGeometry.end();
|
---|
| 386 |
|
---|
| 387 | for (it = mGeometry.begin(); it != it_end; ++ it)
|
---|
| 388 | {
|
---|
| 389 | WriteGeometry(ofile, *it);
|
---|
| 390 | }
|
---|
| 391 |
|
---|
| 392 |
|
---|
| 393 | int entityCount = 1;
|
---|
| 394 | ofile.write(reinterpret_cast<char *>(&entityCount), sizeof(int));
|
---|
| 395 |
|
---|
| 396 |
|
---|
| 397 | //////////
|
---|
| 398 | //-- write single scene entity
|
---|
| 399 |
|
---|
| 400 | // no transformation
|
---|
| 401 | bool hasTrafo = false;
|
---|
| 402 | ofile.write(reinterpret_cast<char *>(&hasTrafo), sizeof(bool));
|
---|
| 403 |
|
---|
| 404 | // a dummy lod
|
---|
| 405 | int numLODs = 1;
|
---|
| 406 | ofile.write(reinterpret_cast<char *>(&numLODs), sizeof(int));
|
---|
| 407 |
|
---|
| 408 | float dist = 0;
|
---|
| 409 | ofile.write(reinterpret_cast<char *>(&dist), sizeof(float));
|
---|
| 410 |
|
---|
| 411 | ofile.write(reinterpret_cast<char *>(&mNumShapes), sizeof(int));
|
---|
| 412 |
|
---|
| 413 | // all shapes belong to this scene entity
|
---|
| 414 | for (int i = 0; i < mNumShapes; ++ i)
|
---|
| 415 | {
|
---|
| 416 | int shapeId = i;
|
---|
| 417 | ofile.write(reinterpret_cast<char *>(&shapeId), sizeof(int));
|
---|
| 418 | }
|
---|
| 419 |
|
---|
| 420 | return true;
|
---|
| 421 | } |
---|