Merge pull request #6 from johnmaf/feature/specular-attributes

Add material attributes in OBJ importer
This commit is contained in:
Paolo Cignoni 2017-04-12 10:29:46 +02:00 committed by GitHub
commit d7e9209a4f
2 changed files with 30 additions and 22 deletions

View File

@ -28,7 +28,7 @@
#include <wrap/callback.h> #include <wrap/callback.h>
#include <wrap/io_trimesh/io_mask.h> #include <wrap/io_trimesh/io_mask.h>
#include "io_material.h" #include <wrap/io_trimesh/io_material.h>
#include <iostream> #include <iostream>
#include <fstream> #include <fstream>
#include <map> #include <map>
@ -129,7 +129,8 @@ public:
int current = 0; int current = 0;
int totalPrimitives = m.vn+m.fn; 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); std::string fn(filename);
int LastSlash=fn.size()-1; int LastSlash=fn.size()-1;
@ -197,29 +198,19 @@ public:
fprintf(fp,"# %d vertices, %d vertices normals\n\n",m.vn,int(NormalVertex.size())); fprintf(fp,"# %d vertices, %d vertices normals\n\n",m.vn,int(NormalVertex.size()));
//faces + texture coords //faces + texture coords
typename SaveMeshType::template PerFaceAttributeHandle<int> mIndHandle =
vcg::tri::Allocator<SaveMeshType>::template FindPerFaceAttribute<int>(m, "mInd");
std::map<TexCoordType,int> CoordIndexTexture; std::map<TexCoordType,int> CoordIndexTexture;
unsigned int material_num = 0;
int mem_index = 0; //var temporany
int curTexCoordIndex = 1; int curTexCoordIndex = 1;
int curMatIndex = -1;
for(FaceIterator fi=m.face.begin(); fi!=m.face.end(); ++fi) if( !(*fi).IsD() ) 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)) if((mask & Mask::IOM_FACECOLOR) || (mask & Mask::IOM_WEDGTEXCOORD) || (mask & Mask::IOM_VERTTEXCOORD))
{ {
int index = Materials<SaveMeshType>::CreateNewMaterial(m,materialVec,material_num,fi); int index = mIndHandle[fi];
if(index != curMatIndex) {
if(index == (int)materialVec.size())//inserts a new element material fprintf(fp,"\nusemtl material_%d\n", index);
{ curMatIndex = index;
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;
}
} }
} }
@ -281,7 +272,7 @@ public:
int errCode = E_NOERROR; int errCode = E_NOERROR;
if((mask & Mask::IOM_WEDGTEXCOORD) || (mask & Mask::IOM_FACECOLOR) || (mask & Mask::IOM_VERTTEXCOORD) ) 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) if(errCode!= E_NOERROR)
return errCode; return errCode;
@ -382,7 +373,7 @@ public:
else else
{ /* fclose(fp); return E_ABORTED; */ } { /* 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,"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,"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]); fprintf(fp,"Ks %f %f %f\n",materialVec[i].Ks[0],materialVec[i].Ks[1],materialVec[i].Ks[2]);

View File

@ -113,6 +113,7 @@ namespace vcg {
int tInd; int tInd;
bool edge[3];// useless if the face is a polygon, no need to have variable length array bool edge[3];// useless if the face is a polygon, no need to have variable length array
Color4b c; Color4b c;
int mInd;
}; };
struct ObjEdge struct ObjEdge
@ -254,7 +255,12 @@ namespace vcg {
stream.close(); stream.close();
return E_CANTOPEN; return E_CANTOPEN;
} }
std::vector<Material> materials; // materials vector
typename OpenMeshType::template PerMeshAttributeHandle<std::vector<Material> > materialsHandle =
vcg::tri::Allocator<OpenMeshType>:: template GetPerMeshAttribute<std::vector<Material> >(m, std::string("materials"));
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<ObjTexCoord> texCoords; // texture coordinates
std::vector<CoordType> normals; // vertex normals std::vector<CoordType> normals; // vertex normals
std::vector<ObjIndexedFace> indexedFaces; std::vector<ObjIndexedFace> indexedFaces;
@ -586,6 +592,8 @@ namespace vcg {
// assigning face color // assigning face color
if( oi.mask & vcg::tri::io::Mask::IOM_FACECOLOR) ff.c = currentColor; if( oi.mask & vcg::tri::io::Mask::IOM_FACECOLOR) ff.c = currentColor;
ff.mInd = currentMaterialIdx;
++numTriangles; ++numTriangles;
indexedFaces.push_back(ff); indexedFaces.push_back(ff);
} }
@ -711,6 +719,7 @@ namespace vcg {
if (((oi.mask & vcg::tri::io::Mask::IOM_FACECOLOR) != 0) && (HasPerFaceColor(m))) if (((oi.mask & vcg::tri::io::Mask::IOM_FACECOLOR) != 0) && (HasPerFaceColor(m)))
{ {
m.face[i].C() = indexedFaces[i].c; m.face[i].C() = indexedFaces[i].c;
mIndHandle[i] = indexedFaces[i].mInd;
} }
if (((oi.mask & vcg::tri::io::Mask::IOM_WEDGNORMAL) != 0) && (HasPerWedgeNormal(m))) if (((oi.mask & vcg::tri::io::Mask::IOM_WEDGNORMAL) != 0) && (HasPerWedgeNormal(m)))
@ -958,7 +967,15 @@ namespace vcg {
materials.clear(); materials.clear();
Material currentMaterial; Material currentMaterial;
// Fill in some default values for the material
currentMaterial.index = (unsigned int)(-1); 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; bool first = true;
while (!stream.eof()) while (!stream.eof())