Merge pull request #191 from sinisterchipmunk/initialize_unparsed_texopts

Properly initialize all texture_option_t's, not just parsed ones
This commit is contained in:
Syoyo Fujita
2018-12-01 13:25:41 +09:00
committed by GitHub
2 changed files with 99 additions and 58 deletions

View File

@@ -986,6 +986,49 @@ TEST_CASE("multiple-group-names", "[group]") {
// single white space. // single white space.
} }
TEST_CASE("initialize_all_texopts", "[ensure unparsed texopts are initialized to defaults]") {
tinyobj::attrib_t attrib;
std::vector<tinyobj::shape_t> shapes;
std::vector<tinyobj::material_t> materials;
std::string warn;
std::string err;
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err,
"../models/cornell_box.obj", gMtlBasePath, false);
REQUIRE(0 < materials.size());
#define REQUIRE_DEFAULT_TEXOPT(texopt) \
REQUIRE(tinyobj::TEXTURE_TYPE_NONE == texopt.type); \
REQUIRE(0.0 == texopt.brightness); \
REQUIRE(1.0 == texopt.contrast); \
REQUIRE(false == texopt.clamp); \
REQUIRE(true == texopt.blendu); \
REQUIRE(true == texopt.blendv); \
REQUIRE(1.0 == texopt.bump_multiplier); \
for (int j = 0; j < 3; j++) { \
REQUIRE(0.0 == texopt.origin_offset[j]); \
REQUIRE(1.0 == texopt.scale[j]); \
REQUIRE(0.0 == texopt.turbulence[j]); \
}
for (size_t i = 0; i < materials.size(); i++) {
REQUIRE_DEFAULT_TEXOPT(materials[i].ambient_texopt);
REQUIRE_DEFAULT_TEXOPT(materials[i].diffuse_texopt);
REQUIRE_DEFAULT_TEXOPT(materials[i].specular_texopt);
REQUIRE_DEFAULT_TEXOPT(materials[i].specular_highlight_texopt);
REQUIRE_DEFAULT_TEXOPT(materials[i].bump_texopt);
REQUIRE_DEFAULT_TEXOPT(materials[i].displacement_texopt);
REQUIRE_DEFAULT_TEXOPT(materials[i].alpha_texopt);
REQUIRE_DEFAULT_TEXOPT(materials[i].reflection_texopt);
REQUIRE_DEFAULT_TEXOPT(materials[i].roughness_texopt);
REQUIRE_DEFAULT_TEXOPT(materials[i].metallic_texopt);
REQUIRE_DEFAULT_TEXOPT(materials[i].sheen_texopt);
REQUIRE_DEFAULT_TEXOPT(materials[i].emissive_texopt);
REQUIRE_DEFAULT_TEXOPT(materials[i].normal_texopt);
}
#undef REQUIRE_DEFAULT_TEXOPT
}
TEST_CASE("colorspace", "[Issue184]") { TEST_CASE("colorspace", "[Issue184]") {
tinyobj::attrib_t attrib; tinyobj::attrib_t attrib;
std::vector<tinyobj::shape_t> shapes; std::vector<tinyobj::shape_t> shapes;

View File

@@ -397,8 +397,7 @@ void LoadMtl(std::map<std::string, int> *material_map,
/// ///
bool ParseTextureNameAndOption(std::string *texname, bool ParseTextureNameAndOption(std::string *texname,
texture_option_t *texopt, texture_option_t *texopt,
const char *linebuf, const char *linebuf);
const bool is_bump);
} // namespace tinyobj } // namespace tinyobj
#endif // TINY_OBJ_LOADER_H_ #endif // TINY_OBJ_LOADER_H_
@@ -906,35 +905,11 @@ static vertex_index_t parseRawTriple(const char **token) {
bool ParseTextureNameAndOption(std::string *texname, bool ParseTextureNameAndOption(std::string *texname,
texture_option_t *texopt, texture_option_t *texopt,
const char *linebuf, const bool is_bump) { const char *linebuf) {
// @todo { write more robust lexer and parser. } // @todo { write more robust lexer and parser. }
bool found_texname = false; bool found_texname = false;
std::string texture_name; std::string texture_name;
// Fill with default value for texopt.
if (is_bump) {
texopt->imfchan = 'l';
} else {
texopt->imfchan = 'm';
}
texopt->bump_multiplier = static_cast<real_t>(1.0);
texopt->clamp = false;
texopt->blendu = true;
texopt->blendv = true;
texopt->sharpness = static_cast<real_t>(1.0);
texopt->brightness = static_cast<real_t>(0.0);
texopt->contrast = static_cast<real_t>(1.0);
texopt->origin_offset[0] = static_cast<real_t>(0.0);
texopt->origin_offset[1] = static_cast<real_t>(0.0);
texopt->origin_offset[2] = static_cast<real_t>(0.0);
texopt->scale[0] = static_cast<real_t>(1.0);
texopt->scale[1] = static_cast<real_t>(1.0);
texopt->scale[2] = static_cast<real_t>(1.0);
texopt->turbulence[0] = static_cast<real_t>(0.0);
texopt->turbulence[1] = static_cast<real_t>(0.0);
texopt->turbulence[2] = static_cast<real_t>(0.0);
texopt->type = TEXTURE_TYPE_NONE;
const char *token = linebuf; // Assume line ends with NULL const char *token = linebuf; // Assume line ends with NULL
while (!IS_NEW_LINE((*token))) { while (!IS_NEW_LINE((*token))) {
@@ -1011,7 +986,45 @@ bool ParseTextureNameAndOption(std::string *texname,
} }
} }
static void InitTexOpt(texture_option_t *texopt, const bool is_bump) {
if (is_bump) {
texopt->imfchan = 'l';
} else {
texopt->imfchan = 'm';
}
texopt->bump_multiplier = static_cast<real_t>(1.0);
texopt->clamp = false;
texopt->blendu = true;
texopt->blendv = true;
texopt->sharpness = static_cast<real_t>(1.0);
texopt->brightness = static_cast<real_t>(0.0);
texopt->contrast = static_cast<real_t>(1.0);
texopt->origin_offset[0] = static_cast<real_t>(0.0);
texopt->origin_offset[1] = static_cast<real_t>(0.0);
texopt->origin_offset[2] = static_cast<real_t>(0.0);
texopt->scale[0] = static_cast<real_t>(1.0);
texopt->scale[1] = static_cast<real_t>(1.0);
texopt->scale[2] = static_cast<real_t>(1.0);
texopt->turbulence[0] = static_cast<real_t>(0.0);
texopt->turbulence[1] = static_cast<real_t>(0.0);
texopt->turbulence[2] = static_cast<real_t>(0.0);
texopt->type = TEXTURE_TYPE_NONE;
}
static void InitMaterial(material_t *material) { static void InitMaterial(material_t *material) {
InitTexOpt(&material->ambient_texopt, /* is_bump */ false);
InitTexOpt(&material->diffuse_texopt, /* is_bump */ false);
InitTexOpt(&material->specular_texopt, /* is_bump */ false);
InitTexOpt(&material->specular_highlight_texopt, /* is_bump */ false);
InitTexOpt(&material->bump_texopt, /* is_bump */ true);
InitTexOpt(&material->displacement_texopt, /* is_bump */ false);
InitTexOpt(&material->alpha_texopt, /* is_bump */ false);
InitTexOpt(&material->reflection_texopt, /* is_bump */ false);
InitTexOpt(&material->roughness_texopt, /* is_bump */ false);
InitTexOpt(&material->metallic_texopt, /* is_bump */ false);
InitTexOpt(&material->sheen_texopt, /* is_bump */ false);
InitTexOpt(&material->emissive_texopt, /* is_bump */ false);
InitTexOpt(&material->normal_texopt, /* is_bump */ false); // @fixme { is_bump will be true? }
material->name = ""; material->name = "";
material->ambient_texname = ""; material->ambient_texname = "";
material->diffuse_texname = ""; material->diffuse_texname = "";
@@ -1560,8 +1573,7 @@ void LoadMtl(std::map<std::string, int> *material_map,
if ((0 == strncmp(token, "map_Ka", 6)) && IS_SPACE(token[6])) { if ((0 == strncmp(token, "map_Ka", 6)) && IS_SPACE(token[6])) {
token += 7; token += 7;
ParseTextureNameAndOption(&(material.ambient_texname), ParseTextureNameAndOption(&(material.ambient_texname),
&(material.ambient_texopt), token, &(material.ambient_texopt), token);
/* is_bump */ false);
continue; continue;
} }
@@ -1569,8 +1581,7 @@ void LoadMtl(std::map<std::string, int> *material_map,
if ((0 == strncmp(token, "map_Kd", 6)) && IS_SPACE(token[6])) { if ((0 == strncmp(token, "map_Kd", 6)) && IS_SPACE(token[6])) {
token += 7; token += 7;
ParseTextureNameAndOption(&(material.diffuse_texname), ParseTextureNameAndOption(&(material.diffuse_texname),
&(material.diffuse_texopt), token, &(material.diffuse_texopt), token);
/* is_bump */ false);
continue; continue;
} }
@@ -1578,8 +1589,7 @@ void LoadMtl(std::map<std::string, int> *material_map,
if ((0 == strncmp(token, "map_Ks", 6)) && IS_SPACE(token[6])) { if ((0 == strncmp(token, "map_Ks", 6)) && IS_SPACE(token[6])) {
token += 7; token += 7;
ParseTextureNameAndOption(&(material.specular_texname), ParseTextureNameAndOption(&(material.specular_texname),
&(material.specular_texopt), token, &(material.specular_texopt), token);
/* is_bump */ false);
continue; continue;
} }
@@ -1587,8 +1597,7 @@ void LoadMtl(std::map<std::string, int> *material_map,
if ((0 == strncmp(token, "map_Ns", 6)) && IS_SPACE(token[6])) { if ((0 == strncmp(token, "map_Ns", 6)) && IS_SPACE(token[6])) {
token += 7; token += 7;
ParseTextureNameAndOption(&(material.specular_highlight_texname), ParseTextureNameAndOption(&(material.specular_highlight_texname),
&(material.specular_highlight_texopt), token, &(material.specular_highlight_texopt), token);
/* is_bump */ false);
continue; continue;
} }
@@ -1596,8 +1605,7 @@ void LoadMtl(std::map<std::string, int> *material_map,
if ((0 == strncmp(token, "map_bump", 8)) && IS_SPACE(token[8])) { if ((0 == strncmp(token, "map_bump", 8)) && IS_SPACE(token[8])) {
token += 9; token += 9;
ParseTextureNameAndOption(&(material.bump_texname), ParseTextureNameAndOption(&(material.bump_texname),
&(material.bump_texopt), token, &(material.bump_texopt), token);
/* is_bump */ true);
continue; continue;
} }
@@ -1605,8 +1613,7 @@ void LoadMtl(std::map<std::string, int> *material_map,
if ((0 == strncmp(token, "map_Bump", 8)) && IS_SPACE(token[8])) { if ((0 == strncmp(token, "map_Bump", 8)) && IS_SPACE(token[8])) {
token += 9; token += 9;
ParseTextureNameAndOption(&(material.bump_texname), ParseTextureNameAndOption(&(material.bump_texname),
&(material.bump_texopt), token, &(material.bump_texopt), token);
/* is_bump */ true);
continue; continue;
} }
@@ -1614,8 +1621,7 @@ void LoadMtl(std::map<std::string, int> *material_map,
if ((0 == strncmp(token, "bump", 4)) && IS_SPACE(token[4])) { if ((0 == strncmp(token, "bump", 4)) && IS_SPACE(token[4])) {
token += 5; token += 5;
ParseTextureNameAndOption(&(material.bump_texname), ParseTextureNameAndOption(&(material.bump_texname),
&(material.bump_texopt), token, &(material.bump_texopt), token);
/* is_bump */ true);
continue; continue;
} }
@@ -1624,8 +1630,7 @@ void LoadMtl(std::map<std::string, int> *material_map,
token += 6; token += 6;
material.alpha_texname = token; material.alpha_texname = token;
ParseTextureNameAndOption(&(material.alpha_texname), ParseTextureNameAndOption(&(material.alpha_texname),
&(material.alpha_texopt), token, &(material.alpha_texopt), token);
/* is_bump */ false);
continue; continue;
} }
@@ -1633,8 +1638,7 @@ void LoadMtl(std::map<std::string, int> *material_map,
if ((0 == strncmp(token, "disp", 4)) && IS_SPACE(token[4])) { if ((0 == strncmp(token, "disp", 4)) && IS_SPACE(token[4])) {
token += 5; token += 5;
ParseTextureNameAndOption(&(material.displacement_texname), ParseTextureNameAndOption(&(material.displacement_texname),
&(material.displacement_texopt), token, &(material.displacement_texopt), token);
/* is_bump */ false);
continue; continue;
} }
@@ -1642,8 +1646,7 @@ void LoadMtl(std::map<std::string, int> *material_map,
if ((0 == strncmp(token, "refl", 4)) && IS_SPACE(token[4])) { if ((0 == strncmp(token, "refl", 4)) && IS_SPACE(token[4])) {
token += 5; token += 5;
ParseTextureNameAndOption(&(material.reflection_texname), ParseTextureNameAndOption(&(material.reflection_texname),
&(material.reflection_texopt), token, &(material.reflection_texopt), token);
/* is_bump */ false);
continue; continue;
} }
@@ -1651,8 +1654,7 @@ void LoadMtl(std::map<std::string, int> *material_map,
if ((0 == strncmp(token, "map_Pr", 6)) && IS_SPACE(token[6])) { if ((0 == strncmp(token, "map_Pr", 6)) && IS_SPACE(token[6])) {
token += 7; token += 7;
ParseTextureNameAndOption(&(material.roughness_texname), ParseTextureNameAndOption(&(material.roughness_texname),
&(material.roughness_texopt), token, &(material.roughness_texopt), token);
/* is_bump */ false);
continue; continue;
} }
@@ -1660,8 +1662,7 @@ void LoadMtl(std::map<std::string, int> *material_map,
if ((0 == strncmp(token, "map_Pm", 6)) && IS_SPACE(token[6])) { if ((0 == strncmp(token, "map_Pm", 6)) && IS_SPACE(token[6])) {
token += 7; token += 7;
ParseTextureNameAndOption(&(material.metallic_texname), ParseTextureNameAndOption(&(material.metallic_texname),
&(material.metallic_texopt), token, &(material.metallic_texopt), token);
/* is_bump */ false);
continue; continue;
} }
@@ -1669,8 +1670,7 @@ void LoadMtl(std::map<std::string, int> *material_map,
if ((0 == strncmp(token, "map_Ps", 6)) && IS_SPACE(token[6])) { if ((0 == strncmp(token, "map_Ps", 6)) && IS_SPACE(token[6])) {
token += 7; token += 7;
ParseTextureNameAndOption(&(material.sheen_texname), ParseTextureNameAndOption(&(material.sheen_texname),
&(material.sheen_texopt), token, &(material.sheen_texopt), token);
/* is_bump */ false);
continue; continue;
} }
@@ -1678,17 +1678,15 @@ void LoadMtl(std::map<std::string, int> *material_map,
if ((0 == strncmp(token, "map_Ke", 6)) && IS_SPACE(token[6])) { if ((0 == strncmp(token, "map_Ke", 6)) && IS_SPACE(token[6])) {
token += 7; token += 7;
ParseTextureNameAndOption(&(material.emissive_texname), ParseTextureNameAndOption(&(material.emissive_texname),
&(material.emissive_texopt), token, &(material.emissive_texopt), token);
/* is_bump */ false);
continue; continue;
} }
// PBR: normal map texture // PBR: normal map texture
if ((0 == strncmp(token, "norm", 4)) && IS_SPACE(token[4])) { if ((0 == strncmp(token, "norm", 4)) && IS_SPACE(token[4])) {
token += 5; token += 5;
ParseTextureNameAndOption( ParseTextureNameAndOption(&(material.normal_texname),
&(material.normal_texname), &(material.normal_texopt), token, &(material.normal_texopt), token);
/* is_bump */ false); // @fixme { is_bump will be true? }
continue; continue;
} }