Significant refactoring. Cleaned up the non glutesselator code. Quad are split in a simpler way. Support for the qobj format preserved...
This commit is contained in:
parent
fca3d016d8
commit
3665268aa4
|
@ -52,6 +52,7 @@ template <class OpenMeshType>
|
|||
class ImporterOBJ
|
||||
{
|
||||
public:
|
||||
static int &MRGBLineCount(){static int _MRGBLineCount=0; return _MRGBLineCount;}
|
||||
|
||||
typedef typename OpenMeshType::VertexPointer VertexPointer;
|
||||
typedef typename OpenMeshType::ScalarType ScalarType;
|
||||
|
@ -263,13 +264,13 @@ public:
|
|||
// vertices and faces allocatetion
|
||||
VertexIterator vi = vcg::tri::Allocator<OpenMeshType>::AddVertices(m,oi.numVertices);
|
||||
//FaceIterator fi = Allocator<OpenMeshType>::AddFaces(m,oi.numFaces);
|
||||
|
||||
std::vector<Color4b> vertexColorVector;
|
||||
ObjIndexedFace ff;
|
||||
char *loadingStr = "Loading";
|
||||
const char *loadingStr = "Loading";
|
||||
while (!stream.eof())
|
||||
{
|
||||
tokens.clear();
|
||||
TokenizeNextLine(stream, tokens);
|
||||
TokenizeNextLine(stream, tokens,&vertexColorVector);
|
||||
|
||||
unsigned int numTokens = static_cast<unsigned int>(tokens.size());
|
||||
if (numTokens > 0)
|
||||
|
@ -351,12 +352,14 @@ public:
|
|||
if (numTokens < 4) return E_LESS_THAN_3VERTINFACE;
|
||||
int vertexesPerFace = static_cast<int>(tokens.size()-1);
|
||||
|
||||
if( (vertexesPerFace>3) && OpenMeshType::FaceType::HasPolyInfo() ){
|
||||
if( (vertexesPerFace>3) && OpenMeshType::FaceType::HasPolyInfo() )
|
||||
{
|
||||
//_BEGIN___ if you are loading a GENERIC POLYGON mesh
|
||||
ff.set(vertexesPerFace);
|
||||
for(int i=0;i<vertexesPerFace;++i) // remember index starts from 1 instead of 0
|
||||
for(int i=0;i<vertexesPerFace;++i) { // remember index starts from 1 instead of 0
|
||||
SplitToken(tokens[i+1], ff.v[i], ff.n[i], ff.t[i], inputMask);
|
||||
|
||||
if(QuadFlag) ff.v[i]++; // NOTE THAT THE STUPID QOBJ FORMAT IS ZERO INDEXED!!!!
|
||||
}
|
||||
if ( oi.mask & vcg::tri::io::Mask::IOM_WEDGTEXCOORD )
|
||||
{
|
||||
// verifying validity of texture coords indices
|
||||
|
@ -378,24 +381,24 @@ public:
|
|||
if(!GoodObjIndex(ff.v[i],numVertices))
|
||||
return E_BAD_VERT_INDEX;
|
||||
|
||||
// assigning face normal
|
||||
if ( oi.mask & vcg::tri::io::Mask::IOM_WEDGNORMAL )
|
||||
|
||||
if ( oi.mask & vcg::tri::io::Mask::IOM_WEDGNORMAL ) // assigning face normal
|
||||
{
|
||||
// verifying validity of vertex normal indices
|
||||
for(int i=0;i<vertexesPerFace;i++)
|
||||
if(!GoodObjIndex(ff.n[i],numVNormals)) return E_BAD_VERT_NORMAL_INDEX;
|
||||
}
|
||||
|
||||
// assigning face color
|
||||
if( oi.mask & vcg::tri::io::Mask::IOM_FACECOLOR)
|
||||
|
||||
if( oi.mask & vcg::tri::io::Mask::IOM_FACECOLOR) // assigning face color
|
||||
ff.c = currentColor;
|
||||
|
||||
++numTriangles;
|
||||
indexedFaces.push_back(ff);
|
||||
|
||||
//_END ___ if you are loading a GENERIC POLYGON mesh
|
||||
}else
|
||||
#ifdef __gl_h_
|
||||
}
|
||||
else
|
||||
{
|
||||
//_BEGIN___ if you are loading a TRIMESH mesh
|
||||
std::vector<std::vector<vcg::Point3f> > polygonVect(1); // it is a vector of polygon loops
|
||||
|
@ -408,12 +411,24 @@ public:
|
|||
for(int pi=0;pi<vertexesPerFace;++pi)
|
||||
{
|
||||
SplitToken(tokens[pi+1], indexVVect[pi],indexNVect[pi],indexTVect[pi], inputMask);
|
||||
if(QuadFlag) indexVVect[pi]++; // NOTE THAT THE STUPID QOBJ FORMAT IS ZERO INDEXED!!!!
|
||||
GoodObjIndex(indexVVect[pi],numVertices);
|
||||
GoodObjIndex(indexTVect[pi],oi.numTexCoords);
|
||||
polygonVect[0][pi]=m.vert[indexVVect[pi]].cP();
|
||||
}
|
||||
|
||||
if(vertexesPerFace<5)
|
||||
InternalFanTessellator(polygonVect, indexTriangulatedVect);
|
||||
else
|
||||
{
|
||||
#ifdef __gl_h_
|
||||
//qDebug("OK: using opengl tessellation for a polygon of %i verteces",vertexesPerFace);
|
||||
vcg::glu_tesselator::tesselate<vcg::Point3f>(polygonVect, indexTriangulatedVect);
|
||||
#else
|
||||
//qDebug("Warning: using fan tessellation for a polygon of %i verteces",vertexesPerFace);
|
||||
InternalFanTessellator(polygonVect, indexTriangulatedVect);
|
||||
#endif
|
||||
}
|
||||
extraTriangles+=((indexTriangulatedVect.size()/3) -1);
|
||||
#ifdef QT_VERSION
|
||||
if( int(indexTriangulatedVect.size()/3) != vertexesPerFace-2)
|
||||
|
@ -428,26 +443,21 @@ public:
|
|||
|
||||
for(size_t pi=0;pi<indexTriangulatedVect.size();pi+=3)
|
||||
{
|
||||
int i0= indexTriangulatedVect [pi+0];
|
||||
int i1= indexTriangulatedVect [pi+1];
|
||||
int i2= indexTriangulatedVect [pi+2];
|
||||
//qDebug("Triangle %i (%i %i %i)",pi/3,i0,i1,i2);
|
||||
|
||||
ff.set(3);
|
||||
ff.v[0]= indexVVect[i0];
|
||||
ff.v[1]= indexVVect[i1];
|
||||
ff.v[2]= indexVVect[i2];
|
||||
ff.t[0]= indexTVect[i0];
|
||||
ff.t[1]= indexTVect[i1];
|
||||
ff.t[2]= indexTVect[i2];
|
||||
int locInd[3];
|
||||
for(int iii=0;iii<3;++iii)
|
||||
{
|
||||
locInd[iii]=indexTriangulatedVect[pi+iii];
|
||||
ff.v[iii]=indexVVect[ locInd[iii] ];
|
||||
ff.t[iii]=indexTVect[ locInd[iii] ];
|
||||
}
|
||||
|
||||
// Setting internal edges: only edges formed by consecutive edges are external.
|
||||
if( (i0+1)%vertexesPerFace == i1) ff.edge[0]=false;
|
||||
else ff.edge[0]=true;
|
||||
if( (i1+1)%vertexesPerFace == i2) ff.edge[1]=false;
|
||||
else ff.edge[1]=true;
|
||||
if( (i2+1)%vertexesPerFace == i0) ff.edge[2]=false;
|
||||
else ff.edge[2]=true;
|
||||
for(int iii=0;iii<3;++iii)
|
||||
{
|
||||
if( (locInd[iii]+1)%vertexesPerFace == locInd[(iii+1)%3]) ff.edge[iii]=false;
|
||||
else ff.edge[iii]=true;
|
||||
}
|
||||
|
||||
if ( oi.mask & vcg::tri::io::Mask::IOM_WEDGTEXCOORD )
|
||||
{ // verifying validity of texture coords indices
|
||||
|
@ -478,122 +488,6 @@ public:
|
|||
}
|
||||
|
||||
}
|
||||
#else
|
||||
{
|
||||
ff.set(3);
|
||||
for(int i=0;i<3;++i)
|
||||
{ // remember index starts from 1 instead of 0
|
||||
SplitToken(tokens[i+1], ff.v[i], ff.n[i], ff.t[i], inputMask);
|
||||
if(QuadFlag) { ff.v[i]+=1; }
|
||||
}
|
||||
if ( oi.mask & vcg::tri::io::Mask::IOM_WEDGTEXCOORD )
|
||||
{
|
||||
// verifying validity of texture coords indices
|
||||
for(int i=0;i<3;i++)
|
||||
if(!GoodObjIndex(ff.t[i],oi.numTexCoords))
|
||||
return E_BAD_VERT_TEX_INDEX;
|
||||
|
||||
ff.tInd=materials[currentMaterialIdx].index;
|
||||
}
|
||||
|
||||
// verifying validity of vertex indices
|
||||
if ((ff.v[0] == ff.v[1]) || (ff.v[0] == ff.v[2]) || (ff.v[1] == ff.v[2]))
|
||||
result = E_VERTICES_WITH_SAME_IDX_IN_FACE;
|
||||
|
||||
for(int i=0;i<3;i++)
|
||||
if(!GoodObjIndex(ff.v[i],numVertices))
|
||||
return E_BAD_VERT_INDEX;
|
||||
|
||||
// assigning face normal
|
||||
if ( oi.mask & vcg::tri::io::Mask::IOM_WEDGNORMAL )
|
||||
{ // verifying validity of vertex normal indices
|
||||
for(int i=0;i<3;i++)
|
||||
if(!GoodObjIndex(ff.n[i],numVNormals)) return E_BAD_VERT_NORMAL_INDEX;
|
||||
}
|
||||
|
||||
// assigning face color
|
||||
// --------------------
|
||||
if( oi.mask & vcg::tri::io::Mask::IOM_FACECOLOR)
|
||||
ff.c = currentColor;
|
||||
|
||||
// by default there are no internal edge
|
||||
ff.edge[0]=ff.edge[1]=ff.edge[2]=false;
|
||||
if(vertexesPerFace>3) ff.edge[2]=true;
|
||||
++numTriangles;
|
||||
indexedFaces.push_back(ff);
|
||||
/*
|
||||
// A face polygon composed of more than three vertices is triangulated
|
||||
// according to the following schema:
|
||||
// v5
|
||||
// / \
|
||||
// / \
|
||||
// / \
|
||||
// v1------v4
|
||||
// |\ /
|
||||
// | \ /
|
||||
// | \ /
|
||||
// v2---v3
|
||||
//
|
||||
// As shown above, the 5 vertices polygon (v1,v2,v3,v4,v5)
|
||||
// has been split into the triangles (v1,v2,v3), (v1,v3,v4) e (v1,v4,v5).
|
||||
// This way vertex v1 becomes the common vertex of all newly generated
|
||||
// triangles, and this may lead to the creation of very thin triangles.
|
||||
*/
|
||||
|
||||
int iVertex = 3;
|
||||
while (iVertex < vertexesPerFace) // add other triangles
|
||||
{
|
||||
oi.mask |= Mask::IOM_BITPOLYGONAL;
|
||||
ObjIndexedFace ffNew=ff;
|
||||
int v4_index;
|
||||
int vt4_index;
|
||||
int vn4_index;
|
||||
|
||||
SplitToken(tokens[++iVertex], v4_index, vn4_index, vt4_index, inputMask);
|
||||
if(QuadFlag) { v4_index+=1; }
|
||||
if(!GoodObjIndex(v4_index, numVertices))
|
||||
return E_BAD_VERT_INDEX;
|
||||
|
||||
// assigning wedge texture coordinates
|
||||
// -----------------------------------
|
||||
if( oi.mask & vcg::tri::io::Mask::IOM_WEDGTEXCOORD )
|
||||
{
|
||||
// verifying validity of texture coords index
|
||||
// ------------------------------------------
|
||||
if(!GoodObjIndex(vt4_index,oi.numTexCoords))
|
||||
return E_BAD_VERT_TEX_INDEX;
|
||||
|
||||
if ( oi.mask & vcg::tri::io::Mask::IOM_WEDGNORMAL )
|
||||
if(!GoodObjIndex(vn4_index,numVNormals))
|
||||
return E_BAD_VERT_NORMAL_INDEX;
|
||||
|
||||
ffNew.t[1]=ff.t[2];
|
||||
ffNew.t[2]=vt4_index;
|
||||
}
|
||||
|
||||
if ((ff.v[0] == v4_index) || (ff.v[2] == v4_index)) result = E_VERTICES_WITH_SAME_IDX_IN_FACE;
|
||||
ffNew.v[1]=ff.v[2];
|
||||
ffNew.v[2]=v4_index;
|
||||
|
||||
// assigning face normal
|
||||
// ---------------------
|
||||
if ( oi.mask & vcg::tri::io::Mask::IOM_WEDGNORMAL )
|
||||
{
|
||||
ffNew.n[1]=ff.n[2];
|
||||
ffNew.n[2]=vn4_index;
|
||||
}
|
||||
// Setting internal edges: edge 1 (the opposite to vertex 0) is always an external edge.
|
||||
ffNew.edge[0]=true;
|
||||
ffNew.edge[1]=false;
|
||||
if(iVertex < vertexesPerFace) ffNew.edge[2]=true;
|
||||
else ffNew.edge[2]=false;
|
||||
++numTriangles;
|
||||
++extraTriangles;
|
||||
indexedFaces.push_back(ffNew);
|
||||
ff.v[2] = v4_index;
|
||||
}
|
||||
}//_END___ if you are loading a TRIMESH mesh
|
||||
#endif
|
||||
}
|
||||
else if (header.compare("mtllib")==0) // material library
|
||||
{
|
||||
|
@ -639,7 +533,8 @@ public:
|
|||
FaceIterator fi = vcg::tri::Allocator<OpenMeshType>::AddFaces(m,numTriangles);
|
||||
//-------------------------------------------------------------------------------
|
||||
|
||||
// Now the final pass to convert indexes into pointers for face to vert/norm/tex references
|
||||
// Now the final passes:
|
||||
// First Pass to convert indexes into pointers for face to vert/norm/tex references
|
||||
for(int i=0; i<numTriangles; ++i)
|
||||
{
|
||||
assert(m.face.size() == size_t(m.fn));
|
||||
|
@ -669,14 +564,8 @@ public:
|
|||
m.face[i].V(j)->N().Import(normals[indexedFaces[i].n[j]]);
|
||||
|
||||
// set faux edge flags according to internals faces
|
||||
if (indexedFaces[i].edge[j])
|
||||
{
|
||||
m.face[i].SetF(j);
|
||||
}
|
||||
else
|
||||
{
|
||||
m.face[i].ClearF(j);
|
||||
}
|
||||
if (indexedFaces[i].edge[j]) m.face[i].SetF(j);
|
||||
else m.face[i].ClearF(j);
|
||||
}
|
||||
|
||||
if (((oi.mask & vcg::tri::io::Mask::IOM_FACECOLOR) != 0) && (m.HasPerFaceColor()))
|
||||
|
@ -698,6 +587,18 @@ public:
|
|||
}
|
||||
}
|
||||
}
|
||||
// final pass to manage the ZBrush PerVertex Color that are managed into comments
|
||||
if(vertexColorVector.size()>0)
|
||||
{
|
||||
// if(vertexColorVector.size()!=m.vn){
|
||||
// qDebug("Warning Read %i vertices and %i vertex colors",m.vn,vertexColorVector.size());
|
||||
// qDebug("line count %i x 64 = %i",MRGBLineCount(), MRGBLineCount()*64);
|
||||
// }
|
||||
for(int i=0;i<m.vn;++i)
|
||||
{
|
||||
m.vert[i].C()=vertexColorVector[i];
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
} // end of Open
|
||||
|
@ -709,12 +610,39 @@ public:
|
|||
* \param stream The object providing the input stream
|
||||
* \param tokens The "tokens" in the next line
|
||||
*/
|
||||
inline static void TokenizeNextLine(std::ifstream &stream, std::vector< std::string > &tokens)
|
||||
inline static void TokenizeNextLine(std::ifstream &stream, std::vector< std::string > &tokens, std::vector<Color4b> *colVec)
|
||||
{
|
||||
if(stream.eof()) return;
|
||||
std::string line;
|
||||
do
|
||||
{
|
||||
std::getline(stream, line);
|
||||
if(colVec && line[0] == '#')
|
||||
{
|
||||
// The following MRGB block contains ZBrush Vertex Color (Polypaint)
|
||||
// and masking output as 4 hexadecimal values per vertex. The vertex color format is MMRRGGBB with up to 64 entries per MRGB line.
|
||||
if(line[1] == 'M' && line[2] == 'R' && line[3] == 'G' && line[4] == 'B')
|
||||
{ // Parsing the polycolor of ZBrush
|
||||
MRGBLineCount()++;
|
||||
size_t len = line.length();
|
||||
char buf[3]="00";
|
||||
Color4b cc(Color4b::Black);
|
||||
for(int i=6;(i+7)<len;i+=8)
|
||||
{
|
||||
for(int j=1;j<4;j++)
|
||||
{
|
||||
buf[0]=line[i+j*2+0];
|
||||
buf[1]=line[i+j*2+1];
|
||||
buf[2]=0;
|
||||
char *p;
|
||||
int val=strtoul(buf,&p,16);
|
||||
cc[j-1]= val;
|
||||
}
|
||||
colVec->push_back(cc);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
while (( line[0] == '#' || line.length()==0) && !stream.eof()); // skip comments and empty lines
|
||||
|
||||
if ((line[0] == '#') || (line.length() == 0)) // can be true only on last line of file
|
||||
|
@ -723,6 +651,7 @@ public:
|
|||
size_t from = 0;
|
||||
size_t to = 0;
|
||||
size_t length = line.size();
|
||||
|
||||
tokens.clear();
|
||||
do
|
||||
{
|
||||
|
@ -740,6 +669,14 @@ public:
|
|||
while (from<length);
|
||||
} // end TokenizeNextLine
|
||||
|
||||
|
||||
|
||||
// This function takes a token and, according to the mask, it returns the indexes of the involved vertex, normal and texcoord indexes.
|
||||
// Example. if the obj file has vertex texcoord (e.g. lines 'vt 0.444 0.5555')
|
||||
// when parsing a line like
|
||||
// f 46/303 619/325 624/326 623/327
|
||||
// if in the mask you have specified to read wedge tex coord
|
||||
// for the first token it will return inside vId and tId the corresponding indexes 46 and 303 )
|
||||
inline static void SplitToken(std::string token, int &vId, int &nId, int &tId, int mask)
|
||||
{
|
||||
std::string vertex;
|
||||
|
@ -965,7 +902,7 @@ public:
|
|||
while (!stream.eof())
|
||||
{
|
||||
tokens.clear();
|
||||
TokenizeNextLine(stream, tokens);
|
||||
TokenizeNextLine(stream, tokens,0);
|
||||
|
||||
if (tokens.size() > 0)
|
||||
{
|
||||
|
@ -1076,6 +1013,39 @@ public:
|
|||
return true;
|
||||
}
|
||||
|
||||
// A face polygon composed of more than three vertices is triangulated
|
||||
// according to the following schema:
|
||||
// v5
|
||||
// / \
|
||||
// / \
|
||||
// / \
|
||||
// v1------v4
|
||||
// |\ /
|
||||
// | \ /
|
||||
// | \ /
|
||||
// v2---v3
|
||||
//
|
||||
// As shown above, the 5 vertices polygon (v1,v2,v3,v4,v5)
|
||||
// has been split into the triangles (v1,v2,v3), (v1,v3,v4) e (v1,v4,v5).
|
||||
// This way vertex v1 becomes the common vertex of all newly generated
|
||||
// triangles, and this may lead to the creation of very thin triangles.
|
||||
//
|
||||
// This function is intended as a trivial fallback when glutessellator is not available.
|
||||
// it assumes just ONE outline
|
||||
static void InternalFanTessellator(const std::vector< std::vector<Point3f> > & outlines, std::vector<int> & indices)
|
||||
{
|
||||
indices.clear();
|
||||
if(outlines.empty()) return;
|
||||
const std::vector<Point3f> &points=outlines[0];
|
||||
|
||||
for(int i=0;i<points.size()-2;++i)
|
||||
{
|
||||
indices.push_back(0);
|
||||
indices.push_back(i+1);
|
||||
indices.push_back(i+2);
|
||||
}
|
||||
}
|
||||
|
||||
}; // end class
|
||||
} // end Namespace tri
|
||||
} // end Namespace io
|
||||
|
|
Loading…
Reference in New Issue