Add a per-mesh attribute in OBJ importer to hold a

std::vector<Material>, and a per-face attribute to hold an index into
that vector.
This commit is contained in:
John Senneker 2016-11-22 16:21:57 -05:00
parent 1abba4a694
commit dc3f714b34
2 changed files with 25 additions and 25 deletions

View File

@ -28,7 +28,6 @@
#include <wrap/callback.h>
#include <wrap/io_trimesh/io_mask.h>
#include "io_material.h"
#include <iostream>
#include <fstream>
#include <map>
@ -129,7 +128,8 @@ public:
int current = 0;
int totalPrimitives = m.vn+m.fn;
std::vector<Material> materialVec;
typename SaveMeshType::template PerMeshAttributeHandle<std::vector<Material>> materialsHandle =
vcg::tri::Allocator<SaveMeshType>::template FindPerMeshAttribute<std::vector<Material>>(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<int> mIndHandle =
vcg::tri::Allocator<SaveMeshType>::template FindPerFaceAttribute<int>(m, "mInd");
std::map<TexCoordType,int> 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]);

View File

@ -51,8 +51,6 @@ namespace vcg {
template <class OpenMeshType>
class ImporterOBJ
{
private:
std::vector<Material> 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<std::vector<Material>> hMaterial =
typename OpenMeshType::template PerMeshAttributeHandle<std::vector<Material>> materialsHandle =
vcg::tri::Allocator<OpenMeshType>:: template GetPerMeshAttribute<std::vector<Material>>(m, std::string("materials"));
std::vector<Material> materials = hMaterial(); // materials vector
typename OpenMeshType::template PerFaceAttributeHandle<int> mIndHandle =
vcg::tri::Allocator<OpenMeshType>:: template GetPerFaceAttribute<int>(m, std::string("mInd"));
std::vector<Material>& materials = materialsHandle(); // materials vector
std::vector<ObjTexCoord> texCoords; // texture coordinates
std::vector<CoordType> normals; // vertex normals
std::vector<ObjIndexedFace> 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())