diff --git a/src/osgPlugins/obj/ReaderWriterOBJ.cpp b/src/osgPlugins/obj/ReaderWriterOBJ.cpp index 2b6727e30..a4dcc3898 100644 --- a/src/osgPlugins/obj/ReaderWriterOBJ.cpp +++ b/src/osgPlugins/obj/ReaderWriterOBJ.cpp @@ -21,6 +21,7 @@ #endif #include +#include #include #include @@ -72,6 +73,7 @@ public: supportsOption("DISPLACEMENT=", "Set texture unit for displacement texture"); supportsOption("REFLECTION=", "Set texture unit for reflection texture"); + supportsOption("precision=","Set the floating point precision when writing out files"); } virtual const char* className() const { return "Wavefront OBJ Reader"; } @@ -94,7 +96,11 @@ public: if (!acceptsExtension(osgDB::getFileExtension(fileName))) return WriteResult(WriteResult::FILE_NOT_HANDLED); + ObjOptionsStruct localOptions = parseOptions(options); + osgDB::ofstream f(fileName.c_str()); + f.precision(localOptions.precision); + std::string materialFile = osgDB::getNameLessExtension(fileName) + ".mtl"; OBJWriterNodeVisitor nv(f, osgDB::getSimpleFileName(materialFile)); @@ -117,8 +123,11 @@ public: return WriteResult(WriteResult::FILE_NOT_HANDLED); } - virtual WriteResult writeNode(const osg::Node& node,std::ostream& fout,const Options* =NULL) const + virtual WriteResult writeNode(const osg::Node& node,std::ostream& fout,const Options* options=NULL) const { + ObjOptionsStruct localOptions = parseOptions(options); + fout.precision(localOptions.precision); + // writing to a stream does not support materials OBJWriterNodeVisitor nv(fout); @@ -144,6 +153,8 @@ protected: // otherwise overriden typedef std::vector< std::pair > TextureAllocationMap; TextureAllocationMap textureUnitAllocation; + /// Coordinates precision. + int precision; }; typedef std::map< std::string, osg::ref_ptr > MaterialToStateSetMap; @@ -826,6 +837,7 @@ ReaderWriterOBJ::ObjOptionsStruct ReaderWriterOBJ::parseOptions(const osgDB::Rea localOptions.generateFacetNormals = false; localOptions.fixBlackMaterials = true; localOptions.noReverseFaces = false; + localOptions.precision = std::numeric_limits::digits10 + 2; if (options!=NULL) { @@ -868,6 +880,16 @@ ReaderWriterOBJ::ObjOptionsStruct ReaderWriterOBJ::parseOptions(const osgDB::Rea { localOptions.noReverseFaces = true; } + else if (pre_equals == "precision") + { + int val = std::atoi(post_equals.c_str()); + if (val <= 0) { + OSG_NOTICE << "Warning: invalid precision value: " << post_equals << std::endl; + } + else { + localOptions.precision = val; + } + } else if (post_equals.length()>0) { obj::Material::Map::TextureMapType type = obj::Material::Map::UNKNOWN;