From 1abba4a694cb18a3ad965ce1b5ddb55b536fc328 Mon Sep 17 00:00:00 2001 From: John Senneker Date: Mon, 21 Nov 2016 18:36:20 -0500 Subject: [PATCH 1/4] Initial commit --- wrap/io_trimesh/export_obj.h | 2 +- wrap/io_trimesh/import_obj.h | 9 ++++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/wrap/io_trimesh/export_obj.h b/wrap/io_trimesh/export_obj.h index effb733f..98fc3053 100644 --- a/wrap/io_trimesh/export_obj.h +++ b/wrap/io_trimesh/export_obj.h @@ -205,7 +205,7 @@ public: { if((mask & Mask::IOM_FACECOLOR) || (mask & Mask::IOM_WEDGTEXCOORD) || (mask & Mask::IOM_VERTTEXCOORD)) { - int index = Materials::CreateNewMaterial(m,materialVec,material_num,fi); + int index = (*fi).mInd; if(index == (int)materialVec.size())//inserts a new element material { diff --git a/wrap/io_trimesh/import_obj.h b/wrap/io_trimesh/import_obj.h index 84c06cba..9debd142 100644 --- a/wrap/io_trimesh/import_obj.h +++ b/wrap/io_trimesh/import_obj.h @@ -51,6 +51,8 @@ namespace vcg { template class ImporterOBJ { + private: + std::vector materials; public: static int &MRGBLineCount(){static int _MRGBLineCount=0; return _MRGBLineCount;} @@ -113,6 +115,7 @@ namespace vcg { int tInd; bool edge[3];// useless if the face is a polygon, no need to have variable length array Color4b c; + int mInd; }; struct ObjEdge @@ -254,7 +257,9 @@ namespace vcg { stream.close(); return E_CANTOPEN; } - std::vector materials; // materials vector + typename OpenMeshType::template PerMeshAttributeHandle> hMaterial = + vcg::tri::Allocator:: template GetPerMeshAttribute>(m, std::string("materials")); + std::vector materials = hMaterial(); // materials vector std::vector texCoords; // texture coordinates std::vector normals; // vertex normals std::vector indexedFaces; @@ -585,6 +590,8 @@ namespace vcg { // assigning face color if( oi.mask & vcg::tri::io::Mask::IOM_FACECOLOR) ff.c = currentColor; + ff.mInd = currentMaterialIdx; + ++numTriangles; indexedFaces.push_back(ff); } From dc3f714b3424d6fc24a6013f9440b4bfdaf9ffda Mon Sep 17 00:00:00 2001 From: John Senneker Date: Tue, 22 Nov 2016 16:21:57 -0500 Subject: [PATCH 2/4] Add a per-mesh attribute in OBJ importer to hold a std::vector, and a per-face attribute to hold an index into that vector. --- wrap/io_trimesh/export_obj.h | 32 +++++++++++--------------------- wrap/io_trimesh/import_obj.h | 18 ++++++++++++++---- 2 files changed, 25 insertions(+), 25 deletions(-) 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()) From 7fdfd7423f3d0809b037904d09eb6faac6a01216 Mon Sep 17 00:00:00 2001 From: John Senneker Date: Tue, 22 Nov 2016 16:44:36 -0500 Subject: [PATCH 3/4] Add back deleted include in obj exporter --- wrap/io_trimesh/export_obj.h | 1 + 1 file changed, 1 insertion(+) diff --git a/wrap/io_trimesh/export_obj.h b/wrap/io_trimesh/export_obj.h index dc1dfc46..cb878a24 100644 --- a/wrap/io_trimesh/export_obj.h +++ b/wrap/io_trimesh/export_obj.h @@ -28,6 +28,7 @@ #include #include +#include #include #include #include From 0ee03d276d88a77b690bf6b5b508e10595f4a230 Mon Sep 17 00:00:00 2001 From: John Senneker Date: Mon, 12 Dec 2016 18:07:20 -0500 Subject: [PATCH 4/4] Fix clang compiler errors (">>" -> "> >") --- wrap/io_trimesh/export_obj.h | 4 ++-- wrap/io_trimesh/import_obj.h | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/wrap/io_trimesh/export_obj.h b/wrap/io_trimesh/export_obj.h index cb878a24..6e00ca5f 100644 --- a/wrap/io_trimesh/export_obj.h +++ b/wrap/io_trimesh/export_obj.h @@ -129,8 +129,8 @@ public: int current = 0; int totalPrimitives = m.vn+m.fn; - typename SaveMeshType::template PerMeshAttributeHandle> materialsHandle = - vcg::tri::Allocator::template FindPerMeshAttribute>(m, "materials"); + typename SaveMeshType::template PerMeshAttributeHandle > materialsHandle = + vcg::tri::Allocator::template FindPerMeshAttribute >(m, "materials"); std::string fn(filename); int LastSlash=fn.size()-1; diff --git a/wrap/io_trimesh/import_obj.h b/wrap/io_trimesh/import_obj.h index 8f374dda..7b6a3bde 100644 --- a/wrap/io_trimesh/import_obj.h +++ b/wrap/io_trimesh/import_obj.h @@ -256,8 +256,8 @@ namespace vcg { return E_CANTOPEN; } - typename OpenMeshType::template PerMeshAttributeHandle> materialsHandle = - vcg::tri::Allocator:: template GetPerMeshAttribute>(m, std::string("materials")); + typename OpenMeshType::template PerMeshAttributeHandle > materialsHandle = + vcg::tri::Allocator:: template GetPerMeshAttribute >(m, std::string("materials")); typename OpenMeshType::template PerFaceAttributeHandle mIndHandle = vcg::tri::Allocator:: template GetPerFaceAttribute(m, std::string("mInd")); std::vector& materials = materialsHandle(); // materials vector