diff --git a/Core/IO/importer/BaseImporter.hh b/Core/IO/importer/BaseImporter.hh index 428ea8ca..119f60de 100644 --- a/Core/IO/importer/BaseImporter.hh +++ b/Core/IO/importer/BaseImporter.hh @@ -77,6 +77,9 @@ public: typedef std::vector VHandles; virtual FaceHandle add_face(const VHandles& _indices) = 0; + // add texture coordinates per face, _vh references the first texcoord + virtual void add_face_texcoords( FaceHandle _fh, VertexHandle _vh, const std::vector& _face_texcoords) = 0; + // set vertex normal virtual void set_normal(VertexHandle _vh, const Vec3f& _normal) = 0; @@ -89,6 +92,9 @@ public: // set vertex texture coordinate virtual void set_texcoord(VertexHandle _vh, const Vec2f& _texcoord) = 0; + // set vertex texture coordinate + virtual void set_texcoord(HalfedgeHandle _heh, const Vec2f& _texcoord) = 0; + // set face normal virtual void set_normal(FaceHandle _fh, const Vec3f& _normal) = 0; diff --git a/Core/IO/importer/ImporterT.hh b/Core/IO/importer/ImporterT.hh index 54cb918a..092df671 100644 --- a/Core/IO/importer/ImporterT.hh +++ b/Core/IO/importer/ImporterT.hh @@ -121,6 +121,24 @@ public: } + virtual void add_face_texcoords( FaceHandle _fh, VertexHandle _vh, const std::vector& _face_texcoords) + { + // get first halfedge handle + HalfedgeHandle cur_heh = mesh_.halfedge_handle(_fh); + HalfedgeHandle end_heh = mesh_.prev_halfedge_handle(cur_heh); + + // find start heh + while( mesh_.to_vertex_handle(cur_heh) != _vh && cur_heh != end_heh ) + cur_heh = mesh_.next_halfedge_handle( cur_heh); + + for(unsigned int i=0; i<_face_texcoords.size(); ++i) + { + set_texcoord( cur_heh, _face_texcoords[i]); + cur_heh = mesh_.next_halfedge_handle( cur_heh); + } + } + + // vertex attributes virtual void set_normal(VertexHandle _vh, const Vec3f& _normal) @@ -148,6 +166,12 @@ public: mesh_.set_texcoord2D(_vh, vector_cast(_texcoord)); } + virtual void set_texcoord(HalfedgeHandle _heh, const Vec2f& _texcoord) + { + if (mesh_.has_halfedge_texcoords2D()) + mesh_.set_texcoord2D(_heh, vector_cast(_texcoord)); + } + // face attributes diff --git a/Core/IO/reader/OBJReader.cc b/Core/IO/reader/OBJReader.cc index cba3a153..d33d6fda 100644 --- a/Core/IO/reader/OBJReader.cc +++ b/Core/IO/reader/OBJReader.cc @@ -239,6 +239,8 @@ read(std::fstream& _in, BaseImporter& _bi, Options& _opt) std::vector normals; std::vector texcoords; + std::vector face_texcoords; + std::map materials; std::string matname; @@ -343,6 +345,7 @@ read(std::fstream& _in, BaseImporter& _bi, Options& _opt) int value; vhandles.clear(); + face_texcoords.clear(); // read full line after detecting a face std::string faceLine; @@ -419,6 +422,7 @@ read(std::fstream& _in, BaseImporter& _bi, Options& _opt) assert(!vhandles.empty()); assert((unsigned int)(value-1) < texcoords.size()); _bi.set_texcoord(vhandles.back(), texcoords[value-1]); + face_texcoords.push_back( texcoords[value-1] ); break; case 2: // normal @@ -446,6 +450,9 @@ read(std::fstream& _in, BaseImporter& _bi, Options& _opt) size_t n_faces = _bi.n_faces(); FaceHandle fh = _bi.add_face(vhandles); + if( !vhandles.empty() ) + _bi.add_face_texcoords( fh, vhandles[0], face_texcoords ); + if ( !matname.empty() && materials_[matname].has_Kd() ) { std::vector newfaces;