Load Texture information from obj
git-svn-id: http://www.openmesh.org/svnrepo/OpenMesh/trunk@98 fdac6126-5c0c-442c-9429-916003d36597
This commit is contained in:
@@ -80,6 +80,9 @@ public:
|
|||||||
// add texture coordinates per face, _vh references the first texcoord
|
// add texture coordinates per face, _vh references the first texcoord
|
||||||
virtual void add_face_texcoords( FaceHandle _fh, VertexHandle _vh, const std::vector<Vec2f>& _face_texcoords) = 0;
|
virtual void add_face_texcoords( FaceHandle _fh, VertexHandle _vh, const std::vector<Vec2f>& _face_texcoords) = 0;
|
||||||
|
|
||||||
|
// Set the texture index for a face
|
||||||
|
virtual void set_face_texindex( FaceHandle _fh, int _texId ) = 0;
|
||||||
|
|
||||||
// set vertex normal
|
// set vertex normal
|
||||||
virtual void set_normal(VertexHandle _vh, const Vec3f& _normal) = 0;
|
virtual void set_normal(VertexHandle _vh, const Vec3f& _normal) = 0;
|
||||||
|
|
||||||
@@ -104,6 +107,10 @@ public:
|
|||||||
// set face color
|
// set face color
|
||||||
virtual void set_color(FaceHandle _fh, const Vec4uc& _color) = 0;
|
virtual void set_color(FaceHandle _fh, const Vec4uc& _color) = 0;
|
||||||
|
|
||||||
|
// Store a property in the mesh mapping from an int to a texture file
|
||||||
|
// Use set_face_texindex to set the index for each face
|
||||||
|
virtual void add_texture_information( int _id , std::string _name ) = 0;
|
||||||
|
|
||||||
// get reference to base kernel
|
// get reference to base kernel
|
||||||
virtual BaseKernel* kernel() { return 0; }
|
virtual BaseKernel* kernel() { return 0; }
|
||||||
|
|
||||||
|
|||||||
@@ -120,25 +120,6 @@ public:
|
|||||||
return fh;
|
return fh;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
virtual void add_face_texcoords( FaceHandle _fh, VertexHandle _vh, const std::vector<Vec2f>& _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
|
// vertex attributes
|
||||||
|
|
||||||
virtual void set_normal(VertexHandle _vh, const Vec3f& _normal)
|
virtual void set_normal(VertexHandle _vh, const Vec3f& _normal)
|
||||||
@@ -193,6 +174,40 @@ public:
|
|||||||
mesh_.set_color(_fh, color_cast<Color>(_color));
|
mesh_.set_color(_fh, color_cast<Color>(_color));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
virtual void add_face_texcoords( FaceHandle _fh, VertexHandle _vh, const std::vector<Vec2f>& _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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual void set_face_texindex( FaceHandle _fh, int _texId ) {
|
||||||
|
if ( mesh_.has_face_texture_index() ) {
|
||||||
|
mesh_.set_texture_index(_fh , _texId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual void add_texture_information( int _id , std::string _name ) {
|
||||||
|
OpenMesh::MPropHandleT< std::map< int, std::string > > property;
|
||||||
|
|
||||||
|
if ( !mesh_.get_property_handle(property,"TextureMapping") ) {
|
||||||
|
mesh_.add_property(property,"TextureMapping");
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( mesh_.property(property).find( _id ) == mesh_.property(property).end() )
|
||||||
|
mesh_.property(property)[_id] = _name;
|
||||||
|
}
|
||||||
|
|
||||||
// low-level access to mesh
|
// low-level access to mesh
|
||||||
|
|
||||||
virtual BaseKernel* kernel() { return &mesh_; }
|
virtual BaseKernel* kernel() { return &mesh_; }
|
||||||
|
|||||||
@@ -64,6 +64,21 @@ _OBJReader_& OBJReader() { return __OBJReaderInstance; }
|
|||||||
|
|
||||||
//=== IMPLEMENTATION ==========================================================
|
//=== IMPLEMENTATION ==========================================================
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
void trimString( std::string& _string) {
|
||||||
|
// Trim Both leading and trailing spaces
|
||||||
|
|
||||||
|
size_t start = _string.find_first_not_of(" \t");
|
||||||
|
size_t end = _string.find_last_not_of(" \t");
|
||||||
|
|
||||||
|
if(( std::string::npos == start ) || ( std::string::npos == end))
|
||||||
|
_string = "";
|
||||||
|
else
|
||||||
|
_string = _string.substr( start, end-start+1 );
|
||||||
|
}
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
_OBJReader_::
|
_OBJReader_::
|
||||||
_OBJReader_()
|
_OBJReader_()
|
||||||
@@ -115,11 +130,13 @@ read_material(std::fstream& _in)
|
|||||||
{
|
{
|
||||||
std::string line;
|
std::string line;
|
||||||
std::string keyWrd;
|
std::string keyWrd;
|
||||||
|
std::string textureName;
|
||||||
|
|
||||||
std::string key;
|
std::string key;
|
||||||
Material mat;
|
Material mat;
|
||||||
float f1,f2,f3;
|
float f1,f2,f3;
|
||||||
bool indef = false;
|
bool indef = false;
|
||||||
|
int textureId = 1;
|
||||||
|
|
||||||
|
|
||||||
mat.cleanup();
|
mat.cleanup();
|
||||||
@@ -190,7 +207,6 @@ read_material(std::fstream& _in)
|
|||||||
|
|
||||||
else if (keyWrd == "map_") // map images
|
else if (keyWrd == "map_") // map images
|
||||||
{
|
{
|
||||||
// map_Kd, diffuse map
|
|
||||||
// map_Ks, specular map
|
// map_Ks, specular map
|
||||||
// map_Ka, ambient map
|
// map_Ka, ambient map
|
||||||
// map_Bump, bump map
|
// map_Bump, bump map
|
||||||
@@ -198,7 +214,12 @@ read_material(std::fstream& _in)
|
|||||||
; // just skip this
|
; // just skip this
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
else if (keyWrd == "map_Kd" ) {
|
||||||
|
// Get the rest of the line, removeing leading or trailing spaces
|
||||||
|
std::getline(stream,textureName);
|
||||||
|
trimString(textureName);
|
||||||
|
mat.set_map_Kd( textureName, textureId++ );
|
||||||
|
}
|
||||||
else if (keyWrd == "Tr") // transparency value
|
else if (keyWrd == "Tr") // transparency value
|
||||||
{
|
{
|
||||||
stream >> f1;
|
stream >> f1;
|
||||||
@@ -222,7 +243,6 @@ read_material(std::fstream& _in)
|
|||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
bool
|
bool
|
||||||
_OBJReader_::
|
_OBJReader_::
|
||||||
read(std::fstream& _in, BaseImporter& _bi, Options& _opt)
|
read(std::fstream& _in, BaseImporter& _bi, Options& _opt)
|
||||||
@@ -238,10 +258,8 @@ read(std::fstream& _in, BaseImporter& _bi, Options& _opt)
|
|||||||
BaseImporter::VHandles vhandles;
|
BaseImporter::VHandles vhandles;
|
||||||
std::vector<Vec3f> normals;
|
std::vector<Vec3f> normals;
|
||||||
std::vector<Vec2f> texcoords;
|
std::vector<Vec2f> texcoords;
|
||||||
|
|
||||||
std::vector<Vec2f> face_texcoords;
|
std::vector<Vec2f> face_texcoords;
|
||||||
|
|
||||||
std::map<std::string,Material> materials;
|
|
||||||
std::string matname;
|
std::string matname;
|
||||||
|
|
||||||
|
|
||||||
@@ -254,14 +272,7 @@ read(std::fstream& _in, BaseImporter& _bi, Options& _opt)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Trim Both leading and trailing spaces
|
// Trim Both leading and trailing spaces
|
||||||
|
trimString(line);
|
||||||
size_t start = line.find_first_not_of(" \t");
|
|
||||||
size_t end = line.find_last_not_of(" \t");
|
|
||||||
|
|
||||||
if(( std::string::npos == start ) || ( std::string::npos == end))
|
|
||||||
line = "";
|
|
||||||
else
|
|
||||||
line = line.substr( start, end-start+1 );
|
|
||||||
|
|
||||||
// comment
|
// comment
|
||||||
if ( line.size() == 0 || line[0] == '#' || isspace(line[0]) ) {
|
if ( line.size() == 0 || line[0] == '#' || isspace(line[0]) ) {
|
||||||
@@ -290,10 +301,22 @@ read(std::fstream& _in, BaseImporter& _bi, Options& _opt)
|
|||||||
if ( !read_material( matStream ) )
|
if ( !read_material( matStream ) )
|
||||||
omerr() << " Warning! Could not read file properly!\n";
|
omerr() << " Warning! Could not read file properly!\n";
|
||||||
matStream.close();
|
matStream.close();
|
||||||
omlog() << " " << materials_.size() << " materials loaded.\n";
|
|
||||||
|
|
||||||
}else
|
}else
|
||||||
omerr() << " Warning! Material file '" << matFile << "' not found!\n";
|
omerr() << " Warning! Material file '" << matFile << "' not found!\n";
|
||||||
|
|
||||||
|
omlog() << " " << materials_.size() << " materials loaded.\n";
|
||||||
|
|
||||||
|
for ( MaterialList::iterator material = materials_.begin(); material != materials_.end(); material++ )
|
||||||
|
{
|
||||||
|
|
||||||
|
if ( (*material).second.has_map_Kd() ) {
|
||||||
|
std::string filename = path_ + (*material).second.map_Kd();
|
||||||
|
_bi.add_texture_information( (*material).second.map_Kd_index() , filename );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// mat.has_map_Kd();
|
||||||
}
|
}
|
||||||
|
|
||||||
// usemtl
|
// usemtl
|
||||||
@@ -467,7 +490,7 @@ read(std::fstream& _in, BaseImporter& _bi, Options& _opt)
|
|||||||
if( !vhandles.empty() )
|
if( !vhandles.empty() )
|
||||||
_bi.add_face_texcoords( fh, vhandles[0], face_texcoords );
|
_bi.add_face_texcoords( fh, vhandles[0], face_texcoords );
|
||||||
|
|
||||||
if ( !matname.empty() && materials_[matname].has_Kd() )
|
if ( !matname.empty() )
|
||||||
{
|
{
|
||||||
std::vector<FaceHandle> newfaces;
|
std::vector<FaceHandle> newfaces;
|
||||||
|
|
||||||
@@ -476,6 +499,7 @@ read(std::fstream& _in, BaseImporter& _bi, Options& _opt)
|
|||||||
|
|
||||||
Material& mat = materials_[matname];
|
Material& mat = materials_[matname];
|
||||||
|
|
||||||
|
if ( mat.has_Kd() ) {
|
||||||
Vec3uc fc = color_cast<Vec3uc, Vec3f>(mat.Kd());
|
Vec3uc fc = color_cast<Vec3uc, Vec3f>(mat.Kd());
|
||||||
|
|
||||||
for (std::vector<FaceHandle>::iterator it = newfaces.begin();
|
for (std::vector<FaceHandle>::iterator it = newfaces.begin();
|
||||||
@@ -484,7 +508,35 @@ read(std::fstream& _in, BaseImporter& _bi, Options& _opt)
|
|||||||
|
|
||||||
_opt += Options::FaceColor;
|
_opt += Options::FaceColor;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Set the texture index in the face index property
|
||||||
|
if ( mat.has_map_Kd() ) {
|
||||||
|
|
||||||
|
for (std::vector<FaceHandle>::iterator it = newfaces.begin();
|
||||||
|
it != newfaces.end(); ++it)
|
||||||
|
_bi.set_face_texindex( *it, mat.map_Kd_index() );
|
||||||
|
|
||||||
|
} else {
|
||||||
|
// If we don't have the info, set it to no texture
|
||||||
|
for (std::vector<FaceHandle>::iterator it = newfaces.begin();
|
||||||
|
it != newfaces.end(); ++it)
|
||||||
|
_bi.set_face_texindex( *it, 0 );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
std::vector<FaceHandle> newfaces;
|
||||||
|
|
||||||
|
for( size_t i=0; i < _bi.n_faces()-n_faces; ++i )
|
||||||
|
newfaces.push_back(FaceHandle(n_faces+i));
|
||||||
|
|
||||||
|
// Set the texture index to zero as we don't have any information
|
||||||
|
for (std::vector<FaceHandle>::iterator it = newfaces.begin();
|
||||||
|
it != newfaces.end(); ++it)
|
||||||
|
_bi.set_face_texindex( *it, 0 );
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
@@ -93,6 +93,7 @@ private:
|
|||||||
Ka_is_set_ = false;
|
Ka_is_set_ = false;
|
||||||
Ks_is_set_ = false;
|
Ks_is_set_ = false;
|
||||||
Tr_is_set_ = false;
|
Tr_is_set_ = false;
|
||||||
|
map_Kd_is_set_ = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool is_valid(void) const
|
bool is_valid(void) const
|
||||||
@@ -102,6 +103,7 @@ private:
|
|||||||
bool has_Ka(void) { return Ka_is_set_; }
|
bool has_Ka(void) { return Ka_is_set_; }
|
||||||
bool has_Ks(void) { return Ks_is_set_; }
|
bool has_Ks(void) { return Ks_is_set_; }
|
||||||
bool has_Tr(void) { return Tr_is_set_; }
|
bool has_Tr(void) { return Tr_is_set_; }
|
||||||
|
bool has_map_Kd(void) { return map_Kd_is_set_; }
|
||||||
|
|
||||||
void set_Kd( float r, float g, float b )
|
void set_Kd( float r, float g, float b )
|
||||||
{ Kd_=Vec3f(r,g,b); Kd_is_set_=true; }
|
{ Kd_=Vec3f(r,g,b); Kd_is_set_=true; }
|
||||||
@@ -115,10 +117,15 @@ private:
|
|||||||
void set_Tr( float t )
|
void set_Tr( float t )
|
||||||
{ Tr_=t; Tr_is_set_=true; }
|
{ Tr_=t; Tr_is_set_=true; }
|
||||||
|
|
||||||
|
void set_map_Kd( std::string _name, int _index_Kd )
|
||||||
|
{ map_Kd_ = _name, index_Kd_ = _index_Kd; map_Kd_is_set_ = true; };
|
||||||
|
|
||||||
const Vec3f& Kd( void ) const { return Kd_; }
|
const Vec3f& Kd( void ) const { return Kd_; }
|
||||||
const Vec3f& Ka( void ) const { return Ka_; }
|
const Vec3f& Ka( void ) const { return Ka_; }
|
||||||
const Vec3f& Ks( void ) const { return Ks_; }
|
const Vec3f& Ks( void ) const { return Ks_; }
|
||||||
float Tr( void ) const { return Tr_; }
|
float Tr( void ) const { return Tr_; }
|
||||||
|
const std::string& map_Kd( void ) { return map_Kd_ ; }
|
||||||
|
const int& map_Kd_index( void ) { return index_Kd_ ; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
@@ -126,6 +133,9 @@ private:
|
|||||||
Vec3f Ka_; bool Ka_is_set_; // ambient
|
Vec3f Ka_; bool Ka_is_set_; // ambient
|
||||||
Vec3f Ks_; bool Ks_is_set_; // specular
|
Vec3f Ks_; bool Ks_is_set_; // specular
|
||||||
float Tr_; bool Tr_is_set_; // transperency
|
float Tr_; bool Tr_is_set_; // transperency
|
||||||
|
|
||||||
|
std::string map_Kd_; int index_Kd_; bool map_Kd_is_set_; // Texture
|
||||||
|
|
||||||
};
|
};
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user