From 86b9c625db8b3c63d504c171b370440207c5d749 Mon Sep 17 00:00:00 2001 From: YarmUI Date: Mon, 21 Apr 2014 19:14:06 +0900 Subject: [PATCH] set default material --- tiny_obj_loader.cc | 61 ++++++++++++++++++++++++++++------------------ 1 file changed, 37 insertions(+), 24 deletions(-) diff --git a/tiny_obj_loader.cc b/tiny_obj_loader.cc index c55d2ca1..e0fb3dbc 100644 --- a/tiny_obj_loader.cc +++ b/tiny_obj_loader.cc @@ -201,6 +201,25 @@ updateVertex( return idx; } +void InitMaterial(material_t& material) { + material.name = ""; + material.ambient_texname = ""; + material.diffuse_texname = ""; + material.specular_texname = ""; + material.normal_texname = ""; + for (int i = 0; i < 3; i ++) { + material.ambient[i] = 0.f; + material.diffuse[i] = 0.f; + material.specular[i] = 0.f; + material.transmittance[i] = 0.f; + material.emission[i] = 0.f; + } + material.illum = 0; + material.dissolve = 1.f; + material.shininess = 1.f; + material.unknown_parameter.clear(); +} + static bool exportFaceGroupToShape( shape_t& shape, @@ -209,7 +228,8 @@ exportFaceGroupToShape( const std::vector &in_texcoords, const std::vector >& faceGroup, const material_t &material, - const std::string &name) + const std::string &name, + const bool is_material_seted) { if (faceGroup.empty()) { return false; @@ -257,31 +277,19 @@ exportFaceGroupToShape( shape.mesh.texcoords.swap(texcoords); shape.mesh.indices.swap(indices); - shape.material = material; + if(is_material_seted) { + shape.material = material; + } else { + InitMaterial(shape.material); + shape.material.diffuse[0] = 1.f; + shape.material.diffuse[1] = 1.f; + shape.material.diffuse[2] = 1.f; + } return true; } - -void InitMaterial(material_t& material) { - material.name = ""; - material.ambient_texname = ""; - material.diffuse_texname = ""; - material.specular_texname = ""; - material.normal_texname = ""; - for (int i = 0; i < 3; i ++) { - material.ambient[i] = 0.f; - material.diffuse[i] = 0.f; - material.specular[i] = 0.f; - material.transmittance[i] = 0.f; - material.emission[i] = 0.f; - } - material.illum = 0; - material.dissolve = 1.f; - material.shininess = 1.f; - material.unknown_parameter.clear(); -} std::string LoadMtl ( std::map& material_map, @@ -512,6 +520,7 @@ LoadObj( // material std::map material_map; material_t material; + bool is_material_seted = false; int maxchars = 8192; // Alloc enough size. std::vector buf(maxchars); // Alloc enough size. @@ -601,6 +610,7 @@ LoadObj( if (material_map.find(namebuf) != material_map.end()) { material = material_map[namebuf]; + is_material_seted = true; } else { // { error!! material not found } InitMaterial(material); @@ -628,11 +638,12 @@ LoadObj( // flush previous face group. shape_t shape; - bool ret = exportFaceGroupToShape(shape, v, vn, vt, faceGroup, material, name); + bool ret = exportFaceGroupToShape(shape, v, vn, vt, faceGroup, material, name, is_material_seted); if (ret) { shapes.push_back(shape); } + is_material_seted = false; faceGroup.clear(); std::vector names; @@ -659,11 +670,12 @@ LoadObj( // flush previous face group. shape_t shape; - bool ret = exportFaceGroupToShape(shape, v, vn, vt, faceGroup, material, name); + bool ret = exportFaceGroupToShape(shape, v, vn, vt, faceGroup, material, name, is_material_seted); if (ret) { shapes.push_back(shape); } + is_material_seted = false; faceGroup.clear(); // @todo { multiple object name? } @@ -680,10 +692,11 @@ LoadObj( } shape_t shape; - bool ret = exportFaceGroupToShape(shape, v, vn, vt, faceGroup, material, name); + bool ret = exportFaceGroupToShape(shape, v, vn, vt, faceGroup, material, name, is_material_seted); if (ret) { shapes.push_back(shape); } + is_material_seted = false; // for safety faceGroup.clear(); // for safety return err.str();