5 Commits

Author SHA1 Message Date
Syoyo Fujita
1e2cd27c64 Fix build on MSC + CXX11 codepath. 2017-12-28 18:05:00 +09:00
Syoyo Fujita
49726abcf1 Introduce TINYOBJ_USE_CXX11 to suppress clang 5.0's -Wzero-as-null-pointer-constatnt warninig. 2017-12-24 18:40:49 +09:00
Syoyo Fujita
b29d34f2e1 Merge pull request #154 from ogrex/master
Bugfix for multiple texture
2017-12-21 01:23:03 +09:00
ogrex
cb085d1fb6 fix multiple texture wrong id problem 2017-12-20 22:17:19 +09:00
ogrex
b38e97b7ec trimming '\r' in material_file name 2017-12-20 22:13:58 +09:00
3 changed files with 82 additions and 19 deletions

View File

@@ -1440,7 +1440,9 @@ bool parseObj(attrib_t *attrib, std::vector<shape_t> *shapes,
// std::cout << "mtllib :" << material_filename << std::endl; // std::cout << "mtllib :" << material_filename << std::endl;
auto t1 = std::chrono::high_resolution_clock::now(); auto t1 = std::chrono::high_resolution_clock::now();
if (material_filename.back() == '\r') {
material_filename.pop_back();
}
std::ifstream ifs(material_filename); std::ifstream ifs(material_filename);
if (ifs.good()) { if (ifs.good()) {
LoadMtl(&material_map, materials, &ifs); LoadMtl(&material_map, materials, &ifs);
@@ -1516,13 +1518,13 @@ bool parseObj(attrib_t *attrib, std::vector<shape_t> *shapes,
StackVector<std::thread, 16> workers; StackVector<std::thread, 16> workers;
for (size_t t = 0; t < num_threads; t++) { for (size_t t = 0; t < num_threads; t++) {
int material_id = -1; // -1 = default unknown material.
workers->push_back(std::thread([&, t]() { workers->push_back(std::thread([&, t]() {
size_t v_count = v_offsets[t]; size_t v_count = v_offsets[t];
size_t n_count = n_offsets[t]; size_t n_count = n_offsets[t];
size_t t_count = t_offsets[t]; size_t t_count = t_offsets[t];
size_t f_count = f_offsets[t]; size_t f_count = f_offsets[t];
size_t face_count = face_offsets[t]; size_t face_count = face_offsets[t];
int material_id = -1; // -1 = default unknown material.
for (size_t i = 0; i < commands[t].size(); i++) { for (size_t i = 0; i < commands[t].size(); i++) {
if (commands[t][i].type == COMMAND_EMPTY) { if (commands[t][i].type == COMMAND_EMPTY) {
@@ -1579,7 +1581,19 @@ bool parseObj(attrib_t *attrib, std::vector<shape_t> *shapes,
for (size_t t = 0; t < workers->size(); t++) { for (size_t t = 0; t < workers->size(); t++) {
workers[t].join(); workers[t].join();
} }
if(material_map.size()>1&& num_threads>1) {
for (size_t t = 0; t < num_threads; t++) {
size_t face_count = face_offsets[t];
if (-1 == attrib->material_ids[face_count]) {
int prev_material_id = attrib->material_ids[face_count - 1];
size_t max_face_offset = (t == num_threads - 1) ? attrib->material_ids.size() : face_offsets[t + 1];
for (int i = face_count; i<max_face_offset; ++i) {
if (attrib->material_ids[i] != -1) break;
attrib->material_ids[i] = prev_material_id;
}
}
}
}
auto t_end = std::chrono::high_resolution_clock::now(); auto t_end = std::chrono::high_resolution_clock::now();
ms_merge = t_end - t_start; ms_merge = t_end - t_start;
} }

View File

@@ -29,6 +29,13 @@ extern "C" {
#endif #endif
#endif #endif
#ifdef TINYOBJ_USE_CXX11
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wc++98-compat"
#endif
#endif
class timerutil { class timerutil {
public: public:
#ifdef _WIN32 #ifdef _WIN32
@@ -63,7 +70,7 @@ class timerutil {
} }
time_t current() { time_t current() {
struct timeval t; struct timeval t;
gettimeofday(&t, NULL); gettimeofday(&t, tobj_null);
return static_cast<time_t>(t.tv_sec * 1000 + t.tv_usec); return static_cast<time_t>(t.tv_sec * 1000 + t.tv_usec);
} }
@@ -261,7 +268,7 @@ static void PrintInfo(const tinyobj::attrib_t& attrib,
} }
} }
static bool TestLoadObj(const char* filename, const char* basepath = NULL, static bool TestLoadObj(const char* filename, const char* basepath = tobj_null,
bool triangulate = true) { bool triangulate = true) {
std::cout << "Loading " << filename << std::endl; std::cout << "Loading " << filename << std::endl;

View File

@@ -51,6 +51,32 @@ THE SOFTWARE.
namespace tinyobj { namespace tinyobj {
//
// Please define `TINYOBJ_USE_CXX11` flag when you compile tinyobjloader with C++11 compiler.
//
#ifdef TINYOBJ_USE_CXX11
#if defined(_MSC_VER)
# if _MSC_VER < 1800
# error This project needs at least Visual Studio 2013
# endif
#define tobj_null nullptr
#elif __cplusplus <= 199711L
# error This project can only be compiled with a compiler that supports C++11
#else
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wc++98-compat"
#endif
#define tobj_null nullptr
#endif
#else // !TINYOBJ_USE_CXX11
#define tobj_null NULL
#endif
// https://en.wikipedia.org/wiki/Wavefront_.obj_file says ... // https://en.wikipedia.org/wiki/Wavefront_.obj_file says ...
// //
// -blendu on | off # set horizontal texture blending // -blendu on | off # set horizontal texture blending
@@ -259,14 +285,14 @@ typedef struct callback_t_ {
void (*object_cb)(void *user_data, const char *name); void (*object_cb)(void *user_data, const char *name);
callback_t_() callback_t_()
: vertex_cb(NULL), : vertex_cb(tobj_null),
normal_cb(NULL), normal_cb(tobj_null),
texcoord_cb(NULL), texcoord_cb(tobj_null),
index_cb(NULL), index_cb(tobj_null),
usemtl_cb(NULL), usemtl_cb(tobj_null),
mtllib_cb(NULL), mtllib_cb(tobj_null),
group_cb(NULL), group_cb(tobj_null),
object_cb(NULL) {} object_cb(tobj_null) {}
} callback_t; } callback_t;
class MaterialReader { class MaterialReader {
@@ -318,7 +344,7 @@ class MaterialStreamReader : public MaterialReader {
/// or not. /// or not.
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 *err,
const char *filename, const char *mtl_basedir = NULL, const char *filename, const char *mtl_basedir = tobj_null,
bool triangulate = true); bool triangulate = true);
/// Loads .obj from a file with custom user callback. /// Loads .obj from a file with custom user callback.
@@ -328,9 +354,9 @@ bool LoadObj(attrib_t *attrib, std::vector<shape_t> *shapes,
/// Returns warning and error message into `err` /// Returns warning 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 = tobj_null,
MaterialReader *readMatFn = NULL, MaterialReader *readMatFn = tobj_null,
std::string *err = NULL); std::string *err = tobj_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.
@@ -338,7 +364,7 @@ bool LoadObjWithCallback(std::istream &inStream, const callback_t &callback,
/// 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 *err,
std::istream *inStream, MaterialReader *readMatFn = NULL, std::istream *inStream, MaterialReader *readMatFn = tobj_null,
bool triangulate = true); bool triangulate = true);
/// Loads materials into std::map /// Loads materials into std::map
@@ -348,6 +374,10 @@ void LoadMtl(std::map<std::string, int> *material_map,
} // namespace tinyobj } // namespace tinyobj
#ifdef __clang__
#pragma clang diagnostic pop
#endif
#endif // TINY_OBJ_LOADER_H_ #endif // TINY_OBJ_LOADER_H_
#ifdef TINYOBJLOADER_IMPLEMENTATION #ifdef TINYOBJLOADER_IMPLEMENTATION
@@ -364,6 +394,13 @@ void LoadMtl(std::map<std::string, int> *material_map,
namespace tinyobj { namespace tinyobj {
#ifdef TINYOBJ_USE_CXX11
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wc++98-compat"
#endif
#endif
MaterialReader::~MaterialReader() {} MaterialReader::~MaterialReader() {}
struct vertex_index { struct vertex_index {
@@ -2030,7 +2067,7 @@ bool LoadObjWithCallback(std::istream &inStream, const callback_t &callback,
static_cast<int>(names_out.size())); static_cast<int>(names_out.size()));
} else { } else {
callback.group_cb(user_data, NULL, 0); callback.group_cb(user_data, tobj_null, 0);
} }
} }
@@ -2100,6 +2137,11 @@ bool LoadObjWithCallback(std::istream &inStream, const callback_t &callback,
return true; return true;
} }
#ifdef __clang__
#pragma clang diagnostic pop
#endif
} // namespace tinyobj } // namespace tinyobj
#endif #endif