Merge pull request #183 from mlimper/master

Added option to disable default vertex colors
This commit is contained in:
Syoyo Fujita
2018-09-01 00:31:29 +09:00
committed by GitHub

View File

@@ -338,10 +338,12 @@ class MaterialStreamReader : public MaterialReader {
/// directory. /// directory.
/// 'triangulate' is optional, and used whether triangulate polygon face in .obj /// 'triangulate' is optional, and used whether triangulate polygon face in .obj
/// or not. /// or not.
/// Option 'default_vcols_fallback' specifies whether vertex colors should
/// 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 *err,
const char *filename, const char *mtl_basedir = NULL, const char *filename, const char *mtl_basedir = NULL,
bool triangulate = true); 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
@@ -361,7 +363,7 @@ bool LoadObjWithCallback(std::istream &inStream, const callback_t &callback,
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 = NULL,
bool triangulate = true); 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,
@@ -710,11 +712,13 @@ 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);
(*r) = parseReal(token, 1.0); const bool found_color = parseReal(token, r) && parseReal(token, g) && parseReal(token, b);
(*g) = parseReal(token, 1.0);
(*b) = parseReal(token, 1.0); if (!found_color) {
(*r) = (*g) = (*b) = 1.0;
return true; }
return found_color;
} }
static inline bool parseOnOff(const char **token, bool default_value = true) { static inline bool parseOnOff(const char **token, bool default_value = true) {
@@ -1733,7 +1737,8 @@ bool MaterialStreamReader::operator()(const std::string &matId,
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, bool trianglulate) { const char *filename, const char *mtl_basedir,
bool trianglulate, bool default_vcols_fallback) {
attrib->vertices.clear(); attrib->vertices.clear();
attrib->normals.clear(); attrib->normals.clear();
attrib->texcoords.clear(); attrib->texcoords.clear();
@@ -1763,13 +1768,13 @@ 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, err, &ifs, &matFileReader,
trianglulate); 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 *err,
std::istream *inStream, MaterialReader *readMatFn /*= NULL*/, std::istream *inStream, MaterialReader *readMatFn /*= NULL*/,
bool triangulate) { bool triangulate, bool default_vcols_fallback) {
std::stringstream errss; std::stringstream errss;
std::vector<real_t> v; std::vector<real_t> v;
@@ -1795,6 +1800,8 @@ bool LoadObj(attrib_t *attrib, std::vector<shape_t> *shapes,
shape_t shape; shape_t shape;
bool found_all_colors = true;
size_t line_num = 0; size_t line_num = 0;
std::string linebuf; std::string linebuf;
while (inStream->peek() != -1) { while (inStream->peek() != -1) {
@@ -1831,14 +1838,19 @@ bool LoadObj(attrib_t *attrib, std::vector<shape_t> *shapes,
token += 2; token += 2;
real_t x, y, z; real_t x, y, z;
real_t r, g, b; real_t r, g, b;
parseVertexWithColor(&x, &y, &z, &r, &g, &b, &token);
found_all_colors &= parseVertexWithColor(&x, &y, &z, &r, &g, &b, &token);
v.push_back(x); v.push_back(x);
v.push_back(y); v.push_back(y);
v.push_back(z); v.push_back(z);
vc.push_back(r); if (found_all_colors || default_vcols_fallback) {
vc.push_back(g); vc.push_back(r);
vc.push_back(b); vc.push_back(g);
vc.push_back(b);
}
continue; continue;
} }
@@ -1925,7 +1937,7 @@ bool LoadObj(attrib_t *attrib, std::vector<shape_t> *shapes,
continue; continue;
} }
// use mtl // use mtl
if ((0 == strncmp(token, "usemtl", 6)) && IS_SPACE((token[6]))) { if ((0 == strncmp(token, "usemtl", 6)) && IS_SPACE((token[6]))) {
token += 7; token += 7;
@@ -2160,7 +2172,12 @@ bool LoadObj(attrib_t *attrib, std::vector<shape_t> *shapes,
// Ignore unknown command. // Ignore unknown command.
} }
// not all vertices have colors, no default colors desired? -> clear colors
if (!found_all_colors && !default_vcols_fallback) {
vc.clear();
}
if (greatest_v_idx >= static_cast<int>(v.size() / 3)) if (greatest_v_idx >= static_cast<int>(v.size() / 3))
{ {
if (err) { if (err) {