53 #include <OpenMesh/Core/IO/BinaryHelper.hh> 54 #include <OpenMesh/Core/IO/writer/OBJWriter.hh> 55 #include <OpenMesh/Core/IO/IOManager.hh> 56 #include <OpenMesh/Core/Utils/color_cast.hh> 86 std::fstream out(_filename.c_str(), std::ios_base::out );
90 omerr() <<
"[OBJWriter] : cannot open file " 91 << _filename << std::endl;
96 out.precision(_precision);
103 std::string::size_type dotposition = _filename.find_last_of(
"\\/");
105 std::string::size_type dotposition = _filename.rfind(
"/");
108 if (dotposition == std::string::npos){
110 objName_ = _filename;
112 path_ = _filename.substr(0,dotposition+1);
113 objName_ = _filename.substr(dotposition+1);
117 dotposition = objName_.find_last_of(
".");
119 if(dotposition != std::string::npos)
120 objName_ = objName_.substr(0,dotposition);
123 bool result =
write(out, _be, _opt, _precision);
133 for (
size_t i=0; i < material_.size(); i++)
134 if(material_[i] == _color)
138 material_.push_back( _color );
139 return material_.size()-1;
146 for (
size_t i=0; i < materialA_.size(); i++)
147 if(materialA_[i] == _color)
151 materialA_.push_back( _color );
152 return materialA_.size()-1;
168 for (
size_t i=0, nF=_be.n_faces(); i<nF; ++i)
171 if ( _opt.color_has_alpha() ){
182 if ( _opt.color_has_alpha() )
183 for (
size_t i=0; i < materialA_.size(); i++){
184 _out <<
"newmtl " <<
"mat" << i <<
'\n';
185 _out <<
"Ka 0.5000 0.5000 0.5000" <<
'\n';
186 _out <<
"Kd " << materialA_[i][0] <<
' ' << materialA_[i][1] <<
' ' << materialA_[i][2] <<
'\n';
187 _out <<
"Tr " << materialA_[i][3] <<
'\n';
188 _out <<
"illum 1" <<
'\n';
191 for (
size_t i=0; i < material_.size(); i++){
192 _out <<
"newmtl " <<
"mat" << i <<
'\n';
193 _out <<
"Ka 0.5000 0.5000 0.5000" <<
'\n';
194 _out <<
"Kd " << material_[i][0] <<
' ' << material_[i][1] <<
' ' << material_[i][2] <<
'\n';
195 _out <<
"illum 1" <<
'\n';
212 std::vector<VertexHandle> vhandles;
213 bool useMatrial =
false;
217 omlog() <<
"[OBJWriter] : write file\n";
219 _out.precision(_precision);
222 if (!check( _be, _opt))
227 omout() <<
"[OBJWriter] : Warning, Binary mode requested for OBJ Writer (No support for Binary mode), falling back to standard." << std::endl;
232 omerr() <<
"[OBJWriter] : FaceNormal not supported by OBJ Writer" << std::endl;
238 omerr() <<
"[OBJWriter] : VertexColor not supported by OBJ Writer" << std::endl;
245 std::string matFile = path_ + objName_ +
".mat";
247 std::fstream matStream(matFile.c_str(), std::ios_base::out );
251 omerr() <<
"[OBJWriter] : cannot write material file " << matFile << std::endl;
254 useMatrial = writeMaterial(matStream, _be, _opt);
261 _out <<
"# " << _be.n_vertices() <<
" vertices, ";
262 _out << _be.n_faces() <<
" faces" <<
'\n';
266 _out <<
"mtllib " << objName_ <<
".mat" <<
'\n';
268 std::map<Vec2f,int> texMap;
272 std::vector<Vec2f> texCoords;
274 unsigned int num = _be.get_face_texcoords(texCoords);
275 for(
size_t i = 0; i < num ; ++i)
277 texMap[texCoords[i]] =
static_cast<int>(i);
284 for (
size_t i=0, nV=_be.n_vertices(); i<nV; ++i)
287 t = _be.texcoord(vh);
288 texMap[t] =
static_cast<int>(i);
297 for(std::map<Vec2f,int>::iterator it = texMap.begin(); it != texMap.end() ; ++it)
299 _out <<
"vt " << it->first[0] <<
" " << it->first[1] <<
'\n';
300 it->second = ++texCount;
305 for (
size_t i=0, nV=_be.n_vertices(); i<nV; ++i)
310 t = _be.texcoord(vh);
312 _out <<
"v " << v[0] <<
" "<< v[1] <<
" "<< v[2] <<
'\n';
315 _out <<
"vn " << n[0] <<
" "<< n[1] <<
" "<< n[2] <<
'\n';
318 size_t lastMat = std::numeric_limits<std::size_t>::max();
326 for (
size_t i=0, nF=_be.n_faces(); i<nF; ++i)
330 size_t material = std::numeric_limits<std::size_t>::max();
333 if ( _opt.color_has_alpha() ){
335 material = getMaterial(cA);
339 material = getMaterial(c);
343 if(lastMat != material) {
344 _out <<
"usemtl mat" << material <<
'\n';
351 _be.get_vhandles(
FaceHandle(
int(i)), vhandles);
353 for (
size_t j=0; j< vhandles.size(); ++j)
357 idx = vhandles[j].idx() + 1;
374 _out << texMap[_be.texcoord(vhandles[j])];
Handle for a face entity.
bool write(const std::string &, BaseExporter &, Options, std::streamsize _precision=6) const override
Has (r) / store (w) vertex colors.
Has (r) / store (w) face colors.
_OBJWriter_ __OBJWriterinstance
Declare the single entity of the OBJ writer.
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 ...
Handle for a vertex entity.
Has (r) / store (w) face texture coordinates.
bool register_module(BaseReader *_bl)
Set options for reader/writer modules.
Has (r) / store (w) vertex normals.
_IOManager_ & IOManager()
Has (r) / store (w) face normals.
Has (r) / store (w) texture coordinates.