Skip to content

Commit

Permalink
Merge pull request tinyobjloader#191 from sinisterchipmunk/initialize…
Browse files Browse the repository at this point in the history
…_unparsed_texopts

Properly initialize all texture_option_t's, not just parsed ones
  • Loading branch information
syoyo authored Dec 1, 2018
2 parents 850d0ff + 0ab0146 commit d793bfb
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 58 deletions.
43 changes: 43 additions & 0 deletions tests/tester.cc
Original file line number Diff line number Diff line change
Expand Up @@ -986,6 +986,49 @@ TEST_CASE("multiple-group-names", "[group]") {
// 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]") {
tinyobj::attrib_t attrib;
std::vector<tinyobj::shape_t> shapes;
Expand Down
114 changes: 56 additions & 58 deletions tiny_obj_loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -397,8 +397,7 @@ void LoadMtl(std::map<std::string, int> *material_map,
///
bool ParseTextureNameAndOption(std::string *texname,
texture_option_t *texopt,
const char *linebuf,
const bool is_bump);
const char *linebuf);
} // namespace tinyobj

#endif // TINY_OBJ_LOADER_H_
Expand Down Expand Up @@ -906,35 +905,11 @@ static vertex_index_t parseRawTriple(const char **token) {

bool ParseTextureNameAndOption(std::string *texname,
texture_option_t *texopt,
const char *linebuf, const bool is_bump) {
const char *linebuf) {
// @todo { write more robust lexer and parser. }
bool found_texname = false;
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

while (!IS_NEW_LINE((*token))) {
Expand Down Expand Up @@ -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) {
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->ambient_texname = "";
material->diffuse_texname = "";
Expand Down Expand Up @@ -1560,62 +1573,55 @@ void LoadMtl(std::map<std::string, int> *material_map,
if ((0 == strncmp(token, "map_Ka", 6)) && IS_SPACE(token[6])) {
token += 7;
ParseTextureNameAndOption(&(material.ambient_texname),
&(material.ambient_texopt), token,
/* is_bump */ false);
&(material.ambient_texopt), token);
continue;
}

// diffuse texture
if ((0 == strncmp(token, "map_Kd", 6)) && IS_SPACE(token[6])) {
token += 7;
ParseTextureNameAndOption(&(material.diffuse_texname),
&(material.diffuse_texopt), token,
/* is_bump */ false);
&(material.diffuse_texopt), token);
continue;
}

// specular texture
if ((0 == strncmp(token, "map_Ks", 6)) && IS_SPACE(token[6])) {
token += 7;
ParseTextureNameAndOption(&(material.specular_texname),
&(material.specular_texopt), token,
/* is_bump */ false);
&(material.specular_texopt), token);
continue;
}

// specular highlight texture
if ((0 == strncmp(token, "map_Ns", 6)) && IS_SPACE(token[6])) {
token += 7;
ParseTextureNameAndOption(&(material.specular_highlight_texname),
&(material.specular_highlight_texopt), token,
/* is_bump */ false);
&(material.specular_highlight_texopt), token);
continue;
}

// bump texture
if ((0 == strncmp(token, "map_bump", 8)) && IS_SPACE(token[8])) {
token += 9;
ParseTextureNameAndOption(&(material.bump_texname),
&(material.bump_texopt), token,
/* is_bump */ true);
&(material.bump_texopt), token);
continue;
}

// bump texture
if ((0 == strncmp(token, "map_Bump", 8)) && IS_SPACE(token[8])) {
token += 9;
ParseTextureNameAndOption(&(material.bump_texname),
&(material.bump_texopt), token,
/* is_bump */ true);
&(material.bump_texopt), token);
continue;
}

// bump texture
if ((0 == strncmp(token, "bump", 4)) && IS_SPACE(token[4])) {
token += 5;
ParseTextureNameAndOption(&(material.bump_texname),
&(material.bump_texopt), token,
/* is_bump */ true);
&(material.bump_texopt), token);
continue;
}

Expand All @@ -1624,71 +1630,63 @@ void LoadMtl(std::map<std::string, int> *material_map,
token += 6;
material.alpha_texname = token;
ParseTextureNameAndOption(&(material.alpha_texname),
&(material.alpha_texopt), token,
/* is_bump */ false);
&(material.alpha_texopt), token);
continue;
}

// displacement texture
if ((0 == strncmp(token, "disp", 4)) && IS_SPACE(token[4])) {
token += 5;
ParseTextureNameAndOption(&(material.displacement_texname),
&(material.displacement_texopt), token,
/* is_bump */ false);
&(material.displacement_texopt), token);
continue;
}

// reflection map
if ((0 == strncmp(token, "refl", 4)) && IS_SPACE(token[4])) {
token += 5;
ParseTextureNameAndOption(&(material.reflection_texname),
&(material.reflection_texopt), token,
/* is_bump */ false);
&(material.reflection_texopt), token);
continue;
}

// PBR: roughness texture
if ((0 == strncmp(token, "map_Pr", 6)) && IS_SPACE(token[6])) {
token += 7;
ParseTextureNameAndOption(&(material.roughness_texname),
&(material.roughness_texopt), token,
/* is_bump */ false);
&(material.roughness_texopt), token);
continue;
}

// PBR: metallic texture
if ((0 == strncmp(token, "map_Pm", 6)) && IS_SPACE(token[6])) {
token += 7;
ParseTextureNameAndOption(&(material.metallic_texname),
&(material.metallic_texopt), token,
/* is_bump */ false);
&(material.metallic_texopt), token);
continue;
}

// PBR: sheen texture
if ((0 == strncmp(token, "map_Ps", 6)) && IS_SPACE(token[6])) {
token += 7;
ParseTextureNameAndOption(&(material.sheen_texname),
&(material.sheen_texopt), token,
/* is_bump */ false);
&(material.sheen_texopt), token);
continue;
}

// PBR: emissive texture
if ((0 == strncmp(token, "map_Ke", 6)) && IS_SPACE(token[6])) {
token += 7;
ParseTextureNameAndOption(&(material.emissive_texname),
&(material.emissive_texopt), token,
/* is_bump */ false);
&(material.emissive_texopt), token);
continue;
}

// PBR: normal map texture
if ((0 == strncmp(token, "norm", 4)) && IS_SPACE(token[4])) {
token += 5;
ParseTextureNameAndOption(
&(material.normal_texname), &(material.normal_texopt), token,
/* is_bump */ false); // @fixme { is_bump will be true? }
ParseTextureNameAndOption(&(material.normal_texname),
&(material.normal_texopt), token);
continue;
}

Expand Down

0 comments on commit d793bfb

Please sign in to comment.