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
dot = _filename.find_last_of(
"\\/");
105 std::string::size_type dot = _filename.rfind(
"/");
108 if (dot == std::string::npos){
110 objName_ = _filename;
112 path_ = _filename.substr(0,dot+1);
113 objName_ = _filename.substr(dot+1);
117 dot = objName_.find_last_of(
".");
119 if(dot != std::string::npos)
120 objName_ = objName_.substr(0,dot);
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';
213 std::vector<VertexHandle> vhandles;
214 bool useMatrial =
false;
218 omlog() <<
"[OBJWriter] : write file\n";
220 _out.precision(_precision);
223 if (!check( _be, _opt))
228 omout() <<
"[OBJWriter] : Warning, Binary mode requested for OBJ Writer (No support for Binary mode), falling back to standard." << std::endl;
233 omerr() <<
"[OBJWriter] : FaceNormal not supported by OBJ Writer" << std::endl;
239 omerr() <<
"[OBJWriter] : VertexColor not supported by OBJ Writer" << std::endl;
246 std::string matFile = path_ + objName_ +
".mat";
248 std::fstream matStream(matFile.c_str(), std::ios_base::out );
252 omerr() <<
"[OBJWriter] : cannot write material file " << matFile << std::endl;
255 useMatrial = writeMaterial(matStream, _be, _opt);
262 _out <<
"# " << _be.n_vertices() <<
" vertices, ";
263 _out << _be.n_faces() <<
" faces" <<
'\n';
267 _out <<
"mtllib " << objName_ <<
".mat" <<
'\n';
269 std::map<Vec2f,int> texMap;
273 std::vector<Vec2f> texCoords;
275 unsigned int num = _be.get_face_texcoords(texCoords);
276 for(
unsigned int i = 0; i < num ; ++i)
278 texMap[texCoords[i]] = i;
285 for (
size_t i=0, nV=_be.n_vertices(); i<nV; ++i)
288 t = _be.texcoord(vh);
289 texMap[t] =
static_cast<int>(i);
298 for(std::map<Vec2f,int>::iterator it = texMap.begin(); it != texMap.end() ; ++it)
300 _out <<
"vt " << it->first[0] <<
" " << it->first[1] <<
'\n';
301 it->second = ++texCount;
306 for (i=0, nV=_be.n_vertices(); i<nV; ++i)
311 t = _be.texcoord(vh);
313 _out <<
"v " << v[0] <<
" "<< v[1] <<
" "<< v[2] <<
'\n';
316 _out <<
"vn " << n[0] <<
" "<< n[1] <<
" "<< n[2] <<
'\n';
319 size_t lastMat = std::numeric_limits<std::size_t>::max();
327 for (i=0, nF=_be.n_faces(); i<nF; ++i)
331 size_t material = std::numeric_limits<std::size_t>::max();
334 if ( _opt.color_has_alpha() ){
336 material = getMaterial(cA);
340 material = getMaterial(c);
344 if(lastMat != material) {
345 _out <<
"usemtl mat" << material <<
'\n';
352 _be.get_vhandles(
FaceHandle(
int(i)), vhandles);
354 for (j=0; j< vhandles.size(); ++j)
358 idx = vhandles[j].idx() + 1;
375 _out << texMap[_be.texcoord(vhandles[j])];
Has (r) / store (w) vertex colors.
Handle for a vertex entity.
bool register_module(BaseReader *_bl)
Has (r) / store (w) face colors.
Has (r) / store (w) face normals.
_IOManager_ & IOManager()
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) texture coordinates.
Has (r) / store (w) face texture coordinates.
Handle for a face entity.
bool write(const std::string &, BaseExporter &, Options, std::streamsize _precision=6) const
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 ...
Set options for reader/writer modules.
Has (r) / store (w) vertex normals.
_OBJWriter_ __OBJWriterinstance
Declare the single entity of the OBJ writer.