9 Commits

Author SHA1 Message Date
Syoyo Fujita
160d6be10f Format code. 2017-12-12 17:31:22 +09:00
Syoyo Fujita
583590767e Fix compilation.
Remove unused file.
2017-12-12 17:30:59 +09:00
tigrazone
dc542d6638 comment for export data and functions block with hashed keywords to other projects 2017-12-10 10:48:05 +02:00
tigrazone
c5976f931b comment for export data and functions block with hashed keywords to other projects 2017-12-10 10:47:32 +02:00
tigrazone
0511658e86 comment for export data and functions block with hashed keywords to other projects 2017-12-10 10:46:35 +02:00
tigrazone
54851f8ac7 comment for export data and functions block with hashed keywords to other projects 2017-12-10 10:46:08 +02:00
tigrazone
a6c1d07560 comment for export data and functions block with hashed keywords to other projects 2017-12-10 10:43:24 +02:00
tigrazone
4fe479453b smallish optimize 2017-12-10 09:34:10 +02:00
tigrazone
af1bcf1e11 errors fix 2017-12-10 00:43:50 +02:00
2 changed files with 262 additions and 244 deletions

View File

@@ -303,7 +303,8 @@ std::string matStream(
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<unsigned int, int>* matMap, //std::map<std::string, int>* matMap,
std::map<uint32_t, int>* matMap,
std::string* err) std::string* err)
{ {
(void)matId; (void)matId;

View File

@@ -290,7 +290,8 @@ class MaterialFileReader : public 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::map<unsigned int, int> *matMap, std::string *err); std::map<unsigned int, int> *matMap,
std::string *err);
private: private:
std::string m_mtlBaseDir; std::string m_mtlBaseDir;
@@ -304,7 +305,8 @@ class MaterialStreamReader : public 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::map<unsigned int, int> *matMap, std::string *err); std::map<unsigned int, int> *matMap,
std::string *err);
private: private:
std::istream &m_inStream; std::istream &m_inStream;
@@ -348,8 +350,9 @@ bool LoadObj(attrib_t *attrib, std::vector<shape_t> *shapes,
/// Loads materials into std::map /// Loads materials into std::map
void LoadMtl( void LoadMtl(
// std::map<std::string, int> *material_map, // std::map<std::string, int> *material_map,
std::map<unsigned int, int> *material_map, std::vector<material_t> *materials, std::map<unsigned int, int> *material_map,
std::istream *inStream, std::string *warning); std::vector<material_t> *materials, std::istream *inStream,
std::string *warning);
} // namespace tinyobj } // namespace tinyobj
@@ -367,10 +370,9 @@ void LoadMtl(
#include <fstream> #include <fstream>
#include <sstream> #include <sstream>
// #define TINYOBJLOADER_IMPLEMENTATION_BUFREAD //#define TINYOBJLOADER_IMPLEMENTATION_BUFREAD
#ifdef TINYOBJLOADER_IMPLEMENTATION_BUFREAD #ifdef TINYOBJLOADER_IMPLEMENTATION_BUFREAD
// TODO(syoyo): Support non-win32 system
#include <fcntl.h> #include <fcntl.h>
#include <io.h> #include <io.h>
#include <sys/stat.h> #include <sys/stat.h>
@@ -442,11 +444,14 @@ class ImportInBuf : public std::streambuf {
char *buffer_; char *buffer_;
int fd_; int fd_;
unsigned long long fsize_, pos_; uint64_t fsize_, pos_;
size_t buf_sz; size_t buf_sz;
}; };
#endif #endif
//***************************************************
//*************** tigro keywords hash functions begin
// tigra: x31 hash function // tigra: x31 hash function
typedef unsigned int khint_t; typedef unsigned int khint_t;
@@ -461,7 +466,8 @@ inline unsigned int X31_hash_stringSZ(const char *s, int sz) {
int i; int i;
khint_t h = static_cast<khint_t>(*s); khint_t h = static_cast<khint_t>(*s);
for (++s, i = sz - 1; i && *s; ++s, i--) h = (h << 5) - h + static_cast<khint_t>(*s); for (++s, i = sz - 1; i && *s; ++s, i--)
h = (h << 5) - h + static_cast<khint_t>(*s);
return h; return h;
} }
@@ -579,10 +585,12 @@ enum tokens_enum {
TOK_norm TOK_norm
}; };
// TODO(syoyo): Do not define in global scope.
#ifdef __clang__ #ifdef __clang__
#pragma clang diagnostic push #pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wexit-time-destructors"
#pragma clang diagnostic ignored "-Wglobal-constructors" #pragma clang diagnostic ignored "-Wglobal-constructors"
#pragma clang diagnostic ignored "-Wexit-time-destructors"
#endif #endif
static std::map<unsigned int, int> hashed_toks; static std::map<unsigned int, int> hashed_toks;
@@ -591,7 +599,7 @@ static std::map<unsigned int, int> hashed_toks;
#pragma clang diagnostic pop #pragma clang diagnostic pop
#endif #endif
// functions!
static void initHashedTokensMap() { static void initHashedTokensMap() {
// init hashed tokens map // init hashed tokens map
@@ -607,19 +615,22 @@ static void initHashedTokensMap() {
// init hashed tokens map END // init hashed tokens map END
} }
// search token in keywords hash map
static int token2tok(const char *token) { static int token2tok(const char *token) {
unsigned int token_sz, a_hash; unsigned int token_sz, a_hash;
int a_tok; int a_tok;
token_sz = static_cast<unsigned int>(strpbrk(token, " \t\r") - token); // token length token_sz = static_cast<unsigned int>(strpbrk(token, " \t\r") -
token); // token length
if (token_sz < 1) // delimiter not found, token_sz = strlen(token) if (token_sz < 1) // delimiter not found, token_sz = strlen(token)
{ {
// token_sz=strlen(token); // token_sz=strlen(token);
a_hash = X31_hash_string(token); a_hash = X31_hash_string(token);
} else } else {
a_hash = X31_hash_stringSZ(token, static_cast<int>(token_sz)); a_hash = X31_hash_stringSZ(token, static_cast<int>(token_sz));
}
a_tok = -1; a_tok = -1;
if (hashed_toks.find(a_hash) != hashed_toks.end()) if (hashed_toks.find(a_hash) != hashed_toks.end())
@@ -628,6 +639,9 @@ static int token2tok(const char *token) {
return a_tok; return a_tok;
} }
//*************** tigro keywords hash functions END
//*************************************************
MaterialReader::~MaterialReader() {} MaterialReader::~MaterialReader() {}
struct vertex_index { struct vertex_index {
@@ -1228,6 +1242,7 @@ static bool ParseTextureNameAndOption(std::string *texname,
parseReal2(&(texopt->brightness), &(texopt->contrast), &token, 0.0, parseReal2(&(texopt->brightness), &(texopt->contrast), &token, 0.0,
1.0); 1.0);
} }
} else { } else {
// Assume texture filename // Assume texture filename
#if 0 #if 0
@@ -1370,8 +1385,9 @@ static void SplitString(const std::string &s, char delim,
void LoadMtl( void LoadMtl(
// std::map<std::string, int> *material_map, // std::map<std::string, int> *material_map,
std::map<unsigned int, int> *material_map, std::vector<material_t> *materials, std::map<unsigned int, int> *material_map,
std::istream *inStream, std::string *warning) { std::vector<material_t> *materials, std::istream *inStream,
std::string *warning) {
// Create a default material anyway. // Create a default material anyway.
material_t material; material_t material;
InitMaterial(&material); InitMaterial(&material);
@@ -1424,71 +1440,59 @@ void LoadMtl(
if (IS_SPACE((token[2]))) { if (IS_SPACE((token[2]))) {
// group K // group K
if (token[0] == 'K') { if (token[0] == 'K') {
switch (token[1]) { // ambient
case 'a': if (token[1] == 'a') {
// ambient token += 2;
// if (token[1] == 'a') real_t r, g, b;
{ parseReal3(&r, &g, &b, &token);
token += 2; material.ambient[0] = r;
real_t r, g, b; material.ambient[1] = g;
parseReal3(&r, &g, &b, &token); material.ambient[2] = b;
material.ambient[0] = r; continue;
material.ambient[1] = g; }
material.ambient[2] = b;
continue;
}
// diffuse // diffuse
case 'd': if (token[1] == 'd') {
// if (token[1] == 'd') token += 2;
{ real_t r, g, b;
token += 2; parseReal3(&r, &g, &b, &token);
real_t r, g, b; material.diffuse[0] = r;
parseReal3(&r, &g, &b, &token); material.diffuse[1] = g;
material.diffuse[0] = r; material.diffuse[2] = b;
material.diffuse[1] = g; continue;
material.diffuse[2] = b; }
continue;
}
// specular // specular
case 's': if (token[1] == 's') {
// if (token[1] == 's') token += 2;
{ real_t r, g, b;
token += 2; parseReal3(&r, &g, &b, &token);
real_t r, g, b; material.specular[0] = r;
parseReal3(&r, &g, &b, &token); material.specular[1] = g;
material.specular[0] = r; material.specular[2] = b;
material.specular[1] = g; continue;
material.specular[2] = b; }
continue;
}
// transmittance // transmittance
case 't': if (token[1] == 't') {
// if (token[1] == 't') token += 2;
{ real_t r, g, b;
token += 2; parseReal3(&r, &g, &b, &token);
real_t r, g, b; material.transmittance[0] = r;
parseReal3(&r, &g, &b, &token); material.transmittance[1] = g;
material.transmittance[0] = r; material.transmittance[2] = b;
material.transmittance[1] = g; continue;
material.transmittance[2] = b; }
continue;
}
// emission // emission
case 'e': if (token[1] == 'e') {
// if (token[1] == 'e') token += 2;
{ real_t r, g, b;
token += 2; parseReal3(&r, &g, &b, &token);
real_t r, g, b; material.emission[0] = r;
parseReal3(&r, &g, &b, &token); material.emission[1] = g;
material.emission[0] = r; material.emission[2] = b;
material.emission[1] = g; continue;
material.emission[2] = b;
continue;
}
} }
} }
@@ -1517,6 +1521,51 @@ void LoadMtl(
material.shininess = parseReal(&token); material.shininess = parseReal(&token);
continue; continue;
} }
if (token[0] == 'T' && token[1] == 'r') {
token += 2;
if (has_d) {
// `d` wins. Ignore `Tr` value.
ss << "WARN: Both `d` and `Tr` parameters defined for \""
<< material.name << "\". Use the value of `d` for dissolve."
<< std::endl;
} else {
// We invert value of Tr(assume Tr is in range [0, 1])
// NOTE: Interpretation of Tr is application(exporter) dependent. For
// some application(e.g. 3ds max obj exporter), Tr = d(Issue 43)
material.dissolve = 1.0f - parseReal(&token);
}
has_tr = true;
continue;
}
// tigra: refactoring for new speedup release
if (token[0] == 'P') {
// PBR: roughness
if (token[1] == 'r') {
token += 2;
material.roughness = parseReal(&token);
continue;
} else
// PBR: metallic
if (token[1] == 'm') {
token += 2;
material.metallic = parseReal(&token);
continue;
} else
// PBR: sheen
if (token[1] == 's') {
token += 2;
material.sheen = parseReal(&token);
continue;
} else
// PBR: clearcoat thickness
if (token[1] == 'c') {
token += 2;
material.clearcoat_thickness = parseReal(&token);
continue;
}
}
} }
// dissolve // dissolve
@@ -1532,43 +1581,6 @@ void LoadMtl(
has_d = true; has_d = true;
continue; continue;
} }
if (token[0] == 'T' && token[1] == 'r' && IS_SPACE(token[2])) {
token += 2;
if (has_d) {
// `d` wins. Ignore `Tr` value.
ss << "WARN: Both `d` and `Tr` parameters defined for \""
<< material.name << "\". Use the value of `d` for dissolve."
<< std::endl;
} else {
// We invert value of Tr(assume Tr is in range [0, 1])
// NOTE: Interpretation of Tr is application(exporter) dependent. For
// some application(e.g. 3ds max obj exporter), Tr = d(Issue 43)
material.dissolve = 1.0f - parseReal(&token);
}
has_tr = true;
continue;
}
// tigra: refactoring for new speedup release
if (token[0] == 'P' && IS_SPACE(token[2])) {
token += 2;
// PBR: roughness
if (token[1] == 'r')
material.roughness = parseReal(&token);
else
// PBR: metallic
if (token[1] == 'm')
material.metallic = parseReal(&token);
else
// PBR: sheen
if (token[1] == 's')
material.sheen = parseReal(&token);
else
// PBR: clearcoat thickness
if (token[1] == 'c')
material.clearcoat_thickness = parseReal(&token);
}
a_tok = token2tok(token); a_tok = token2tok(token);
@@ -1991,164 +2003,169 @@ bool LoadObj(attrib_t *attrib, std::vector<shape_t> *shapes,
if (token[0] == '#') continue; // comment line if (token[0] == '#') continue; // comment line
// vertex if (IS_SPACE((token[1]))) {
if (token[0] == 'v' && IS_SPACE((token[1]))) { // vertex
token += 2; if (token[0] == 'v') {
real_t x, y, z; token += 2;
real_t r, g, b; real_t x, y, z;
parseVertexWithColor(&x, &y, &z, &r, &g, &b, &token); real_t r, g, b;
v.push_back(x); parseVertexWithColor(&x, &y, &z, &r, &g, &b, &token);
v.push_back(y); v.push_back(x);
v.push_back(z); v.push_back(y);
v.push_back(z);
vc.push_back(r); vc.push_back(r);
vc.push_back(g); vc.push_back(g);
vc.push_back(b); vc.push_back(b);
continue; continue;
} }
// normal // face
if (token[0] == 'v' && token[1] == 'n' && IS_SPACE((token[2]))) { if (token[0] == 'f') {
token += 3; token += 2;
real_t x, y, z; token += strspn(token, " \t");
parseReal3(&x, &y, &z, &token);
vn.push_back(x);
vn.push_back(y);
vn.push_back(z);
continue;
}
// texcoord std::vector<vertex_index> face;
if (token[0] == 'v' && token[1] == 't' && IS_SPACE((token[2]))) { face.reserve(3);
token += 3;
real_t x, y;
parseReal2(&x, &y, &token);
vt.push_back(x);
vt.push_back(y);
continue;
}
// face while (!IS_NEW_LINE(token[0])) {
if (token[0] == 'f' && IS_SPACE((token[1]))) { vertex_index vi;
token += 2; if (!parseTriple(&token, static_cast<int>(v.size() / 3),
token += strspn(token, " \t"); static_cast<int>(vn.size() / 3),
static_cast<int>(vt.size() / 2), &vi)) {
std::vector<vertex_index> face; if (err) {
face.reserve(3); (*err) =
"Failed parse `f' line(e.g. zero value for face index).\n";
while (!IS_NEW_LINE(token[0])) { }
vertex_index vi; return false;
if (!parseTriple(&token, static_cast<int>(v.size() / 3),
static_cast<int>(vn.size() / 3),
static_cast<int>(vt.size() / 2), &vi)) {
if (err) {
(*err) = "Failed parse `f' line(e.g. zero value for face index).\n";
} }
return false;
face.push_back(vi);
size_t n = strspn(token, " \t\r");
token += n;
} }
face.push_back(vi); // replace with emplace_back + std::move on C++11
size_t n = strspn(token, " \t\r"); faceGroup.push_back(std::vector<vertex_index>());
token += n; faceGroup[faceGroup.size() - 1].swap(face);
continue;
} }
// replace with emplace_back + std::move on C++11 // group name
faceGroup.push_back(std::vector<vertex_index>()); if (token[0] == 'g') {
faceGroup[faceGroup.size() - 1].swap(face); // flush previous face group.
bool ret = exportFaceGroupToShape(&shape, faceGroup, tags, material,
name, triangulate);
(void)ret; // return value not used.
continue; if (shape.mesh.indices.size() > 0) {
} shapes->push_back(shape);
}
// group name shape = shape_t();
if (token[0] == 'g' && IS_SPACE((token[1]))) {
// flush previous face group.
bool ret = exportFaceGroupToShape(&shape, faceGroup, tags, material, name,
triangulate);
(void)ret; // return value not used.
if (shape.mesh.indices.size() > 0) { // material = -1;
shapes->push_back(shape); faceGroup.clear();
std::vector<std::string> names;
names.reserve(2);
while (!IS_NEW_LINE(token[0])) {
std::string str = parseString(&token);
names.push_back(str);
token += strspn(token, " \t\r"); // skip tag
}
assert(names.size() > 0);
// names[0] must be 'g', so skip the 0th element.
if (names.size() > 1) {
name = names[1];
} else {
name = "";
}
continue;
} }
shape = shape_t(); // object name
if (token[0] == 'o') {
// flush previous face group.
bool ret = exportFaceGroupToShape(&shape, faceGroup, tags, material,
name, triangulate);
if (ret) {
shapes->push_back(shape);
}
// material = -1; // material = -1;
faceGroup.clear(); faceGroup.clear();
shape = shape_t();
std::vector<std::string> names; // @todo { multiple object name? }
names.reserve(2); token += 2;
while (!IS_NEW_LINE(token[0])) { /*
std::string str = parseString(&token);
names.push_back(str);
token += strspn(token, " \t\r"); // skip tag
}
assert(names.size() > 0);
// names[0] must be 'g', so skip the 0th element.
if (names.size() > 1) {
name = names[1];
} else {
name = "";
}
continue;
}
// object name
if (token[0] == 'o' && IS_SPACE((token[1]))) {
// flush previous face group.
bool ret = exportFaceGroupToShape(&shape, faceGroup, tags, material, name,
triangulate);
if (ret) {
shapes->push_back(shape);
}
// material = -1;
faceGroup.clear();
shape = shape_t();
// @todo { multiple object name? }
token += 2;
/*
std::stringstream ss; std::stringstream ss;
ss << token; ss << token;
name = ss.str(); name = ss.str();
*/ */
name = std::string(token); name = std::string(token);
continue; continue;
}
if (token[0] == 't') {
tag_t tag;
token += 2;
tag.name = parseString(&token);
tag_sizes ts = parseTagTriple(&token);
tag.intValues.resize(static_cast<size_t>(ts.num_ints));
for (size_t i = 0; i < static_cast<size_t>(ts.num_ints); ++i) {
tag.intValues[i] = parseInt(&token);
}
tag.floatValues.resize(static_cast<size_t>(ts.num_reals));
for (size_t i = 0; i < static_cast<size_t>(ts.num_reals); ++i) {
tag.floatValues[i] = parseReal(&token);
}
tag.stringValues.resize(static_cast<size_t>(ts.num_strings));
for (size_t i = 0; i < static_cast<size_t>(ts.num_strings); ++i) {
tag.stringValues[i] = parseString(&token);
}
tags.push_back(tag);
}
} }
if (token[0] == 't' && IS_SPACE(token[1])) { if (token[0] == 'v' && IS_SPACE((token[2]))) {
tag_t tag; // normal
if (token[1] == 'n') {
token += 2; token += 3;
real_t x, y, z;
tag.name = parseString(&token); parseReal3(&x, &y, &z, &token);
vn.push_back(x);
tag_sizes ts = parseTagTriple(&token); vn.push_back(y);
vn.push_back(z);
tag.intValues.resize(static_cast<size_t>(ts.num_ints)); continue;
for (size_t i = 0; i < static_cast<size_t>(ts.num_ints); ++i) {
tag.intValues[i] = parseInt(&token);
} }
tag.floatValues.resize(static_cast<size_t>(ts.num_reals)); // texcoord
for (size_t i = 0; i < static_cast<size_t>(ts.num_reals); ++i) { if (token[1] == 't') {
tag.floatValues[i] = parseReal(&token); token += 3;
real_t x, y;
parseReal2(&x, &y, &token);
vt.push_back(x);
vt.push_back(y);
continue;
} }
tag.stringValues.resize(static_cast<size_t>(ts.num_strings));
for (size_t i = 0; i < static_cast<size_t>(ts.num_strings); ++i) {
tag.stringValues[i] = parseString(&token);
}
tags.push_back(tag);
} }
// tigra: refactoring for new speedup release // tigra: refactoring for new speedup release