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;
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';
261 for (i=0, nV=_be.n_vertices(); i<nV; ++i)
266 t = _be.texcoord(vh);
268 _out <<
"v " << v[0] <<
" "<< v[1] <<
" "<< v[2] <<
'\n';
271 _out <<
"vn " << n[0] <<
" "<< n[1] <<
" "<< n[2] <<
'\n';
274 _out <<
"vt " << t[0] <<
" "<< t[1] <<
'\n';
277 size_t lastMat = std::numeric_limits<std::size_t>::max();
284 for (i=0, nF=_be.n_faces(); i<nF; ++i)
288 size_t material = std::numeric_limits<std::size_t>::max();
291 if ( _opt.color_has_alpha() ){
293 material = getMaterial(cA);
297 material = getMaterial(c);
301 if(lastMat != material) {
302 _out <<
"usemtl mat" << material <<
'\n';
309 _be.get_vhandles(
FaceHandle(
int(i)), vhandles);
311 for (j=0; j< vhandles.size(); ++j)
315 idx = vhandles[j].idx() + 1;
Has (r) / store (w) texture coordinates.
Has (r) / store (w) vertex normals.
Has (r) / store (w) face normals.
_OBJWriter_ __OBJWriterinstance
Declare the single entity of the OBJ writer.
Set options for reader/writer modules.
Handle for a vertex entity.
bool write(const std::string &, BaseExporter &, Options, std::streamsize _precision=6) const
bool register_module(BaseReader *_bl)
osg::Vec3f::ValueType dot(const osg::Vec3f &_v1, const osg::Vec3f &_v2)
Adapter for osg vector member computing a scalar product.
Handle for a face entity.
Has (r) / store (w) face colors.
_IOManager_ & IOManager()