From dfba8417145a7fee39f77f02958abff132611e93 Mon Sep 17 00:00:00 2001 From: cnr-isti-vclab Date: Thu, 13 Jan 2011 18:04:32 +0000 Subject: [PATCH] add support for vertex per color with OBJ files (read/write) --- wrap/io_trimesh/export_obj.h | 18 ++++++++++++---- wrap/io_trimesh/import_obj.h | 40 ++++++++++++++++++++++++++---------- 2 files changed, 43 insertions(+), 15 deletions(-) diff --git a/wrap/io_trimesh/export_obj.h b/wrap/io_trimesh/export_obj.h index 30537bcf..d01c996d 100644 --- a/wrap/io_trimesh/export_obj.h +++ b/wrap/io_trimesh/export_obj.h @@ -93,6 +93,7 @@ namespace io { //vert capability |= vcg::tri::io::Mask::IOM_VERTNORMAL; capability |= vcg::tri::io::Mask::IOM_VERTTEXCOORD; + capability |= vcg::tri::io::Mask::IOM_VERTCOLOR; //face capability |= vcg::tri::io::Mask::IOM_FACECOLOR; @@ -172,11 +173,20 @@ namespace io { //} //saves vertex - fprintf(fp,"v %f %f %f\n",(*vi).P()[0],(*vi).P()[1],(*vi).P()[2]); + + fprintf(fp,"v %f %f %f",(*vi).P()[0],(*vi).P()[1],(*vi).P()[2]); + if(mask & Mask::IOM_VERTCOLOR) + fprintf(fp," %f %f %f",double((*vi).C()[0])/255.,double((*vi).C()[1])/255.,double((*vi).C()[2])/255.); + fprintf(fp,"\n"); - if (cb !=NULL) { - if(!(*cb)((100*++current)/totalPrimitives, "writing vertices ")) - { fclose(fp); return E_ABORTED;} } + if (cb !=NULL) + { + if(!(*cb)((100*++current)/totalPrimitives, "writing vertices ")) + { + fclose(fp); + return E_ABORTED; + } + } numvert++; } assert(numvert == m.vn); diff --git a/wrap/io_trimesh/import_obj.h b/wrap/io_trimesh/import_obj.h index 508d23fd..97c6380b 100644 --- a/wrap/io_trimesh/import_obj.h +++ b/wrap/io_trimesh/import_obj.h @@ -289,7 +289,18 @@ public: // ---------------------- if (((oi.mask & vcg::tri::io::Mask::IOM_VERTCOLOR) != 0) && (m.HasPerVertexColor())) { - (*vi).C() = currentColor; + if(numTokens>=7) + { + unsigned char r = (unsigned char) ((ScalarType) atof(tokens[4].c_str()) * 255.0); + unsigned char g = (unsigned char) ((ScalarType) atof(tokens[5].c_str()) * 255.0); + unsigned char b = (unsigned char) ((ScalarType) atof(tokens[6].c_str()) * 255.0); + unsigned char alpha = (unsigned char) ((numTokens>=8 ? (ScalarType) atof(tokens[7].c_str()) : 1) * 255.0); + (*vi).C() = Color4b(r, g, b, alpha); + } + else + { + (*vi).C() = currentColor; + } } ++vi; // move to next vertex iterator @@ -865,8 +876,9 @@ public: if (length == 0) return false; - bool bHasPerFaceColor = false; - bool bHasNormals = false; + bool bHasPerFaceColor = false; + bool bHasNormals = false; + bool bHasPerVertexColor = false; oi.numVertices=0; oi.numFaces=0; @@ -886,7 +898,12 @@ public: { if(line[0]=='v') { - if(line[1]==' ') oi.numVertices++; + if(line[1]==' ') + { + oi.numVertices++; + if(line.size()>=7) + bHasPerVertexColor = true; + } if(line[1]=='t') oi.numTexCoords++; if(line[1]=='n') { oi.numNormals ++; @@ -902,15 +919,16 @@ public: } oi.mask = 0; if (oi.numTexCoords) - { - if (oi.numTexCoords==oi.numVertices) - oi.mask |= vcg::tri::io::Mask::IOM_VERTTEXCOORD; + { + if (oi.numTexCoords==oi.numVertices) + oi.mask |= vcg::tri::io::Mask::IOM_VERTTEXCOORD; oi.mask |= vcg::tri::io::Mask::IOM_WEDGTEXCOORD; - // Usually if you have tex coords you also have materials - oi.mask |= vcg::tri::io::Mask::IOM_FACECOLOR; - } - if(bHasPerFaceColor) oi.mask |= vcg::tri::io::Mask::IOM_FACECOLOR; + // Usually if you have tex coords you also have materials + oi.mask |= vcg::tri::io::Mask::IOM_FACECOLOR; + } + if(bHasPerFaceColor) oi.mask |= vcg::tri::io::Mask::IOM_FACECOLOR; + if(bHasPerVertexColor) oi.mask |= vcg::tri::io::Mask::IOM_VERTCOLOR; if (bHasNormals) { if (oi.numTexCoords==oi.numVertices) oi.mask |= vcg::tri::io::Mask::IOM_VERTNORMAL;