58 #include <OpenMesh/Core/IO/BinaryHelper.hh>
59 #include <OpenMesh/Core/IO/writer/OBJWriter.hh>
60 #include <OpenMesh/Core/IO/IOManager.hh>
61 #include <OpenMesh/Core/Utils/color_cast.hh>
91 std::fstream out(_filename.c_str(), std::ios_base::out );
95 omerr() <<
"[OBJWriter] : cannot open file "
96 << _filename << std::endl;
100 out.precision(_precision);
104 std::string::size_type
dot = _filename.find_last_of(
"\\/");
106 std::string::size_type dot = _filename.rfind(
"/");
109 if (dot == std::string::npos){
111 objName_ = _filename;
113 path_ = _filename.substr(0,dot+1);
114 objName_ = _filename.substr(dot+1);
118 dot = objName_.find_last_of(
".");
120 if(dot != std::string::npos)
121 objName_ = objName_.substr(0,dot);
124 bool result =
write(out, _be, _opt, _precision);
134 for (
size_t i=0; i < material_.size(); i++)
135 if(material_[i] == _color)
139 material_.push_back( _color );
140 return material_.size()-1;
147 for (
size_t i=0; i < materialA_.size(); i++)
148 if(materialA_[i] == _color)
152 materialA_.push_back( _color );
153 return materialA_.size()-1;
160 writeMaterial(std::ostream& _out,
BaseExporter& _be, Options _opt)
const
169 for (
size_t i=0, nF=_be.n_faces(); i<nF; ++i)
172 if ( _opt.color_has_alpha() ){
183 if ( _opt.color_has_alpha() )
184 for (
size_t i=0; i < materialA_.size(); i++){
185 _out <<
"newmtl " <<
"mat" << i <<
'\n';
186 _out <<
"Ka 0.5000 0.5000 0.5000" <<
'\n';
187 _out <<
"Kd " << materialA_[i][0] <<
' ' << materialA_[i][1] <<
' ' << materialA_[i][2] <<
'\n';
188 _out <<
"Tr " << materialA_[i][3] <<
'\n';
189 _out <<
"illum 1" <<
'\n';
192 for (
size_t i=0; i < material_.size(); i++){
193 _out <<
"newmtl " <<
"mat" << i <<
'\n';
194 _out <<
"Ka 0.5000 0.5000 0.5000" <<
'\n';
195 _out <<
"Kd " << material_[i][0] <<
' ' << material_[i][1] <<
' ' << material_[i][2] <<
'\n';
196 _out <<
"illum 1" <<
'\n';
214 std::vector<VertexHandle> vhandles;
215 bool useMatrial =
false;
219 omlog() <<
"[OBJWriter] : write file\n";
221 _out.precision(_precision);
224 if (!check( _be, _opt))
237 std::string matFile = path_ + objName_ +
".mat";
239 std::fstream matStream(matFile.c_str(), std::ios_base::out );
243 omerr() <<
"[OBJWriter] : cannot write material file " << matFile << std::endl;
246 useMatrial = writeMaterial(matStream, _be, _opt);
253 _out <<
"# " << _be.n_vertices() <<
" vertices, ";
254 _out << _be.n_faces() <<
" faces" <<
'\n';
258 _out <<
"mtllib " << objName_ <<
".mat" <<
'\n';
260 std::map<Vec2f,int> texMap;
264 std::vector<Vec2f> texCoords;
266 unsigned int num = _be.get_face_texcoords(texCoords);
267 for(
unsigned int i = 0; i < num ; ++i)
269 texMap[texCoords[i]] = i;
276 for (
int i=0, nF=_be.n_faces(); i<nF; ++i)
279 t = _be.texcoord(vh);
289 for(std::map<Vec2f,int>::iterator it = texMap.begin(); it != texMap.end() ; ++it)
291 _out <<
"vt " << it->first[0] <<
" " << it->first[1] <<
'\n';
292 it->second = ++texCount;
297 for (i=0, nV=_be.n_vertices(); i<nV; ++i)
302 t = _be.texcoord(vh);
304 _out <<
"v " << v[0] <<
" "<< v[1] <<
" "<< v[2] <<
'\n';
307 _out <<
"vn " << n[0] <<
" "<< n[1] <<
" "<< n[2] <<
'\n';
310 size_t lastMat = std::numeric_limits<std::size_t>::max();
318 for (i=0, nF=_be.n_faces(); i<nF; ++i)
322 size_t material = std::numeric_limits<std::size_t>::max();
325 if ( _opt.color_has_alpha() ){
327 material = getMaterial(cA);
331 material = getMaterial(c);
335 if(lastMat != material) {
336 _out <<
"usemtl mat" << material <<
'\n';
343 _be.get_vhandles(
FaceHandle(
int(i)), vhandles);
345 for (j=0; j< vhandles.size(); ++j)
349 idx = vhandles[j].idx() + 1;
366 _out << texMap[_be.texcoord(vh)];
_OBJWriter_ __OBJWriterinstance
Declare the single entity of the OBJ writer.
osg::Vec3f::ValueType dot(const osg::Vec3f &_v1, const osg::Vec3f &_v2)
Adapter for osg vector member computing a scalar product.
Has (r) / store (w) face colors.
Has (r) / store (w) vertex normals.
Handle for a vertex entity.
Has (r) / store (w) face normals.
Has (r) / store (w) texture coordinates.
virtual HalfedgeHandle getHeh(FaceHandle _fh, VertexHandle _vh) const =0
getHeh returns the HalfEdgeHandle that belongs to the face specified by _fh and has a toVertexHandle ...
_IOManager_ & IOManager()
bool write(const std::string &, BaseExporter &, Options, std::streamsize _precision=6) const
Set options for reader/writer modules.
Has (r) / store (w) face texture coordinates.
bool register_module(BaseReader *_bl)
Handle for a face entity.