[2961] | 1 | #include "ObjConverter.h"
|
---|
[3012] | 2 | #include "SimpleTri.h"
|
---|
| 3 | #include "SimpleVec.h"
|
---|
[2961] | 4 | #include "gzstream.h"
|
---|
| 5 | #include <iostream>
|
---|
| 6 |
|
---|
| 7 |
|
---|
| 8 | using namespace std;
|
---|
| 9 |
|
---|
[3146] | 10 | //#define USE_TEXTURE
|
---|
[2961] | 11 |
|
---|
[3146] | 12 |
|
---|
[2961] | 13 | static void LoadIndices(char *str,
|
---|
| 14 | const VertexArray &vertices,
|
---|
| 15 | const VertexArray &normals,
|
---|
| 16 | const vector<pair<float, float> > &texcoords,
|
---|
| 17 | VertexArray &faceVertices,
|
---|
| 18 | VertexArray &faceNormals,
|
---|
| 19 | vector<Texcoord> &faceTexcoords
|
---|
| 20 | )
|
---|
| 21 | {
|
---|
[3146] | 22 | vector<string> triples;
|
---|
[2961] | 23 |
|
---|
| 24 | char *next_token;
|
---|
| 25 |
|
---|
| 26 | // extract the triples of the form v/t/n v/t/n ...
|
---|
| 27 | char *pch = strtok_s(str + 1, " ", &next_token);
|
---|
| 28 |
|
---|
[3146] | 29 | while (pch)
|
---|
[2961] | 30 | {
|
---|
[3146] | 31 | string s(pch);
|
---|
| 32 | triples.push_back(s);
|
---|
| 33 |
|
---|
[2961] | 34 | pch = strtok_s(NULL, " ", &next_token);
|
---|
| 35 | }
|
---|
| 36 |
|
---|
[3263] | 37 | // throw away last symbol (\n)
|
---|
| 38 | triples.back().resize(triples.back().size() - 1);
|
---|
| 39 |
|
---|
[2961] | 40 | vector<int> indices;
|
---|
| 41 | vector<int> nIndices;
|
---|
| 42 | vector<int> tIndices;
|
---|
| 43 |
|
---|
[3263] | 44 | char seps[] = " ";
|
---|
| 45 | char seps2[] = "/";
|
---|
[3146] | 46 |
|
---|
| 47 | for (size_t i = 0; i < triples.size(); ++ i)
|
---|
[2961] | 48 | {
|
---|
[3263] | 49 | //cout << "triple " << i << " " << triples[i] << endl;
|
---|
[3146] | 50 | size_t found;
|
---|
[3263] | 51 | found = triples[i].find_first_of(seps2);
|
---|
[3146] | 52 | size_t prevfound = 0;
|
---|
[3263] | 53 |
|
---|
| 54 | // extract vertex, normal, texture indices
|
---|
[3146] | 55 | string str = triples[i].substr(prevfound, found);
|
---|
[2961] | 56 |
|
---|
[3146] | 57 | int index = (int)strtol(str.c_str(), NULL, 10) - 1;
|
---|
[2963] | 58 | int tIndex = index;
|
---|
[3146] | 59 | int nIndex = index;
|
---|
| 60 |
|
---|
[3263] | 61 | // try to extract texture and normal indices
|
---|
| 62 | prevfound = found + 1;
|
---|
| 63 | found = triples[i].find_first_of(seps2, prevfound);
|
---|
[2963] | 64 |
|
---|
[3146] | 65 | if (found != string::npos)
|
---|
| 66 | {
|
---|
| 67 | str = triples[i].substr(prevfound, found);
|
---|
[2961] | 68 |
|
---|
[3146] | 69 | int idx = (int)strtol(str.c_str(), NULL, 10) - 1;
|
---|
| 70 | if (idx > 0) tIndex = idx;
|
---|
[3012] | 71 | }
|
---|
[3263] | 72 |
|
---|
| 73 | if (found != string::npos)
|
---|
[3012] | 74 | {
|
---|
[3146] | 75 | str = triples[i].substr(found + 1);
|
---|
| 76 |
|
---|
| 77 | int idx = (int)strtol(str.c_str(), NULL, 10) - 1;
|
---|
| 78 | if (idx > 0) nIndex = idx;
|
---|
[3012] | 79 | }
|
---|
| 80 |
|
---|
[2961] | 81 | // store indices
|
---|
| 82 | if (index >= 0)
|
---|
| 83 | {
|
---|
| 84 | indices.push_back(index);
|
---|
| 85 | nIndices.push_back(nIndex);
|
---|
| 86 | tIndices.push_back(tIndex);
|
---|
[3263] | 87 |
|
---|
| 88 | //cout << index << " " << tIndex << " " << nIndex << endl;
|
---|
[2961] | 89 | }
|
---|
| 90 |
|
---|
| 91 | // new triangle found
|
---|
| 92 | if (indices.size() > 2)
|
---|
| 93 | {
|
---|
[3076] | 94 | // change orientation of faces?
|
---|
[3059] | 95 | #if 1
|
---|
[2961] | 96 | int idx1 = 0;
|
---|
| 97 | int idx2 = (int)indices.size() - 2;
|
---|
| 98 | int idx3 = (int)indices.size() - 1;
|
---|
| 99 | #else
|
---|
| 100 | int idx3 = 0;
|
---|
| 101 | int idx2 = (int)indices.size() - 2;
|
---|
| 102 | int idx1 = (int)indices.size() - 1;
|
---|
| 103 | #endif
|
---|
| 104 | faceVertices.push_back(vertices[indices[idx1]]);
|
---|
| 105 | faceVertices.push_back(vertices[indices[idx2]]);
|
---|
| 106 | faceVertices.push_back(vertices[indices[idx3]]);
|
---|
[2963] | 107 |
|
---|
[3146] | 108 |
|
---|
[3323] | 109 | if (!normals.empty())
|
---|
[3012] | 110 | {
|
---|
| 111 | faceNormals.push_back(normals[nIndices[idx1]]);
|
---|
| 112 | faceNormals.push_back(normals[nIndices[idx2]]);
|
---|
| 113 | faceNormals.push_back(normals[nIndices[idx3]]);
|
---|
| 114 | }
|
---|
| 115 | else
|
---|
| 116 | {
|
---|
[3146] | 117 | // no face normals? => create normals
|
---|
| 118 | const SimpleTri tri(vertices[indices[idx1]],
|
---|
| 119 | vertices[indices[idx2]],
|
---|
| 120 | vertices[indices[idx3]]);
|
---|
[3012] | 121 | const SimpleVec n = tri.GetNormal();
|
---|
[2963] | 122 |
|
---|
[3012] | 123 | faceNormals.push_back(n);
|
---|
| 124 | faceNormals.push_back(n);
|
---|
| 125 | faceNormals.push_back(n);
|
---|
| 126 | }
|
---|
[2961] | 127 |
|
---|
[3012] | 128 | if (!texcoords.empty())
|
---|
| 129 | {
|
---|
| 130 | faceTexcoords.push_back(texcoords[tIndices[idx1]]);
|
---|
| 131 | faceTexcoords.push_back(texcoords[tIndices[idx2]]);
|
---|
| 132 | faceTexcoords.push_back(texcoords[tIndices[idx3]]);
|
---|
| 133 | }
|
---|
[2961] | 134 | }
|
---|
| 135 | }
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 |
|
---|
| 139 | ObjConverter::ObjConverter()
|
---|
| 140 | {}
|
---|
| 141 |
|
---|
| 142 |
|
---|
| 143 | ObjConverter::~ObjConverter()
|
---|
| 144 | {
|
---|
| 145 | for (size_t i = 0; i < mGeometry.size(); ++ i)
|
---|
| 146 | delete mGeometry[i];
|
---|
| 147 |
|
---|
| 148 | mGeometry.clear();
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 |
|
---|
| 152 | void ObjConverter::LoadShape(const VertexArray &faceVertices,
|
---|
| 153 | const VertexArray &faceNormals,
|
---|
[3247] | 154 | const vector<Texcoord> &faceTexcoords,
|
---|
| 155 | Material *mat)
|
---|
[2961] | 156 | {
|
---|
| 157 | int numElements = (int)faceVertices.size();
|
---|
| 158 | Geometry *geom = new Geometry();
|
---|
| 159 |
|
---|
| 160 | // convert the triangles to geometry
|
---|
[3012] | 161 | geom->mVertices = new SimpleVec[numElements];
|
---|
| 162 | geom->mNormals = new SimpleVec[numElements];
|
---|
[2961] | 163 | geom->mTexcoords = new Texcoord[numElements];
|
---|
| 164 |
|
---|
| 165 | geom->mVertexCount = numElements;
|
---|
| 166 | geom->mTexcoordCount = (int)faceTexcoords.size();
|
---|
| 167 |
|
---|
[3247] | 168 | geom->mMaterial = mat;
|
---|
[2961] | 169 |
|
---|
[3260] | 170 | cout << "creating new geometry with " << numElements << " vertices" << endl;
|
---|
[3247] | 171 |
|
---|
[2961] | 172 | for (int i = 0; i < numElements; ++ i)
|
---|
| 173 | {
|
---|
[3161] | 174 | #if 0
|
---|
[3076] | 175 | // convert to our camera system: change y and z
|
---|
[2961] | 176 | geom->mVertices[i].x = faceVertices[i].x;
|
---|
| 177 | geom->mVertices[i].y = -faceVertices[i].z;
|
---|
| 178 | geom->mVertices[i].z = faceVertices[i].y;
|
---|
[3012] | 179 |
|
---|
[2961] | 180 | geom->mNormals[i].x = faceNormals[i].x;
|
---|
| 181 | geom->mNormals[i].y = -faceNormals[i].z;
|
---|
| 182 | geom->mNormals[i].z = faceNormals[i].y;
|
---|
[3146] | 183 |
|
---|
[3012] | 184 | #else
|
---|
| 185 | geom->mVertices[i].x = faceVertices[i].x;
|
---|
| 186 | geom->mVertices[i].y = faceVertices[i].y;
|
---|
| 187 | geom->mVertices[i].z = faceVertices[i].z;
|
---|
| 188 |
|
---|
| 189 | geom->mNormals[i].x = faceNormals[i].x;
|
---|
| 190 | geom->mNormals[i].y = faceNormals[i].y;
|
---|
| 191 | geom->mNormals[i].z = faceNormals[i].z;
|
---|
[2961] | 192 |
|
---|
[3012] | 193 | #endif
|
---|
[3089] | 194 |
|
---|
[2965] | 195 | if (i < geom->mTexcoordCount)
|
---|
[2963] | 196 | {
|
---|
| 197 | geom->mTexcoords[i].first = faceTexcoords[i].first;
|
---|
| 198 | geom->mTexcoords[i].second = faceTexcoords[i].second;
|
---|
| 199 | }
|
---|
[2961] | 200 | }
|
---|
| 201 |
|
---|
| 202 | mGeometry.push_back(geom);
|
---|
| 203 | }
|
---|
| 204 |
|
---|
| 205 |
|
---|
| 206 | bool ObjConverter::Convert(const string &filename, const std::string &outputFilename)
|
---|
| 207 | {
|
---|
| 208 | mNumShapes = 0;
|
---|
| 209 |
|
---|
| 210 | for (size_t i = 0; i < mGeometry.size(); ++ i)
|
---|
| 211 | delete mGeometry[i];
|
---|
| 212 |
|
---|
| 213 | mGeometry.clear();
|
---|
| 214 |
|
---|
| 215 | if (!ReadFile(filename))
|
---|
| 216 | {
|
---|
| 217 | cerr << "could not read file" << endl;
|
---|
| 218 | return false;
|
---|
| 219 | }
|
---|
| 220 |
|
---|
| 221 | if (!WriteFile(outputFilename))
|
---|
| 222 | {
|
---|
| 223 | cerr << "could not write file" << endl;
|
---|
| 224 | return false;
|
---|
| 225 | }
|
---|
| 226 |
|
---|
| 227 |
|
---|
| 228 | return true;
|
---|
| 229 | }
|
---|
| 230 |
|
---|
| 231 |
|
---|
| 232 | bool ObjConverter::ReadFile(const string &filename)
|
---|
| 233 | {
|
---|
| 234 | FILE *file;
|
---|
| 235 |
|
---|
| 236 | if ((file = fopen(filename.c_str(), "r")) == NULL)
|
---|
| 237 | {
|
---|
| 238 | return false;
|
---|
| 239 | }
|
---|
| 240 |
|
---|
[3247] | 241 | Material *currentMat = NULL;
|
---|
[2961] | 242 | VertexArray faceVertices;
|
---|
| 243 | VertexArray faceNormals;
|
---|
| 244 | vector<Texcoord> faceTexcoords;
|
---|
| 245 |
|
---|
| 246 | VertexArray vertices;
|
---|
| 247 | VertexArray normals;
|
---|
| 248 | vector<Texcoord> texcoords;
|
---|
| 249 |
|
---|
| 250 | vector<int> indices;
|
---|
| 251 |
|
---|
| 252 | int line = 0;
|
---|
| 253 | const int len = 10000;
|
---|
| 254 | char str[len];
|
---|
| 255 |
|
---|
| 256 | while (fgets(str, len, file) != NULL)
|
---|
| 257 | {
|
---|
[3146] | 258 | switch (str[0])
|
---|
[2961] | 259 | {
|
---|
| 260 | case 'v': // vertex or normal
|
---|
| 261 | {
|
---|
| 262 | float x, y, z;
|
---|
| 263 |
|
---|
| 264 | switch (str[1])
|
---|
| 265 | {
|
---|
| 266 | case 'n' :
|
---|
| 267 | sscanf(str + 2, "%f %f %f", &x, &y, &z);
|
---|
[3012] | 268 | normals.push_back(SimpleVec(x, y, z));
|
---|
[2961] | 269 | break;
|
---|
| 270 | case 't':
|
---|
| 271 | sscanf(str + 2, "%f %f", &x, &y);
|
---|
| 272 | texcoords.push_back(pair<float, float>(x, y));
|
---|
| 273 | break;
|
---|
| 274 | default:
|
---|
| 275 | sscanf(str + 1, "%f %f %f", &x, &y, &z);
|
---|
[3161] | 276 | //const float scale = 5e-3f;
|
---|
[3247] | 277 | const float scale = 5.0f;
|
---|
[3146] | 278 | vertices.push_back(SimpleVec(x * scale, y * scale, z* scale));
|
---|
[2961] | 279 | //cout <<"v " << x << " " << y << " "<< z << " ";
|
---|
| 280 | }
|
---|
| 281 | break;
|
---|
| 282 | }
|
---|
| 283 | case 'f':
|
---|
| 284 | {
|
---|
| 285 | //////////
|
---|
| 286 | //-- indices in the current line
|
---|
| 287 |
|
---|
| 288 | LoadIndices(str,
|
---|
| 289 | vertices, normals, texcoords,
|
---|
| 290 | faceVertices, faceNormals, faceTexcoords);
|
---|
| 291 |
|
---|
| 292 | break;
|
---|
| 293 | } // end face
|
---|
[3076] | 294 | case 'g': // load a new shape
|
---|
[3247] | 295 | {
|
---|
[2961] | 296 | if (!faceVertices.empty())
|
---|
| 297 | {
|
---|
| 298 | ++ mNumShapes;
|
---|
| 299 |
|
---|
[3247] | 300 | LoadShape(faceVertices, faceNormals, faceTexcoords, currentMat);
|
---|
[2961] | 301 |
|
---|
| 302 | faceVertices.clear();
|
---|
| 303 | faceNormals.clear();
|
---|
| 304 | faceTexcoords.clear();
|
---|
[3247] | 305 |
|
---|
| 306 | currentMat = NULL;
|
---|
| 307 | }
|
---|
[2961] | 308 | }
|
---|
| 309 | break;
|
---|
[3247] | 310 | case 'u': // usemtl => material
|
---|
| 311 | {
|
---|
| 312 | string matName(str + 7);
|
---|
| 313 | // throw away linebreak character
|
---|
| 314 | matName.resize(matName.size() - 1);
|
---|
| 315 |
|
---|
| 316 | currentMat = mMaterialTable[matName];
|
---|
[3261] | 317 | //cout << "matname: " << matName << endl;
|
---|
[3247] | 318 | }
|
---|
| 319 |
|
---|
| 320 | break;
|
---|
[2961] | 321 | default:
|
---|
[3146] | 322 | // throw away line
|
---|
[2961] | 323 | break;
|
---|
| 324 | }
|
---|
| 325 |
|
---|
| 326 | ++ line;
|
---|
| 327 | }
|
---|
| 328 |
|
---|
| 329 | if (!faceVertices.empty())
|
---|
| 330 | {
|
---|
| 331 | ++ mNumShapes;
|
---|
| 332 |
|
---|
[3247] | 333 | LoadShape(faceVertices, faceNormals, faceTexcoords, currentMat);
|
---|
[2961] | 334 |
|
---|
| 335 | faceVertices.clear();
|
---|
| 336 | faceNormals.clear();
|
---|
| 337 | faceTexcoords.clear();
|
---|
| 338 | }
|
---|
| 339 |
|
---|
| 340 | fclose(file);
|
---|
| 341 |
|
---|
| 342 | return true;
|
---|
| 343 | }
|
---|
| 344 |
|
---|
| 345 |
|
---|
| 346 | void ObjConverter::WriteGeometry(ogzstream &str, Geometry *geom)
|
---|
| 347 | {
|
---|
| 348 | int vertexCount = geom->mVertexCount;
|
---|
| 349 | str.write(reinterpret_cast<char *>(&vertexCount), sizeof(int));
|
---|
| 350 |
|
---|
[3012] | 351 | str.write(reinterpret_cast<char *>(geom->mVertices), sizeof(SimpleVec) * vertexCount);
|
---|
| 352 | str.write(reinterpret_cast<char *>(geom->mNormals), sizeof(SimpleVec) * vertexCount);
|
---|
[2961] | 353 |
|
---|
| 354 | int texCoordCount = geom->mTexcoordCount;
|
---|
| 355 | str.write(reinterpret_cast<char *>(&texCoordCount), sizeof(int));
|
---|
| 356 |
|
---|
| 357 | if (texCoordCount)
|
---|
| 358 | str.write(reinterpret_cast<char *>(geom->mTexcoords), sizeof(float) * texCoordCount * 2);
|
---|
| 359 |
|
---|
[3323] | 360 |
|
---|
[2961] | 361 | ///////
|
---|
| 362 | //-- texture
|
---|
[3323] | 363 | /*
|
---|
[3146] | 364 | #ifdef USE_TEXTURE
|
---|
[3089] | 365 | int texId = 0;
|
---|
[3146] | 366 | #else
|
---|
| 367 | int texId = -1;
|
---|
| 368 | #endif
|
---|
| 369 |
|
---|
[2961] | 370 | str.write(reinterpret_cast<char *>(&texId), sizeof(int));
|
---|
| 371 |
|
---|
| 372 | bool alphaTestEnabled = false;
|
---|
[3146] | 373 | //bool cullFaceEnabled = false;
|
---|
| 374 | bool cullFaceEnabled = true;
|
---|
[2961] | 375 |
|
---|
| 376 | str.write(reinterpret_cast<char *>(&alphaTestEnabled), sizeof(bool));
|
---|
| 377 | str.write(reinterpret_cast<char *>(&cullFaceEnabled), sizeof(bool));
|
---|
| 378 |
|
---|
| 379 | // material
|
---|
[3076] | 380 | bool hasMaterial = true;
|
---|
| 381 | //bool hasMaterial = false;
|
---|
[2961] | 382 | str.write(reinterpret_cast<char *>(&hasMaterial), sizeof(bool));
|
---|
| 383 |
|
---|
| 384 | if (hasMaterial)
|
---|
| 385 | {
|
---|
[3128] | 386 | SimpleVec ambient, diffuse, spec, emm;
|
---|
[2961] | 387 |
|
---|
| 388 | ambient.x = ambient.y = ambient.z = 0.2f;
|
---|
[3161] | 389 | //diffuse.x = diffuse.y = diffuse.z = 1.0f;
|
---|
| 390 | diffuse.x = 0.7f; diffuse.y = 0.5f; diffuse.z = 0.2f;
|
---|
[3146] | 391 | spec.x = spec.y = spec.z = .0f;
|
---|
[3128] | 392 | emm = spec;
|
---|
[2961] | 393 |
|
---|
| 394 | // only write rgb part of the material
|
---|
[3012] | 395 | str.write(reinterpret_cast<char *>(&ambient), sizeof(SimpleVec));
|
---|
| 396 | str.write(reinterpret_cast<char *>(&diffuse), sizeof(SimpleVec));
|
---|
[3128] | 397 | str.write(reinterpret_cast<char *>(&spec), sizeof(SimpleVec));
|
---|
| 398 | str.write(reinterpret_cast<char *>(&emm), sizeof(SimpleVec));
|
---|
[2961] | 399 | }
|
---|
[3247] | 400 | */
|
---|
[2961] | 401 | }
|
---|
| 402 |
|
---|
[3247] | 403 | #if 0
|
---|
[3295] | 404 |
|
---|
[2961] | 405 | bool ObjConverter::WriteFile(const string &filename)
|
---|
| 406 | {
|
---|
| 407 | ogzstream ofile(filename.c_str());
|
---|
| 408 |
|
---|
| 409 | if (!ofile.is_open())
|
---|
| 410 | return false;
|
---|
| 411 |
|
---|
| 412 |
|
---|
| 413 | /////////
|
---|
| 414 | //-- write textures
|
---|
| 415 |
|
---|
[3146] | 416 | #ifdef USE_TEXTURE
|
---|
[3089] | 417 | int textureCount = 1;
|
---|
[3146] | 418 | #else
|
---|
| 419 | int textureCount = 0;
|
---|
| 420 | #endif
|
---|
[2961] | 421 | ofile.write(reinterpret_cast<char *>(&textureCount), sizeof(int));
|
---|
| 422 |
|
---|
| 423 | if (textureCount > 0)
|
---|
| 424 | {
|
---|
[3076] | 425 | // hack
|
---|
[2961] | 426 | const string texName("wood.jpg");
|
---|
| 427 |
|
---|
| 428 | int texnameSize = (int)texName.length() + 1;
|
---|
| 429 | ofile.write(reinterpret_cast<char *>(&texnameSize), sizeof(int));
|
---|
| 430 |
|
---|
| 431 | ofile.write(texName.c_str(), sizeof(char) * texnameSize);
|
---|
| 432 |
|
---|
[3089] | 433 | int boundS = 1, boundT = 1;
|
---|
[2961] | 434 |
|
---|
| 435 | ofile.write(reinterpret_cast<char *>(&boundS), sizeof(int));
|
---|
| 436 | ofile.write(reinterpret_cast<char *>(&boundT), sizeof(int));
|
---|
| 437 | }
|
---|
| 438 |
|
---|
| 439 |
|
---|
| 440 | ///////////
|
---|
| 441 | //-- write shapes
|
---|
| 442 |
|
---|
| 443 | ofile.write(reinterpret_cast<char *>(&mNumShapes), sizeof(int));
|
---|
| 444 |
|
---|
| 445 | vector<Geometry *>::const_iterator it, it_end = mGeometry.end();
|
---|
| 446 |
|
---|
| 447 | for (it = mGeometry.begin(); it != it_end; ++ it)
|
---|
| 448 | {
|
---|
| 449 | WriteGeometry(ofile, *it);
|
---|
| 450 | }
|
---|
| 451 |
|
---|
| 452 |
|
---|
| 453 | int entityCount = 1;
|
---|
| 454 | ofile.write(reinterpret_cast<char *>(&entityCount), sizeof(int));
|
---|
| 455 |
|
---|
| 456 |
|
---|
| 457 | //////////
|
---|
| 458 | //-- write single scene entity
|
---|
| 459 |
|
---|
| 460 | // no transformation
|
---|
| 461 | bool hasTrafo = false;
|
---|
| 462 | ofile.write(reinterpret_cast<char *>(&hasTrafo), sizeof(bool));
|
---|
| 463 |
|
---|
| 464 | // a dummy lod
|
---|
| 465 | int numLODs = 1;
|
---|
| 466 | ofile.write(reinterpret_cast<char *>(&numLODs), sizeof(int));
|
---|
| 467 |
|
---|
| 468 | float dist = 0;
|
---|
| 469 | ofile.write(reinterpret_cast<char *>(&dist), sizeof(float));
|
---|
| 470 |
|
---|
| 471 | ofile.write(reinterpret_cast<char *>(&mNumShapes), sizeof(int));
|
---|
| 472 |
|
---|
| 473 | // all shapes belong to this scene entity
|
---|
| 474 | for (int i = 0; i < mNumShapes; ++ i)
|
---|
| 475 | {
|
---|
| 476 | int shapeId = i;
|
---|
| 477 | ofile.write(reinterpret_cast<char *>(&shapeId), sizeof(int));
|
---|
| 478 | }
|
---|
| 479 |
|
---|
| 480 | return true;
|
---|
[3247] | 481 | }
|
---|
[3295] | 482 |
|
---|
[3247] | 483 | #else
|
---|
[3295] | 484 |
|
---|
[3247] | 485 | bool ObjConverter::WriteFile(const string &filename)
|
---|
| 486 | {
|
---|
| 487 | ogzstream ofile(filename.c_str());
|
---|
| 488 |
|
---|
| 489 | if (!ofile.is_open())
|
---|
| 490 | return false;
|
---|
| 491 |
|
---|
| 492 |
|
---|
| 493 | /////////
|
---|
| 494 | //-- write textures
|
---|
| 495 |
|
---|
| 496 | int textureCount = (int)mTextures.size();
|
---|
| 497 |
|
---|
| 498 | ofile.write(reinterpret_cast<char *>(&textureCount), sizeof(int));
|
---|
| 499 |
|
---|
| 500 | TextureArray::const_iterator tit, tit_end = mTextures.end();
|
---|
| 501 |
|
---|
| 502 | for (tit = mTextures.begin(); tit != tit_end; ++ tit)
|
---|
| 503 | {
|
---|
| 504 | const string texName = (*tit);
|
---|
| 505 |
|
---|
| 506 | int texnameSize = (int)texName.length() + 1;
|
---|
| 507 | ofile.write(reinterpret_cast<char *>(&texnameSize), sizeof(int));
|
---|
| 508 |
|
---|
| 509 | ofile.write(texName.c_str(), sizeof(char) * texnameSize);
|
---|
| 510 |
|
---|
| 511 | int boundS = 1, boundT = 1;
|
---|
| 512 |
|
---|
| 513 | ofile.write(reinterpret_cast<char *>(&boundS), sizeof(int));
|
---|
| 514 | ofile.write(reinterpret_cast<char *>(&boundT), sizeof(int));
|
---|
| 515 | }
|
---|
| 516 |
|
---|
| 517 |
|
---|
| 518 | ///////////
|
---|
| 519 | //-- write shapes
|
---|
| 520 |
|
---|
| 521 | ofile.write(reinterpret_cast<char *>(&mNumShapes), sizeof(int));
|
---|
| 522 |
|
---|
| 523 | vector<Geometry *>::const_iterator it, it_end = mGeometry.end();
|
---|
| 524 |
|
---|
[3335] | 525 | Material defaultMaterial;
|
---|
| 526 |
|
---|
[3247] | 527 | for (it = mGeometry.begin(); it != it_end; ++ it)
|
---|
| 528 | {
|
---|
[3335] | 529 |
|
---|
| 530 | ///////////////////////
|
---|
| 531 | //-- write geometry
|
---|
| 532 |
|
---|
[3247] | 533 | WriteGeometry(ofile, *it);
|
---|
| 534 |
|
---|
| 535 |
|
---|
| 536 | /////////
|
---|
[3335] | 537 | //-- write material
|
---|
[3247] | 538 |
|
---|
[3335] | 539 | Material *mat = (*it)->mMaterial ? (*it)->mMaterial : &defaultMaterial;
|
---|
[3247] | 540 |
|
---|
| 541 | ofile.write(reinterpret_cast<char *>(&mat->texture), sizeof(int));
|
---|
| 542 |
|
---|
| 543 | bool alphaTestEnabled = false;
|
---|
| 544 | //bool cullFaceEnabled = false;
|
---|
| 545 | bool cullFaceEnabled = true;
|
---|
[3335] | 546 |
|
---|
[3247] | 547 | ofile.write(reinterpret_cast<char *>(&alphaTestEnabled), sizeof(bool));
|
---|
| 548 | ofile.write(reinterpret_cast<char *>(&cullFaceEnabled), sizeof(bool));
|
---|
| 549 |
|
---|
| 550 | // material
|
---|
| 551 | bool hasMaterial = true;
|
---|
| 552 | ofile.write(reinterpret_cast<char *>(&hasMaterial), sizeof(bool));
|
---|
[3335] | 553 |
|
---|
[3247] | 554 | SimpleVec ambient, diffuse, spec, emm;
|
---|
| 555 |
|
---|
| 556 | ambient.x = ambient.y = ambient.z = 0.2f;
|
---|
| 557 | //diffuse.x = diffuse.y = diffuse.z = 1.0f;
|
---|
[3335] | 558 | diffuse.x = mat->rgb[0]; diffuse.y = mat->rgb[1]; diffuse.z = mat->rgb[2];
|
---|
[3247] | 559 | spec.x = spec.y = spec.z = .0f;
|
---|
| 560 | emm = spec;
|
---|
| 561 |
|
---|
| 562 | // only write rgb part of the material
|
---|
| 563 | ofile.write(reinterpret_cast<char *>(&ambient), sizeof(SimpleVec));
|
---|
| 564 | ofile.write(reinterpret_cast<char *>(&diffuse), sizeof(SimpleVec));
|
---|
| 565 | ofile.write(reinterpret_cast<char *>(&spec), sizeof(SimpleVec));
|
---|
| 566 | ofile.write(reinterpret_cast<char *>(&emm), sizeof(SimpleVec));
|
---|
| 567 | }
|
---|
| 568 |
|
---|
| 569 |
|
---|
[3295] | 570 | //int entityCount = 1;
|
---|
| 571 | int entityCount = mNumShapes;
|
---|
[3247] | 572 | ofile.write(reinterpret_cast<char *>(&entityCount), sizeof(int));
|
---|
| 573 |
|
---|
| 574 |
|
---|
| 575 | //////////
|
---|
[3295] | 576 | //-- q: write single scene entity with all shapes or many entities with one shape
|
---|
[3247] | 577 |
|
---|
[3295] | 578 | // all shapes belong to this scene entity
|
---|
| 579 | for (int i = 0; i < mNumShapes; ++ i)
|
---|
| 580 | {
|
---|
| 581 | // no transformation
|
---|
| 582 | bool hasTrafo = false;
|
---|
| 583 | ofile.write(reinterpret_cast<char *>(&hasTrafo), sizeof(bool));
|
---|
[3247] | 584 |
|
---|
[3295] | 585 | // a dummy lod
|
---|
| 586 | int numLODs = 1;
|
---|
| 587 | ofile.write(reinterpret_cast<char *>(&numLODs), sizeof(int));
|
---|
[3247] | 588 |
|
---|
[3295] | 589 | float dist = 0;
|
---|
| 590 | ofile.write(reinterpret_cast<char *>(&dist), sizeof(float));
|
---|
[3247] | 591 |
|
---|
[3295] | 592 | //ofile.write(reinterpret_cast<char *>(&mNumShapes), sizeof(int));
|
---|
| 593 | int one = 1;
|
---|
| 594 | ofile.write(reinterpret_cast<char *>(&one), sizeof(int));
|
---|
[3247] | 595 |
|
---|
| 596 | int shapeId = i;
|
---|
| 597 | ofile.write(reinterpret_cast<char *>(&shapeId), sizeof(int));
|
---|
| 598 | }
|
---|
| 599 |
|
---|
| 600 | return true;
|
---|
| 601 | }
|
---|
| 602 | #endif
|
---|
| 603 |
|
---|
| 604 |
|
---|
| 605 | bool ObjConverter::LoadMaterials(const std::string &matFileName)
|
---|
| 606 | {
|
---|
| 607 | FILE *file;
|
---|
| 608 |
|
---|
| 609 | if ((file = fopen(matFileName.c_str(), "r")) == NULL)
|
---|
| 610 | {
|
---|
| 611 | return false;
|
---|
| 612 | }
|
---|
| 613 |
|
---|
| 614 | int line = 0;
|
---|
| 615 | const int len = 10000;
|
---|
| 616 | char str[len];
|
---|
| 617 |
|
---|
| 618 | Material *currentMat = NULL;
|
---|
| 619 |
|
---|
| 620 |
|
---|
| 621 | while (fgets(str, len, file) != NULL)
|
---|
| 622 | {
|
---|
| 623 | //sscanf(str + 1, "%f %f %f", &x, &y, &z);
|
---|
| 624 | vector<string> strings;
|
---|
| 625 |
|
---|
| 626 | char *next_token;
|
---|
| 627 |
|
---|
| 628 | // extract the triples of the form v/t/n v/t/n ...
|
---|
| 629 | char *pch = strtok_s(str, " \n", &next_token);
|
---|
| 630 |
|
---|
| 631 | while (pch)
|
---|
| 632 | {
|
---|
| 633 | string s(pch);
|
---|
| 634 | strings.push_back(s);
|
---|
| 635 |
|
---|
| 636 | pch = strtok_s(NULL, " \n", &next_token);
|
---|
| 637 | }
|
---|
| 638 |
|
---|
[3261] | 639 | if ((strings.size() == 2) && (strcmp(strings[0].c_str(), "newmtl") == 0))
|
---|
[3247] | 640 | {
|
---|
| 641 | currentMat = new Material();
|
---|
| 642 | mMaterialTable[strings[1].c_str()] = currentMat;
|
---|
| 643 | }
|
---|
| 644 |
|
---|
[3261] | 645 | if ((strings.size() == 2) && (strcmp(strings[0].c_str(), "map_Kd") == 0))
|
---|
[3247] | 646 | {
|
---|
| 647 | TextureTable::const_iterator it = mTextureTable.find(strings[1]);
|
---|
| 648 |
|
---|
| 649 | int id;
|
---|
| 650 |
|
---|
| 651 | if (it == mTextureTable.end()) // parameter not found
|
---|
| 652 | {
|
---|
| 653 | mTextures.push_back(strings[1]);
|
---|
| 654 | id = (int)mTextures.size();
|
---|
| 655 | mTextureTable[strings[1]] = id;
|
---|
| 656 | }
|
---|
| 657 | else
|
---|
| 658 | {
|
---|
| 659 | id = (*it).second;
|
---|
| 660 | }
|
---|
| 661 |
|
---|
| 662 | currentMat->texture = id;
|
---|
| 663 | }
|
---|
| 664 |
|
---|
| 665 | if ((strings.size() == 4) && (strcmp(strings[0].c_str(),"Kd") == 0))
|
---|
| 666 | {
|
---|
| 667 | currentMat->rgb[0] = (float)atof(strings[1].c_str());
|
---|
| 668 | currentMat->rgb[1] = (float)atof(strings[2].c_str());
|
---|
| 669 | currentMat->rgb[2] = (float)atof(strings[3].c_str());
|
---|
| 670 | }
|
---|
| 671 |
|
---|
| 672 | ++ line;
|
---|
| 673 | }
|
---|
| 674 |
|
---|
| 675 | return true;
|
---|
| 676 | }
|
---|