corrected various bugs in the export of texture coords

This commit is contained in:
Paolo Cignoni 2008-02-27 00:34:43 +00:00
parent de91366540
commit 2828e5c8e4
2 changed files with 80 additions and 82 deletions

View File

@ -25,6 +25,9 @@
History History
$Log: not supported by cvs2svn $ $Log: not supported by cvs2svn $
Revision 1.10 2007/10/17 09:49:50 cignoni
correct management of point only files
Revision 1.9 2007/03/12 16:40:16 tarini Revision 1.9 2007/03/12 16:40:16 tarini
Texture coord name change! "TCoord" and "Texture" are BAD. "TexCoord" is GOOD. Texture coord name change! "TCoord" and "Texture" are BAD. "TexCoord" is GOOD.
@ -176,16 +179,16 @@ namespace io {
/* /*
function which saves in OBJ file format function which saves in OBJ file format
*/ */
static int SaveASCII(SaveMeshType &m, const char * filename, int mask, CallBackPos *cb=0) static int Save(SaveMeshType &m, const char * filename, int mask, CallBackPos *cb=0)
{ {
if(m.vn == 0) return E_NOTVEXTEXVALID; if(m.vn == 0) return E_NOTVEXTEXVALID;
// Commented out this control. You should be allowed to save a point cloud. // Commented out this control. You should be allowed to save a point cloud.
// if(m.fn == 0) return E_NOTFACESVALID; // if(m.fn == 0) return E_NOTFACESVALID;
int current = 0; int current = 0;
int max = m.vn+ m.fn; int totalPrimitives = m.vn+m.fn;
std::vector<Material> materials; std::vector<Material> materialVec;
std::string fn(filename); std::string fn(filename);
int LastSlash=fn.size()-1; int LastSlash=fn.size()-1;
@ -194,13 +197,13 @@ namespace io {
FILE *fp; FILE *fp;
fp = fopen(filename,"w"); fp = fopen(filename,"w");
if(fp == NULL)return E_CANTOPENFILE; if(fp == NULL) return E_CANTOPENFILE;
fprintf(fp,"####\n#\n# OBJ File Generated by Meshlab\n#\n####\n"); fprintf(fp,"####\n#\n# OBJ File Generated by Meshlab\n#\n####\n");
fprintf(fp,"# Object %s\n#\n# Vertices: %d\n# Faces: %d\n#\n####\n",fn.substr(LastSlash+1).c_str(),m.vn,m.fn); fprintf(fp,"# Object %s\n#\n# Vertices: %d\n# Faces: %d\n#\n####\n",fn.substr(LastSlash+1).c_str(),m.vn,m.fn);
//library materials //library materialVec
if(mask & vcg::tri::io::Mask::IOM_FACECOLOR) if( (mask & vcg::tri::io::Mask::IOM_FACECOLOR) || (mask & Mask::IOM_WEDGTEXCOORD) )
fprintf(fp,"mtllib ./%s.mtl\n\n",fn.substr(LastSlash+1).c_str()); fprintf(fp,"mtllib ./%s.mtl\n\n",fn.substr(LastSlash+1).c_str());
//vertexs + normal //vertexs + normal
@ -208,17 +211,17 @@ namespace io {
std::map<Point3f,int> NormalVertex; std::map<Point3f,int> NormalVertex;
std::vector<int> VertexId(m.vert.size()); std::vector<int> VertexId(m.vert.size());
int numvert = 0; int numvert = 0;
int value = 1; int curNormalIndex = 1;
for(vi=m.vert.begin(); vi!=m.vert.end(); ++vi) if( !(*vi).IsD() ) for(vi=m.vert.begin(); vi!=m.vert.end(); ++vi) if( !(*vi).IsD() )
{ {
VertexId[vi-m.vert.begin()]=numvert; VertexId[vi-m.vert.begin()]=numvert;
//saves normal per vertex //saves normal per vertex
if(mask & vcg::tri::io::Mask::IOM_VERTNORMAL | mask & vcg::tri::io::Mask::IOM_WEDGNORMAL) if((mask & Mask::IOM_VERTNORMAL) || (mask & vcg::tri::io::Mask::IOM_WEDGTEXCOORD) )
{ {
if(AddNewNormalVertex(NormalVertex,(*vi).N(),value)) if(AddNewNormalVertex(NormalVertex,(*vi).N(),curNormalIndex))
{ {
fprintf(fp,"vn %f %f %f\n",(*vi).N()[0],(*vi).N()[1],(*vi).N()[2]); fprintf(fp,"vn %f %f %f\n",(*vi).N()[0],(*vi).N()[1],(*vi).N()[2]);
value++; curNormalIndex++;
} }
} }
@ -226,7 +229,7 @@ namespace io {
fprintf(fp,"v %f %f %f\n",(*vi).P()[0],(*vi).P()[1],(*vi).P()[2]); fprintf(fp,"v %f %f %f\n",(*vi).P()[0],(*vi).P()[1],(*vi).P()[2]);
if (cb !=NULL) { if (cb !=NULL) {
if(!(*cb)((100*++current)/max, "writing vertices ")) if(!(*cb)((100*++current)/totalPrimitives, "writing vertices "))
{ fclose(fp); return E_ABORTED;} } { fclose(fp); return E_ABORTED;} }
numvert++; numvert++;
} }
@ -239,68 +242,64 @@ namespace io {
std::map<vcg::TexCoord2<float>,int> CoordIndexTexture; std::map<vcg::TexCoord2<float>,int> CoordIndexTexture;
unsigned int material_num = 0; unsigned int material_num = 0;
int mem_index = 0; //var temporany int mem_index = 0; //var temporany
/*int*/ value = 1;//tmp int curTexCoordIndex = 1;
for(fi=m.face.begin(); fi!=m.face.end(); ++fi) if( !(*fi).IsD() ) for(fi=m.face.begin(); fi!=m.face.end(); ++fi) if( !(*fi).IsD() )
{ {
if(mask & vcg::tri::io::Mask::IOM_FACECOLOR) if((mask & Mask::IOM_FACECOLOR) || (mask & Mask::IOM_WEDGTEXCOORD) )
{ {
int index = vcg::tri::io::Materials<SaveMeshType>::CreateNewMaterial(m,materials,material_num,fi); int index = Materials<SaveMeshType>::CreateNewMaterial(m,materialVec,material_num,fi);
if(index == materials.size())//inserts a new element material if(index == materialVec.size())//inserts a new element material
{ {
material_num++; material_num++;
fprintf(fp,"\nusemtl material_%d\n",materials[index-1].index); fprintf(fp,"\nusemtl material_%d\n",materialVec[index-1].index);
mem_index = index-1; mem_index = index-1;
} }
else else
{ {
if(index != mem_index)//inserts old name elemente material if(index != mem_index)//inserts old name elemente material
{ {
fprintf(fp,"\nusemtl material_%d\n",materials[index].index); fprintf(fp,"\nusemtl material_%d\n",materialVec[index].index);
mem_index=index; mem_index=index;
} }
} }
} }
//saves texture coord //saves texture coord
unsigned int MAX = 3; for(unsigned int k=0;k<3;k++)
for(unsigned int k=0;k<MAX;k++)
{ {
if(m.HasPerWedgeTexCoord() && mask & vcg::tri::io::Mask::IOM_WEDGTEXCOORD) if(HasPerWedgeTexCoord(m) && (mask & Mask::IOM_WEDGTEXCOORD))
{ {
if(AddNewTextureCoord(CoordIndexTexture,(*fi).WT(k),value)) if(AddNewTextureCoord(CoordIndexTexture,(*fi).WT(k),curTexCoordIndex))
{ {
fprintf(fp,"vt %f %f\n",(*fi).WT(k).u(),(*fi).WT(k).v()); fprintf(fp,"vt %f %f\n",(*fi).WT(k).u(),(*fi).WT(k).v());
value++;//ncreases the value number to be associated to the Texture curTexCoordIndex++; //ncreases the value number to be associated to the Texture
} }
} }
} }
fprintf(fp,"f "); fprintf(fp,"f ");
for(unsigned int k=0;k<MAX;k++) for(unsigned int k=0;k<3;k++)
{ {
int v = -1; if(k!=0) fprintf(fp," ");
int vInd = -1;
// +1 because Obj file format begins from index = 1 but not from index = 0. // +1 because Obj file format begins from index = 1 but not from index = 0.
v = VertexId[GetIndexVertex(m, (*fi).V(k))] + 1;//index of vertex per face vInd = VertexId[GetIndexVertex(m, (*fi).V(k))] + 1;//index of vertex per face
int vt = -1; int vt = -1;
if(mask & vcg::tri::io::Mask::IOM_WEDGTEXCOORD) if(mask & Mask::IOM_WEDGTEXCOORD)
vt = GetIndexVertexTexture(CoordIndexTexture,(*fi).WT(k));//index of vertex texture per face vt = GetIndexVertexTexture(CoordIndexTexture,(*fi).WT(k));//index of vertex texture per face
int vn = -1; int vn = -1;
if(mask & vcg::tri::io::Mask::IOM_VERTNORMAL | mask & vcg::tri::io::Mask::IOM_WEDGNORMAL) if((mask & Mask::IOM_VERTNORMAL) || (mask & Mask::IOM_WEDGNORMAL) )
vn = GetIndexVertexNormal(m, NormalVertex, v);//index of vertex normal per face. vn = GetIndexVertexNormal(m, NormalVertex, (*fi).V(k)->cN());//index of vertex normal per face.
//writes elements on file obj //writes elements on file obj
WriteFacesElement(fp,v,vt,vn); WriteFacesElement(fp,vInd,vt,vn);
if(k!=MAX-1)
fprintf(fp," ");
else
fprintf(fp,"\n");
} }
fprintf(fp,"\n");
if (cb !=NULL) { if (cb !=NULL) {
if(!(*cb)((100*++current)/max, "writing vertices ")) if(!(*cb)((100*++current)/totalPrimitives, "writing vertices "))
{ fclose(fp); return E_ABORTED;} { fclose(fp); return E_ABORTED;}
} }
@ -311,30 +310,14 @@ namespace io {
fclose(fp); fclose(fp);
int r = 0; int r = 0;
if(mask & vcg::tri::io::Mask::IOM_WEDGTEXCOORD | mask & vcg::tri::io::Mask::IOM_FACECOLOR) if((mask & Mask::IOM_WEDGTEXCOORD) || (mask & Mask::IOM_FACECOLOR) )
r = WriteMaterials(materials, filename,cb);//write material r = WriteMaterials(materialVec, filename,cb);//write material
if(r!= E_NOERROR) if(r!= E_NOERROR)
return r; return r;
return E_NOERROR; return E_NOERROR;
} }
/*
function which saves in OBJ file format
*/
static int SaveBinary(SaveMeshType &m, const char * filename)
{
return E_NOTDEFINITION;
}
/*
function which saves in OBJ file format
*/
static int Save(SaveMeshType &m, const char * filename, const int &mask, CallBackPos *cb=0)
{
return SaveASCII(m,filename,mask,cb);
}
/* /*
returns index of the vertex returns index of the vertex
*/ */
@ -346,25 +329,37 @@ namespace io {
/* /*
returns index of the texture coord returns index of the texture coord
*/ */
inline static int GetIndexVertexTexture(std::map<vcg::TexCoord2<float>,int> &m, const vcg::TexCoord2<float> &wt) inline static int GetIndexVertexTexture(std::map<TexCoord2<float>,int> &mapTexToInt, const vcg::TexCoord2<float> &wt)
{ {
int index = m[wt]; std::map<vcg::TexCoord2<float>,int>::iterator iter= mapTexToInt.find(wt);
if(index!=0){return index;} if(iter != mapTexToInt.end()) return (*iter).second;
return -1; else return -1;
// Old wrong version.
// int index = mapTexToInt[wt];
// if(index!=0){return index;}
} }
/* /*
returns index of the vertex normal returns index of the vertex normal
*/ */
inline static int GetIndexVertexNormal(SaveMeshType &m, std::map<Point3f,int> &ma, unsigned int iv ) inline static int GetIndexVertexNormal(SaveMeshType &m, std::map<Point3f,int> &mapNormToInt, const Point3f &norm )
{ {
int index = ma[m.vert[iv].N()]; std::map<Point3f,int>::iterator iter= mapNormToInt.find(norm);
if(index!=0){return index;} if(iter != mapNormToInt.end()) return (*iter).second;
return -1; else return -1;
// Old wrong version.
// int index = mapNormToInt[m.vert[iv].N()];
// if(index!=0){return index;}
} }
/* /*
write elements on file write elements on file
f v/tc/vn v/tc/vn v/tc/vn ...
f v/tc v/tc v/tc ...
f v//vn v//vn v//vn ...
f v v v ...
*/ */
inline static void WriteFacesElement(FILE *fp,int v,int vt, int vn) inline static void WriteFacesElement(FILE *fp,int v,int vt, int vn)
{ {
@ -404,12 +399,12 @@ namespace io {
/* /*
writes material into file writes material into file
*/ */
inline static int WriteMaterials(std::vector<Material> &materials, const char * filename, CallBackPos *cb=0) inline static int WriteMaterials(std::vector<Material> &materialVec, const char * filename, CallBackPos *cb=0)
{ {
std::string fileName = std::string(filename); std::string fileName = std::string(filename);
fileName+=".mtl"; fileName+=".mtl";
if(materials.size() > 0) if(materialVec.size() > 0)
{ {
FILE *fp; FILE *fp;
fp = fopen(fileName.c_str(),"w"); fp = fopen(fileName.c_str(),"w");
@ -419,23 +414,23 @@ namespace io {
int current = 0; int current = 0;
for(unsigned int i=0;i<materials.size();i++) for(unsigned int i=0;i<materialVec.size();i++)
{ {
if (cb !=NULL) if (cb !=NULL)
(*cb)((100 * ++current)/materials.size(), "saving material file "); (*cb)((100 * ++current)/materialVec.size(), "saving material file ");
else else
{ /* fclose(fp); return E_ABORTED; */ } { /* fclose(fp); return E_ABORTED; */ }
fprintf(fp,"newmtl material_%d\n",materials[i].index); fprintf(fp,"newmtl material_%d\n",materialVec[i].index);
fprintf(fp,"Ka %f %f %f\n",materials[i].Ka[0],materials[i].Ka[1],materials[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",materials[i].Kd[0],materials[i].Kd[1],materials[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",materials[i].Ks[0],materials[i].Ks[1],materials[i].Ks[2]); fprintf(fp,"Ks %f %f %f\n",materialVec[i].Ks[0],materialVec[i].Ks[1],materialVec[i].Ks[2]);
fprintf(fp,"Tr %f\n",materials[i].Tr); fprintf(fp,"Tr %f\n",materialVec[i].Tr);
fprintf(fp,"illum %d\n",materials[i].illum); fprintf(fp,"illum %d\n",materialVec[i].illum);
fprintf(fp,"Ns %f\n",materials[i].Ns); fprintf(fp,"Ns %f\n",materialVec[i].Ns);
if(materials[i].map_Kd.size()>0) if(materialVec[i].map_Kd.size()>0)
fprintf(fp,"map_Kd %s\n",materials[i].map_Kd.c_str()); fprintf(fp,"map_Kd %s\n",materialVec[i].map_Kd.c_str());
fprintf(fp,"\n"); fprintf(fp,"\n");
} }
fclose(fp); fclose(fp);

View File

@ -25,6 +25,9 @@
History History
$Log: not supported by cvs2svn $ $Log: not supported by cvs2svn $
Revision 1.4 2007/06/20 10:28:04 tarini
"newline at end of file" and "endif" warnings fixed
Revision 1.3 2006/11/09 07:51:44 cignoni Revision 1.3 2006/11/09 07:51:44 cignoni
bug due to wrong access to eventually unexistent FaceColor bug due to wrong access to eventually unexistent FaceColor
@ -126,14 +129,14 @@ namespace io {
{ {
for(unsigned int i=0;i<materials.size();i++) for(unsigned int i=0;i<materials.size();i++)
{ {
bool ka = materials[i].Ka == mtl.Ka; if(materials[i].Kd != mtl.Kd ) continue;
bool kd = materials[i].Kd == mtl.Kd; if(materials[i].Ka != mtl.Ka ) continue;
bool ks = materials[i].Ks == mtl.Ks; if(materials[i].Ks != mtl.Ks ) continue;
bool tr = materials[i].Tr == mtl.Tr; if(materials[i].Tr != mtl.Tr ) continue;
bool illum = materials[i].illum == mtl.illum; if(materials[i].illum != mtl.illum ) continue;
bool ns = materials[i].Ns == mtl.Ns; if(materials[i].Ns != mtl.Ns ) continue;
bool map = materials[i].map_Kd == mtl.map_Kd; if(materials[i].map_Kd != mtl.map_Kd) continue;
if(ka & kd & ks & tr & illum & ns & map){return i;} return i;
} }
return -1; return -1;
} }