Separate error message into warning and error. Breaks API, so bump to version 1.3.0

This commit is contained in:
Syoyo Fujita
2018-10-10 01:56:07 +09:00
parent 1cdfd786d8
commit e07a835f02
7 changed files with 568 additions and 378 deletions

View File

@@ -129,6 +129,7 @@ int main(int argc, char **argv) {
cb.object_cb = object_cb; cb.object_cb = object_cb;
MyMesh mesh; MyMesh mesh;
std::string warn;
std::string err; std::string err;
std::string filename = "../../models/cornell_box.obj"; std::string filename = "../../models/cornell_box.obj";
if (argc > 1) { if (argc > 1) {
@@ -143,7 +144,11 @@ int main(int argc, char **argv) {
tinyobj::MaterialFileReader mtlReader("../../models/"); tinyobj::MaterialFileReader mtlReader("../../models/");
bool ret = tinyobj::LoadObjWithCallback(ifs, cb, &mesh, &mtlReader, &err); bool ret = tinyobj::LoadObjWithCallback(ifs, cb, &mesh, &mtlReader, &warn, &err);
if (!warn.empty()) {
std::cout << "WARN: " << warn << std::endl;
}
if (!err.empty()) { if (!err.empty()) {
std::cerr << err << std::endl; std::cerr << err << std::endl;

View File

@@ -1,11 +1,10 @@
// //
// Stiches multiple .obj files into one .obj. // Stiches multiple .obj files into one .obj.
// //
#define TINYOBJLOADER_IMPLEMENTATION
#include "../../tiny_obj_loader.h"
#include "obj_writer.h" #include "obj_writer.h"
#include "../../tiny_obj_loader.h"
#include <cassert> #include <cassert>
#include <iostream> #include <iostream>
#include <cstdlib> #include <cstdlib>
@@ -134,8 +133,9 @@ int main(int argc, char **argv)
for (int i = 0; i < num_objfiles; i++) { for (int i = 0; i < num_objfiles; i++) {
std::cout << "Loading " << argv[i+1] << " ... " << std::flush; std::cout << "Loading " << argv[i+1] << " ... " << std::flush;
std::string warn;
std::string err; std::string err;
bool ret = tinyobj::LoadObj(&attributes[i], &shapes[i], &materials[i], &err, argv[i+1]); bool ret = tinyobj::LoadObj(&attributes[i], &shapes[i], &materials[i], &warn, &err, argv[i+1]);
if (!err.empty()) { if (!err.empty()) {
std::cerr << err << std::endl; std::cerr << err << std::endl;
} }

View File

@@ -308,9 +308,13 @@ static bool LoadObjAndConvert(float bmin[3], float bmax[3],
base_dir += "/"; base_dir += "/";
#endif #endif
std::string warn;
std::string err; std::string err;
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, filename, bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err, filename,
base_dir.c_str()); base_dir.c_str());
if (!warn.empty()) {
std::cout << "WARN: " << warn << std::endl;
}
if (!err.empty()) { if (!err.empty()) {
std::cerr << err << std::endl; std::cerr << err << std::endl;
} }

View File

@@ -9,8 +9,9 @@ bool Voxelize(const char* filename, float voxelsizex, float voxelsizey, float vo
tinyobj::attrib_t attrib; tinyobj::attrib_t attrib;
std::vector<tinyobj::shape_t> shapes; std::vector<tinyobj::shape_t> shapes;
std::vector<tinyobj::material_t> materials; std::vector<tinyobj::material_t> materials;
std::string warn;
std::string err; std::string err;
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, filename); bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err, filename);
if (!err.empty()) { if (!err.empty()) {
printf("err: %s\n", err.c_str()); printf("err: %s\n", err.c_str());

View File

@@ -285,14 +285,19 @@ static bool TestLoadObj(const char* filename, const char* basepath = NULL,
timerutil t; timerutil t;
t.start(); t.start();
std::string warn;
std::string err; std::string err;
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, filename, bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err, filename,
basepath, triangulate); basepath, triangulate);
t.end(); t.end();
printf("Parsing time: %lu [msecs]\n", t.msec()); printf("Parsing time: %lu [msecs]\n", t.msec());
if (!warn.empty()) {
std::cout << "WARN: " << warn << std::endl;
}
if (!err.empty()) { if (!err.empty()) {
std::cerr << err << std::endl; std::cerr << "ERR: " << err << std::endl;
} }
if (!ret) { if (!ret) {
@@ -376,16 +381,12 @@ static bool TestStreamLoadObj() {
virtual bool operator()(const std::string& matId, virtual bool operator()(const std::string& matId,
std::vector<material_t>* materials, std::vector<material_t>* materials,
std::map<std::string, int>* matMap, std::map<std::string, int>* matMap,
std::string* warn,
std::string* err) { std::string* err) {
(void)err;
(void)matId; (void)matId;
std::string warning; LoadMtl(matMap, materials, &m_matSStream, warn);
LoadMtl(matMap, materials, &m_matSStream, &warning);
if (!warning.empty()) {
if (err) {
(*err) += warning;
}
}
return true; return true;
} }
@@ -397,8 +398,9 @@ static bool TestStreamLoadObj() {
tinyobj::attrib_t attrib; tinyobj::attrib_t attrib;
std::vector<tinyobj::shape_t> shapes; std::vector<tinyobj::shape_t> shapes;
std::vector<tinyobj::material_t> materials; std::vector<tinyobj::material_t> materials;
std::string warn;
std::string err; std::string err;
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, &objStream, bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err, &objStream,
&matSSReader); &matSSReader);
if (!err.empty()) { if (!err.empty()) {

View File

@@ -1,57 +1,62 @@
#define TINYOBJLOADER_IMPLEMENTATION #define TINYOBJLOADER_IMPLEMENTATION
#include "../tiny_obj_loader.h" #include "../tiny_obj_loader.h"
#define CATCH_CONFIG_MAIN // This tells Catch to provide a main() - only do this in one cpp file #define CATCH_CONFIG_MAIN // This tells Catch to provide a main() - only do
// this in one cpp file
#include "catch.hpp" #include "catch.hpp"
#include <cassert>
#include <cstdio> #include <cstdio>
#include <cstdlib> #include <cstdlib>
#include <cassert> #include <fstream>
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
#include <fstream>
static void PrintInfo(const tinyobj::attrib_t &attrib, const std::vector<tinyobj::shape_t>& shapes, const std::vector<tinyobj::material_t>& materials, bool triangulate = true) static void PrintInfo(const tinyobj::attrib_t& attrib,
{ const std::vector<tinyobj::shape_t>& shapes,
const std::vector<tinyobj::material_t>& materials,
bool triangulate = true) {
std::cout << "# of vertices : " << (attrib.vertices.size() / 3) << std::endl; std::cout << "# of vertices : " << (attrib.vertices.size() / 3) << std::endl;
std::cout << "# of normals : " << (attrib.normals.size() / 3) << std::endl; std::cout << "# of normals : " << (attrib.normals.size() / 3) << std::endl;
std::cout << "# of texcoords : " << (attrib.texcoords.size() / 2) << std::endl; std::cout << "# of texcoords : " << (attrib.texcoords.size() / 2)
<< std::endl;
std::cout << "# of shapes : " << shapes.size() << std::endl; std::cout << "# of shapes : " << shapes.size() << std::endl;
std::cout << "# of materials : " << materials.size() << std::endl; std::cout << "# of materials : " << materials.size() << std::endl;
for (size_t v = 0; v < attrib.vertices.size() / 3; v++) { for (size_t v = 0; v < attrib.vertices.size() / 3; v++) {
printf(" v[%ld] = (%f, %f, %f)\n", v, printf(" v[%ld] = (%f, %f, %f)\n", v,
static_cast<const double>(attrib.vertices[3*v+0]), static_cast<const double>(attrib.vertices[3 * v + 0]),
static_cast<const double>(attrib.vertices[3*v+1]), static_cast<const double>(attrib.vertices[3 * v + 1]),
static_cast<const double>(attrib.vertices[3*v+2])); static_cast<const double>(attrib.vertices[3 * v + 2]));
} }
for (size_t v = 0; v < attrib.normals.size() / 3; v++) { for (size_t v = 0; v < attrib.normals.size() / 3; v++) {
printf(" n[%ld] = (%f, %f, %f)\n", v, printf(" n[%ld] = (%f, %f, %f)\n", v,
static_cast<const double>(attrib.normals[3*v+0]), static_cast<const double>(attrib.normals[3 * v + 0]),
static_cast<const double>(attrib.normals[3*v+1]), static_cast<const double>(attrib.normals[3 * v + 1]),
static_cast<const double>(attrib.normals[3*v+2])); static_cast<const double>(attrib.normals[3 * v + 2]));
} }
for (size_t v = 0; v < attrib.texcoords.size() / 2; v++) { for (size_t v = 0; v < attrib.texcoords.size() / 2; v++) {
printf(" uv[%ld] = (%f, %f)\n", v, printf(" uv[%ld] = (%f, %f)\n", v,
static_cast<const double>(attrib.texcoords[2*v+0]), static_cast<const double>(attrib.texcoords[2 * v + 0]),
static_cast<const double>(attrib.texcoords[2*v+1])); static_cast<const double>(attrib.texcoords[2 * v + 1]));
} }
for (size_t i = 0; i < shapes.size(); i++) { for (size_t i = 0; i < shapes.size(); i++) {
printf("shape[%ld].name = %s\n", i, shapes[i].name.c_str()); printf("shape[%ld].name = %s\n", i, shapes[i].name.c_str());
printf("Size of shape[%ld].indices: %ld\n", i, shapes[i].mesh.indices.size()); printf("Size of shape[%ld].indices: %ld\n", i,
shapes[i].mesh.indices.size());
if (triangulate) if (triangulate) {
{ printf("Size of shape[%ld].material_ids: %ld\n", i,
printf("Size of shape[%ld].material_ids: %ld\n", i, shapes[i].mesh.material_ids.size()); shapes[i].mesh.material_ids.size());
assert((shapes[i].mesh.indices.size() % 3) == 0); assert((shapes[i].mesh.indices.size() % 3) == 0);
for (size_t f = 0; f < shapes[i].mesh.indices.size() / 3; f++) { for (size_t f = 0; f < shapes[i].mesh.indices.size() / 3; f++) {
tinyobj::index_t i0 = shapes[i].mesh.indices[3*f+0]; tinyobj::index_t i0 = shapes[i].mesh.indices[3 * f + 0];
tinyobj::index_t i1 = shapes[i].mesh.indices[3*f+1]; tinyobj::index_t i1 = shapes[i].mesh.indices[3 * f + 1];
tinyobj::index_t i2 = shapes[i].mesh.indices[3*f+2]; tinyobj::index_t i2 = shapes[i].mesh.indices[3 * f + 2];
printf(" idx[%ld] = %d/%d/%d, %d/%d/%d, %d/%d/%d. mat_id = %d\n", f, printf(" idx[%ld] = %d/%d/%d, %d/%d/%d, %d/%d/%d. mat_id = %d\n", f,
i0.vertex_index, i0.normal_index, i0.texcoord_index, i0.vertex_index, i0.normal_index, i0.texcoord_index,
i1.vertex_index, i1.normal_index, i1.texcoord_index, i1.vertex_index, i1.normal_index, i1.texcoord_index,
@@ -61,27 +66,29 @@ static void PrintInfo(const tinyobj::attrib_t &attrib, const std::vector<tinyobj
} else { } else {
for (size_t f = 0; f < shapes[i].mesh.indices.size(); f++) { for (size_t f = 0; f < shapes[i].mesh.indices.size(); f++) {
tinyobj::index_t idx = shapes[i].mesh.indices[f]; tinyobj::index_t idx = shapes[i].mesh.indices[f];
printf(" idx[%ld] = %d/%d/%d\n", f, idx.vertex_index, idx.normal_index, idx.texcoord_index); printf(" idx[%ld] = %d/%d/%d\n", f, idx.vertex_index, idx.normal_index,
idx.texcoord_index);
} }
printf("Size of shape[%ld].material_ids: %ld\n", i, shapes[i].mesh.material_ids.size()); printf("Size of shape[%ld].material_ids: %ld\n", i,
assert(shapes[i].mesh.material_ids.size() == shapes[i].mesh.num_face_vertices.size()); shapes[i].mesh.material_ids.size());
assert(shapes[i].mesh.material_ids.size() ==
shapes[i].mesh.num_face_vertices.size());
for (size_t m = 0; m < shapes[i].mesh.material_ids.size(); m++) { for (size_t m = 0; m < shapes[i].mesh.material_ids.size(); m++) {
printf(" material_id[%ld] = %d\n", m, printf(" material_id[%ld] = %d\n", m, shapes[i].mesh.material_ids[m]);
shapes[i].mesh.material_ids[m]); }
} }
} printf("shape[%ld].num_faces: %ld\n", i,
shapes[i].mesh.num_face_vertices.size());
printf("shape[%ld].num_faces: %ld\n", i, shapes[i].mesh.num_face_vertices.size());
for (size_t v = 0; v < shapes[i].mesh.num_face_vertices.size(); v++) { for (size_t v = 0; v < shapes[i].mesh.num_face_vertices.size(); v++) {
printf(" num_vertices[%ld] = %ld\n", v, printf(" num_vertices[%ld] = %ld\n", v,
static_cast<long>(shapes[i].mesh.num_face_vertices[v])); static_cast<long>(shapes[i].mesh.num_face_vertices[v]));
} }
//printf("shape[%ld].vertices: %ld\n", i, shapes[i].mesh.positions.size()); // printf("shape[%ld].vertices: %ld\n", i, shapes[i].mesh.positions.size());
//assert((shapes[i].mesh.positions.size() % 3) == 0); // assert((shapes[i].mesh.positions.size() % 3) == 0);
//for (size_t v = 0; v < shapes[i].mesh.positions.size() / 3; v++) { // for (size_t v = 0; v < shapes[i].mesh.positions.size() / 3; v++) {
// printf(" v[%ld] = (%f, %f, %f)\n", v, // printf(" v[%ld] = (%f, %f, %f)\n", v,
// static_cast<const double>(shapes[i].mesh.positions[3*v+0]), // static_cast<const double>(shapes[i].mesh.positions[3*v+0]),
// static_cast<const double>(shapes[i].mesh.positions[3*v+1]), // static_cast<const double>(shapes[i].mesh.positions[3*v+1]),
@@ -92,33 +99,28 @@ static void PrintInfo(const tinyobj::attrib_t &attrib, const std::vector<tinyobj
for (size_t t = 0; t < shapes[i].mesh.tags.size(); t++) { for (size_t t = 0; t < shapes[i].mesh.tags.size(); t++) {
printf(" tag[%ld] = %s ", t, shapes[i].mesh.tags[t].name.c_str()); printf(" tag[%ld] = %s ", t, shapes[i].mesh.tags[t].name.c_str());
printf(" ints: ["); printf(" ints: [");
for (size_t j = 0; j < shapes[i].mesh.tags[t].intValues.size(); ++j) for (size_t j = 0; j < shapes[i].mesh.tags[t].intValues.size(); ++j) {
{
printf("%ld", static_cast<long>(shapes[i].mesh.tags[t].intValues[j])); printf("%ld", static_cast<long>(shapes[i].mesh.tags[t].intValues[j]));
if (j < (shapes[i].mesh.tags[t].intValues.size()-1)) if (j < (shapes[i].mesh.tags[t].intValues.size() - 1)) {
{
printf(", "); printf(", ");
} }
} }
printf("]"); printf("]");
printf(" floats: ["); printf(" floats: [");
for (size_t j = 0; j < shapes[i].mesh.tags[t].floatValues.size(); ++j) for (size_t j = 0; j < shapes[i].mesh.tags[t].floatValues.size(); ++j) {
{ printf("%f", static_cast<const double>(
printf("%f", static_cast<const double>(shapes[i].mesh.tags[t].floatValues[j])); shapes[i].mesh.tags[t].floatValues[j]));
if (j < (shapes[i].mesh.tags[t].floatValues.size()-1)) if (j < (shapes[i].mesh.tags[t].floatValues.size() - 1)) {
{
printf(", "); printf(", ");
} }
} }
printf("]"); printf("]");
printf(" strings: ["); printf(" strings: [");
for (size_t j = 0; j < shapes[i].mesh.tags[t].stringValues.size(); ++j) for (size_t j = 0; j < shapes[i].mesh.tags[t].stringValues.size(); ++j) {
{
printf("%s", shapes[i].mesh.tags[t].stringValues[j].c_str()); printf("%s", shapes[i].mesh.tags[t].stringValues[j].c_str());
if (j < (shapes[i].mesh.tags[t].stringValues.size()-1)) if (j < (shapes[i].mesh.tags[t].stringValues.size() - 1)) {
{
printf(", "); printf(", ");
} }
} }
@@ -129,25 +131,45 @@ static void PrintInfo(const tinyobj::attrib_t &attrib, const std::vector<tinyobj
for (size_t i = 0; i < materials.size(); i++) { for (size_t i = 0; i < materials.size(); i++) {
printf("material[%ld].name = %s\n", i, materials[i].name.c_str()); printf("material[%ld].name = %s\n", i, materials[i].name.c_str());
printf(" material.Ka = (%f, %f ,%f)\n", static_cast<const double>(materials[i].ambient[0]), static_cast<const double>(materials[i].ambient[1]), static_cast<const double>(materials[i].ambient[2])); printf(" material.Ka = (%f, %f ,%f)\n",
printf(" material.Kd = (%f, %f ,%f)\n", static_cast<const double>(materials[i].diffuse[0]), static_cast<const double>(materials[i].diffuse[1]), static_cast<const double>(materials[i].diffuse[2])); static_cast<const double>(materials[i].ambient[0]),
printf(" material.Ks = (%f, %f ,%f)\n", static_cast<const double>(materials[i].specular[0]), static_cast<const double>(materials[i].specular[1]), static_cast<const double>(materials[i].specular[2])); static_cast<const double>(materials[i].ambient[1]),
printf(" material.Tr = (%f, %f ,%f)\n", static_cast<const double>(materials[i].transmittance[0]), static_cast<const double>(materials[i].transmittance[1]), static_cast<const double>(materials[i].transmittance[2])); static_cast<const double>(materials[i].ambient[2]));
printf(" material.Ke = (%f, %f ,%f)\n", static_cast<const double>(materials[i].emission[0]), static_cast<const double>(materials[i].emission[1]), static_cast<const double>(materials[i].emission[2])); printf(" material.Kd = (%f, %f ,%f)\n",
printf(" material.Ns = %f\n", static_cast<const double>(materials[i].shininess)); static_cast<const double>(materials[i].diffuse[0]),
static_cast<const double>(materials[i].diffuse[1]),
static_cast<const double>(materials[i].diffuse[2]));
printf(" material.Ks = (%f, %f ,%f)\n",
static_cast<const double>(materials[i].specular[0]),
static_cast<const double>(materials[i].specular[1]),
static_cast<const double>(materials[i].specular[2]));
printf(" material.Tr = (%f, %f ,%f)\n",
static_cast<const double>(materials[i].transmittance[0]),
static_cast<const double>(materials[i].transmittance[1]),
static_cast<const double>(materials[i].transmittance[2]));
printf(" material.Ke = (%f, %f ,%f)\n",
static_cast<const double>(materials[i].emission[0]),
static_cast<const double>(materials[i].emission[1]),
static_cast<const double>(materials[i].emission[2]));
printf(" material.Ns = %f\n",
static_cast<const double>(materials[i].shininess));
printf(" material.Ni = %f\n", static_cast<const double>(materials[i].ior)); printf(" material.Ni = %f\n", static_cast<const double>(materials[i].ior));
printf(" material.dissolve = %f\n", static_cast<const double>(materials[i].dissolve)); printf(" material.dissolve = %f\n",
static_cast<const double>(materials[i].dissolve));
printf(" material.illum = %d\n", materials[i].illum); printf(" material.illum = %d\n", materials[i].illum);
printf(" material.map_Ka = %s\n", materials[i].ambient_texname.c_str()); printf(" material.map_Ka = %s\n", materials[i].ambient_texname.c_str());
printf(" material.map_Kd = %s\n", materials[i].diffuse_texname.c_str()); printf(" material.map_Kd = %s\n", materials[i].diffuse_texname.c_str());
printf(" material.map_Ks = %s\n", materials[i].specular_texname.c_str()); printf(" material.map_Ks = %s\n", materials[i].specular_texname.c_str());
printf(" material.map_Ns = %s\n", materials[i].specular_highlight_texname.c_str()); printf(" material.map_Ns = %s\n",
materials[i].specular_highlight_texname.c_str());
printf(" material.map_bump = %s\n", materials[i].bump_texname.c_str()); printf(" material.map_bump = %s\n", materials[i].bump_texname.c_str());
printf(" material.map_d = %s\n", materials[i].alpha_texname.c_str()); printf(" material.map_d = %s\n", materials[i].alpha_texname.c_str());
printf(" material.disp = %s\n", materials[i].displacement_texname.c_str()); printf(" material.disp = %s\n", materials[i].displacement_texname.c_str());
printf(" material.refl = %s\n", materials[i].reflection_texname.c_str()); printf(" material.refl = %s\n", materials[i].reflection_texname.c_str());
std::map<std::string, std::string>::const_iterator it(materials[i].unknown_parameter.begin()); std::map<std::string, std::string>::const_iterator it(
std::map<std::string, std::string>::const_iterator itEnd(materials[i].unknown_parameter.end()); materials[i].unknown_parameter.begin());
std::map<std::string, std::string>::const_iterator itEnd(
materials[i].unknown_parameter.end());
for (; it != itEnd; it++) { for (; it != itEnd; it++) {
printf(" material.%s = %s\n", it->first.c_str(), it->second.c_str()); printf(" material.%s = %s\n", it->first.c_str(), it->second.c_str());
@@ -156,23 +178,25 @@ static void PrintInfo(const tinyobj::attrib_t &attrib, const std::vector<tinyobj
} }
} }
static bool static bool TestLoadObj(const char* filename, const char* basepath = NULL,
TestLoadObj( bool triangulate = true) {
const char* filename,
const char* basepath = NULL,
bool triangulate = true)
{
std::cout << "Loading " << filename << std::endl; std::cout << "Loading " << filename << std::endl;
tinyobj::attrib_t attrib; tinyobj::attrib_t attrib;
std::vector<tinyobj::shape_t> shapes; std::vector<tinyobj::shape_t> shapes;
std::vector<tinyobj::material_t> materials; std::vector<tinyobj::material_t> materials;
std::string warn;
std::string err; std::string err;
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, filename, basepath, triangulate); bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err,
filename, basepath, triangulate);
if (!warn.empty()) {
std::cout << "WARN: " << warn << std::endl;
}
if (!err.empty()) { if (!err.empty()) {
std::cerr << err << std::endl; std::cerr << "ERR: " << err << std::endl;
} }
if (!ret) { if (!ret) {
@@ -185,13 +209,10 @@ TestLoadObj(
return true; return true;
} }
static bool static bool TestLoadObjFromPreopenedFile(const char* filename,
TestLoadObjFromPreopenedFile(
const char* filename,
const char* basepath = NULL, const char* basepath = NULL,
bool readMaterials = true, bool readMaterials = true,
bool triangulate = true) bool triangulate = true) {
{
std::string fullFilename = std::string(basepath) + filename; std::string fullFilename = std::string(basepath) + filename;
std::cout << "Loading " << fullFilename << std::endl; std::cout << "Loading " << fullFilename << std::endl;
@@ -203,19 +224,24 @@ TestLoadObjFromPreopenedFile(
} }
tinyobj::MaterialStreamReader materialStreamReader(fileStream); tinyobj::MaterialStreamReader materialStreamReader(fileStream);
tinyobj::MaterialStreamReader* materialReader = readMaterials tinyobj::MaterialStreamReader* materialReader =
? &materialStreamReader readMaterials ? &materialStreamReader : NULL;
: NULL;
tinyobj::attrib_t attrib; tinyobj::attrib_t attrib;
std::vector<tinyobj::shape_t> shapes; std::vector<tinyobj::shape_t> shapes;
std::vector<tinyobj::material_t> materials; std::vector<tinyobj::material_t> materials;
std::string warn;
std::string err; std::string err;
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, &fileStream, materialReader); bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err,
&fileStream, materialReader);
if (!warn.empty()) {
std::cout << "WARN: " << warn << std::endl;
}
if (!err.empty()) { if (!err.empty()) {
std::cerr << err << std::endl; std::cerr << "ERR: " << err << std::endl;
} }
if (!ret) { if (!ret) {
@@ -228,14 +254,11 @@ TestLoadObjFromPreopenedFile(
return true; return true;
} }
static bool static bool TestStreamLoadObj() {
TestStreamLoadObj()
{
std::cout << "Stream Loading " << std::endl; std::cout << "Stream Loading " << std::endl;
std::stringstream objStream; std::stringstream objStream;
objStream objStream << "mtllib cube.mtl\n"
<< "mtllib cube.mtl\n"
"\n" "\n"
"v 0.000000 2.000000 2.000000\n" "v 0.000000 2.000000 2.000000\n"
"v 0.000000 0.000000 2.000000\n" "v 0.000000 0.000000 2.000000\n"
@@ -267,7 +290,7 @@ TestStreamLoadObj()
"f 2 6 7 3\n" "f 2 6 7 3\n"
"# 6 elements"; "# 6 elements";
std::string matStream( std::string matStream(
"newmtl white\n" "newmtl white\n"
"Ka 0 0 0\n" "Ka 0 0 0\n"
"Kd 1 1 1\n" "Kd 1 1 1\n"
@@ -294,22 +317,21 @@ std::string matStream(
"Ks 0 0 0"); "Ks 0 0 0");
using namespace tinyobj; using namespace tinyobj;
class MaterialStringStreamReader: class MaterialStringStreamReader : public MaterialReader {
public MaterialReader
{
public: public:
MaterialStringStreamReader(const std::string& matSStream): m_matSStream(matSStream) {} MaterialStringStreamReader(const std::string& matSStream)
: m_matSStream(matSStream) {}
virtual ~MaterialStringStreamReader() {} virtual ~MaterialStringStreamReader() {}
virtual bool operator() ( virtual bool operator()(const std::string& matId,
const std::string& matId,
std::vector<material_t>* materials, std::vector<material_t>* materials,
std::map<std::string, int>* matMap, std::map<std::string, int>* matMap,
std::string* err) std::string* warn, std::string* err) {
{
(void)matId; (void)matId;
(void)warn;
(void)err; (void)err;
std::string warning; std::string warning;
LoadMtl(matMap, materials, &m_matSStream, &warning); std::string error_msg;
LoadMtl(matMap, materials, &m_matSStream, &warning, &error_msg);
return true; return true;
} }
@@ -321,11 +343,17 @@ std::string matStream(
tinyobj::attrib_t attrib; tinyobj::attrib_t attrib;
std::vector<tinyobj::shape_t> shapes; std::vector<tinyobj::shape_t> shapes;
std::vector<tinyobj::material_t> materials; std::vector<tinyobj::material_t> materials;
std::string warn;
std::string err; std::string err;
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, &objStream, &matSSReader); bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err,
&objStream, &matSSReader);
if (!warn.empty()) {
std::cout << "WARN: " << warn << std::endl;
}
if (!err.empty()) { if (!err.empty()) {
std::cerr << err << std::endl; std::cerr << "ERR: " << err << std::endl;
} }
if (!ret) { if (!ret) {
@@ -340,21 +368,26 @@ std::string matStream(
const char* gMtlBasePath = "../models/"; const char* gMtlBasePath = "../models/";
TEST_CASE("cornell_box", "[Loader]") { TEST_CASE("cornell_box", "[Loader]") {
REQUIRE(true == TestLoadObj("../models/cornell_box.obj", gMtlBasePath)); REQUIRE(true == TestLoadObj("../models/cornell_box.obj", gMtlBasePath));
} }
TEST_CASE("catmark_torus_creases0", "[Loader]") { TEST_CASE("catmark_torus_creases0", "[Loader]") {
tinyobj::attrib_t attrib; tinyobj::attrib_t attrib;
std::vector<tinyobj::shape_t> shapes; std::vector<tinyobj::shape_t> shapes;
std::vector<tinyobj::material_t> materials; std::vector<tinyobj::material_t> materials;
std::string warn;
std::string err; std::string err;
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, "../models/catmark_torus_creases0.obj", gMtlBasePath, /*triangulate*/false); bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err,
"../models/catmark_torus_creases0.obj",
gMtlBasePath, /*triangulate*/ false);
if (!warn.empty()) {
std::cout << "WARN: " << warn << std::endl;
}
if (!err.empty()) { if (!err.empty()) {
std::cerr << err << std::endl; std::cerr << "ERR: " << err << std::endl;
} }
REQUIRE(true == ret); REQUIRE(true == ret);
@@ -364,16 +397,22 @@ TEST_CASE("catmark_torus_creases0", "[Loader]") {
} }
TEST_CASE("pbr", "[Loader]") { TEST_CASE("pbr", "[Loader]") {
tinyobj::attrib_t attrib; tinyobj::attrib_t attrib;
std::vector<tinyobj::shape_t> shapes; std::vector<tinyobj::shape_t> shapes;
std::vector<tinyobj::material_t> materials; std::vector<tinyobj::material_t> materials;
std::string warn;
std::string err; std::string err;
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, "../models/pbr-mat-ext.obj", gMtlBasePath, /*triangulate*/false); bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err,
"../models/pbr-mat-ext.obj", gMtlBasePath,
/*triangulate*/ false);
if (!warn.empty()) {
std::cout << "WARN: " << warn << std::endl;
}
if (!err.empty()) { if (!err.empty()) {
std::cerr << err << std::endl; std::cerr << "ERR: " << err << std::endl;
} }
REQUIRE(true == ret); REQUIRE(true == ret);
REQUIRE(1 == materials.size()); REQUIRE(1 == materials.size());
@@ -391,18 +430,18 @@ TEST_CASE("pbr", "[Loader]") {
REQUIRE(0 == materials[0].normal_texname.compare("normalmap.tex")); REQUIRE(0 == materials[0].normal_texname.compare("normalmap.tex"));
} }
TEST_CASE("stream_load", "[Stream]") { TEST_CASE("stream_load", "[Stream]") { REQUIRE(true == TestStreamLoadObj()); }
REQUIRE(true == TestStreamLoadObj());
}
TEST_CASE("stream_load_from_file_skipping_materials", "[Stream]") { TEST_CASE("stream_load_from_file_skipping_materials", "[Stream]") {
REQUIRE(true == TestLoadObjFromPreopenedFile( REQUIRE(true == TestLoadObjFromPreopenedFile(
"../models/pbr-mat-ext.obj", gMtlBasePath, /*readMaterials*/false, /*triangulate*/false)); "../models/pbr-mat-ext.obj", gMtlBasePath,
/*readMaterials*/ false, /*triangulate*/ false));
} }
TEST_CASE("stream_load_from_file_with_materials", "[Stream]") { TEST_CASE("stream_load_from_file_with_materials", "[Stream]") {
REQUIRE(true == TestLoadObjFromPreopenedFile( REQUIRE(true == TestLoadObjFromPreopenedFile(
"../models/pbr-mat-ext.obj", gMtlBasePath, /*readMaterials*/true, /*triangulate*/false)); "../models/pbr-mat-ext.obj", gMtlBasePath,
/*readMaterials*/ true, /*triangulate*/ false));
} }
TEST_CASE("trailing_whitespace_in_mtl", "[Issue92]") { TEST_CASE("trailing_whitespace_in_mtl", "[Issue92]") {
@@ -410,11 +449,17 @@ TEST_CASE("trailing_whitespace_in_mtl", "[Issue92]") {
std::vector<tinyobj::shape_t> shapes; std::vector<tinyobj::shape_t> shapes;
std::vector<tinyobj::material_t> materials; std::vector<tinyobj::material_t> materials;
std::string warn;
std::string err; std::string err;
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, "../models/issue-92.obj", gMtlBasePath); bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err,
"../models/issue-92.obj", gMtlBasePath);
if (!warn.empty()) {
std::cout << "WARN: " << warn << std::endl;
}
if (!err.empty()) { if (!err.empty()) {
std::cerr << err << std::endl; std::cerr << "ERR: " << err << std::endl;
} }
REQUIRE(true == ret); REQUIRE(true == ret);
REQUIRE(1 == materials.size()); REQUIRE(1 == materials.size());
@@ -426,11 +471,17 @@ TEST_CASE("transmittance_filter", "[Issue95]") {
std::vector<tinyobj::shape_t> shapes; std::vector<tinyobj::shape_t> shapes;
std::vector<tinyobj::material_t> materials; std::vector<tinyobj::material_t> materials;
std::string warn;
std::string err; std::string err;
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, "../models/issue-95.obj", gMtlBasePath); bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err,
"../models/issue-95.obj", gMtlBasePath);
if (!warn.empty()) {
std::cout << "WARN: " << warn << std::endl;
}
if (!err.empty()) { if (!err.empty()) {
std::cerr << err << std::endl; std::cerr << "ERR: " << err << std::endl;
} }
REQUIRE(true == ret); REQUIRE(true == ret);
REQUIRE(1 == materials.size()); REQUIRE(1 == materials.size());
@@ -444,11 +495,17 @@ TEST_CASE("transmittance_filter_Tf", "[Issue95-Tf]") {
std::vector<tinyobj::shape_t> shapes; std::vector<tinyobj::shape_t> shapes;
std::vector<tinyobj::material_t> materials; std::vector<tinyobj::material_t> materials;
std::string warn;
std::string err; std::string err;
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, "../models/issue-95-2.obj", gMtlBasePath); bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err,
"../models/issue-95-2.obj", gMtlBasePath);
if (!warn.empty()) {
std::cout << "WARN: " << warn << std::endl;
}
if (!err.empty()) { if (!err.empty()) {
std::cerr << err << std::endl; std::cerr << "ERR: " << err << std::endl;
} }
REQUIRE(true == ret); REQUIRE(true == ret);
REQUIRE(1 == materials.size()); REQUIRE(1 == materials.size());
@@ -462,11 +519,17 @@ TEST_CASE("transmittance_filter_Kt", "[Issue95-Kt]") {
std::vector<tinyobj::shape_t> shapes; std::vector<tinyobj::shape_t> shapes;
std::vector<tinyobj::material_t> materials; std::vector<tinyobj::material_t> materials;
std::string warn;
std::string err; std::string err;
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, "../models/issue-95.obj", gMtlBasePath); bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err,
"../models/issue-95.obj", gMtlBasePath);
if (!warn.empty()) {
std::cout << "WARN: " << warn << std::endl;
}
if (!err.empty()) { if (!err.empty()) {
std::cerr << err << std::endl; std::cerr << "ERR: " << err << std::endl;
} }
REQUIRE(true == ret); REQUIRE(true == ret);
REQUIRE(1 == materials.size()); REQUIRE(1 == materials.size());
@@ -480,11 +543,17 @@ TEST_CASE("usemtl_at_last_line", "[Issue104]") {
std::vector<tinyobj::shape_t> shapes; std::vector<tinyobj::shape_t> shapes;
std::vector<tinyobj::material_t> materials; std::vector<tinyobj::material_t> materials;
std::string warn;
std::string err; std::string err;
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, "../models/usemtl-issue-104.obj", gMtlBasePath); bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err,
"../models/usemtl-issue-104.obj", gMtlBasePath);
if (!warn.empty()) {
std::cout << "WARN: " << warn << std::endl;
}
if (!err.empty()) { if (!err.empty()) {
std::cerr << err << std::endl; std::cerr << "ERR: " << err << std::endl;
} }
REQUIRE(true == ret); REQUIRE(true == ret);
REQUIRE(1 == shapes.size()); REQUIRE(1 == shapes.size());
@@ -495,11 +564,18 @@ TEST_CASE("texture_opts", "[Issue85]") {
std::vector<tinyobj::shape_t> shapes; std::vector<tinyobj::shape_t> shapes;
std::vector<tinyobj::material_t> materials; std::vector<tinyobj::material_t> materials;
std::string warn;
std::string err; std::string err;
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, "../models/texture-options-issue-85.obj", gMtlBasePath); bool ret =
tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err,
"../models/texture-options-issue-85.obj", gMtlBasePath);
if (!warn.empty()) {
std::cout << "WARN: " << warn << std::endl;
}
if (!err.empty()) { if (!err.empty()) {
std::cerr << err << std::endl; std::cerr << "ERR: " << err << std::endl;
} }
REQUIRE(true == ret); REQUIRE(true == ret);
REQUIRE(1 == shapes.size()); REQUIRE(1 == shapes.size());
@@ -525,11 +601,13 @@ TEST_CASE("texture_opts", "[Issue85]") {
REQUIRE(tinyobj::TEXTURE_TYPE_SPHERE == materials[2].diffuse_texopt.type); REQUIRE(tinyobj::TEXTURE_TYPE_SPHERE == materials[2].diffuse_texopt.type);
REQUIRE(tinyobj::TEXTURE_TYPE_CUBE_TOP == materials[2].specular_texopt.type); REQUIRE(tinyobj::TEXTURE_TYPE_CUBE_TOP == materials[2].specular_texopt.type);
REQUIRE(tinyobj::TEXTURE_TYPE_CUBE_BOTTOM == materials[2].specular_highlight_texopt.type); REQUIRE(tinyobj::TEXTURE_TYPE_CUBE_BOTTOM ==
materials[2].specular_highlight_texopt.type);
REQUIRE(tinyobj::TEXTURE_TYPE_CUBE_LEFT == materials[2].ambient_texopt.type); REQUIRE(tinyobj::TEXTURE_TYPE_CUBE_LEFT == materials[2].ambient_texopt.type);
REQUIRE(tinyobj::TEXTURE_TYPE_CUBE_RIGHT == materials[2].alpha_texopt.type); REQUIRE(tinyobj::TEXTURE_TYPE_CUBE_RIGHT == materials[2].alpha_texopt.type);
REQUIRE(tinyobj::TEXTURE_TYPE_CUBE_FRONT == materials[2].bump_texopt.type); REQUIRE(tinyobj::TEXTURE_TYPE_CUBE_FRONT == materials[2].bump_texopt.type);
REQUIRE(tinyobj::TEXTURE_TYPE_CUBE_BACK == materials[2].displacement_texopt.type); REQUIRE(tinyobj::TEXTURE_TYPE_CUBE_BACK ==
materials[2].displacement_texopt.type);
} }
TEST_CASE("mtllib_multiple_filenames", "[Issue112]") { TEST_CASE("mtllib_multiple_filenames", "[Issue112]") {
@@ -537,11 +615,18 @@ TEST_CASE("mtllib_multiple_filenames", "[Issue112]") {
std::vector<tinyobj::shape_t> shapes; std::vector<tinyobj::shape_t> shapes;
std::vector<tinyobj::material_t> materials; std::vector<tinyobj::material_t> materials;
std::string warn;
std::string err; std::string err;
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, "../models/mtllib-multiple-files-issue-112.obj", gMtlBasePath); bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err,
"../models/mtllib-multiple-files-issue-112.obj",
gMtlBasePath);
if (!warn.empty()) {
std::cout << "WARN: " << warn << std::endl;
}
if (!err.empty()) { if (!err.empty()) {
std::cerr << err << std::endl; std::cerr << "ERR: " << err << std::endl;
} }
REQUIRE(true == ret); REQUIRE(true == ret);
REQUIRE(1 == materials.size()); REQUIRE(1 == materials.size());
@@ -552,11 +637,17 @@ TEST_CASE("tr_and_d", "[Issue43]") {
std::vector<tinyobj::shape_t> shapes; std::vector<tinyobj::shape_t> shapes;
std::vector<tinyobj::material_t> materials; std::vector<tinyobj::material_t> materials;
std::string warn;
std::string err; std::string err;
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, "../models/tr-and-d-issue-43.obj", gMtlBasePath); bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err,
"../models/tr-and-d-issue-43.obj", gMtlBasePath);
if (!warn.empty()) {
std::cout << "WARN: " << warn << std::endl;
}
if (!err.empty()) { if (!err.empty()) {
std::cerr << err << std::endl; std::cerr << "ERR: " << err << std::endl;
} }
REQUIRE(true == ret); REQUIRE(true == ret);
REQUIRE(2 == materials.size()); REQUIRE(2 == materials.size());
@@ -570,11 +661,17 @@ TEST_CASE("refl", "[refl]") {
std::vector<tinyobj::shape_t> shapes; std::vector<tinyobj::shape_t> shapes;
std::vector<tinyobj::material_t> materials; std::vector<tinyobj::material_t> materials;
std::string warn;
std::string err; std::string err;
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, "../models/refl.obj", gMtlBasePath); bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err,
"../models/refl.obj", gMtlBasePath);
if (!warn.empty()) {
std::cout << "WARN: " << warn << std::endl;
}
if (!err.empty()) { if (!err.empty()) {
std::cerr << err << std::endl; std::cerr << "ERR: " << err << std::endl;
} }
PrintInfo(attrib, shapes, materials); PrintInfo(attrib, shapes, materials);
@@ -590,11 +687,17 @@ TEST_CASE("map_Bump", "[bump]") {
std::vector<tinyobj::shape_t> shapes; std::vector<tinyobj::shape_t> shapes;
std::vector<tinyobj::material_t> materials; std::vector<tinyobj::material_t> materials;
std::string warn;
std::string err; std::string err;
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, "../models/map-bump.obj", gMtlBasePath); bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err,
"../models/map-bump.obj", gMtlBasePath);
if (!warn.empty()) {
std::cout << "WARN: " << warn << std::endl;
}
if (!err.empty()) { if (!err.empty()) {
std::cerr << err << std::endl; std::cerr << "ERR: " << err << std::endl;
} }
PrintInfo(attrib, shapes, materials); PrintInfo(attrib, shapes, materials);
@@ -610,11 +713,17 @@ TEST_CASE("g_ignored", "[Issue138]") {
std::vector<tinyobj::shape_t> shapes; std::vector<tinyobj::shape_t> shapes;
std::vector<tinyobj::material_t> materials; std::vector<tinyobj::material_t> materials;
std::string warn;
std::string err; std::string err;
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, "../models/issue-138.obj", gMtlBasePath); bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err,
"../models/issue-138.obj", gMtlBasePath);
if (!warn.empty()) {
std::cout << "WARN: " << warn << std::endl;
}
if (!err.empty()) { if (!err.empty()) {
std::cerr << err << std::endl; std::cerr << "ERR: " << err << std::endl;
} }
PrintInfo(attrib, shapes, materials); PrintInfo(attrib, shapes, materials);
@@ -622,7 +731,6 @@ TEST_CASE("g_ignored", "[Issue138]") {
REQUIRE(true == ret); REQUIRE(true == ret);
REQUIRE(2 == shapes.size()); REQUIRE(2 == shapes.size());
REQUIRE(2 == materials.size()); REQUIRE(2 == materials.size());
} }
TEST_CASE("vertex-col-ext", "[Issue144]") { TEST_CASE("vertex-col-ext", "[Issue144]") {
@@ -630,15 +738,21 @@ TEST_CASE("vertex-col-ext", "[Issue144]") {
std::vector<tinyobj::shape_t> shapes; std::vector<tinyobj::shape_t> shapes;
std::vector<tinyobj::material_t> materials; std::vector<tinyobj::material_t> materials;
std::string warn;
std::string err; std::string err;
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, "../models/cube-vertexcol.obj", gMtlBasePath); bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err,
"../models/cube-vertexcol.obj", gMtlBasePath);
if (!err.empty()) { if (!warn.empty()) {
std::cerr << err << std::endl; std::cout << "WARN: " << warn << std::endl;
} }
//PrintInfo(attrib, shapes, materials); if (!err.empty()) {
std::cerr << "ERR: " << err << std::endl;
}
// PrintInfo(attrib, shapes, materials);
REQUIRE(true == ret); REQUIRE(true == ret);
REQUIRE((8 * 3) == attrib.colors.size()); REQUIRE((8 * 3) == attrib.colors.size());
@@ -663,18 +777,23 @@ TEST_CASE("norm_texopts", "[norm]") {
std::vector<tinyobj::shape_t> shapes; std::vector<tinyobj::shape_t> shapes;
std::vector<tinyobj::material_t> materials; std::vector<tinyobj::material_t> materials;
std::string warn;
std::string err; std::string err;
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, "../models/norm-texopt.obj", gMtlBasePath); bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err,
"../models/norm-texopt.obj", gMtlBasePath);
if (!warn.empty()) {
std::cout << "WARN: " << warn << std::endl;
}
if (!err.empty()) { if (!err.empty()) {
std::cerr << err << std::endl; std::cerr << "ERR: " << err << std::endl;
} }
REQUIRE(true == ret); REQUIRE(true == ret);
REQUIRE(1 == shapes.size()); REQUIRE(1 == shapes.size());
REQUIRE(1 == materials.size()); REQUIRE(1 == materials.size());
REQUIRE(3.0 == Approx(materials[0].normal_texopt.bump_multiplier)); REQUIRE(3.0 == Approx(materials[0].normal_texopt.bump_multiplier));
} }
TEST_CASE("zero-face-idx-value", "[Issue140]") { TEST_CASE("zero-face-idx-value", "[Issue140]") {
@@ -682,16 +801,21 @@ TEST_CASE("zero-face-idx-value", "[Issue140]") {
std::vector<tinyobj::shape_t> shapes; std::vector<tinyobj::shape_t> shapes;
std::vector<tinyobj::material_t> materials; std::vector<tinyobj::material_t> materials;
std::string warn;
std::string err; std::string err;
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, "../models/issue-140-zero-face-idx.obj", gMtlBasePath); bool ret =
tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err,
"../models/issue-140-zero-face-idx.obj", gMtlBasePath);
if (!warn.empty()) {
std::cout << "WARN: " << warn << std::endl;
}
if (!err.empty()) { if (!err.empty()) {
std::cerr << err << std::endl; std::cerr << "ERR: " << err << std::endl;
} }
REQUIRE(false == ret); REQUIRE(false == ret);
REQUIRE(!err.empty()); REQUIRE(!err.empty());
} }
TEST_CASE("texture-name-whitespace", "[Issue145]") { TEST_CASE("texture-name-whitespace", "[Issue145]") {
@@ -699,12 +823,18 @@ TEST_CASE("texture-name-whitespace", "[Issue145]") {
std::vector<tinyobj::shape_t> shapes; std::vector<tinyobj::shape_t> shapes;
std::vector<tinyobj::material_t> materials; std::vector<tinyobj::material_t> materials;
std::string warn;
std::string err; std::string err;
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, "../models/texture-filename-with-whitespace.obj", gMtlBasePath); bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err,
"../models/texture-filename-with-whitespace.obj",
gMtlBasePath);
if (!warn.empty()) {
std::cout << "WARN: " << warn << std::endl;
}
if (!err.empty()) { if (!err.empty()) {
std::cerr << "[Issue145] " << err << std::endl; std::cerr << "ERR: " << err << std::endl;
} }
REQUIRE(true == ret); REQUIRE(true == ret);
@@ -714,7 +844,6 @@ TEST_CASE("texture-name-whitespace", "[Issue145]") {
REQUIRE(0 == materials[0].diffuse_texname.compare("texture 01.png")); REQUIRE(0 == materials[0].diffuse_texname.compare("texture 01.png"));
REQUIRE(0 == materials[1].bump_texname.compare("bump 01.png")); REQUIRE(0 == materials[1].bump_texname.compare("bump 01.png"));
REQUIRE(2 == Approx(materials[1].bump_texopt.bump_multiplier)); REQUIRE(2 == Approx(materials[1].bump_texopt.bump_multiplier));
} }
TEST_CASE("smoothing-group", "[Issue162]") { TEST_CASE("smoothing-group", "[Issue162]") {
@@ -722,12 +851,18 @@ TEST_CASE("smoothing-group", "[Issue162]") {
std::vector<tinyobj::shape_t> shapes; std::vector<tinyobj::shape_t> shapes;
std::vector<tinyobj::material_t> materials; std::vector<tinyobj::material_t> materials;
std::string warn;
std::string err; std::string err;
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, "../models/issue-162-smoothing-group.obj", gMtlBasePath); bool ret =
tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err,
"../models/issue-162-smoothing-group.obj", gMtlBasePath);
if (!warn.empty()) {
std::cout << "WARN: " << warn << std::endl;
}
if (!err.empty()) { if (!err.empty()) {
std::cerr << "[Issue162] " << err << std::endl; std::cerr << "ERR: " << err << std::endl;
} }
REQUIRE(true == ret); REQUIRE(true == ret);
@@ -748,7 +883,6 @@ TEST_CASE("smoothing-group", "[Issue162]") {
REQUIRE(0 == shapes[1].mesh.smoothing_group_ids[7]); REQUIRE(0 == shapes[1].mesh.smoothing_group_ids[7]);
REQUIRE(6 == shapes[1].mesh.smoothing_group_ids[8]); REQUIRE(6 == shapes[1].mesh.smoothing_group_ids[8]);
REQUIRE(6 == shapes[1].mesh.smoothing_group_ids[9]); REQUIRE(6 == shapes[1].mesh.smoothing_group_ids[9]);
} }
TEST_CASE("invalid-face-definition", "[face]") { TEST_CASE("invalid-face-definition", "[face]") {
@@ -756,11 +890,18 @@ TEST_CASE("invalid-face-definition", "[face]") {
std::vector<tinyobj::shape_t> shapes; std::vector<tinyobj::shape_t> shapes;
std::vector<tinyobj::material_t> materials; std::vector<tinyobj::material_t> materials;
std::string warn;
std::string err; std::string err;
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, "../models/invalid-face-definition.obj", gMtlBasePath); bool ret =
tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err,
"../models/invalid-face-definition.obj", gMtlBasePath);
if (!warn.empty()) {
std::cout << "WARN: " << warn << std::endl;
}
if (!err.empty()) { if (!err.empty()) {
std::cerr << "[face] " << err << std::endl; std::cerr << "ERR: " << err << std::endl;
} }
REQUIRE(true == ret); REQUIRE(true == ret);
@@ -773,18 +914,24 @@ TEST_CASE("Empty mtl basedir", "[Issue177]") {
std::vector<tinyobj::shape_t> shapes; std::vector<tinyobj::shape_t> shapes;
std::vector<tinyobj::material_t> materials; std::vector<tinyobj::material_t> materials;
std::string warn;
std::string err; std::string err;
// A case where the user explicitly provides an empty string // A case where the user explicitly provides an empty string
// Win32 specific? // Win32 specific?
const char * userBaseDir = ""; const char* userBaseDir = "";
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, "issue-177.obj", userBaseDir); bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err,
"issue-177.obj", userBaseDir);
// if mtl loading fails, we get an error message (WARN) here // if mtl loading fails, we get an warning message here
ret &= err.empty(); ret &= warn.empty();
if (!warn.empty()) {
std::cout << "WARN: " << warn << std::endl;
}
if (!err.empty()) { if (!err.empty()) {
std::cerr << "[Issue177] " << err << std::endl; std::cerr << "ERR: " << err << std::endl;
} }
REQUIRE(true == ret); REQUIRE(true == ret);
@@ -795,11 +942,17 @@ TEST_CASE("line-primitive", "[line]") {
std::vector<tinyobj::shape_t> shapes; std::vector<tinyobj::shape_t> shapes;
std::vector<tinyobj::material_t> materials; std::vector<tinyobj::material_t> materials;
std::string warn;
std::string err; std::string err;
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, "../models/line-prim.obj", gMtlBasePath); bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err,
"../models/line-prim.obj", gMtlBasePath);
if (!warn.empty()) {
std::cout << "WARN: " << warn << std::endl;
}
if (!err.empty()) { if (!err.empty()) {
std::cerr << "[line] " << err << std::endl; std::cerr << "ERR: " << err << std::endl;
} }
REQUIRE(true == ret); REQUIRE(true == ret);
@@ -812,17 +965,25 @@ TEST_CASE("multiple-group-names", "[group]") {
std::vector<tinyobj::shape_t> shapes; std::vector<tinyobj::shape_t> shapes;
std::vector<tinyobj::material_t> materials; std::vector<tinyobj::material_t> materials;
std::string warn;
std::string err; std::string err;
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, "../models/cube.obj", gMtlBasePath); bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err,
"../models/cube.obj", gMtlBasePath);
if (!warn.empty()) {
std::cout << "WARN: " << warn << std::endl;
}
if (!err.empty()) { if (!err.empty()) {
std::cerr << "[group] " << err << std::endl; std::cerr << "ERR: " << err << std::endl;
} }
REQUIRE(true == ret); REQUIRE(true == ret);
REQUIRE(6 == shapes.size()); REQUIRE(6 == shapes.size());
REQUIRE(0 == shapes[0].name.compare("front cube")); REQUIRE(0 == shapes[0].name.compare("front cube"));
REQUIRE(0 == shapes[1].name.compare("back cube")); // multiple whitespaces are aggregated as single white space. REQUIRE(0 == shapes[1].name.compare("back cube")); // multiple whitespaces
// are aggregated as
// single white space.
} }
TEST_CASE("colorspace", "[Issue184]") { TEST_CASE("colorspace", "[Issue184]") {
@@ -830,12 +991,20 @@ TEST_CASE("colorspace", "[Issue184]") {
std::vector<tinyobj::shape_t> shapes; std::vector<tinyobj::shape_t> shapes;
std::vector<tinyobj::material_t> materials; std::vector<tinyobj::material_t> materials;
std::string warn;
std::string err; std::string err;
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, "../models/colorspace-issue-184.obj", gMtlBasePath); bool ret =
tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err,
"../models/colorspace-issue-184.obj", gMtlBasePath);
if (!warn.empty()) {
std::cout << "WARN: " << warn << std::endl;
}
if (!err.empty()) { if (!err.empty()) {
std::cerr << err << std::endl; std::cerr << "ERR: " << err << std::endl;
} }
REQUIRE(true == ret); REQUIRE(true == ret);
REQUIRE(1 == shapes.size()); REQUIRE(1 == shapes.size());
REQUIRE(1 == materials.size()); REQUIRE(1 == materials.size());
@@ -846,7 +1015,8 @@ TEST_CASE("colorspace", "[Issue184]") {
// Fuzzer test. // Fuzzer test.
// Just check if it does not crash. // Just check if it does not crash.
// Disable by default since Windows filesystem can't create filename of afl testdata // Disable by default since Windows filesystem can't create filename of afl
// testdata
#if 0 #if 0
TEST_CASE("afl000000", "[AFL]") { TEST_CASE("afl000000", "[AFL]") {

View File

@@ -23,6 +23,7 @@ THE SOFTWARE.
*/ */
// //
// version 1.3.0 : Separate warning and error message(breaking API of LoadObj)
// version 1.2.3 : Added color space extension('-colorspace') to tex opts. // version 1.2.3 : Added color space extension('-colorspace') to tex opts.
// version 1.2.2 : Parse multiple group names. // version 1.2.2 : Parse multiple group names.
// version 1.2.1 : Added initial support for line('l') primitive(PR #178) // version 1.2.1 : Added initial support for line('l') primitive(PR #178)
@@ -117,7 +118,8 @@ namespace tinyobj {
// //
// TinyObjLoader extension. // TinyObjLoader extension.
// //
// -colorspace SPACE # Color space of the texture. e.g. 'sRGB` or 'linear' // -colorspace SPACE # Color space of the texture. e.g.
// 'sRGB` or 'linear'
// //
#ifdef TINYOBJLOADER_USE_DOUBLE #ifdef TINYOBJLOADER_USE_DOUBLE
@@ -155,7 +157,8 @@ typedef struct {
real_t bump_multiplier; // -bm (for bump maps only, default 1.0) real_t bump_multiplier; // -bm (for bump maps only, default 1.0)
// extension // extension
std::string colorspace; // Explicitly specify color space of stored value. Usually `sRGB` or `linear` (default empty). std::string colorspace; // Explicitly specify color space of stored value.
// Usually `sRGB` or `linear` (default empty).
} texture_option_t; } texture_option_t;
typedef struct { typedef struct {
@@ -307,7 +310,7 @@ class MaterialReader {
virtual bool operator()(const std::string &matId, virtual bool operator()(const std::string &matId,
std::vector<material_t> *materials, std::vector<material_t> *materials,
std::map<std::string, int> *matMap, std::map<std::string, int> *matMap, std::string *warn,
std::string *err) = 0; std::string *err) = 0;
}; };
@@ -318,7 +321,8 @@ class MaterialFileReader : public MaterialReader {
virtual ~MaterialFileReader() {} virtual ~MaterialFileReader() {}
virtual bool operator()(const std::string &matId, virtual bool operator()(const std::string &matId,
std::vector<material_t> *materials, std::vector<material_t> *materials,
std::map<std::string, int> *matMap, std::string *err); std::map<std::string, int> *matMap, std::string *warn,
std::string *err);
private: private:
std::string m_mtlBaseDir; std::string m_mtlBaseDir;
@@ -331,7 +335,8 @@ class MaterialStreamReader : public MaterialReader {
virtual ~MaterialStreamReader() {} virtual ~MaterialStreamReader() {}
virtual bool operator()(const std::string &matId, virtual bool operator()(const std::string &matId,
std::vector<material_t> *materials, std::vector<material_t> *materials,
std::map<std::string, int> *matMap, std::string *err); std::map<std::string, int> *matMap, std::string *warn,
std::string *err);
private: private:
std::istream &m_inStream; std::istream &m_inStream;
@@ -341,7 +346,7 @@ class MaterialStreamReader : public MaterialReader {
/// 'attrib', 'shapes' and 'materials' will be filled with parsed shape data /// 'attrib', 'shapes' and 'materials' will be filled with parsed shape data
/// 'shapes' will be filled with parsed shape data /// 'shapes' will be filled with parsed shape data
/// Returns true when loading .obj become success. /// Returns true when loading .obj become success.
/// Returns warning and error message into `err` /// Returns warning message into `warn`, and error message into `err`
/// 'mtl_basedir' is optional, and used for base directory for .mtl file. /// 'mtl_basedir' is optional, and used for base directory for .mtl file.
/// In default(`NULL'), .mtl file is searched from an application's working /// In default(`NULL'), .mtl file is searched from an application's working
/// directory. /// directory.
@@ -350,34 +355,36 @@ class MaterialStreamReader : public MaterialReader {
/// Option 'default_vcols_fallback' specifies whether vertex colors should /// Option 'default_vcols_fallback' specifies whether vertex colors should
/// always be defined, even if no colors are given (fallback to white). /// always be defined, even if no colors are given (fallback to white).
bool LoadObj(attrib_t *attrib, std::vector<shape_t> *shapes, bool LoadObj(attrib_t *attrib, std::vector<shape_t> *shapes,
std::vector<material_t> *materials, std::string *err, std::vector<material_t> *materials, std::string *warn,
const char *filename, const char *mtl_basedir = NULL, std::string *err, const char *filename,
bool triangulate = true, bool default_vcols_fallback = true); const char *mtl_basedir = NULL, bool triangulate = true,
bool default_vcols_fallback = true);
/// Loads .obj from a file with custom user callback. /// Loads .obj from a file with custom user callback.
/// .mtl is loaded as usual and parsed material_t data will be passed to /// .mtl is loaded as usual and parsed material_t data will be passed to
/// `callback.mtllib_cb`. /// `callback.mtllib_cb`.
/// Returns true when loading .obj/.mtl become success. /// Returns true when loading .obj/.mtl become success.
/// Returns warning and error message into `err` /// Returns warning message into `warn`, and error message into `err`
/// See `examples/callback_api/` for how to use this function. /// See `examples/callback_api/` for how to use this function.
bool LoadObjWithCallback(std::istream &inStream, const callback_t &callback, bool LoadObjWithCallback(std::istream &inStream, const callback_t &callback,
void *user_data = NULL, void *user_data = NULL,
MaterialReader *readMatFn = NULL, MaterialReader *readMatFn = NULL,
std::string *err = NULL); std::string *warn = NULL, std::string *err = NULL);
/// Loads object from a std::istream, uses GetMtlIStreamFn to retrieve /// Loads object from a std::istream, uses GetMtlIStreamFn to retrieve
/// std::istream for materials. /// std::istream for materials.
/// Returns true when loading .obj become success. /// Returns true when loading .obj become success.
/// Returns warning and error message into `err` /// Returns warning and error message into `err`
bool LoadObj(attrib_t *attrib, std::vector<shape_t> *shapes, bool LoadObj(attrib_t *attrib, std::vector<shape_t> *shapes,
std::vector<material_t> *materials, std::string *err, std::vector<material_t> *materials, std::string *warn,
std::istream *inStream, MaterialReader *readMatFn = NULL, std::string *err, std::istream *inStream,
bool triangulate = true, bool default_vcols_fallback = true); MaterialReader *readMatFn = NULL, bool triangulate = true,
bool default_vcols_fallback = true);
/// Loads materials into std::map /// Loads materials into std::map
void LoadMtl(std::map<std::string, int> *material_map, void LoadMtl(std::map<std::string, int> *material_map,
std::vector<material_t> *materials, std::istream *inStream, std::vector<material_t> *materials, std::istream *inStream,
std::string *warning); std::string *warning, std::string *err);
} // namespace tinyobj } // namespace tinyobj
@@ -721,7 +728,8 @@ static inline bool parseVertexWithColor(real_t *x, real_t *y, real_t *z,
(*y) = parseReal(token, default_y); (*y) = parseReal(token, default_y);
(*z) = parseReal(token, default_z); (*z) = parseReal(token, default_z);
const bool found_color = parseReal(token, r) && parseReal(token, g) && parseReal(token, b); const bool found_color =
parseReal(token, r) && parseReal(token, g) && parseReal(token, b);
if (!found_color) { if (!found_color) {
(*r) = (*g) = (*b) = 1.0; (*r) = (*g) = (*b) = 1.0;
@@ -959,11 +967,12 @@ static bool ParseTextureNameAndOption(std::string *texname,
} else if ((0 == strncmp(token, "-mm", 3)) && IS_SPACE((token[3]))) { } else if ((0 == strncmp(token, "-mm", 3)) && IS_SPACE((token[3]))) {
token += 4; token += 4;
parseReal2(&(texopt->brightness), &(texopt->contrast), &token, 0.0, 1.0); parseReal2(&(texopt->brightness), &(texopt->contrast), &token, 0.0, 1.0);
} else if ((0 == strncmp(token, "-colorspace", 11)) && IS_SPACE((token[11]))) { } else if ((0 == strncmp(token, "-colorspace", 11)) &&
IS_SPACE((token[11]))) {
token += 12; token += 12;
texopt->colorspace = parseString(&token); texopt->colorspace = parseString(&token);
} else { } else {
// Assume texture filename // Assume texture filename
#if 0 #if 0
size_t len = strcspn(token, " \t\r"); // untile next space size_t len = strcspn(token, " \t\r"); // untile next space
texture_name = std::string(token, token + len); texture_name = std::string(token, token + len);
@@ -1303,7 +1312,9 @@ static void SplitString(const std::string &s, char delim,
void LoadMtl(std::map<std::string, int> *material_map, void LoadMtl(std::map<std::string, int> *material_map,
std::vector<material_t> *materials, std::istream *inStream, std::vector<material_t> *materials, std::istream *inStream,
std::string *warning) { std::string *warning, std::string *err) {
(void)err;
// Create a default material anyway. // Create a default material anyway.
material_t material; material_t material;
InitMaterial(&material); InitMaterial(&material);
@@ -1312,7 +1323,7 @@ void LoadMtl(std::map<std::string, int> *material_map,
bool has_d = false; bool has_d = false;
bool has_tr = false; bool has_tr = false;
std::stringstream ss; std::stringstream warn_ss;
std::string linebuf; std::string linebuf;
while (inStream->peek() != -1) { while (inStream->peek() != -1) {
@@ -1455,7 +1466,7 @@ void LoadMtl(std::map<std::string, int> *material_map,
material.dissolve = parseReal(&token); material.dissolve = parseReal(&token);
if (has_tr) { if (has_tr) {
ss << "WARN: Both `d` and `Tr` parameters defined for \"" warn_ss << "Both `d` and `Tr` parameters defined for \""
<< material.name << "\". Use the value of `d` for dissolve." << material.name << "\". Use the value of `d` for dissolve."
<< std::endl; << std::endl;
} }
@@ -1466,7 +1477,7 @@ void LoadMtl(std::map<std::string, int> *material_map,
token += 2; token += 2;
if (has_d) { if (has_d) {
// `d` wins. Ignore `Tr` value. // `d` wins. Ignore `Tr` value.
ss << "WARN: Both `d` and `Tr` parameters defined for \"" warn_ss << "Both `d` and `Tr` parameters defined for \""
<< material.name << "\". Use the value of `d` for dissolve." << material.name << "\". Use the value of `d` for dissolve."
<< std::endl; << std::endl;
} else { } else {
@@ -1683,14 +1694,14 @@ void LoadMtl(std::map<std::string, int> *material_map,
materials->push_back(material); materials->push_back(material);
if (warning) { if (warning) {
(*warning) = ss.str(); (*warning) = warn_ss.str();
} }
} }
bool MaterialFileReader::operator()(const std::string &matId, bool MaterialFileReader::operator()(const std::string &matId,
std::vector<material_t> *materials, std::vector<material_t> *materials,
std::map<std::string, int> *matMap, std::map<std::string, int> *matMap,
std::string *err) { std::string *warn, std::string *err) {
std::string filepath; std::string filepath;
if (!m_mtlBaseDir.empty()) { if (!m_mtlBaseDir.empty()) {
@@ -1702,21 +1713,14 @@ bool MaterialFileReader::operator()(const std::string &matId,
std::ifstream matIStream(filepath.c_str()); std::ifstream matIStream(filepath.c_str());
if (!matIStream) { if (!matIStream) {
std::stringstream ss; std::stringstream ss;
ss << "WARN: Material file [ " << filepath << " ] not found." << std::endl; ss << "Material file [ " << filepath << " ] not found." << std::endl;
if (err) { if (warn) {
(*err) += ss.str(); (*warn) += ss.str();
} }
return false; return false;
} }
std::string warning; LoadMtl(matMap, materials, &matIStream, warn, err);
LoadMtl(matMap, materials, &matIStream, &warning);
if (!warning.empty()) {
if (err) {
(*err) += warning;
}
}
return true; return true;
} }
@@ -1724,32 +1728,26 @@ bool MaterialFileReader::operator()(const std::string &matId,
bool MaterialStreamReader::operator()(const std::string &matId, bool MaterialStreamReader::operator()(const std::string &matId,
std::vector<material_t> *materials, std::vector<material_t> *materials,
std::map<std::string, int> *matMap, std::map<std::string, int> *matMap,
std::string *err) { std::string *warn, std::string *err) {
(void)err;
(void)matId; (void)matId;
if (!m_inStream) { if (!m_inStream) {
std::stringstream ss; std::stringstream ss;
ss << "WARN: Material stream in error state. " << std::endl; ss << "Material stream in error state. " << std::endl;
if (err) { if (warn) {
(*err) += ss.str(); (*warn) += ss.str();
} }
return false; return false;
} }
std::string warning; LoadMtl(matMap, materials, &m_inStream, warn, err);
LoadMtl(matMap, materials, &m_inStream, &warning);
if (!warning.empty()) {
if (err) {
(*err) += warning;
}
}
return true; return true;
} }
bool LoadObj(attrib_t *attrib, std::vector<shape_t> *shapes, bool LoadObj(attrib_t *attrib, std::vector<shape_t> *shapes,
std::vector<material_t> *materials, std::string *err, std::vector<material_t> *materials, std::string *warn,
const char *filename, const char *mtl_basedir, std::string *err, const char *filename, const char *mtl_basedir,
bool trianglulate, bool default_vcols_fallback) { bool trianglulate, bool default_vcols_fallback) {
attrib->vertices.clear(); attrib->vertices.clear();
attrib->normals.clear(); attrib->normals.clear();
@@ -1779,14 +1777,15 @@ bool LoadObj(attrib_t *attrib, std::vector<shape_t> *shapes,
} }
MaterialFileReader matFileReader(baseDir); MaterialFileReader matFileReader(baseDir);
return LoadObj(attrib, shapes, materials, err, &ifs, &matFileReader, return LoadObj(attrib, shapes, materials, warn, err, &ifs, &matFileReader,
trianglulate, default_vcols_fallback); trianglulate, default_vcols_fallback);
} }
bool LoadObj(attrib_t *attrib, std::vector<shape_t> *shapes, bool LoadObj(attrib_t *attrib, std::vector<shape_t> *shapes,
std::vector<material_t> *materials, std::string *err, std::vector<material_t> *materials, std::string *warn,
std::istream *inStream, MaterialReader *readMatFn /*= NULL*/, std::string *err, std::istream *inStream,
bool triangulate, bool default_vcols_fallback) { MaterialReader *readMatFn /*= NULL*/, bool triangulate,
bool default_vcols_fallback) {
std::stringstream errss; std::stringstream errss;
std::vector<real_t> v; std::vector<real_t> v;
@@ -1936,8 +1935,10 @@ bool LoadObj(attrib_t *attrib, std::vector<shape_t> *shapes,
} }
greatest_v_idx = greatest_v_idx > vi.v_idx ? greatest_v_idx : vi.v_idx; greatest_v_idx = greatest_v_idx > vi.v_idx ? greatest_v_idx : vi.v_idx;
greatest_vn_idx = greatest_vn_idx > vi.vn_idx ? greatest_vn_idx : vi.vn_idx; greatest_vn_idx =
greatest_vt_idx = greatest_vt_idx > vi.vt_idx ? greatest_vt_idx : vi.vt_idx; greatest_vn_idx > vi.vn_idx ? greatest_vn_idx : vi.vn_idx;
greatest_vt_idx =
greatest_vt_idx > vi.vt_idx ? greatest_vt_idx : vi.vt_idx;
face.vertex_indices.push_back(vi); face.vertex_indices.push_back(vi);
size_t n = strspn(token, " \t\r"); size_t n = strspn(token, " \t\r");
@@ -1986,19 +1987,24 @@ bool LoadObj(attrib_t *attrib, std::vector<shape_t> *shapes,
SplitString(std::string(token), ' ', filenames); SplitString(std::string(token), ' ', filenames);
if (filenames.empty()) { if (filenames.empty()) {
if (err) { if (warn) {
(*err) += (*warn) +=
"WARN: Looks like empty filename for mtllib. Use default " "Looks like empty filename for mtllib. Use default "
"material. \n"; "material. \n";
} }
} else { } else {
bool found = false; bool found = false;
for (size_t s = 0; s < filenames.size(); s++) { for (size_t s = 0; s < filenames.size(); s++) {
std::string warn_mtl;
std::string err_mtl; std::string err_mtl;
bool ok = (*readMatFn)(filenames[s].c_str(), materials, bool ok = (*readMatFn)(filenames[s].c_str(), materials,
&material_map, &err_mtl); &material_map, &warn_mtl, &err_mtl);
if (warn && (!warn_mtl.empty())) {
(*warn) += warn_mtl;
}
if (err && (!err_mtl.empty())) { if (err && (!err_mtl.empty())) {
(*err) += err_mtl; // This should be warn message. (*err) += err_mtl;
} }
if (ok) { if (ok) {
@@ -2008,9 +2014,9 @@ bool LoadObj(attrib_t *attrib, std::vector<shape_t> *shapes,
} }
if (!found) { if (!found) {
if (err) { if (warn) {
(*err) += (*warn) +=
"WARN: Failed to load material file(s). Use default " "Failed to load material file(s). Use default "
"material.\n"; "material.\n";
} }
} }
@@ -2048,14 +2054,13 @@ bool LoadObj(attrib_t *attrib, std::vector<shape_t> *shapes,
if (names.size() < 2) { if (names.size() < 2) {
// 'g' with empty names // 'g' with empty names
if (err) { if (warn) {
std::stringstream ss; std::stringstream ss;
ss << "WARN: Empty group name. line: " << line_num << "\n"; ss << "Empty group name. line: " << line_num << "\n";
(*err) += ss.str(); (*warn) += ss.str();
name = ""; name = "";
} }
} else { } else {
std::stringstream ss; std::stringstream ss;
ss << names[1]; ss << names[1];
@@ -2068,7 +2073,6 @@ bool LoadObj(attrib_t *attrib, std::vector<shape_t> *shapes,
} }
name = ss.str(); name = ss.str();
} }
continue; continue;
@@ -2190,28 +2194,25 @@ bool LoadObj(attrib_t *attrib, std::vector<shape_t> *shapes,
vc.clear(); vc.clear();
} }
if (greatest_v_idx >= static_cast<int>(v.size() / 3)) if (greatest_v_idx >= static_cast<int>(v.size() / 3)) {
{ if (warn) {
if (err) {
std::stringstream ss; std::stringstream ss;
ss << "WARN: Vertex indices out of bounds.\n" << std::endl; ss << "Vertex indices out of bounds.\n" << std::endl;
(*err) += ss.str(); (*warn) += ss.str();
} }
} }
if (greatest_vn_idx >= static_cast<int>(vn.size() / 3)) if (greatest_vn_idx >= static_cast<int>(vn.size() / 3)) {
{ if (warn) {
if (err) {
std::stringstream ss; std::stringstream ss;
ss << "WARN: Vertex normal indices out of bounds.\n" << std::endl; ss << "Vertex normal indices out of bounds.\n" << std::endl;
(*err) += ss.str(); (*warn) += ss.str();
} }
} }
if (greatest_vt_idx >= static_cast<int>(vt.size() / 2)) if (greatest_vt_idx >= static_cast<int>(vt.size() / 2)) {
{ if (warn) {
if (err) {
std::stringstream ss; std::stringstream ss;
ss << "WARN: Vertex texcoord indices out of bounds.\n" << std::endl; ss << "Vertex texcoord indices out of bounds.\n" << std::endl;
(*err) += ss.str(); (*warn) += ss.str();
} }
} }
@@ -2241,6 +2242,7 @@ bool LoadObj(attrib_t *attrib, std::vector<shape_t> *shapes,
bool LoadObjWithCallback(std::istream &inStream, const callback_t &callback, bool LoadObjWithCallback(std::istream &inStream, const callback_t &callback,
void *user_data /*= NULL*/, void *user_data /*= NULL*/,
MaterialReader *readMatFn /*= NULL*/, MaterialReader *readMatFn /*= NULL*/,
std::string *warn, /* = NULL*/
std::string *err /*= NULL*/) { std::string *err /*= NULL*/) {
std::stringstream errss; std::stringstream errss;
@@ -2377,19 +2379,25 @@ bool LoadObjWithCallback(std::istream &inStream, const callback_t &callback,
SplitString(std::string(token), ' ', filenames); SplitString(std::string(token), ' ', filenames);
if (filenames.empty()) { if (filenames.empty()) {
if (err) { if (warn) {
(*err) += (*warn) +=
"WARN: Looks like empty filename for mtllib. Use default " "Looks like empty filename for mtllib. Use default "
"material. \n"; "material. \n";
} }
} else { } else {
bool found = false; bool found = false;
for (size_t s = 0; s < filenames.size(); s++) { for (size_t s = 0; s < filenames.size(); s++) {
std::string warn_mtl;
std::string err_mtl; std::string err_mtl;
bool ok = (*readMatFn)(filenames[s].c_str(), &materials, bool ok = (*readMatFn)(filenames[s].c_str(), &materials,
&material_map, &err_mtl); &material_map, &warn_mtl, &err_mtl);
if (warn && (!warn_mtl.empty())) {
(*warn) += warn_mtl; // This should be warn message.
}
if (err && (!err_mtl.empty())) { if (err && (!err_mtl.empty())) {
(*err) += err_mtl; // This should be warn message. (*err) += err_mtl;
} }
if (ok) { if (ok) {
@@ -2399,9 +2407,9 @@ bool LoadObjWithCallback(std::istream &inStream, const callback_t &callback,
} }
if (!found) { if (!found) {
if (err) { if (warn) {
(*err) += (*warn) +=
"WARN: Failed to load material file(s). Use default " "Failed to load material file(s). Use default "
"material.\n"; "material.\n";
} }
} else { } else {