From 7ec8b65ec4a3580d11d9c668f07718a19bce254b Mon Sep 17 00:00:00 2001 From: Long Huan <8551701+longhuan2018@users.noreply.github.com> Date: Thu, 18 Jul 2019 09:13:25 +0800 Subject: [PATCH] Add OutputTextureFiles Option Add OutputTextureFiles Option support Write out the texture images to file --- src/osgPlugins/obj/OBJWriterNodeVisitor.cpp | 7 ++++--- src/osgPlugins/obj/OBJWriterNodeVisitor.h | 6 ++++-- src/osgPlugins/obj/ReaderWriterOBJ.cpp | 11 +++++++++-- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/src/osgPlugins/obj/OBJWriterNodeVisitor.cpp b/src/osgPlugins/obj/OBJWriterNodeVisitor.cpp index 5b17944a7..7ba1d5547 100644 --- a/src/osgPlugins/obj/OBJWriterNodeVisitor.cpp +++ b/src/osgPlugins/obj/OBJWriterNodeVisitor.cpp @@ -421,7 +421,7 @@ void ObjPrimitiveIndexWriter::drawArrays(GLenum mode,GLint first,GLsizei count) } -OBJWriterNodeVisitor::OBJMaterial::OBJMaterial(osg::Material* mat, osg::Texture* tex, const osgDB::Options* options) : +OBJWriterNodeVisitor::OBJMaterial::OBJMaterial(osg::Material* mat, osg::Texture* tex, bool outputTextureFiles, const osgDB::Options* options) : diffuse(1,1,1,1), ambient(0.2,0.2,0.2,1), specular(0,0,0,1), @@ -445,7 +445,8 @@ OBJWriterNodeVisitor::OBJMaterial::OBJMaterial(osg::Material* mat, osg::Texture* osg::Image* img = tex->getImage(0); if ((img) && (!img->getFileName().empty())) { image = img->getFileName(); - osgDB::writeImageFile(*img, image, options); + if(outputTextureFiles) + osgDB::writeImageFile(*img, image, options); } } @@ -526,7 +527,7 @@ void OBJWriterNodeVisitor::processStateSet(osg::StateSet* ss) if (mat || tex) { - _materialMap.insert(std::make_pair(osg::ref_ptr(ss), OBJMaterial(mat, tex, _options))); + _materialMap.insert(std::make_pair(osg::ref_ptr(ss), OBJMaterial(mat, tex, _outputTextureFiles, _options))); _fout << "usemtl " << _materialMap[ss].name << std::endl; } diff --git a/src/osgPlugins/obj/OBJWriterNodeVisitor.h b/src/osgPlugins/obj/OBJWriterNodeVisitor.h index cde950c5a..243296f25 100644 --- a/src/osgPlugins/obj/OBJWriterNodeVisitor.h +++ b/src/osgPlugins/obj/OBJWriterNodeVisitor.h @@ -50,13 +50,14 @@ class OBJWriterNodeVisitor: public osg::NodeVisitor { public: - OBJWriterNodeVisitor(std::ostream& fout, const std::string materialFileName = "", const osgDB::Options* options = NULL) : + OBJWriterNodeVisitor(std::ostream& fout, const std::string materialFileName = "", bool outputTextureFiles = false, const osgDB::Options* options = NULL) : osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN), _fout(fout), _currentStateSet(new osg::StateSet()), _lastVertexIndex(1), _lastNormalIndex(1), _lastTexIndex(1), + _outputTextureFiles(outputTextureFiles), _options(options) { _fout << "# file written by OpenSceneGraph" << std::endl << std::endl; @@ -114,7 +115,7 @@ class OBJWriterNodeVisitor: public osg::NodeVisitor { class OBJMaterial { public: OBJMaterial() {} - OBJMaterial(osg::Material* mat, osg::Texture* tex, const osgDB::Options* options); + OBJMaterial(osg::Material* mat, osg::Texture* tex, bool outputTextureFiles = false, const osgDB::Options* options = NULL); osg::Vec4 diffuse, ambient, specular; float shininess; @@ -155,6 +156,7 @@ class OBJWriterNodeVisitor: public osg::NodeVisitor { std::map _nameMap; unsigned int _lastVertexIndex, _lastNormalIndex, _lastTexIndex; MaterialMap _materialMap; + bool _outputTextureFiles; osg::ref_ptr _options; }; diff --git a/src/osgPlugins/obj/ReaderWriterOBJ.cpp b/src/osgPlugins/obj/ReaderWriterOBJ.cpp index f0faccb8e..773469d1f 100644 --- a/src/osgPlugins/obj/ReaderWriterOBJ.cpp +++ b/src/osgPlugins/obj/ReaderWriterOBJ.cpp @@ -75,6 +75,7 @@ public: supportsOption("NsIfNotPresent=", "set specular exponent if not present"); supportsOption("precision=","Set the floating point precision when writing out files"); + supportsOption("OutputTextureFiles", "Write out the texture images to file"); } virtual const char* className() const { return "Wavefront OBJ Reader"; } @@ -103,7 +104,7 @@ public: f.precision(localOptions.precision); std::string materialFile = osgDB::getNameLessExtension(fileName) + ".mtl"; - OBJWriterNodeVisitor nv(f, osgDB::getSimpleFileName(materialFile), options); + OBJWriterNodeVisitor nv(f, osgDB::getSimpleFileName(materialFile), localOptions.outputTextureFiles, options); // we must cast away constness (const_cast(&node))->accept(nv); @@ -131,7 +132,7 @@ public: // writing to a stream does not support materials - OBJWriterNodeVisitor nv(fout, "", options); + OBJWriterNodeVisitor nv(fout, "", localOptions.outputTextureFiles, options); // we must cast away constness (const_cast(&node))->accept(nv); @@ -157,6 +158,7 @@ protected: TextureAllocationMap textureUnitAllocation; /// Coordinates precision. int precision; + bool outputTextureFiles; int specularExponent; ObjOptionsStruct() @@ -168,6 +170,7 @@ protected: fixBlackMaterials = true; noReverseFaces = false; precision = std::numeric_limits::digits10 + 2; + outputTextureFiles = false; specularExponent = -1; } }; @@ -881,6 +884,10 @@ ReaderWriterOBJ::ObjOptionsStruct ReaderWriterOBJ::parseOptions(const osgDB::Rea { localOptions.noReverseFaces = true; } + else if (pre_equals == "OutputTextureFiles") + { + localOptions.outputTextureFiles = true; + } else if (pre_equals == "precision") { int val = std::atoi(post_equals.c_str());