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>
84write(
const std::string& _filename,
BaseExporter& _be,
const Options& _writeOptions, std::streamsize _precision)
const
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, _writeOptions, _precision);
133 auto idx_it = material_idx_.find(_color);
134 if (idx_it != material_idx_.end()) {
135 return idx_it->second;
137 size_t idx = material_.size();
138 material_.push_back(_color);
139 material_idx_[_color] = idx;
149 auto idx_it = materialA_idx_.find(_color);
150 if (idx_it != materialA_idx_.end()) {
151 return idx_it->second;
153 size_t idx = materialA_.size();
154 materialA_.push_back(_color);
155 materialA_idx_[_color] = idx;
164writeMaterial(std::ostream& _out,
BaseExporter& _be, Options _opt)
const
170 material_idx_.clear();
172 materialA_idx_.clear();
175 for (
size_t i=0, nF=_be.n_faces(); i<nF; ++i)
178 if ( _opt.color_has_alpha() ){
179 cA = color_cast<OpenMesh::Vec4f> (_be.colorA( FaceHandle(
int(i)) ));
183 c = color_cast<OpenMesh::Vec3f> (_be.color( FaceHandle(
int(i)) ));
189 if ( _opt.color_has_alpha() )
190 for (
size_t i=0; i < materialA_.size(); i++){
191 _out <<
"newmtl " <<
"mat" << i <<
'\n';
192 _out <<
"Ka 0.5000 0.5000 0.5000" <<
'\n';
193 _out <<
"Kd " << materialA_[i][0] <<
' ' << materialA_[i][1] <<
' ' << materialA_[i][2] <<
'\n';
194 _out <<
"Tr " << materialA_[i][3] <<
'\n';
195 _out <<
"illum 1" <<
'\n';
198 for (
size_t i=0; i < material_.size(); i++){
199 _out <<
"newmtl " <<
"mat" << i <<
'\n';
200 _out <<
"Ka 0.5000 0.5000 0.5000" <<
'\n';
201 _out <<
"Kd " << material_[i][0] <<
' ' << material_[i][1] <<
' ' << material_[i][2] <<
'\n';
202 _out <<
"illum 1" <<
'\n';
205 if (_opt.texture_file !=
"") {
206 _out <<
"map_Kd " << _opt.texture_file << std::endl;
220 std::vector<VertexHandle> vhandles;
221 bool useMatrial =
false;
225 omlog() <<
"[OBJWriter] : write file\n";
227 _out.precision(_precision);
230 if (!check( _be, _writeOptions)) {
237 omout() <<
"[OBJWriter] : Warning, Binary mode requested for OBJ Writer (No support for Binary mode), falling back to standard." << std::endl;
242 omerr() <<
"[OBJWriter] : FaceNormal not supported by OBJ Writer" << std::endl;
248 omerr() <<
"[OBJWriter] : VertexColor not supported by OBJ Writer" << std::endl;
257 std::fstream matStream(matFile.c_str(), std::ios_base::out );
261 omerr() <<
"[OBJWriter] : cannot write material file " << matFile << std::endl;
264 useMatrial = writeMaterial(matStream, _be, _writeOptions);
271 _out <<
"# " << _be.n_vertices() <<
" vertices, ";
272 _out << _be.n_faces() <<
" faces" <<
'\n';
278 std::map<Vec2f,int> texMap;
282 std::vector<Vec2f> texCoords;
284 unsigned int num = _be.get_face_texcoords(texCoords);
285 for(
size_t i = 0; i < num ; ++i)
287 texMap[texCoords[i]] =
static_cast<int>(i);
294 for (
size_t i=0, nV=_be.n_vertices(); i<nV; ++i)
297 Vec2f t = _be.texcoord(vh);
298 texMap[t] =
static_cast<int>(i);
307 for(std::map<Vec2f,int>::iterator it = texMap.begin(); it != texMap.end() ; ++it)
309 _out <<
"vt " << it->first[0] <<
" " << it->first[1] <<
'\n';
310 it->second = ++texCount;
314 const bool normal_double = _be.is_normal_double();
315 const bool point_double = _be.is_point_double();
316 for (
size_t i=0, nV=_be.n_vertices(); i<nV; ++i)
320 auto v = _be.pointd(vh);
321 _out <<
"v " << v[0] <<
" "<< v[1] <<
" "<< v[2] <<
'\n';
323 auto v = _be.point(vh);
324 _out <<
"v " << v[0] <<
" "<< v[1] <<
" "<< v[2] <<
'\n';
328 auto n = _be.normald(vh);
329 _out <<
"vn " << n[0] <<
" "<< n[1] <<
" "<< n[2] <<
'\n';
331 auto n = _be.normal(vh);
332 _out <<
"vn " << n[0] <<
" "<< n[1] <<
" "<< n[2] <<
'\n';
337 size_t lastMat = std::numeric_limits<std::size_t>::max();
345 for (
size_t i=0, nF=_be.n_faces(); i<nF; ++i)
352 if ( _writeOptions.color_has_alpha() ){
353 cA = color_cast<OpenMesh::Vec4f> (_be.colorA(
FaceHandle(
int(i)) ));
354 material = getMaterial(cA);
357 c = color_cast<OpenMesh::Vec3f> (_be.color(
FaceHandle(
int(i)) ));
358 material = getMaterial(c);
362 if(lastMat != material) {
363 _out <<
"usemtl mat" << material <<
'\n';
370 _be.get_vhandles(
FaceHandle(
int(i)), vhandles);
372 for (
size_t j=0; j< vhandles.size(); ++j)
376 idx = vhandles[j].idx() + 1;
393 _out << texMap[_be.texcoord(vhandles[j])];
409 material_idx_.clear();
411 materialA_idx_.clear();
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.
std::string material_file_extension
@ FaceNormal
Has (r) / store (w) face normals.
@ FaceColor
Has (r) / store (w) face colors.
@ FaceTexCoord
Has (r) / store (w) face texture coordinates.
@ Binary
Set binary mode for r/w.
@ VertexNormal
Has (r) / store (w) vertex normals.
@ VertexTexCoord
Has (r) / store (w) texture coordinates.
@ VertexColor
Has (r) / store (w) vertex colors.
bool register_module(BaseReader *_bl)
bool write(const std::string &, BaseExporter &, const Options &_writeOptions, std::streamsize _precision=6) const override
_IOManager_ & IOManager()
_OBJWriter_ __OBJWriterinstance
Declare the single entity of the OBJ writer.
Handle for a face entity.
Handle for a vertex entity.