diff --git a/wrap/io_trimesh/export_obj.h b/wrap/io_trimesh/export_obj.h index 98fc3053..dc1dfc46 100644 --- a/wrap/io_trimesh/export_obj.h +++ b/wrap/io_trimesh/export_obj.h @@ -28,7 +28,6 @@ #include #include -#include "io_material.h" #include #include #include @@ -129,7 +128,8 @@ public: int current = 0; int totalPrimitives = m.vn+m.fn; - std::vector materialVec; + typename SaveMeshType::template PerMeshAttributeHandle> materialsHandle = + vcg::tri::Allocator::template FindPerMeshAttribute>(m, "materials"); std::string fn(filename); int LastSlash=fn.size()-1; @@ -197,29 +197,19 @@ public: fprintf(fp,"# %d vertices, %d vertices normals\n\n",m.vn,int(NormalVertex.size())); //faces + texture coords + typename SaveMeshType::template PerFaceAttributeHandle mIndHandle = + vcg::tri::Allocator::template FindPerFaceAttribute(m, "mInd"); std::map CoordIndexTexture; - unsigned int material_num = 0; - int mem_index = 0; //var temporany int curTexCoordIndex = 1; + int curMatIndex = -1; for(FaceIterator fi=m.face.begin(); fi!=m.face.end(); ++fi) if( !(*fi).IsD() ) { if((mask & Mask::IOM_FACECOLOR) || (mask & Mask::IOM_WEDGTEXCOORD) || (mask & Mask::IOM_VERTTEXCOORD)) { - int index = (*fi).mInd; - - if(index == (int)materialVec.size())//inserts a new element material - { - material_num++; - fprintf(fp,"\nusemtl material_%d\n",materialVec[index-1].index); - mem_index = index-1; - } - else - { - if(index != mem_index)//inserts old name elemente material - { - fprintf(fp,"\nusemtl material_%d\n",materialVec[index].index); - mem_index=index; - } + int index = mIndHandle[fi]; + if(index != curMatIndex) { + fprintf(fp,"\nusemtl material_%d\n", index); + curMatIndex = index; } } @@ -281,7 +271,7 @@ public: int errCode = E_NOERROR; if((mask & Mask::IOM_WEDGTEXCOORD) || (mask & Mask::IOM_FACECOLOR) || (mask & Mask::IOM_VERTTEXCOORD) ) - errCode = WriteMaterials(materialVec, filename,cb);//write material + errCode = WriteMaterials(materialsHandle(), filename,cb);//write material if(errCode!= E_NOERROR) return errCode; @@ -382,7 +372,7 @@ public: else { /* fclose(fp); return E_ABORTED; */ } - fprintf(fp,"newmtl material_%d\n",materialVec[i].index); + fprintf(fp,"newmtl material_%d\n",i); fprintf(fp,"Ka %f %f %f\n",materialVec[i].Ka[0],materialVec[i].Ka[1],materialVec[i].Ka[2]); fprintf(fp,"Kd %f %f %f\n",materialVec[i].Kd[0],materialVec[i].Kd[1],materialVec[i].Kd[2]); fprintf(fp,"Ks %f %f %f\n",materialVec[i].Ks[0],materialVec[i].Ks[1],materialVec[i].Ks[2]); diff --git a/wrap/io_trimesh/import_obj.h b/wrap/io_trimesh/import_obj.h index 9debd142..8f374dda 100644 --- a/wrap/io_trimesh/import_obj.h +++ b/wrap/io_trimesh/import_obj.h @@ -51,8 +51,6 @@ namespace vcg { template class ImporterOBJ { - private: - std::vector materials; public: static int &MRGBLineCount(){static int _MRGBLineCount=0; return _MRGBLineCount;} @@ -257,9 +255,12 @@ namespace vcg { stream.close(); return E_CANTOPEN; } - typename OpenMeshType::template PerMeshAttributeHandle> hMaterial = + + typename OpenMeshType::template PerMeshAttributeHandle> materialsHandle = vcg::tri::Allocator:: template GetPerMeshAttribute>(m, std::string("materials")); - std::vector materials = hMaterial(); // materials vector + typename OpenMeshType::template PerFaceAttributeHandle mIndHandle = + vcg::tri::Allocator:: template GetPerFaceAttribute(m, std::string("mInd")); + std::vector& materials = materialsHandle(); // materials vector std::vector texCoords; // texture coordinates std::vector normals; // vertex normals std::vector indexedFaces; @@ -707,6 +708,7 @@ namespace vcg { if (((oi.mask & vcg::tri::io::Mask::IOM_FACECOLOR) != 0) && (HasPerFaceColor(m))) { m.face[i].C() = indexedFaces[i].c; + mIndHandle[i] = indexedFaces[i].mInd; } if (((oi.mask & vcg::tri::io::Mask::IOM_WEDGNORMAL) != 0) && (HasPerWedgeNormal(m))) @@ -953,7 +955,15 @@ namespace vcg { materials.clear(); Material currentMaterial; + + // Fill in some default values for the material currentMaterial.index = (unsigned int)(-1); + currentMaterial.Ka = Point3f(0.2, 0.2, 0.2); + currentMaterial.Kd = Point3f(1, 1, 1); + currentMaterial.Ks = Point3f(1, 1, 1); + currentMaterial.Tr = 1; + currentMaterial.Ns = 0; + currentMaterial.illum = 2; bool first = true; while (!stream.eof())