load material file */
| 345 | |
| 346 | /* load material file */ |
| 347 | void OBJLoader::loadMTL(const FileName &fileName) |
| 348 | { |
| 349 | std::ifstream cin; |
| 350 | cin.open(fileName.c_str()); |
| 351 | if (!cin.is_open()) { |
| 352 | std::cerr << "cannot open " << fileName.str() << std::endl; |
| 353 | return; |
| 354 | } |
| 355 | |
| 356 | std::string name; |
| 357 | ExtObjMaterial cur; |
| 358 | |
| 359 | while (cin.peek() != -1) |
| 360 | { |
| 361 | /* load next multiline */ |
| 362 | std::string line; std::getline(cin,line); |
| 363 | while (!line.empty() && line[line.size()-1] == '\\') { |
| 364 | line[line.size()-1] = ' '; |
| 365 | std::string next_line; std::getline(cin,next_line); |
| 366 | if (next_line.empty()) break; |
| 367 | line += next_line; |
| 368 | } |
| 369 | const char* token = trimEnd(line.c_str() + strspn(line.c_str(), " \t")); |
| 370 | |
| 371 | if (token[0] == 0 ) continue; // ignore empty lines |
| 372 | if (token[0] == '#') continue; // ignore comments |
| 373 | |
| 374 | if (!strncmp(token, "newmtl", 6)) |
| 375 | { |
| 376 | if (name != "") { |
| 377 | material[name] = cur.select(); |
| 378 | material[name]->name = name; |
| 379 | } |
| 380 | |
| 381 | parseSep(token+=6); |
| 382 | name = token; |
| 383 | cur = ExtObjMaterial(); |
| 384 | continue; |
| 385 | } |
| 386 | if (name == "") THROW_RUNTIME_ERROR("invalid material file: newmtl expected first"); |
| 387 | |
| 388 | try |
| 389 | { |
| 390 | if (!strncmp(token, "illum", 5)) { parseSep(token += 5); continue; } |
| 391 | |
| 392 | if (!strncmp(token, "d", 1)) { parseSep(token += 1); cur.d = getFloat(token); continue; } |
| 393 | if (!strncmp(token, "Ns", 2)) { parseSep(token += 2); cur.Ns = getFloat(token); continue; } |
| 394 | if (!strncmp(token, "Ni", 2)) { parseSep(token += 2); cur.Ni = getFloat(token); continue; } |
| 395 | |
| 396 | if (!strncmp(token, "map_d", 5) || !strncmp(token, "d_map", 5)) { |
| 397 | parseSep(token += 5); |
| 398 | cur.map_d = loadTexture(FileName(token)); |
| 399 | continue; |
| 400 | } |
| 401 | if (!strncmp(token, "map_Ka", 6) || !strncmp(token, "Ka_map", 6)) { |
| 402 | parseSep(token += 6); |
| 403 | cur.map_Ka = loadTexture(FileName(token)); |
| 404 | continue; |
nothing calls this directly
no test coverage detected